Skip to content

Commit

Permalink
add support for multiple inline citations
Browse files Browse the repository at this point in the history
fix #41
  • Loading branch information
inukshuk committed Apr 13, 2014
1 parent 7f3a3bf commit b99e77a
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 31 deletions.
69 changes: 69 additions & 0 deletions features/citation.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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"

1 change: 1 addition & 0 deletions features/step_definitions/jekyll_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
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

Expand Down
6 changes: 3 additions & 3 deletions lib/jekyll/scholar/tags/cite.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ def initialize(tag_name, arguments, tokens)
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

end
end

Liquid::Template.register_tag('cite', Jekyll::Scholar::CiteTag)
Liquid::Template.register_tag('cite', Jekyll::Scholar::CiteTag)
6 changes: 4 additions & 2 deletions lib/jekyll/scholar/tags/cite_details.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ def initialize(tag_name, arguments, tokens)
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

Expand Down
24 changes: 12 additions & 12 deletions lib/jekyll/scholar/tags/quote.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,32 @@ class Scholar

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)
Liquid::Template.register_tag('quote', Jekyll::Scholar::QuoteTag)
6 changes: 4 additions & 2 deletions lib/jekyll/scholar/tags/reference.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ def initialize(tag_name, arguments, tokens)
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

Expand Down
51 changes: 39 additions & 12 deletions lib/jekyll/scholar/utilities.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,17 @@ class Scholar
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?
Expand All @@ -42,6 +52,10 @@ def optparse(arguments)
@text = text
end

opts.on('-l', '--locator LOCATOR') do |locator|
locators << locator
end

opts.on('-s', '--style STYLE') do |style|
@style = style
end
Expand All @@ -51,11 +65,15 @@ def optparse(arguments)
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
Expand Down Expand Up @@ -260,8 +278,12 @@ def renderer
: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)
Expand All @@ -274,18 +296,23 @@ def citation_item_for(entry)
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)
Expand Down

0 comments on commit b99e77a

Please sign in to comment.