Commit 2095c4a3 authored by Sylvester Keil's avatar Sylvester Keil

added separate defaults and utilities

parent 80e27522
...@@ -5,36 +5,10 @@ require 'bibtex' ...@@ -5,36 +5,10 @@ require 'bibtex'
require 'citeproc' require 'citeproc'
require 'jekyll/scholar/version' require 'jekyll/scholar/version'
require 'jekyll/scholar/defaults'
require 'jekyll/scholar/utilities' require 'jekyll/scholar/utilities'
require 'jekyll/scholar/converters/bibtex' require 'jekyll/scholar/converters/bibtex'
require 'jekyll/scholar/tags/bibliography' require 'jekyll/scholar/tags/bibliography'
require 'jekyll/scholar/tags/cite' require 'jekyll/scholar/tags/cite'
require 'jekyll/scholar/generators/details' require 'jekyll/scholar/generators/details'
module Jekyll
class Scholar
@defaults = Hash[*%w{
style apa
locale en
sort_by none
order ascending
source ./_bibliography
bibliography references.bib
details_dir ./bibliography
details_layout bibtex.html
query @*
}].freeze
class << self
attr_reader :defaults
end
end
end
module Jekyll
class Scholar
@defaults = Hash[*%w{
style apa
locale en
sort_by none
order ascending
source ./_bibliography
bibliography references.bib
details_dir bibliography
details_layout bibtex.html
details_link Details
query @*
}].freeze
class << self
attr_reader :defaults
end
end
end
\ No newline at end of file
...@@ -2,15 +2,14 @@ module Jekyll ...@@ -2,15 +2,14 @@ module Jekyll
class Scholar class Scholar
class Details < Page class Details < Page
include Scholar::Utilities
attr_reader :config
def initialize(site, base, dir, entry) def initialize(site, base, dir, entry)
@site, @base, @dir = site, base, dir @site, @base, @dir = site, base, dir
@config = Scholar.defaults.merge(site.config['scholar'] || {}) @config = Scholar.defaults.merge(site.config['scholar'] || {})
@name = filename_for(entry) @name = details_file_for(entry)
process(@name) process(@name)
read_yaml(File.join(base, '_layouts'), config['details_layout']) read_yaml(File.join(base, '_layouts'), config['details_layout'])
...@@ -20,14 +19,6 @@ module Jekyll ...@@ -20,14 +19,6 @@ module Jekyll
private private
def filename_for(entry)
n = entry.key.dup
n.gsub!(/[:\s]+/, '_')
[n, 'html'].join('.')
end
def liquidify(entry) def liquidify(entry)
data['entry'] = {} data['entry'] = {}
...@@ -42,16 +33,17 @@ module Jekyll ...@@ -42,16 +33,17 @@ module Jekyll
end end
class DetailsGenerator < Generator class DetailsGenerator < Generator
include Scholar::Utilities
safe true safe true
attr_reader :config attr_reader :config
def generate(site) def generate(site)
@config = Scholar.defaults.merge(site.config['scholar'] || {}) @site, @config = site, Scholar.defaults.merge(site.config['scholar'] || {})
if site.layouts.key?(File.basename(config['details_layout'], '.html')) if generate_details?
bibliography[config['query']].each do |entry| entries.each do |entry|
details = Details.new(site, site.source, config['details_dir'], entry) details = Details.new(site, site.source, details_path, entry)
details.render(site.layouts, site.site_payload) details.render(site.layouts, site.site_payload)
details.write(site.dest) details.write(site.dest)
...@@ -61,17 +53,6 @@ module Jekyll ...@@ -61,17 +53,6 @@ module Jekyll
end end
end end
private
def bibliography
@bibliography ||= BibTeX.open(bibliography_path, :filter => :latex)
end
def bibliography_path
p = File.join(config['source'], config['bibliography'])
p << '.bib' unless File.exists?(p)
p
end
end end
......
...@@ -2,65 +2,35 @@ module Jekyll ...@@ -2,65 +2,35 @@ module Jekyll
class Scholar class Scholar
class BibliographyTag < Liquid::Tag class BibliographyTag < Liquid::Tag
include Scholar::Utilities
attr_reader :file, :config
def initialize(tag_name, arguments, tokens) def initialize(tag_name, arguments, tokens)
super super
@config = Scholar.defaults.dup @config = Scholar.defaults.dup
@file = arguments.strip @bibtex_file = arguments.strip
end end
def render(context) def render(context)
config.merge!(context.registers[:site].config['scholar'] || {}) @site = context.registers[:site]
config.merge!(site.config['scholar'] || {})
references = entries.map do |e| references = entries.map do |e|
r = CiteProc.process e.to_citeproc, :style => config['style'], reference = CiteProc.process e.to_citeproc, :style => config['style'],
:locale => config['locale'], :format => 'html' :locale => config['locale'], :format => 'html'
r = "<span id='#{e.key}'>#{r}</span>" reference = "<span id='#{e.key}'>#{reference}</span>"
if e.field?(:url) if generate_details?
r << "<a href='#{e.url}'>URL</a>" reference << "<a href='#{details_link_for(e)}'>#{config['details_link']}</a>"
end
if e.field?(:doi)
r << "<a href='#{e.doi}'>DOI</a>"
end end
"<li>#{r}</li>" "<li>#{reference}</li>"
end end
"<ol>\n#{references.join("\n")}\n</ol>" "<ol>\n#{references.join("\n")}\n</ol>"
end end
private
def bibliography
@bibliography ||= BibTeX.open(extend_path(file), :filter => :latex)
end
def entries
b = bibliography[config['query']]
unless config['sort_by'] == 'none'
b.sort_by! { |e| e[config['sort_by']].to_s }
b.reverse! if config['order'] =~ /^(desc|reverse)/i
end
b
end
def extend_path(name)
if name.nil? || name.empty?
name = config['bibliography']
end
p = File.join(config['source'], name)
p << '.bib' unless File.exists?(p)
p
end
end end
end end
......
...@@ -2,7 +2,8 @@ module Jekyll ...@@ -2,7 +2,8 @@ module Jekyll
class Scholar class Scholar
class CiteTag < Liquid::Tag class CiteTag < Liquid::Tag
include Scholar::Utilities
attr_reader :key, :pages, :config attr_reader :key, :pages, :config
def initialize(tag_name, arguments, tokens) def initialize(tag_name, arguments, tokens)
...@@ -15,29 +16,18 @@ module Jekyll ...@@ -15,29 +16,18 @@ module Jekyll
def render(context) def render(context)
config.merge!(context.registers[:site].config['scholar'] || {}) config.merge!(context.registers[:site].config['scholar'] || {})
e = bibliography[key] entry = bibliography[key]
if e if entry
c = CiteProc.process e.to_citeproc, :style => config['style'], c = CiteProc.process entry.to_citeproc, :style => config['style'],
:locale => config['locale'], :format => 'html', :mode => :citation :locale => config['locale'], :format => 'html', :mode => :citation
"<a href='##{e.key}'>#{c}</a>" "<a href='##{entry.key}'>#{c}</a>"
else else
"(missing reference)" "(missing reference)"
end end
end end
private
def bibliography
@bibliography ||= BibTeX.open(extend_path(config['bibliography']), :filter => :latex)
end
def extend_path(name)
p = File.join(config['source'], name)
p << '.bib' unless File.exists?(p)
p
end
end end
end end
......
module Jekyll module Jekyll
class Scholar class Scholar
module Utilities # Utility methods used by several Scholar plugins. The methods in this
# module may depend on the presence of #config, #bibtex_file, and
# #site readers
end module Utilities
end attr_reader :bibtex_file, :config, :site
def bibtex_options
@bibtex_options ||= { :filter => :latex }
end
def bibtex_path
@bibtex_path ||= extend_path(bibtex_file)
end
def bibliography
@bibliography ||= BibTeX.open(bibtex_path, bibtex_options)
end
def entries
b = bibliography[config['query']]
unless config['sort_by'] == 'none'
b.sort_by! { |e| e[config['sort_by']].to_s }
b.reverse! if config['order'] =~ /^(desc|reverse)/i
end
b
end
def extend_path(name)
if name.nil? || name.empty?
name = config['bibliography']
end
p = File.join(config['source'], name)
p << '.bib' unless File.exists?(p)
p
end
def generate_details?
site.layouts.key?(File.basename(config['details_layout'], '.html'))
end
def details_file_for(entry)
name = entry.key.to_s.dup
name.gsub!(/[:\s]+/, '_')
[name, 'html'].join('.')
end
def details_link_for(entry)
[site.source, details_path, details_file_for(entry)].join('/')
end
def details_path
config['details_dir']
end
end
end
end end
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment