Skip to content

Commit

Permalink
[output:CSL] Add registers for custom locales and templates
Browse files Browse the repository at this point in the history
  • Loading branch information
larsgw committed Jul 27, 2017
1 parent c8ac45b commit 3aaaf7c
Show file tree
Hide file tree
Showing 30 changed files with 493 additions and 231 deletions.
48 changes: 34 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,42 +183,55 @@ When using the `.get()` function, your output depends on the options you pass. I

#### <a id="cite.out.templates" href="#cite.out.templates">CSL Templates</a>

Currently, the following CSL Templates are suppported in Citation.js.
Currently, the following CSL Templates are built-in in Citation.js:

* `apa`
* `vancouver`
* `harvard1`

Different [CSL Templates](https://github.com/citation-style-language/styles) can be used by passing an XML string to `.get()` with the option `template:<string>`. E.g.
Different [CSL Templates](https://github.com/citation-style-language/styles) can be registered like this:

```js
var data = new Cite(...)
var templateName = 'custom'
var template = '<?xml version="1.0" encoding="utf-8"?><style ...>...</style>' // The actual XML file

Cite.CSL.register.addTemplate(templateName, template)

var data = new Cite(...)
data.get({
format: 'string',
type: 'html',
style: 'citation-<TEMPLATE NAME>',
lang: 'en-US',

template: '...' // XML String
style: 'citation-' + templateName,
lang: 'en-US'
})
```

Replace `<TEMPLATE NAME>` with the template name you want to use. After calling `.get()` with these options once, you can omit the template property, if you use the same locale.
Replace `templateName` with the template name you want to use.

#### <a id="cite.out.locales" href="#cite.out.locales">CSL Locales</a>

If you want different languages than the standard, you can pass a [CSL Locale](https://github.com/citation-style-language/locales) as an XML string to `.get()` with the option `locale:<string>`. E.g.
Currently, the following CSL Locales are built-in in Citation.js:

* `en-US`
* `es-ES`
* `de-DE`
* `fr-FR`
* `nl-NL`

Different [CSL Locales](https://github.com/citation-style-language/locales) can be registered like this:

```js
var data = new Cite(...)
var language = 'en-GB'
var locale = '<?xml version="1.0" encoding="utf-8"?><locale ...>...</locale>' // The actual XML file

Cite.CSL.register.addLocale(language, locale)

var data = new Cite(...)
data.get({
format: 'string',
type: 'html',
style: 'citation-apa',

locale: '...' // XML String
lang: language
})
```
### <a id="cite.misc" href="#cite.misc">Misc</a>
Expand Down Expand Up @@ -253,7 +266,7 @@ array // [1, 2, 3]

## <a id="cite.internal" href="#cite.internal">Internal functions</a>

`Cite` holds all internal functions, too. These are documented [here](https://larsgw.github.io/citation.js/api/global.html) and can be accessed like this:
`Cite` holds most internal functions, too. These are documented [here](https://larsgw.github.io/citation.js/api/global.html) and can be accessed like this:

Note that most `get*` functions expect CSL-JSON normalised with `Cite.parse.csl: [Function: parseCsl]`.

Expand All @@ -276,7 +289,14 @@ Note that most `get*` functions expect CSL-JSON normalised with `Cite.parse.csl:
{ style: [Function: fetchCSLStyle],
locale: [Function: fetchCSLLocale],
engine: [Function: fetchCSLEngine],
item: [Function: fetchCSLItemCallback] },
item: [Function: fetchCSLItemCallback],
register:
{ addTemplate: [Function: addTemplate],
addLocale: [Function: addLocale],
getTemplate: [Function: getTemplate],
getLocale: [Function: getLocale],
hasTemplate: [Function: hasTemplate],
hasLocale: [Function: hasLocale] } },
parse:
{ input:
{ type: [Function: parseInputType],
Expand Down
11 changes: 9 additions & 2 deletions lib/CSL/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.item = exports.engine = exports.locale = exports.style = undefined;
exports.register = exports.item = exports.engine = exports.locale = exports.style = undefined;

var _styles = require('./styles');

Expand All @@ -21,9 +21,16 @@ var _items = require('./items');

var _items2 = _interopRequireDefault(_items);

var _register = require('./register');

var register = _interopRequireWildcard(_register);

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

exports.style = _styles2.default;
exports.locale = _locales2.default;
exports.engine = _engines2.default;
exports.item = _items2.default;
exports.item = _items2.default;
exports.register = register;
4 changes: 1 addition & 3 deletions lib/CSL/items.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@ Object.defineProperty(exports, "__esModule", {
* @return {Cite~retrieveItem} Code to retreive item
*/
var fetchCSLItemCallback = function fetchCSLItemCallback(data) {
var _data = data;

return function (id) {
return _data.find(function (entry) {
return data.find(function (entry) {
return entry.id === id;
});
};
Expand Down
32 changes: 18 additions & 14 deletions lib/CSL/locales.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Object.defineProperty(exports, "__esModule", {
value: true
});

var _register = require('./register');

var _locales = require('./locales.json');

var _locales2 = _interopRequireDefault(_locales);
Expand All @@ -16,22 +18,24 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
* @access protected
* @method fetchCSLLocale
*
* @param {String} lang - lang code
* @param {String} [lang="en-US"] - lang code
*
* @return {String} CSL locale
*/
var fetchCSLLocale = function fetchCSLLocale(lang) {
return _locales2.default[lang];
}; /**
* Object containing CSL locales
*
* Locales from the [CSL Project](http://citationstyles.org/)<br>
* [REPO](https://github.com/citation-style-language/locales)
*
* Accesed 10/22/2016
*
* @access private
* @constant varCSLLocales
* @default
*/
return (0, _register.hasLocale)(lang) ? (0, _register.getLocale)(lang) : _locales2.default.hasOwnProperty(lang) ? _locales2.default[lang] : _locales2.default['en-US'];
};

/**
* Object containing CSL locales
*
* Locales from the [CSL Project](http://citationstyles.org/)<br>
* [REPO](https://github.com/citation-style-language/locales)
*
* Accesed 10/22/2016
*
* @access private
* @constant varCSLLocales
* @default
*/
exports.default = fetchCSLLocale;
107 changes: 107 additions & 0 deletions lib/CSL/register.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
});
/**
* Internal custom templates register.
*
* @access private
* @constant templates
* @default {}
*/
var templates = {};

/**
* Add a template to the register.
*
* @access public
* @method addTemplate
*
* @param {String} templateName - template name (used as 'citation-NAME' in Cite#get() style options)
* @param {String} template - CSL template string
*/
var addTemplate = function addTemplate(templateName, template) {
templates[templateName] = template;
};

/**
* Get a template from the register.
*
* @access protected
* @method getTemplate
*
* @param {String} templateName - template name (used as 'citation-NAME' in Cite#get() style options)
* @return {String} CSL template string
*/
var getTemplate = function getTemplate(templateName) {
return templates[templateName];
};

/**
* Check if the register has a template (identified by name).
*
* @access protected
* @method hasTemplate
*
* @param {String} templateName - template name (used as 'citation-NAME' in Cite#get() style options)
* @return {Boolean} true if register has template
*/
var hasTemplate = function hasTemplate(templateName) {
return templates.hasOwnProperty(templateName);
};

/**
* Internal custom locales register.
*
* @access private
* @constant locales
* @default {}
*/
var locales = {};

/**
* Add a locale to the register.
*
* @access public
* @method addLocale
*
* @param {String} localeName - locale name (used as lang in Cite#get() options)
* @param {String} locale - CSL locale string
*/
var addLocale = function addLocale(localeName, locale) {
locales[localeName] = locale;
};

/**
* Get a locale from the register.
*
* @access protected
* @method getLocale
*
* @param {String} localeName - locale name (used as lang in Cite#get() options)
* @return {String} CSL locale string
*/
var getLocale = function getLocale(localeName) {
return locales[localeName];
};

/**
* Check if the register has a locale (identified by name).
*
* @access protected
* @method hasLocale
*
* @param {String} localeName - locale name (used as lang in Cite#get() options)
* @return {Boolean} true if register has locale
*/
var hasLocale = function hasLocale(localeName) {
return locales.hasOwnProperty(localeName);
};

exports.addTemplate = addTemplate;
exports.addLocale = addLocale;
exports.getTemplate = getTemplate;
exports.getLocale = getLocale;
exports.hasTemplate = hasTemplate;
exports.hasLocale = hasLocale;
30 changes: 17 additions & 13 deletions lib/CSL/styles.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Object.defineProperty(exports, "__esModule", {
value: true
});

var _register = require('./register');

var _styles = require('./styles.json');

var _styles2 = _interopRequireDefault(_styles);
Expand All @@ -21,17 +23,19 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
* @return {String} CSL style
*/
var fetchCSLStyle = function fetchCSLStyle(style) {
return _styles2.default.hasOwnProperty(style || '') ? _styles2.default[style] : _styles2.default['apa'];
}; /**
* Object containing CSL templates
*
* Templates from the [CSL Project](http://citationstyles.org/)<br>
* [REPO](https://github.com/citation-style-language/styles), [LICENSE](https://creativecommons.org/licenses/by-sa/3.0/)
*
* Accesed 10/22/2016
*
* @access private
* @constant varCSLStyles
* @default
*/
return (0, _register.hasTemplate)(style) ? (0, _register.getTemplate)(style) : _styles2.default.hasOwnProperty(style) ? _styles2.default[style] : _styles2.default['apa'];
};

/**
* Object containing CSL templates
*
* Templates from the [CSL Project](http://citationstyles.org/)<br>
* [REPO](https://github.com/citation-style-language/styles), [LICENSE](https://creativecommons.org/licenses/by-sa/3.0/)
*
* Accesed 10/22/2016
*
* @access private
* @constant varCSLStyles
* @default
*/
exports.default = fetchCSLStyle;
Loading

0 comments on commit 3aaaf7c

Please sign in to comment.