UNPKG

simple-cite

Version:

A simple package for generating citations and bibliographies

260 lines (224 loc) 7.31 kB
'use strict'; function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; } require('core-js/modules/es6.number.constructor'); require('core-js/modules/es6.function.name'); require('core-js/modules/es6.array.find-index'); var citeproc = _interopDefault(require('citeproc')); require('core-js/modules/es6.object.assign'); var _createClass = _interopDefault(require('@babel/runtime/helpers/createClass')); require('core-js/modules/es7.symbol.async-iterator'); require('core-js/modules/es6.symbol'); require('core-js/modules/es6.string.repeat'); require('core-js/modules/es6.string.starts-with'); require('core-js/modules/web.dom.iterable'); require('core-js/modules/es6.regexp.match'); require('core-js/modules/es6.regexp.split'); var Citation = /*#__PURE__*/ function () { function Citation(_ref) { var citationItems = _ref.citationItems, properties = _ref.properties, id = _ref.id, processor = _ref.processor; this.value = null; this.processor = null; this.citationItems = citationItems; this.properties = properties || {}; this.properties.noteIndex = this.properties.noteIndex || 0; this.id = id; this.processor = processor; } var _proto = Citation.prototype; _proto.rerender = function rerender() { if (this.properties['in-narrative']) { var clone = this.clone(); clone.properties['in-narrative'] = false; clone.citationItems = [{ id: this.citationItems[0].id, 'author-only': true }]; var authorOnly = this.processor.preview(clone); clone.citationItems = [Object.assign({}, this.citationItems[0], { 'suppress-author': true })]; this.value = [authorOnly, this.processor.preview(clone)].join(' ').trim(); } else { this.value = this.processor.preview(this); } return this; }; _proto.clone = function clone() { return new Citation({ id: this.id, citationItems: this.citationItems, processor: this.processor, properties: Object.assign({}, this.properties) }); }; return Citation; }(); var h = function h(selector, value, indent) { var tag, fields = selector.split(/(?=[#.])/); if (fields[0].charAt(0).match(/[#.]/)) { tag = 'div'; } else { tag = fields[0]; fields = fields.slice(1); } var id, classes = []; fields.forEach(function (v) { return v.startsWith('.') ? classes.push(v.substr(1)) : id = v.substr(1); }); var open = "<" + tag + (id ? " id=\"" + id + "\"" : '') + (classes.length ? ' class="' + classes.join(' ') + '"' : '') + ">"; var close = '</' + tag + '>\n'; return ' '.repeat(indent) + open + value + close; }; var TITLE = Symbol('title'); var Bibliography = /*#__PURE__*/ function () { function Bibliography(_ref) { var _ref$title = _ref.title, title = _ref$title === void 0 ? false : _ref$title, processor = _ref.processor; this.value = null; this.processor = null; this.ids = []; this.title = title; this.processor = processor; } var _proto = Bibliography.prototype; _proto.rerender = function rerender() { var _this$processor$makeB = this.processor.makeBibliography(), start = _this$processor$makeB.start, data = _this$processor$makeB.data, end = _this$processor$makeB.end, ids = _this$processor$makeB.ids; this.ids = ids; if (this.title) start += this.processor.format === 'html' ? h('h2.csl-bib-title', this.title, 2) : this.title + '\n\n'; this.value = [start].concat(data, [end]).join(''); return this; }; _createClass(Bibliography, [{ key: "title", get: function get() { var title = this[TITLE]; if (title && title === true) { title = this.processor.term({ name: 'reference', plural: this.ids.length !== 1, capitalize: true }); } return title; }, set: function set(value) { this[TITLE] = value; } }]); return Bibliography; }(); var Processor = /*#__PURE__*/ function () { function Processor(_ref) { var items = _ref.items, style = _ref.style, locale = _ref.locale, _ref$format = _ref.format, format = _ref$format === void 0 ? 'text' : _ref$format; this.citations = []; this.noCites = []; var itemLookup = {}; items.forEach(function (item) { return itemLookup[item.id] = item; }); var sys = { retrieveItem: function retrieveItem(item) { return itemLookup[item]; }, retrieveLocale: function retrieveLocale() { return locale; } }; this.engine = new citeproc.Engine(sys, style); this.engine.opt.development_extensions.wrap_url_and_doi = true; this.format = format; } var _proto = Processor.prototype; _proto.process = function process(citation) { var _this = this; var clone = citation.clone(); var result = this.engine.processCitationCluster(clone, this.citations.map(function (c) { return [c.id, c.properties.noteIndex]; }), []); citation.processor = this; citation.id = clone.citationID; this.citations.push(citation); result[1].forEach(function (_ref2) { var idx = _ref2[0]; return _this.citations[idx].rerender(); }); return citation; }; _proto.preview = function preview(citation) { var index = this.citations.findIndex(function (c) { return c.id === citation.id; }); var background = this.citations.map(function (c) { return [c.id, c.properties.noteIndex]; }); var clone = citation.clone(); return this.engine.previewCitationCluster(clone, background.slice(0, index), background.slice(index), this.format); }; _proto.term = function term(_ref3) { var name = _ref3.name, _ref3$plural = _ref3.plural, plural = _ref3$plural === void 0 ? false : _ref3$plural, _ref3$capitalize = _ref3.capitalize, capitalize = _ref3$capitalize === void 0 ? false : _ref3$capitalize; var term = this.engine.getTerm(name, null, Number(plural)); return capitalize ? term.charAt(0).toUpperCase() + term.substr(1) : term; }; _proto.makeBibliography = function makeBibliography() { var _this$engine$makeBibl = this.engine.makeBibliography(), params = _this$engine$makeBibl[0], data = _this$engine$makeBibl[1]; return { start: params.bibstart, data: data, end: params.bibend, ids: params.entry_ids }; }; _proto.cite = function cite(citation) { return this.process(new Citation(citation)); }; _proto.noCite = function noCite(citeIds) { this.noCites = this.noCites.concat(citeIds); this.engine.updateUncitedItems(citeIds); }; _proto.bibliography = function bibliography(_temp) { var _ref4 = _temp === void 0 ? {} : _temp, title = _ref4.title; return new Bibliography({ title: title, processor: this }).rerender(); }; _createClass(Processor, [{ key: "format", get: function get() { return this._format; }, set: function set(fmt) { this.engine.setOutputFormat(fmt); this._format = fmt; } }]); return Processor; }(); module.exports = Processor;