larsgw / citation.js

Citation.js converts formats like BibTeX, Wikidata JSON and ContentMine JSON to CSL-JSON to convert to other formats like APA, Vancouver and back to BibTeX.
https://citation.js.org/
MIT License
226 stars 30 forks source link

options with a custom csl template #28

Closed argirok closed 7 years ago

argirok commented 7 years ago

Hello, I am trying to get citations providing a csl template, but it seems to ignore the template and return a default citation format (apa I think). Is this feature provided? Is something wrong with the following options?

var opt = { format: 'string', type : 'string', style : 'citation', lang : 'en-US', template : <XML_CSL> };

Thanks!

larsgw commented 7 years ago

The custom template thing is currently a bit fiddly, this should be fixed in v0.3 and the accompanying API update (async input parsing, Cite prototypes, static methods, this, and more). For now the following should work:

var opt = { format: 'string', type : 'string', style : 'citation-<NAME>', lang : 'en-US', template : <XML_CSL> };

Where <NAME> is anything you'd like it to be that isn't already used. In subsequent calls you should be able to omit the template property, but still use style: 'citation-<NAME>'.

argirok commented 7 years ago

Thank you for the answer. It doesn't seem to work for me.. Here's my options. Also the template xml is copied from here: https://github.com/citation-style-language/styles/blob/master/ieee.csl But again I get the default citation format, mentioned earlier.

var opt = {
      format: 'string',
      type  : 'string',
      style : 'citation-ieee',
      lang  : 'en-US',
      template : '<?xml  ..... </bibliography> </style>'
    };

Is there an estimation for the version 3?

larsgw commented 7 years ago

It definitely works for me with Citation.js v0.2.15. Do you have the right version? And what version of citeproc-js? I have citeproc-js v1.1.143 and CSL v1.0. What platform are you using (browser/node.js/etc.)? Are there any error messages or abnormal debug messages?

The syntax in v0.3 will probably be something like:

Cite.registerTemplate(<NAME>, <TEMPLATE>)

after which you can use it the same way you'd use the default ones (i.e. style: 'citation-<NAME>').

argirok commented 7 years ago

I had installed it using npm (v0.2.15) and today I updated it to v0.3. I tried different versions of nodejs (6.2.0, 6.8.0 and 7.4.0) to call the functions but with no luck.

Has the syntax changed in version 0.3? The only error I got is function undefined when I tried to use registerTemplate funtion you mentioned earlier.

Could you please provide a minimum example with a custom csl?

larsgw commented 7 years ago

v0.3.0-0 isn't regular v0.3.0 yet; I'm adding requested/important features or API changes step by step. Cite.registerTemplate() isn't implemented yet.

Anyway, turns out I forgot to mention one detail to make the already weird API work: it only looks for templates if you pass it as an option to .get(). The following works for me. I used a really simple CSL for brevity.

var test = new Cite('[ { id: "Q23571040", type: "article-journal", title: "Correlation of the Base Strengths of Amines 1", DOI: "10.1021/ja01577a030", author: [ { given: "H. K.", family: "Hall" } ], issued: [ { date-parts: [ "1957", "1", "1" ] } ], container-title: "Journal of the American Chemical Society", volume: "79", issue: "20", page: "5441-5444" } ]')

var customTemplate = 
  '<?xml version="1.0" encoding="utf-8"?>' +
    '<style xmlns="http://purl.org/net/xbiblio/csl" class="in-text" version="1.0" demote-non-dropping-particle="sort-only" page-range-format="minimal">' +
      '<bibliography>' +
        '<layout>' +
          '<text variable="title"/>' +
        '</layout>' +
      '</bibliography>' +
  '</style>'

var data_a = test.get({     // First call
  format: 'string',
  type: 'string',
  style: 'citation-custom',
  template: customTemplate
})

var data_b = test.get({     // Subsequent calls
  format: 'string',
  type: 'string',
  style: 'citation-custom'
})

I'm really sorry for all the trouble and not providing a working example earlier.

argirok commented 7 years ago

It worked! Thanks for the example.