1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-06-27 07:43:14 +00:00

Update BibTeX parser to use later fork of library

https://github.com/ORCID/bibtexParseJs
This commit is contained in:
jeremy@jermolene.com 2020-10-18 17:05:43 +01:00
parent 7e7ecbe7a5
commit f863acf8ac
3 changed files with 42 additions and 26 deletions

0
plugins/tiddlywiki/bibtex/files/LICENSE Executable file → Normal file
View File

0
plugins/tiddlywiki/bibtex/files/README.md Executable file → Normal file
View File

68
plugins/tiddlywiki/bibtex/files/bibtexParse.js Executable file → Normal file
View File

@ -1,9 +1,12 @@
/* start bibtexParse 0.0.22 */ /* start bibtexParse 0.0.24 */
//Original work by Henrik Muehe (c) 2010 //Original work by Henrik Muehe (c) 2010
// //
//CommonJS port by Mikola Lysenko 2013 //CommonJS port by Mikola Lysenko 2013
// //
//Choice of compact (default) or pretty output from toBibtex:
// Nick Bailey, 2017.
//
//Port to Browser lib by ORCID / RCPETERS //Port to Browser lib by ORCID / RCPETERS
// //
//Issues: //Issues:
@ -24,7 +27,7 @@
(function(exports) { (function(exports) {
function BibtexParser() { function BibtexParser() {
this.months = ["jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"]; this.months = ["jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"];
this.notKey = [',','{','}',' ','=']; this.notKey = [',','{','}',' ','='];
this.pos = 0; this.pos = 0;
@ -52,8 +55,8 @@
if (this.input.substring(this.pos, this.pos + s.length) == s) { if (this.input.substring(this.pos, this.pos + s.length) == s) {
this.pos += s.length; this.pos += s.length;
} else { } else {
throw "Token mismatch, expected " + s + ", found " throw TypeError("Token mismatch: match", "expected " + s + ", found "
+ this.input.substring(this.pos); + this.input.substring(this.pos));
}; };
this.skipWhitespace(canCommentOut); this.skipWhitespace(canCommentOut);
}; };
@ -112,7 +115,7 @@
} else if (this.input[this.pos] == '{') { } else if (this.input[this.pos] == '{') {
bracecount++; bracecount++;
} else if (this.pos >= this.input.length - 1) { } else if (this.pos >= this.input.length - 1) {
throw "Unterminated value"; throw TypeError("Unterminated value: value_braces");
}; };
}; };
if (this.input[this.pos] == '\\' && escaped == false) if (this.input[this.pos] == '\\' && escaped == false)
@ -133,7 +136,7 @@
if (this.input[this.pos] == '}') if (this.input[this.pos] == '}')
brcktCnt--; brcktCnt--;
if (this.pos >= this.input.length - 1) { if (this.pos >= this.input.length - 1) {
throw "Unterminated value:" + this.input.substring(start); throw TypeError("Unterminated value: value_comment", + this.input.substring(start));
}; };
this.pos++; this.pos++;
}; };
@ -151,7 +154,7 @@
this.match('"', false); this.match('"', false);
return this.input.substring(start, end); return this.input.substring(start, end);
} else if (this.pos >= this.input.length - 1) { } else if (this.pos >= this.input.length - 1) {
throw "Unterminated value:" + this.input.substring(start); throw TypeError("Unterminated value: value_quotes", this.input.substring(start));
}; };
} }
if (this.input[this.pos] == '\\' && escaped == false) if (this.input[this.pos] == '\\' && escaped == false)
@ -175,8 +178,8 @@
else if (this.months.indexOf(k.toLowerCase()) >= 0) else if (this.months.indexOf(k.toLowerCase()) >= 0)
return k.toLowerCase(); return k.toLowerCase();
else else
throw "Value expected:" + this.input.substring(start) + ' for key: ' + k; throw "Value expected: single_value" + this.input.substring(start) + ' for key: ' + k;
}; };
}; };
@ -194,7 +197,7 @@
var start = this.pos; var start = this.pos;
while (true) { while (true) {
if (this.pos >= this.input.length) { if (this.pos >= this.input.length) {
throw "Runaway key"; throw TypeError("Runaway key: key");
}; };
// а-яА-Я is Cyrillic // а-яА-Я is Cyrillic
//console.log(this.input[this.pos]); //console.log(this.input[this.pos]);
@ -206,7 +209,7 @@
return this.input.substring(start, this.pos); return this.input.substring(start, this.pos);
} else { } else {
this.pos++; this.pos++;
}; };
}; };
}; };
@ -216,10 +219,11 @@
if (this.tryMatch("=")) { if (this.tryMatch("=")) {
this.match("="); this.match("=");
var val = this.value(); var val = this.value();
key = key.trim()
return [ key, val ]; return [ key, val ];
} else { } else {
throw "... = value expected, equals sign missing:" throw TypeError("Value expected, equals sign missing: key_equals_value",
+ this.input.substring(this.pos); this.input.substring(this.pos));
}; };
}; };
@ -243,7 +247,7 @@
this.currentEntry = {}; this.currentEntry = {};
this.currentEntry['citationKey'] = this.key(true); this.currentEntry['citationKey'] = this.key(true);
this.currentEntry['entryType'] = d.substring(1); this.currentEntry['entryType'] = d.substring(1);
if (this.currentEntry['citationKey'] != null) { if (this.currentEntry['citationKey'] != null) {
this.match(","); this.match(",");
} }
this.key_value_list(); this.key_value_list();
@ -289,11 +293,11 @@
while (this.matchAt()) { while (this.matchAt()) {
var d = this.directive(); var d = this.directive();
this.match("{"); this.match("{");
if (d == "@STRING") { if (d.toUpperCase() == "@STRING") {
this.string(); this.string();
} else if (d == "@PREAMBLE") { } else if (d.toUpperCase() == "@PREAMBLE") {
this.preamble(); this.preamble();
} else if (d == "@COMMENT") { } else if (d.toUpperCase() == "@COMMENT") {
this.comment(); this.comment();
} else { } else {
this.entry(d); this.entry(d);
@ -304,7 +308,7 @@
this.alernativeCitationKey(); this.alernativeCitationKey();
}; };
}; };
exports.toJSON = function(bibtex) { exports.toJSON = function(bibtex) {
var b = new BibtexParser(); var b = new BibtexParser();
b.setInput(bibtex); b.setInput(bibtex);
@ -313,28 +317,40 @@
}; };
/* added during hackathon don't hate on me */ /* added during hackathon don't hate on me */
exports.toBibtex = function(json) { /* Increased the amount of white-space to make entries
* more attractive to humans. Pass compact as false
* to enable */
exports.toBibtex = function(json, compact) {
if (compact === undefined) compact = true;
var out = ''; var out = '';
var entrysep = ',';
var indent = '';
if (!compact) {
entrysep = ',\n';
indent = ' ';
}
for ( var i in json) { for ( var i in json) {
out += "@" + json[i].entryType; out += "@" + json[i].entryType;
out += '{'; out += '{';
if (json[i].citationKey) if (json[i].citationKey)
out += json[i].citationKey + ', '; out += json[i].citationKey + entrysep;
if (json[i].entry) if (json[i].entry)
out += json[i].entry ; out += json[i].entry ;
if (json[i].entryTags) { if (json[i].entryTags) {
var tags = ''; var tags = indent;
for (var jdx in json[i].entryTags) { for (var jdx in json[i].entryTags) {
if (tags.length != 0) if (tags.trim().length != 0)
tags += ', '; tags += entrysep + indent;
tags += jdx + '= {' + json[i].entryTags[jdx] + '}'; tags += jdx + (compact ? '={' : ' = {') +
json[i].entryTags[jdx] + '}';
} }
out += tags; out += tags;
} }
out += '}\n\n'; out += compact ? '}\n' : '\n}\n\n';
} }
return out; return out;
}; };
})(typeof exports === 'undefined' ? this['bibtexParse'] = {} : exports); })(typeof exports === 'undefined' ? this['bibtexParse'] = {} : exports);