inukshuk / citeproc-ruby

A Citation Style Language (CSL) Cite Processor
103 stars 23 forks source link

How to render "dependent" styles? #48

Closed joshweir closed 7 years ago

joshweir commented 7 years ago

I want to render styles in both the CSL::Style.root directory and the dependent sub directory, currently i can only render styles in the CSL::Style.root and not the dependent sub dir. How to do this?

This works fine because the apa style sits in the CSL::Style.root directory:

require 'citeproc'
require 'csl/styles'

cp = CiteProc::Processor.new style: "apa", format: 'html'
cp << CiteProc::Item.new(:id => 'theid', :title => 'The Title')
cp.render :bibliography, id: 'theid'

If I want try to render the 3-biotech style (which sits in the dependent sub directory) I get error:

cp = CiteProc::Processor.new style: "3-biotech", format: 'html'
cp << CiteProc::Item.new(:id => 'theid', :title => 'The Title')
cp.render :bibliography, id: 'theid'
CSL::ParseError: failed to extract CSL data from "3-biotech": No such file or directory @ rb_sysopen - 3-biotech
        from /home/resrev/.rvm/gems/ruby-2.3.1/gems/csl-1.4.5/lib/csl/loader.rb:90:in `rescue in extract_data_from'
        from /home/resrev/.rvm/gems/ruby-2.3.1/gems/csl-1.4.5/lib/csl/loader.rb:88:in `extract_data_from'
        from /home/resrev/.rvm/gems/ruby-2.3.1/gems/csl-1.4.5/lib/csl/loader.rb:31:in `load'

I think this is because the CSL::Style.root is not pointing to this dependent sub directory - but if I change this to point to the sub directory i wont be able to call the apa style any longer as its in the parent directory?

Even if I do make this change, i get another error:

CSL::Style.root = File.join(CSL::Style.root,"dependent")
cp.render :bibliography, id: 'theid'
ArgumentError: no CSL node: nil
        from /home/resrev/.rvm/gems/ruby-2.3.1/gems/citeproc-ruby-1.1.6/lib/citeproc/ruby/renderer.rb:37:in `render'
        from /home/resrev/.rvm/gems/ruby-2.3.1/gems/citeproc-ruby-1.1.6/lib/citeproc/ruby/engine.rb:95:in `block in render'
        from /home/resrev/.rvm/gems/ruby-2.3.1/gems/citeproc-1.0.5/lib/citeproc/citation_data.rb:177:in `each'
        from /home/resrev/.rvm/gems/ruby-2.3.1/gems/citeproc-1.0.5/lib/citeproc/citation_data.rb:177:in `each'
        from /home/resrev/.rvm/gems/ruby-2.3.1/gems/citeproc-ruby-1.1.6/lib/citeproc/ruby/engine.rb:93:in `map'
        from /home/resrev/.rvm/gems/ruby-2.3.1/gems/citeproc-ruby-1.1.6/lib/citeproc/ruby/engine.rb:93:in `render'
        from /home/resrev/.rvm/gems/ruby-2.3.1/gems/citeproc-1.0.5/lib/citeproc/processor.rb:113:in `render'
        from (irb):23

Thanks for this nice library btw :)

inukshuk commented 7 years ago

There is currently no shortcut to loading dependent styles, so you need to pass the full path to the style to load it. Note that you can't do much with a dependent style, but load its independent parent style (in theory could have different locale information, but when I last checked, no style in the official repo actually does that).

For context, also see: https://github.com/inukshuk/csl-ruby/issues/6 https://github.com/inukshuk/jekyll-scholar/issues/108

joshweir commented 7 years ago

I made a change to address the rendering of dependent styles in this commit:

https://github.com/joshweir/citeproc-ruby/commit/8c8fbe16e24521a7b0f59fcb368b94ec8eb13fda

I retrieve the independent_parent within the render method and not in the initialize method because I figure that the concern with the parent is only rendering and the dependent style may be used directly elsewhere within Engine.

If this is something worth merging into main im happy to submit pull request, otherwise if there is any issues with this im happy to modify.

Thanks again.

inukshuk commented 7 years ago

We do something similar in jekyll-scholar (namely, simply load the independent parent is a user loads a dependent style), but if we do it in citeproc-ruby I suggest that we should check for and merge (or replace?) the style's locale data (and check the spec to see if there was anything else that dependent styles could define). In other words, when loading a dependent style, we should automatically try to load the independent parent and merge the two into a virtual style which can be used to render citations. Happy to accept a PR if you want to give it a try!