From 9c258e1cd19a6abf46c0296afe36e8775c77cf8b Mon Sep 17 00:00:00 2001 From: Sylvester Keil Date: Mon, 30 Apr 2012 18:01:48 +0200 Subject: [PATCH] improved utility functions #1 --- lib/jekyll/scholar/tags/bibliography.rb | 21 +++++++++--------- lib/jekyll/scholar/tags/cite.rb | 8 +++---- lib/jekyll/scholar/utilities.rb | 29 +++++++++++++++++++++++-- 3 files changed, 41 insertions(+), 17 deletions(-) diff --git a/lib/jekyll/scholar/tags/bibliography.rb b/lib/jekyll/scholar/tags/bibliography.rb index 6f79efe..d880fb5 100644 --- a/lib/jekyll/scholar/tags/bibliography.rb +++ b/lib/jekyll/scholar/tags/bibliography.rb @@ -12,23 +12,22 @@ module Jekyll end def render(context) - @site = context.registers[:site] - config.merge!(site.config['scholar'] || {}) + set_context_to context - references = entries.map do |e| - reference = CiteProc.process e.to_citeproc, :style => config['style'], + references = entries.map do |entry| + reference = CiteProc.process entry.to_citeproc, :style => config['style'], :locale => config['locale'], :format => 'html' - - reference = "#{reference}" - + + reference = content_tag :span, reference, :id => entry.key + if generate_details? - reference << "#{config['details_link']}" + reference << link_to(details_link_for(entry), config['details_link']) end - - "
  • #{reference}
  • " + + content_tag :li, reference end - "
      \n#{references.join("\n")}\n
    " + content_tag :ol, references.join("\n") end end diff --git a/lib/jekyll/scholar/tags/cite.rb b/lib/jekyll/scholar/tags/cite.rb index b22ac74..55ca348 100644 --- a/lib/jekyll/scholar/tags/cite.rb +++ b/lib/jekyll/scholar/tags/cite.rb @@ -14,15 +14,15 @@ module Jekyll end def render(context) - config.merge!(context.registers[:site].config['scholar'] || {}) - + set_context_to context + entry = bibliography[key] if entry - c = CiteProc.process entry.to_citeproc, :style => config['style'], + citation = CiteProc.process entry.to_citeproc, :style => config['style'], :locale => config['locale'], :format => 'html', :mode => :citation - "#{c}" + link_to "##{entry.key}", citation else "(missing reference)" end diff --git a/lib/jekyll/scholar/utilities.rb b/lib/jekyll/scholar/utilities.rb index 28b1d70..655e450 100644 --- a/lib/jekyll/scholar/utilities.rb +++ b/lib/jekyll/scholar/utilities.rb @@ -53,14 +53,39 @@ module Jekyll [name, 'html'].join('.') end - def details_link_for(entry) - [site.source, details_path, details_file_for(entry)].join('/') + def details_link_for(entry, base = site.config['baseurl'] || '/') + [base, details_path, details_file_for(entry)].compact.join('/') end def details_path config['details_dir'] end + def content_tag(name, content_or_attributes, attributes = {}) + if content_or_attributes.is_a?(Hash) + content, attributes = nil, content_or_attributes + else + content = content_or_attributes + end + + attributes = attributes.map { |k,v| %Q(#{k}="#{v}") } + + if content.nil? + "<#{[name, attributes].flatten.compact.join(' ')}/>" + else + "<#{[name, attributes].flatten.compact.join(' ')}>#{content}" + end + end + + def link_to(href, content, attributes = {}) + content_tag :a, content || href, attributes.merge(:href => href) + end + + def set_context_to(context) + @site = context.registers[:site] + config.merge!(site.config['scholar'] || {}) + end + end end -- GitLab