Skip to content

Instantly share code, notes, and snippets.

@inukshuk
Created January 27, 2014 21:44
Show Gist options
  • Save inukshuk/8657987 to your computer and use it in GitHub Desktop.
Save inukshuk/8657987 to your computer and use it in GitHub Desktop.
Experiment with the CiteProc-Ruby pre-release!
require 'nokogiri' # optional
require 'edtf' # optional
require 'citeproc'
require 'bibtex'
require 'csl/styles'
cp = CiteProc::Processor.new locale: 'en', style: 'apa', format: 'html'
bib = BibTeX.parse(<<-EOS)
@article{greenwade93,
author = "George D. Greenwade",
title = "The {C}omprehensive {T}ex {A}rchive {N}etwork ({CTAN})",
year = "1993",
journal = "TUGBoat",
volume = "14",
number = "3",
pages = "342--351"
}
EOS
bib.convert :latex
cp.import bib.to_citeproc
puts cp.render :citation, id: 'greenwade93', locator: '23'
puts cp.render :citation, id: 'greenwade93', locator: '23-26'
puts cp.render :bibliography, id: 'greenwade93'
# Change the style
cp.options[:style] = 'chicago-author-date'
cp.engine.update!
puts cp.render :bibliography, id: 'greenwade93'
# Parse the CSL Snippet below (can be a full style too)
csl = CSL.parse!(DATA)
# Fetch a citation item from the processor
item = cp['greenwade93']
# Render the item with the csl snippet
puts cp.engine.renderer.render item.cite, csl
__END__
<bibliography>
<layout suffix=".">
<group delimiter=" ">
<label variable="page"/>
<text variable="page"/>
</group>
</layout>
</bibliography>
source 'https://rubygems.org'
gem 'citeproc', :git => 'https://github.com/inukshuk/citeproc.git'
gem 'citeproc-ruby', :git => 'https://github.com/inukshuk/citeproc-ruby.git'
gem 'csl', :git => 'https://github.com/inukshuk/csl-ruby.git'
gem 'nokogiri' # not required: speeds up XML parsing
gem 'edtf' # not required: more advanced name parsers
gem 'bibtex-ruby'
# not required but convenient: gives you
# local access to all CSL styles and locales
gem 'csl-styles'
@inukshuk
Copy link
Author

Put these files in a directory. Then run:

$ bundle install
$ bundle exec ruby example.rb

Have fun!

@inukshuk
Copy link
Author

Oh, this should produce the following output:

(Greenwade, 1993, p. 23)
(Greenwade, 1993, pp. 23-26)
Greenwade, G. D. (1993). The Comprehensive Tex Archive Network (CTAN). <i>TUGBoat</i>, <i>14</i>(3), 342–351.
Greenwade, George D. 1993. “The Comprehensive Tex Archive Network (Ctan).” <i>TUGBoat</i> 14 (3): 342–51.
pages 342–351.

@andriusvelykis
Copy link

I tried running this just now, getting an error:

