-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
================== | ||
Deployment example | ||
================== | ||
|
||
.. include:: substitutions.txt | ||
|CCBYSA|_ `Frank Bennett <https://twitter.com/fgbjr>`_ | ||
|
||
|alert| | ||
|
||
------------------------ | ||
|
||
----------- | ||
Hello there | ||
----------- | ||
|
||
demo.js | ||
for citeproc-js CSL citation formatter | ||
|
||
Get the citations that we are supposed to render, in the CSL-json format | ||
|
||
.. code-block:: javascript | ||
var xhr = new XMLHttpRequest(); | ||
xhr.open('GET', 'citations.json', false); | ||
xhr.send(null); | ||
var citations = JSON.parse(xhr.responseText);; | ||
Initialize a system object, which contains two methods needed by the | ||
engine. | ||
|
||
.. code-block:: javascript | ||
citeprocSys = { | ||
retrieveLocale: function (lang){ | ||
var xhr = new XMLHttpRequest(); | ||
xhr.open('GET', 'locales-' + lang + '.xml', false); | ||
xhr.send(null); | ||
return xhr.responseText; | ||
}, | ||
retrieveItem: function(id){ | ||
return citations[id]; | ||
} | ||
}; | ||
Given a language tag in RFC-4646 form, this method retrieves the | ||
locale definition file. This method must return a valid *serialized* | ||
CSL locale. (In other words, an blob of XML as an unparsed string. The | ||
processor will fail on a native XML object or buffer). | ||
|
||
Given an identifier, this retrieves one citation item. This method | ||
must return a valid CSL-JSON object. | ||
|
||
|
||
Given the identifier of a CSL style, this function instantiates a CSL.Engine | ||
object that can render citations in that style. | ||
|
||
.. code-block:: javascript | ||
function getProcessor(styleID) { | ||
// Get the CSL style as a serialized string of XML | ||
var xhr = new XMLHttpRequest(); | ||
xhr.open('GET', styleID + '.csl', false); | ||
xhr.send(null); | ||
var styleAsText = xhr.responseText; | ||
// Instantiate and return the engine | ||
var citeproc = new CSL.Engine(citeprocSys, styleAsText); | ||
return citeproc; | ||
}; | ||
This runs at document ready, and renders the bibliography | ||
|
||
.. code-block:: javascript | ||
function renderBib() { | ||
var bibDivs = document.getElementsByClassName('bib-div'); | ||
for (var i = 0, ilen = bibDivs.length; i < ilen; ++i) { | ||
var bibDiv = bibDivs[i]; | ||
var citeproc = getProcessor(bibDiv.getAttribute('data-csl')); | ||
var itemIDs = []; | ||
for (var key in citations) { | ||
itemIDs.push(key); | ||
} | ||
citeproc.updateItems(itemIDs); | ||
var bibResult = citeproc.makeBibliography(); | ||
bibDiv.innerHTML = bibResult[1].join('\n'); | ||
} | ||
} |