Commit b515a535 authored by David Moxey's avatar David Moxey

Merge remote-tracking branch 'upstream/master'

parents b0b74cfb 733e79b2
......@@ -255,6 +255,23 @@ For example, this could be rendered as:
</cite>
</blockquote>
#### Multiple citation
You can cite multiple items in a single citation by referencing all ids
of the items you wish to quote separated by spaces. For example,
`{% cite ruby microscope %}` would produce a cite tag like:
<a href="#ruby">(Flanagan &amp; Matsumoto 2008; Shaughnessy 2013)</a>
#### Page numbers and locators
If you would like to add page numbers to your citation, you can use the
`-l` or `--locator` option. For example, `{% cite ruby -l 23-5 %}` would
produce a citation like `(Matsumoto, 2008, pp. 23-5)`.
When quoting multiple items (see above) you can add multiple locators after
the list of ids. For example, `{% cite ruby microscope -l 2 -l 24 & 32 %}`.
#### Displaying formatted references
If you want to display the full formatted reference entry, you can use the
......@@ -446,7 +463,7 @@ License
Jekyll-Scholar is distributed under the same license as Jekyll.
Copyright (c) 2011-2013 [Sylvester Keil](http://sylvester.keil.or.at/)
Copyright (c) 2011-2014 [Sylvester Keil](http://sylvester.keil.or.at/)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the 'Software'), to deal
......
......@@ -204,7 +204,7 @@ Feature: BibTeX
"""
---
---
{% bibliography --style mla %}
{% bibliography --style modern-language-association %}
"""
When I run jekyll
Then the _site directory should exist
......
......@@ -111,3 +111,107 @@ 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 &amp; 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 &amp; 42" in "_site/scholar.html"
@tags @cite @citation_number
Scenario: Multiple citations using citation numbers
Given I have a scholar configuration with:
| key | value |
| source | ./_bibliography |
| bibliography | my_references |
| style | ieee |
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 "\[1\], \[2\]" in "_site/scholar.html"
Feature: Cited-only Bibliographies
As a scholar who likes to blog
I want to cite references on my website
And generate bibliographies for the cited items
Scenario: Cited-only references from a single bibliography
Given I have a scholar configuration with:
| key | value |
| source | ./_bibliography |
And I have a "_bibliography" directory
And I have a file "_bibliography/references.bib":
"""
@book{ruby,
title = {The Ruby Programming Language},
author = {Flanagan, David and Matsumoto, Yukihiro},
year = {2008},
publisher = {O'Reilly Media}
},
@book{smalltalk,
title = {Smalltalk Best Practice Patterns},
author = {Kent Beck},
year = {1996},
publisher = {Prentice Hall}
}
"""
And I have a page "scholar.html":
"""
---
---
{% cite smalltalk %}
{% bibliography --cited %}
"""
When I run jekyll
Then the _site directory should exist
And the "_site/scholar.html" file should exist
And I should not see "<i>The Ruby Programming Language</i>" in "_site/scholar.html"
And I should see "<i>Smalltalk Best Practice Patterns</i>" in "_site/scholar.html"
Scenario: No-cited items result in empty bibliography
Given I have a scholar configuration with:
| key | value |
| source | ./_bibliography |
And I have a "_bibliography" directory
And I have a file "_bibliography/references.bib":
"""
@book{ruby,
title = {The Ruby Programming Language},
author = {Flanagan, David and Matsumoto, Yukihiro},
year = {2008},
publisher = {O'Reilly Media}
},
@book{smalltalk,
title = {Smalltalk Best Practice Patterns},
author = {Kent Beck},
year = {1996},
publisher = {Prentice Hall}
}
"""
And I have a page "scholar.html":
"""
---
---
{% bibliography --cited %}
"""
When I run jekyll
Then the _site directory should exist
And the "_site/scholar.html" file should exist
And I should not see "<i>The Ruby Programming Language</i>" in "_site/scholar.html"
And I should not see "<i>Smalltalk Best Practice Patterns</i>" in "_site/scholar.html"
......@@ -44,7 +44,6 @@ 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
......
Given /^I have a "([^"]*)" directory/ do |dir|
Given(/^I have a "([^"]*)" directory/) do |dir|
FileUtils.mkdir(dir)
end
Given /^I have a (?:page|file) "([^"]*)":$/ do |file, string|
Given(/^I have a (?:page|file) "([^"]*)":$/) do |file, string|
File.open(file, 'w') do |f|
f.write(string)
end
end
Given /^I have a configuration file with "([^\"]*)" set to:$/ do |key, table|
Given(/^I have a configuration file with "([^\"]*)" set to:$/) do |key, table|
File.open('_config.yml', 'w') do |f|
f.write("#{key}:\n")
table.hashes.each do |row|
......@@ -18,7 +18,7 @@ Given /^I have a configuration file with "([^\"]*)" set to:$/ do |key, table|
end
end
Given /^I have a scholar configuration with:$/ do |table|
Given(/^I have a scholar configuration with:$/) do |table|
File.open('_config.yml', 'w') do |f|
f.write("scholar:\n")
table.hashes.each do |row|
......@@ -28,7 +28,7 @@ Given /^I have a scholar configuration with:$/ do |table|
end
Then /^"(.*)" should come before "(.*)" in "(.*)"$/ do |p1, p2, file|
Then(/^"(.*)" should come before "(.*)" in "(.*)"$/) do |p1, p2, file|
data = File.open(file).readlines.join('')
m1 = data.match(p1)
......
Before do
FileUtils.mkdir_p(TEST_DIR) unless File.exist?(TEST_DIR)
FileUtils.rm_rf(TEST_DIR) if File.exist?(TEST_DIR)
FileUtils.mkdir_p(TEST_DIR)
Dir.chdir(TEST_DIR)
end
......
......@@ -26,7 +26,8 @@ Gem::Specification.new do |s|
s.rubyforge_project = s.name
s.add_runtime_dependency('jekyll', '~> 1.0')
s.add_runtime_dependency('citeproc-ruby', '~> 0.0.6')
s.add_runtime_dependency('citeproc-ruby', '~> 1.0')
s.add_runtime_dependency('csl-styles', '~> 1.0')
s.add_runtime_dependency('bibtex-ruby', '~> 3.0')
s.files = `git ls-files`.split("\n")
......
......@@ -4,7 +4,8 @@ require 'jekyll'
require 'optparse'
require 'bibtex'
require 'citeproc'
require 'citeproc/ruby'
require 'csl/styles'
require 'jekyll/scholar/version'
require 'jekyll/scholar/defaults'
......
module Jekyll
class Scholar
class BibTeXConverter < Converter
include Scholar::Utilities
safe true
priority :highest
attr_reader :config
attr_reader :config
@pattern = (/bib(tex)?$/i).freeze
@extension = '.html'.freeze
class << self
attr_reader :pattern, :extension
end
class << self
attr_reader :pattern, :extension
end
def initialize(config = {})
super
@config['scholar'] = Scholar.defaults.merge(@config['scholar'] || {})
@markdown = Jekyll::Converters::Markdown.new(config)
end
def matches(extension)
extension =~ BibTeXConverter.pattern
end
extension =~ BibTeXConverter.pattern
end
def output_ext(extension)
BibTeXConverter.extension
end
BibTeXConverter.extension
end
def convert(content)
content = BibTeX.parse(content, :strict => true, :include => [:meta_content], :filter => [:latex]).map do |b|
content = BibTeX.parse(content, :strict => true, :include => [:meta_content], :filter => [:latex]).map do |b|
if b.respond_to?(:to_citeproc)
CiteProc.process b.to_citeproc, :style => config['style'],
:locale => config['locale'], :format => 'html'
render_bibliography b
else
b.is_a?(BibTeX::MetaContent) ? b.to_s : ''
b.is_a?(BibTeX::MetaContent) ? b.to_s : ''
end
end
@markdown.convert(content.join("\n"))
end
end
end
end
\ No newline at end of file
end
......@@ -38,15 +38,6 @@ module Jekyll
end
end
private
def citeproc
@citeproc ||= CiteProc::Processor.new do |p|
p.style = config['style']
p.format = 'html'
p.locale = config['locale']
end
end
end
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,16 +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]
rescue
"(#{key})"
keys.map { |key|
reference_tag bibliography[key]
}.join("\n")
end
end
......
module Jekyll
class Scholar
# Load styles into static memory.
# They should be thread safe as long as they are
# treated as being read-only.
STYLES = Hash.new do |h, k|
h[k.to_s] = CSL::Style.load k
end
# Utility methods used by several Scholar plugins. The methods in this
# module may depend on the presence of #config, #bibtex_files, and
# #site readers
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?
......@@ -34,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
......@@ -43,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
......@@ -145,12 +171,11 @@ module Jekyll
p
end
def reference_tag(entry)
def reference_tag(entry, index = nil)
return missing_reference unless entry
entry = entry.convert(*bibtex_filters) unless bibtex_filters.empty?
reference = CiteProc.process entry.to_citeproc,
:style => style, :locale => config['locale'], :format => 'html'
reference = render_bibliography entry, index
content_tag reference_tagname, reference,
:id => [prefix, entry.key].compact.join('-')
......@@ -192,7 +217,7 @@ module Jekyll
liquid_template.render({
'entry' => liquidify(entry),
'reference' => reference_tag(entry),
'reference' => reference_tag(entry, index),
'key' => entry.key,
'type' => entry.type,
'link' => repository_link_for(entry),
......@@ -253,23 +278,55 @@ module Jekyll
config['details_dir']
end
def cite(key)
context['cited'] ||= []
context['cited'] << key
def renderer
@renderer ||= CiteProc::Ruby::Renderer.new :format => 'html',
:style => style, :locale => config['locale']
end
if bibliography.key?(key)
entry = bibliography[key]
entry = entry.convert(*bibtex_filters) unless bibtex_filters.empty?
def render_citation(items)
renderer.render items.zip(locators).map { |entry, locator|
cited_keys << entry.key
citation = CiteProc.process entry.to_citeproc, :style => style,
:locale => config['locale'], :format => 'html', :mode => :citation
item = citation_item_for entry, citation_number
item.locator = locator
link_to "##{[prefix, entry.key].compact.join('-')}", citation.join
else
missing_reference
item
}, STYLES[style].citation
end
def render_bibliography(entry, index = nil)
renderer.render citation_item_for(entry, index),
STYLES[style].bibliography
end
def citation_item_for(entry, citation_number = nil)
CiteProc::CitationItem.new id: entry.id do |c|
c.data = CiteProc::Item.new entry.to_citeproc
c.data[:'citation-number'] = citation_number
end
rescue
"(#{key})"
end
def cited_keys
context['cited'] ||= []
end
def citation_number
number = context['citation_number'] || 1
context['citation_number'] = number.succ
number
end
def cite(keys)
items = keys.map do |key|
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)
......
module Jekyll
class Scholar
VERSION = '2.0.0'.freeze
VERSION = '3.0.0'.freeze
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