Commit 0945a419 authored by Sylvester Keil's avatar Sylvester Keil

normalize tag parameters and add key prefix

Normalize tag parameters using Ruby's OptionParser. This
breaks the API but gives us a standard way of handling
parameters going forward.

This commit also adds optional prefixes used by the
bibliography, cite and reference tags. See #12.
parent 14c3d2b5
...@@ -83,7 +83,7 @@ Feature: BibTeX ...@@ -83,7 +83,7 @@ Feature: BibTeX
""" """
--- ---
--- ---
{% bibliography references %} {% bibliography -f references %}
""" """
When I run jekyll When I run jekyll
Then the _site directory should exist Then the _site directory should exist
...@@ -116,7 +116,7 @@ Feature: BibTeX ...@@ -116,7 +116,7 @@ Feature: BibTeX
""" """
--- ---
--- ---
{% bibliography references filter: @book[year <= 2000] %} {% bibliography -f references --query @book[year <= 2000] %}
""" """
When I run jekyll When I run jekyll
Then the _site directory should exist Then the _site directory should exist
...@@ -150,7 +150,7 @@ Feature: BibTeX ...@@ -150,7 +150,7 @@ Feature: BibTeX
""" """
--- ---
--- ---
{% bibliography references prefix: a filter: @book[year <= 2000] %} {% bibliography --file references --prefix a -q @book[year <= 2000] %}
""" """
When I run jekyll When I run jekyll
Then the _site directory should exist Then the _site directory should exist
......
...@@ -105,7 +105,7 @@ Feature: Citations ...@@ -105,7 +105,7 @@ Feature: Citations
""" """
--- ---
--- ---
{% cite ruby prefix: a %} {% cite ruby --prefix a %}
""" """
When I run jekyll When I run jekyll
Then the _site directory should exist Then the _site directory should exist
......
...@@ -49,7 +49,7 @@ Feature: Citations ...@@ -49,7 +49,7 @@ Feature: Citations
""" """
--- ---
--- ---
{% cite_details ruby, Click For More %} {% cite_details ruby --text Click For More %}
""" """
When I run jekyll When I run jekyll
Then the _site directory should exist Then the _site directory should exist
......
...@@ -27,7 +27,7 @@ Feature: Sorting BibTeX Bibliographies ...@@ -27,7 +27,7 @@ Feature: Sorting BibTeX Bibliographies
""" """
--- ---
--- ---
{% bibliography references %} {% bibliography -f references %}
""" """
When I run jekyll When I run jekyll
Then the _site directory should exist Then the _site directory should exist
...@@ -60,7 +60,7 @@ Feature: Sorting BibTeX Bibliographies ...@@ -60,7 +60,7 @@ Feature: Sorting BibTeX Bibliographies
""" """
--- ---
--- ---
{% bibliography references %} {% bibliography -f references %}
""" """
When I run jekyll When I run jekyll
Then the _site directory should exist Then the _site directory should exist
......
require 'jekyll' require 'jekyll'
require 'optparse'
require 'bibtex' require 'bibtex'
require 'citeproc' require 'citeproc'
......
...@@ -8,12 +8,8 @@ module Jekyll ...@@ -8,12 +8,8 @@ module Jekyll
super super
@config = Scholar.defaults.dup @config = Scholar.defaults.dup
@bibtex_file, @query = arguments.strip.split(/\s*filter:\s*/)
optparse(arguments)
if @bibtex_file == 'cited'
@bibtex_file = nil
@cited = true
end
end end
def render(context) def render(context)
...@@ -21,17 +17,14 @@ module Jekyll ...@@ -21,17 +17,14 @@ module Jekyll
references = entries references = entries
if @cited if cited_only?
references = cited_references.map do |key| references = cited_references.map do |key|
references.detect { |e| e.key == key } references.detect { |e| e.key == key }
end end
end end
references.map! do |entry| references.map! do |entry|
reference = CiteProc.process entry.to_citeproc, :style => config['style'], reference = reference_tag entry
:locale => config['locale'], :format => 'html'
reference = content_tag :span, reference, :id => entry.key
if generate_details? if generate_details?
reference << link_to(details_link_for(entry), reference << link_to(details_link_for(entry),
......
...@@ -4,21 +4,19 @@ module Jekyll ...@@ -4,21 +4,19 @@ module Jekyll
class CiteTag < Liquid::Tag class CiteTag < Liquid::Tag
include Scholar::Utilities include Scholar::Utilities
attr_reader :key, :pages attr_reader :pages
def initialize(tag_name, arguments, tokens) def initialize(tag_name, arguments, tokens)
super super
@config = Scholar.defaults.dup @config = Scholar.defaults.dup
@key = arguments.strip.split(/\s+/)[0] @key, arguments = arguments.strip.split(/\s+/, 2)
optparse(arguments)
end end
def render(context) def render(context)
set_context_to context set_context_to context
context['cited'] ||= []
context['cited'] << key
cite key cite key
end end
......
...@@ -4,20 +4,19 @@ module Jekyll ...@@ -4,20 +4,19 @@ module Jekyll
class CiteDetailsTag < Liquid::Tag class CiteDetailsTag < Liquid::Tag
include Scholar::Utilities include Scholar::Utilities
attr_reader :key, :text
def initialize(tag_name, arguments, tokens) def initialize(tag_name, arguments, tokens)
super super
@config = Scholar.defaults.dup @config = Scholar.defaults.dup
@key, @text = arguments.strip.split(/\s*,\s*/, 2) @key, arguments = arguments.strip.split(/\s+/, 2)
optparse(arguments)
end end
def render(context) def render(context)
set_context_to context set_context_to context
cite_details key, text cite_details key, text
end end
end end
end end
......
...@@ -4,13 +4,13 @@ module Jekyll ...@@ -4,13 +4,13 @@ module Jekyll
class QuoteTag < Liquid::Block class QuoteTag < Liquid::Block
include Scholar::Utilities include Scholar::Utilities
attr_reader :key, :pages attr_reader :pages
def initialize(tag_name, arguments, tokens) def initialize(tag_name, arguments, tokens)
super super
@config = Scholar.defaults.dup @config = Scholar.defaults.dup
@key = arguments.strip.split(/\s+/)[0] @key, arguments = arguments.strip.split(/\s+/, 2)
end end
def render(context) def render(context)
...@@ -19,7 +19,7 @@ module Jekyll ...@@ -19,7 +19,7 @@ module Jekyll
quote = super.strip.gsub(/\n\n/, '</p><p>').gsub(/\n/, '<br/>') quote = super.strip.gsub(/\n\n/, '</p><p>').gsub(/\n/, '<br/>')
quote = content_tag :p, quote quote = content_tag :p, quote
citation = cite(key) citation = cite key
quote << content_tag(:cite, citation) quote << content_tag(:cite, citation)
......
...@@ -4,34 +4,18 @@ module Jekyll ...@@ -4,34 +4,18 @@ module Jekyll
class ReferenceTag < Liquid::Tag class ReferenceTag < Liquid::Tag
include Scholar::Utilities include Scholar::Utilities
attr_reader :key, :file
def initialize(tag_name, arguments, tokens) def initialize(tag_name, arguments, tokens)
super super
@config = Scholar.defaults.dup @config = Scholar.defaults.dup
@key, @file = arguments.strip.split(/\s*,\s*/, 2) @key, arguments = arguments.strip.split(/\s+/, 2)
optparse(arguments)
end end
def render(context) def render(context)
set_context_to context set_context_to context
bib = unless file.nil? reference_tag bibliography[key]
BibTeX.open(file, { :filter => :latex })
else
set_context_to context
bibliography
end
entry = bib[key]
if bib.key?(key)
CiteProc.process entry.to_citeproc,
:style => config['style'],
:locale => config['locale'],
:format => 'html'
else
"(missing reference)"
end
rescue rescue
"(#{key})" "(#{key})"
end end
......
...@@ -6,7 +6,38 @@ module Jekyll ...@@ -6,7 +6,38 @@ module Jekyll
# #site readers # #site readers
module Utilities module Utilities
attr_reader :bibtex_file, :config, :site, :query, :context attr_reader :bibtex_file, :config, :site, :query,
:context, :prefix, :key, :text
def optparse(arguments)
return if arguments.nil? || arguments.empty?
parser = OptionParser.new do |opts|
opts.on('-c', '--cited') do |cited|
@cited = true
end
opts.on('-f', '--file FILE') do |file|
@bibtex_file = file
end
opts.on('-q', '--query QUERY') do |query|
@query = query
end
opts.on('-p', '--prefix PREFIX') do |prefix|
@prefix = prefix
end
opts.on('-t', '--text TEXT') do |text|
@text = text
end
end
argv = arguments.split(/(\B-[cfqpt]|\B--(?:cited|file|query|prefix|text))/)
parser.parse argv.map(&:strip).reject(&:empty?)
end
def bibtex_options def bibtex_options
@bibtex_options ||= { :filter => :latex } @bibtex_options ||= { :filter => :latex }
...@@ -31,6 +62,10 @@ module Jekyll ...@@ -31,6 +62,10 @@ module Jekyll
b b
end end
def cited_only?
!!@cited
end
def extend_path(name) def extend_path(name)
if name.nil? || name.empty? if name.nil? || name.empty?
name = config['bibliography'] name = config['bibliography']
...@@ -41,6 +76,15 @@ module Jekyll ...@@ -41,6 +76,15 @@ module Jekyll
p p
end end
def reference_tag(entry)
return '(missing reference)' unless entry
reference = CiteProc.process entry.to_citeproc,
:style => config['style'], :locale => config['locale'], :format => 'html'
content_tag :span, reference, :id => [prefix, entry.key].compact.join('-')
end
def generate_details? def generate_details?
site.layouts.key?(File.basename(config['details_layout'], '.html')) site.layouts.key?(File.basename(config['details_layout'], '.html'))
end end
...@@ -68,13 +112,16 @@ module Jekyll ...@@ -68,13 +112,16 @@ module Jekyll
def cite(key) def cite(key)
entry = bibliography[key] entry = bibliography[key]
context['cited'] ||= []
context['cited'] << key
if bibliography.key?(key) if bibliography.key?(key)
citation = CiteProc.process entry.to_citeproc, :style => config['style'], citation = CiteProc.process entry.to_citeproc, :style => config['style'],
:locale => config['locale'], :format => 'html', :mode => :citation :locale => config['locale'], :format => 'html', :mode => :citation
link_to "##{entry.key}", citation.join link_to "##{[prefix, entry.key].compact.join('-')}", citation.join
else else
"(missing reference)" '(missing reference)'
end end
rescue rescue
"(#{key})" "(#{key})"
...@@ -84,7 +131,7 @@ module Jekyll ...@@ -84,7 +131,7 @@ module Jekyll
if bibliography.key?(key) if bibliography.key?(key)
link_to details_link_for(bibliography[key]), text || config['details_link'] link_to details_link_for(bibliography[key]), text || config['details_link']
else else
"(missing reference)" '(missing reference)'
end end
end end
......
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