Is there a JavaScript that can make a bibliography from a style CSL?

68 views
Skip to first unread message

Lennart Borgman

unread,
Jun 1, 2015, 10:30:19 PM6/1/15
to zoter...@googlegroups.com
Is there a JavaScript function that takes a style CSL (possibly in JSON format) and data (authors, title, etc) and outputs a bibliography?

Aurimas Vinckevicius

unread,
Jun 1, 2015, 10:36:37 PM6/1/15
to zoter...@googlegroups.com
Pretty much http://gsl-nagoya-u.net/http/pub/citeproc-doc.html If you're working within Zotero, then you can look around for some examples of how this is used. E.g. https://github.com/zotero/zotero/blob/4.0/chrome/content/zotero/tools/csledit.js#L135

On Mon, Jun 1, 2015 at 9:30 PM, Lennart Borgman <lennart...@gmail.com> wrote:
Is there a JavaScript function that takes a style CSL (possibly in JSON format) and data (authors, title, etc) and outputs a bibliography?

--
You received this message because you are subscribed to the Google Groups "zotero-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to zotero-dev+...@googlegroups.com.
To post to this group, send email to zoter...@googlegroups.com.
Visit this group at http://groups.google.com/group/zotero-dev.
For more options, visit https://groups.google.com/d/optout.

Lennart Borgman

unread,
Jun 18, 2015, 8:47:29 PM6/18/15
to zoter...@googlegroups.com
Thanks Aurimas,

I have seen citeproc and will take a look again then. I want do build a function like this:

/**
 * Create a citation as an HTML DOM fragment.
 *
 * @param {Object} csl - csl citation style (as JSON).
 * @param {Object] data - reference data (as JSON).
 * @return {Object} DOM element.
 */
function getCitation(csl, data) { ... }

For use in a bookmarklet. The user should be able to choose a csl when installing the bookmarklet.

Lennart Borgman

unread,
Jun 18, 2015, 8:48:47 PM6/18/15
to zoter...@googlegroups.com
Hm, maybe that should be called "bibliography", not "citation"?

Lennart Borgman

unread,
Jun 20, 2015, 7:17:12 AM6/20/15
to zoter...@googlegroups.com
I tried to place the original citproc.js on a web page. It stops since it does not find any xml parser:

var XML_PARSING;
if ("undefined" !== typeof CSL_IS_NODEJS) {
    XML_PARSING = CSL_NODEJS;
} else if ("undefined" !== typeof CSL_E4X) {
    XML_PARSING = CSL_E4X;
} else if ("undefined" !== typeof CSL_JSON) {
    XML_PARSING = CSL_JSON;
} else {
    XML_PARSING = CSL_CHROME;
}

I found that the citeproc.js in Zotero defines CSL_CHROME. Is that version perhaps better to start from if I want something that can work on a web page? Or should I look at the version used in JsNode?

Frank Bennett

unread,
Jun 20, 2015, 10:08:46 AM6/20/15
to zoter...@googlegroups.com

Yes - sorry that the parser setup and options are such a mess. It wasn't made obfuscated by intention; I just didn't have much experience in the early going, and the early choices have been stuck there for quite awhile. Inertia.

 You will find three files in the source: xml_e4x.js, xml_json.js, and xml_dom.js. The first of those you should ignore, since e4x has gone away everywhere. To run in a browser, xml_dom.js is easier to set up initially. Just load it before the raw citeproc.js file, and it should work - the demo page shipped with the processor is a worked example of the loading sequence.

To run with the xml_json.js parsing module is a little more tricky, but it with styles and locales encoded in JSON (actually, loaded as JS objects), it is possible to run the processor async in a web worker, which makes the page much more responsive. For anything more than a basic demo, I would recommend exploring that route.

Lennart Borgman

unread,
Jun 28, 2015, 10:59:01 AM6/28/15
to zoter...@googlegroups.com
Many thanks, Frank! :-)

I have made a bookmarklet using citeproc-js. Quite easy with you help: https://dl.dropboxusercontent.com/u/848981/it/citep/citeproc-bm-setup.html

The output part is working rather well, but the scraping part is not there yet.

(Though I do not understand the "locale" part of the output. I just use locales-en-US.xml at the moment. I would be glad for some help there.)

The scraper. I do not know what to do at the moment. The code is organised like this:
  1. The bookmarklet makes a string copy of the page DOM.
  2. A new window is opened which loads citeproc.js etc.
  3. The bookmaklet uses postMessage to send the string copy of the page, csl and locale to the new window.
  4. The new window uses DOMParser to recreate a DOM which is then used for scraping.
  5. The result of the scraping is then (of course) used as data citeproc.js.
This setup actually technically allows for fetching relevant JavaScript code from for example Zotero and do the scraping. I mean, there are no security concerns, you can just add the relevant code as new script tags to the new window.

Of course then the translators must be organized so that this is possible.

At the moment I am trying to understand if the Zotero translators in their current forms can be used directly or if some changes must be made. Could someone please help me a bit there?

In another thread I told about a rewrite of the Zotero bookmarklet. That works, but my proxy is down at the moment (see https://dl.dropboxusercontent.com/u/848981/it/zotero-connectors/build/bookmarklet/bookmarklet.html). However this new approach seems much easier. No sandboxing is needed for example, AFAICS.

Lennart Borgman

unread,
Jun 28, 2015, 11:00:12 AM6/28/15
to zoter...@googlegroups.com
"as data for citeproc.js"...
Reply all
Reply to author
Forward
0 new messages