(Greenwade, 1993, p. 23)
(Greenwade, 1993, pp. 23-26)
/Users/andrius/.rvm/gems/ruby-1.9.3-p374/bundler/gems/citeproc-ruby-4134687ebcc9/lib/citeproc/ruby/renderer/label.rb:15:in `render_label': undefined method `page?' for nil:NilClass (NoMethodError)
    from /Users/andrius/.rvm/gems/ruby-1.9.3-p374/bundler/gems/citeproc-ruby-4134687ebcc9/lib/citeproc/ruby/renderer/names.rb:80:in `block in render_names'
    from /Users/andrius/.rvm/gems/ruby-1.9.3-p374/bundler/gems/citeproc-ruby-4134687ebcc9/lib/citeproc/ruby/renderer/names.rb:78:in `map!'
    from /Users/andrius/.rvm/gems/ruby-1.9.3-p374/bundler/gems/citeproc-ruby-4134687ebcc9/lib/citeproc/ruby/renderer/names.rb:78:in `render_names'
    from /Users/andrius/.rvm/gems/ruby-1.9.3-p374/bundler/gems/citeproc-ruby-4134687ebcc9/lib/citeproc/ruby/renderer.rb:29:in `render'
    from /Users/andrius/.rvm/gems/ruby-1.9.3-p374/bundler/gems/citeproc-ruby-4134687ebcc9/lib/citeproc/ruby/renderer/macro.rb:13:in `block in render_macro'
    from /Users/andrius/.rvm/gems/ruby-1.9.3-p374/bundler/gems/csl-ruby-72d9bb15d8ac/lib/csl/treelike.rb:22:in `each'
    from /Users/andrius/.rvm/gems/ruby-1.9.3-p374/bundler/gems/csl-ruby-72d9bb15d8ac/lib/csl/treelike.rb:22:in `each_child'
    from /Users/andrius/.rvm/gems/ruby-1.9.3-p374/bundler/gems/citeproc-ruby-4134687ebcc9/lib/citeproc/ruby/renderer/macro.rb:12:in `each'
    from /Users/andrius/.rvm/gems/ruby-1.9.3-p374/bundler/gems/citeproc-ruby-4134687ebcc9/lib/citeproc/ruby/renderer/macro.rb:12:in `map'
    from /Users/andrius/.rvm/gems/ruby-1.9.3-p374/bundler/gems/citeproc-ruby-4134687ebcc9/lib/citeproc/ruby/renderer/macro.rb:12:in `render_macro'
    from /Users/andrius/.rvm/gems/ruby-1.9.3-p374/bundler/gems/citeproc-ruby-4134687ebcc9/lib/citeproc/ruby/renderer.rb:29:in `render'
    from /Users/andrius/.rvm/gems/ruby-1.9.3-p374/bundler/gems/citeproc-ruby-4134687ebcc9/lib/citeproc/ruby/renderer/text.rb:39:in `render_text'
    from /Users/andrius/.rvm/gems/ruby-1.9.3-p374/bundler/gems/citeproc-ruby-4134687ebcc9/lib/citeproc/ruby/renderer.rb:29:in `render'
    from /Users/andrius/.rvm/gems/ruby-1.9.3-p374/bundler/gems/citeproc-ruby-4134687ebcc9/lib/citeproc/ruby/renderer/group.rb:17:in `block in render_group'
    from /Users/andrius/.rvm/gems/ruby-1.9.3-p374/bundler/gems/csl-ruby-72d9bb15d8ac/lib/csl/treelike.rb:22:in `each'
    from /Users/andrius/.rvm/gems/ruby-1.9.3-p374/bundler/gems/csl-ruby-72d9bb15d8ac/lib/csl/treelike.rb:22:in `each_child'
    from /Users/andrius/.rvm/gems/ruby-1.9.3-p374/bundler/gems/citeproc-ruby-4134687ebcc9/lib/citeproc/ruby/renderer/group.rb:16:in `each'
    from /Users/andrius/.rvm/gems/ruby-1.9.3-p374/bundler/gems/citeproc-ruby-4134687ebcc9/lib/citeproc/ruby/renderer/group.rb:16:in `map'
    from /Users/andrius/.rvm/gems/ruby-1.9.3-p374/bundler/gems/citeproc-ruby-4134687ebcc9/lib/citeproc/ruby/renderer/group.rb:16:in `render_group'
    from /Users/andrius/.rvm/gems/ruby-1.9.3-p374/bundler/gems/citeproc-ruby-4134687ebcc9/lib/citeproc/ruby/renderer.rb:29:in `render'
    from /Users/andrius/.rvm/gems/ruby-1.9.3-p374/bundler/gems/citeproc-ruby-4134687ebcc9/lib/citeproc/ruby/renderer/group.rb:17:in `block in render_group'
    from /Users/andrius/.rvm/gems/ruby-1.9.3-p374/bundler/gems/csl-ruby-72d9bb15d8ac/lib/csl/treelike.rb:22:in `each'
    from /Users/andrius/.rvm/gems/ruby-1.9.3-p374/bundler/gems/csl-ruby-72d9bb15d8ac/lib/csl/treelike.rb:22:in `each_child'
    from /Users/andrius/.rvm/gems/ruby-1.9.3-p374/bundler/gems/citeproc-ruby-4134687ebcc9/lib/citeproc/ruby/renderer/group.rb:16:in `each'
    from /Users/andrius/.rvm/gems/ruby-1.9.3-p374/bundler/gems/citeproc-ruby-4134687ebcc9/lib/citeproc/ruby/renderer/group.rb:16:in `map'
    from /Users/andrius/.rvm/gems/ruby-1.9.3-p374/bundler/gems/citeproc-ruby-4134687ebcc9/lib/citeproc/ruby/renderer/group.rb:16:in `render_group'
    from /Users/andrius/.rvm/gems/ruby-1.9.3-p374/bundler/gems/citeproc-ruby-4134687ebcc9/lib/citeproc/ruby/renderer.rb:29:in `render'
    from /Users/andrius/.rvm/gems/ruby-1.9.3-p374/bundler/gems/citeproc-ruby-4134687ebcc9/lib/citeproc/ruby/renderer/layout.rb:13:in `block in render_layout'
    from /Users/andrius/.rvm/gems/ruby-1.9.3-p374/bundler/gems/csl-ruby-72d9bb15d8ac/lib/csl/treelike.rb:22:in `each'
    from /Users/andrius/.rvm/gems/ruby-1.9.3-p374/bundler/gems/csl-ruby-72d9bb15d8ac/lib/csl/treelike.rb:22:in `each_child'
    from /Users/andrius/.rvm/gems/ruby-1.9.3-p374/bundler/gems/citeproc-ruby-4134687ebcc9/lib/citeproc/ruby/renderer/layout.rb:12:in `each'
    from /Users/andrius/.rvm/gems/ruby-1.9.3-p374/bundler/gems/citeproc-ruby-4134687ebcc9/lib/citeproc/ruby/renderer/layout.rb:12:in `map'
    from /Users/andrius/.rvm/gems/ruby-1.9.3-p374/bundler/gems/citeproc-ruby-4134687ebcc9/lib/citeproc/ruby/renderer/layout.rb:12:in `render_layout'
    from /Users/andrius/.rvm/gems/ruby-1.9.3-p374/bundler/gems/citeproc-ruby-4134687ebcc9/lib/citeproc/ruby/renderer.rb:29:in `render'
    from /Users/andrius/.rvm/gems/ruby-1.9.3-p374/bundler/gems/citeproc-ruby-4134687ebcc9/lib/citeproc/ruby/renderer.rb:49:in `render_bibliography'
    from /Users/andrius/.rvm/gems/ruby-1.9.3-p374/bundler/gems/citeproc-ruby-4134687ebcc9/lib/citeproc/ruby/renderer.rb:29:in `render'
    from /Users/andrius/.rvm/gems/ruby-1.9.3-p374/bundler/gems/citeproc-ruby-4134687ebcc9/lib/citeproc/ruby/engine.rb:71:in `block in render'
    from /Users/andrius/.rvm/gems/ruby-1.9.3-p374/bundler/gems/citeproc-5788affbc6ef/lib/citeproc/citation_data.rb:167:in `each'
    from /Users/andrius/.rvm/gems/ruby-1.9.3-p374/bundler/gems/citeproc-5788affbc6ef/lib/citeproc/citation_data.rb:167:in `each'
    from /Users/andrius/.rvm/gems/ruby-1.9.3-p374/bundler/gems/citeproc-ruby-4134687ebcc9/lib/citeproc/ruby/engine.rb:69:in `map'
    from /Users/andrius/.rvm/gems/ruby-1.9.3-p374/bundler/gems/citeproc-ruby-4134687ebcc9/lib/citeproc/ruby/engine.rb:69:in `render'
    from /Users/andrius/.rvm/gems/ruby-1.9.3-p374/bundler/gems/citeproc-5788affbc6ef/lib/citeproc/processor.rb:103:in `render'
    from example.rb:30:in `<main>'

The dependencies during bundle install are the following:

Using i18n (0.6.9)
Using minitest (4.7.5)
Using multi_json (1.8.4)
Using atomic (1.1.14)
Using thread_safe (0.1.3)
Using tzinfo (0.3.38)
Using activesupport (4.0.2)
Using unicode (0.4.4)
Using latex-decode (0.1.1)
Using bibtex-ruby (3.1.1)
Using namae (0.8.3)
Using citeproc (1.0.0.pre12) from https://github.com/inukshuk/citeproc.git (at master)
Using citeproc-ruby (1.0.0.pre1) from https://github.com/inukshuk/citeproc-ruby.git (at master)
Using csl (1.1.0) from https://github.com/inukshuk/csl-ruby.git (at master)
Installing csl-styles (1.0.1.4)
Using edtf (2.0.0)
Using mini_portile (0.5.2)
Using nokogiri (1.6.1)
Using bundler (1.5.2)

@andriusvelykis
Copy link

Weird, it works now :) Dunno what has changed (I have rebuilt it)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment