Commit b99e77ad authored by Sylvester Keil's avatar Sylvester Keil

add support for multiple inline citations

fix #41
parent 7f3a3bf5
......@@ -111,3 +111,72 @@ Feature: Citations
Then the _site directory should exist
And the "_site/scholar.html" file should exist
And I should see "#a-ruby" in "_site/scholar.html"
@tags @cite
Scenario: Multiple Citations
Given I have a scholar configuration with:
| key | value |
| source | ./_bibliography |
| bibliography | my_references |
And I have a "_bibliography" directory
And I have a file "_bibliography/my_references.bib":
"""
@book{ruby,
title = {The Ruby Programming Language},
author = {Flanagan, David and Matsumoto, Yukihiro},
year = {2008},
publisher = {O'Reilly Media}
}
@book{microscope,
title = {Ruby Under a Microscope},
author = {Pat Shaughnessy},
year = {2013},
publisher = {No Starch Press}
}
"""
And I have a page "scholar.html":
"""
---
---
{% cite ruby microscope %}
"""
When I run jekyll
Then the _site directory should exist
And the "_site/scholar.html" file should exist
And I should see "Flanagan & Matsumoto, 2008; Shaughnessy, 2013" in "_site/scholar.html"
@tags @cite @locator
Scenario: Multiple Citations with locators
Given I have a scholar configuration with:
| key | value |
| source | ./_bibliography |
| bibliography | my_references |
And I have a "_bibliography" directory
And I have a file "_bibliography/my_references.bib":
"""
@book{ruby,
title = {The Ruby Programming Language},
author = {Flanagan, David and Matsumoto, Yukihiro},
year = {2008},
publisher = {O'Reilly Media}
}
@book{microscope,
title = {Ruby Under a Microscope},
author = {Pat Shaughnessy},
year = {2013},
publisher = {No Starch Press}
}
"""
And I have a page "scholar.html":
"""
---
---
{% cite ruby microscope -l 2-3 --locator 23 & 42 %}
"""
When I run jekyll
Then the _site directory should exist
And the "_site/scholar.html" file should exist
And I should see "Matsumoto, 2008, pp. 2-3; Shaughnessy, 2013, pp. 23 & 42" in "_site/scholar.html"
......@@ -44,6 +44,7 @@ Then(/^the (.*) directory should exist$/) do |dir|
end
Then(/^I should see "(.*)" in "(.*)"$/) do |text, file|
puts File.open(file).readlines.join
assert_match Regexp.new(text), File.open(file).readlines.join
end
......
......@@ -10,14 +10,14 @@ module Jekyll
super
@config = Scholar.defaults.dup
@key, arguments = arguments.strip.split(/\s+/, 2)
@keys, arguments = split_arguments(arguments)
optparse(arguments)
end
def render(context)
set_context_to context
cite key
cite keys
end
end
......@@ -25,4 +25,4 @@ module Jekyll
end
end
Liquid::Template.register_tag('cite', Jekyll::Scholar::CiteTag)
\ No newline at end of file
Liquid::Template.register_tag('cite', Jekyll::Scholar::CiteTag)
......@@ -8,14 +8,16 @@ module Jekyll
super
@config = Scholar.defaults.dup
@key, arguments = arguments.strip.split(/\s+/, 2)
@keys, arguments = split_arguments arguments
optparse(arguments)
end
def render(context)
set_context_to context
cite_details key, text
keys.map { |key|
cite_details key, text
}.join("\n")
end
end
......
......@@ -3,32 +3,32 @@ module Jekyll
class QuoteTag < Liquid::Block
include Scholar::Utilities
attr_reader :pages
def initialize(tag_name, arguments, tokens)
super
@config = Scholar.defaults.dup
@key, arguments = arguments.strip.split(/\s+/, 2)
@keys, arguments = split_arguments arguments
end
def render(context)
set_context_to context
quote = super.strip.gsub(/\n\n/, '</p><p>').gsub(/\n/, '<br/>')
quote = content_tag :p, quote
citation = cite key
citation = cite keys
quote << content_tag(:cite, citation)
content_tag :blockquote, quote
end
end
end
end
Liquid::Template.register_tag('quote', Jekyll::Scholar::QuoteTag)
\ No newline at end of file
Liquid::Template.register_tag('quote', Jekyll::Scholar::QuoteTag)
......@@ -8,14 +8,16 @@ module Jekyll
super
@config = Scholar.defaults.dup
@key, arguments = arguments.strip.split(/\s+/, 2)
@keys, arguments = split_arguments arguments
optparse(arguments)
end
def render(context)
set_context_to context
reference_tag bibliography[key]
keys.map { |key|
reference_tag bibliography[key]
}.join("\n")
end
end
......
......@@ -15,7 +15,17 @@ module Jekyll
module Utilities
attr_reader :config, :site, :query,
:context, :prefix, :key, :text
:context, :prefix, :keys, :text
def split_arguments(arguments)
tokens = arguments.strip.split(/\s+/)
args = tokens.take_while { |a| !a.start_with?('-') }
opts = (tokens - args).join(' ')
[args, opts]
end
def optparse(arguments)
return if arguments.nil? || arguments.empty?
......@@ -42,6 +52,10 @@ module Jekyll
@text = text
end
opts.on('-l', '--locator LOCATOR') do |locator|
locators << locator
end
opts.on('-s', '--style STYLE') do |style|
@style = style
end
......@@ -51,11 +65,15 @@ module Jekyll
end
end
argv = arguments.split(/(\B-[cfqptTs]|\B--(?:cited|file|query|prefix|text|style|template|))/)
argv = arguments.split(/(\B-[cfqptTsl]|\B--(?:cited|file|query|prefix|text|style|template|locator|))/)
parser.parse argv.map(&:strip).reject(&:empty?)
end
def locators
@locators ||= []
end
def bibtex_files
@bibtex_files ||= [config['bibliography']]
end
......@@ -260,8 +278,12 @@ module Jekyll
:style => style, :locale => config['locale']
end
def render_citation(entry)
renderer.render [citation_item_for(entry)], STYLES[style].citation
def render_citation(items)
renderer.render items.zip(locators).map { |entry, locator|
item = citation_item_for entry
item.locator = locator
item
}, STYLES[style].citation
end
def render_bibliography(entry)
......@@ -274,18 +296,23 @@ module Jekyll
end
end
def cite(key)
def cited_keys
context['cited'] ||= []
context['cited'] << key
end
if bibliography.key?(key)
entry = bibliography[key]
entry = entry.convert(*bibtex_filters) unless bibtex_filters.empty?
def cite(keys)
items = keys.map do |key|
cited_keys << key
link_to "##{[prefix, entry.key].compact.join('-')}", render_citation(entry)
else
missing_reference
if bibliography.key?(key)
entry = bibliography[key]
entry = entry.convert(*bibtex_filters) unless bibtex_filters.empty?
else
return missing_reference
end
end
link_to "##{[prefix, keys[0]].compact.join('-')}", render_citation(items)
end
def cite_details(key, text)
......
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