JSFiddle - React, Tailwind, and code Playground
by Christian Sonne
JavaScript
/**
* Parser.js
* Copyright 2012-13 Mayank Lahiri
* [email protected]
* Released under the BSD License.
*
* A forgiving Bibtex parser that can:
*
* (1) operate in streaming or block mode, extracting entries as dictionaries.
* (2) convert Latex special characters to UTF-8.
* (3) best-effort parse malformed entries.
* (4) run in a CommonJS environment or a browser, without any dependencies.
* (5) be advanced-compiled by Google Closure Compiler.
*
* Handwritten as a labor of love, not auto-generated from a grammar.
*
* Modes of usage:
*
* (1) Synchronous, string
*
* var entries = BibtexParser(text);
* console.log(entries);
*
* (2) Asynchronous, stream
*
* var entryCallback = function(entry) { console.log(entry); }
* var parser = new BibtexParser(entryCallback);
* parser.parse(chunk1);
* parser.parse(chunk2);
* ...
*
* @param {text|function(Object)} arg Either a Bibtex string or callback
* function for processing parsed entries.
* @constructor
*/
function BibtexParser(arg0) {
// Determine how this function is to be used
if (typeof arg0 == 'string') {
// Passed a string, synchronous call without 'new'
var tempStorage = {};
var entries = [];
function accumulator(entry) {
entries.push(entry);
}
var parser = BibtexParser.call(tempStorage, accumulator);
parser.parse(arg0);
return {
'entries': entries,
'errors': parser.getErrors()
}
}
if (typeof arg0 != 'function') {
throw 'Invalid parser construction.';
}
/** @enum {number} */
this.STATES_ = {
ENTRY_OR_JUNK: 0,
OBJECT_TYPE: 1,
ENTRY_KEY: 2,
KV_KEY: 3,
EQUALS: 4,
KV_VALUE: 5
}
/** @private */ this.DATA_ = {};
/** @private */ this.CALLBACK_ = arg0;
/** @private */ this.CHAR_ = 0;
/** @private */ this.LINE_ = 1;
/** @private */ this.CHAR_IN_LINE_ ...