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

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