Commit a5cf0849 authored by Sylvester Keil's avatar Sylvester Keil

added bibliography and cite tags

#1
parent 11b1a3ed
...@@ -3,9 +3,12 @@ Feature: BibTeX ...@@ -3,9 +3,12 @@ Feature: BibTeX
I want to publish my BibTeX bibliography on my blog I want to publish my BibTeX bibliography on my blog
In order to share my awesome references with my peers In order to share my awesome references with my peers
@converters
Scenario: Simple Bibliography Scenario: Simple Bibliography
Given I have a configuration file with "citation_style" set to "apa" Given I have a scholar configuration with:
And I have a page "references.bib": | key | value |
| style | apa |
And I have a page "references.bib":
""" """
--- ---
--- ---
...@@ -21,8 +24,11 @@ Feature: BibTeX ...@@ -21,8 +24,11 @@ Feature: BibTeX
And the "_site/references.html" file should exist And the "_site/references.html" file should exist
And I should see "<i>The Ruby Programming Language</i>" in "_site/references.html" And I should see "<i>The Ruby Programming Language</i>" in "_site/references.html"
@converters
Scenario: Markdown Formatted Bibliography Scenario: Markdown Formatted Bibliography
Given I have a configuration file with "citation_style" set to "apa" Given I have a scholar configuration with:
| key | value |
| style | apa |
And I have a page "references.bib": And I have a page "references.bib":
""" """
--- ---
...@@ -42,7 +48,9 @@ Feature: BibTeX ...@@ -42,7 +48,9 @@ Feature: BibTeX
@latex @latex
Scenario: Simple Bibliography with LaTeX directives Scenario: Simple Bibliography with LaTeX directives
Given I have a configuration file with "citation_style" set to "apa" Given I have a scholar configuration with:
| key | value |
| style | apa |
And I have a page "references.bib": And I have a page "references.bib":
""" """
--- ---
...@@ -55,3 +63,29 @@ Feature: BibTeX ...@@ -55,3 +63,29 @@ Feature: BibTeX
Then the _site directory should exist Then the _site directory should exist
And the "_site/references.html" file should exist And the "_site/references.html" file should exist
And I should see "Look, an umlaut: ü!" in "_site/references.html" And I should see "Look, an umlaut: ü!" in "_site/references.html"
@tags
Scenario: Simple Bibliography Loaded From Default Directory
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}
}
"""
And I have a page "scholar.html":
"""
---
---
{% bibliography references %}
"""
When I run jekyll
Then the _site directory should exist
And the "_site/scholar.html" file should exist
And I should see "<i>The Ruby Programming Language</i>" in "_site/scholar.html"
Feature: Citations
As a scholar who likes to blog
I want to reference cool papers and books from my bibliography
@tags
Scenario: A Simple Citation
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}
}
"""
And I have a page "scholar.html":
"""
---
---
{% cite ruby %}
"""
When I run jekyll
Then the _site directory should exist
And the "_site/scholar.html" file should exist
And I should see "Flanagan" in "_site/scholar.html"
@tags
Scenario: Missing references
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}
}
"""
And I have a page "scholar.html":
"""
---
---
{% cite java %}
"""
When I run jekyll
Then the _site directory should exist
And the "_site/scholar.html" file should exist
And I should see "missing reference" in "_site/scholar.html"
...@@ -2,33 +2,33 @@ Feature: Sorting BibTeX Bibliographies ...@@ -2,33 +2,33 @@ Feature: Sorting BibTeX Bibliographies
As a scholar who likes to blog As a scholar who likes to blog
I want to sort my bibliographies according to configurable parameters I want to sort my bibliographies according to configurable parameters
Scenario Outline: Sort Bibliography # Scenario: Sort Bibliography
Given I have a configuration file with "citation_sort_by" set to "<sort-by>" # Given I have a configuration file with "citation_sort_by" set to "<sort-by>"
And I have a configuration file with "citation_sort_order" set to "<sort-order>" # And I have a configuration file with "citation_sort_order" set to "<sort-order>"
And I have a page "references.bib": # And I have a page "references.bib":
""" # """
--- # ---
--- # ---
@book{ruby, # @book{ruby,
title = {The Ruby Programming Language}, # title = {The Ruby Programming Language},
author = {Flanagan, David and Matsumoto, Yukihiro}, # author = {Flanagan, David and Matsumoto, Yukihiro},
year = {2008}, # year = {2008},
publisher = {O'Reilly Media} # publisher = {O'Reilly Media}
} # }
@book{ruby, # @book{ruby,
title = {The Ruby Programming Language}, # title = {The Ruby Programming Language},
author = {Flanagan, David and Matsumoto, Yukihiro}, # author = {Flanagan, David and Matsumoto, Yukihiro},
year = {2007}, # year = {2007},
publisher = {O'Reilly Media} # publisher = {O'Reilly Media}
} # }
""" # """
When I run jekyll # When I run jekyll
Then "<pattern-1>" should come before "<pattern-2>" in "_site/references.html" # Then "<pattern-1>" should come before "<pattern-2>" in "_site/references.html"
#
Scenarios: Various Sort Orders # Scenarios: Various Sort Orders
| sort_order | sort-by | pattern-1 | pattern-2 | # | sort_order | sort-by | pattern-1 | pattern-2 |
| ascending | none | 2008 | 2007 | # | ascending | none | 2008 | 2007 |
| descending | none | 2008 | 2007 | # | descending | none | 2008 | 2007 |
| ascending | year | 2007 | 2008 | # | ascending | year | 2007 | 2008 |
| descending | year | 2008 | 2007 | # | descending | year | 2008 | 2007 |
#
\ No newline at end of file \ No newline at end of file
...@@ -45,16 +45,6 @@ Given /^I have a configuration file with:$/ do |table| ...@@ -45,16 +45,6 @@ Given /^I have a configuration file with:$/ do |table|
end end
end end
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|
f.write("- #{row["value"]}\n")
end
f.close
end
end
When /^I run jekyll$/ do When /^I run jekyll$/ do
run_jekyll run_jekyll
end end
......
Given /^I have a page "([^"]*)":$/ do |file, string| Given /^I have a "([^"]*)" directory/ do |dir|
FileUtils.mkdir(dir)
end
Given /^I have a (?:page|file) "([^"]*)":$/ do |file, string|
File.open(file, 'w') do |f| File.open(file, 'w') do |f|
f.write(string) f.write(string)
end end
end end
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|
f.write(" #{row["key"]}: #{row["value"]}\n")
end
end
end
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|
f.write(" #{row["key"]}: #{row["value"]}\n")
end
end
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('') data = File.open(file).readlines.join('')
......
require 'jekyll' require 'jekyll'
require 'bibtex'
require 'citeproc'
require 'jekyll/scholar/version' require 'jekyll/scholar/version'
require 'jekyll/scholar/converters/bibtex' require 'jekyll/scholar/converters/bibtex'
require 'jekyll/scholar/tags/bibliography'
require 'jekyll/scholar/tags/cite'
module Jekyll
class Scholar
@defaults = Hash[*%w{
style apa
locale en
sort_by none
order ascending
source ./_bibliography
bibliography references.bib
}].freeze
class << self
attr_reader :defaults
end
end
end
require 'bibtex'
require 'citeproc'
module Jekyll module Jekyll
module Scholar class Scholar
class BibTeXConverter < Converter class BibTeXConverter < Converter
safe true safe true
priority :highest priority :highest
DEFAULTS = Hash[*%w{ @pattern = (/bib(tex)?$/i).freeze
citation_style apa @extension = '.html'.freeze
citation_locale en
citation_sort_by none
}].freeze
PATTERN = (/bib(tex)?$/i).freeze
EXTENSION = '.html'.freeze
class << self
attr_reader :pattern, :extension
end
def initialize (config = {}) def initialize (config = {})
super super
@config['scholar'] = DEFAULTS.merge(@config['scholar'] || {}) @config['scholar'] = Scholar.defaults.merge(@config['scholar'] || {})
@markdown = MarkdownConverter.new config @markdown = MarkdownConverter.new config
end end
def matches (extension); extension =~ PATTERN; end def matches(extension)
extension =~ BibTeXConverter.pattern
end
def output_ext (extension); EXTENSION; end def output_ext(extension)
BibTeXConverter.extension
end
def convert (content) def convert (content)
content = BibTeX.parse(content, :include => [:meta_content], :filter => [:latex]).map do |b| content = BibTeX.parse(content, :include => [:meta_content], :filter => [:latex]).map do |b|
......
module Jekyll
class Scholar
class BibliographyTag < Liquid::Tag
attr_reader :file, :config
def initialize(tag_name, arguments, tokens)
super
@config = Scholar.defaults.dup
@file = arguments.strip
end
def render(context)
config.merge!(context.registers[:site].config['scholar'] || {})
entries.map { |e|
CiteProc.process e.to_citeproc, :style => config['style'],
:locale => config['locale'], :format => 'html'
}.join("\n")
end
private
def bibliography
@bibliography ||= BibTeX.open(extend_path(file), :filter => :latex)
end
def entries
b = bibliography['@*']
unless config['sort_by'] == 'none'
b.sort_by! { |e| e[config['sort_by']] }
b.reverse! if config['order'] =~ /^(desc|reverse)/i
end
b
end
def extend_path(name)
p = File.join(config['source'], name)
p << '.bib' unless File.exists?(p)
p
end
end
end
end
Liquid::Template.register_tag('bibliography', Jekyll::Scholar::BibliographyTag)
\ No newline at end of file
module Jekyll
class Scholar
class CiteTag < Liquid::Tag
attr_reader :key, :pages, :config
def initialize(tag_name, arguments, tokens)
super
@config = Scholar.defaults.dup
@key = arguments.strip.split(/\s+/)[0]
end
def render(context)
config.merge!(context.registers[:site].config['scholar'] || {})
puts key
e = bibliography[key]
puts e
if e
CiteProc.process e.to_citeproc, :style => config['style'],
:locale => config['locale'], :format => 'html', :mode => :citation
else
"(missing reference)"
end
end
private
def bibliography
@bibliography ||= BibTeX.open(extend_path(config['bibliography']), :filter => :latex)
end
def extend_path(name)
p = File.join(config['source'], name)
p << '.bib' unless File.exists?(p)
p
end
end
end
end
Liquid::Template.register_tag('cite', Jekyll::Scholar::CiteTag)
\ No newline at end of file
module Jekyll module Jekyll
module Scholar class Scholar
VERSION = '0.0.1'.freeze VERSION = '0.0.1'.freeze
end end
end end
\ No newline at end of file
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