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

56
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
//
//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
//
//Issues:
@ -52,8 +55,8 @@
if (this.input.substring(this.pos, this.pos + s.length) == s) {
this.pos += s.length;
} else {
throw "Token mismatch, expected " + s + ", found "
+ this.input.substring(this.pos);
throw TypeError("Token mismatch: match", "expected " + s + ", found "
+ this.input.substring(this.pos));
};
this.skipWhitespace(canCommentOut);
};
@ -112,7 +115,7 @@
} else if (this.input[this.pos] == '{') {
bracecount++;
} else if (this.pos >= this.input.length - 1) {
throw "Unterminated value";
throw TypeError("Unterminated value: value_braces");
};
};
if (this.input[this.pos] == '\\' && escaped == false)
@ -133,7 +136,7 @@
if (this.input[this.pos] == '}')
brcktCnt--;
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++;
};
@ -151,7 +154,7 @@
this.match('"', false);
return this.input.substring(start, end);
} 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)
@ -175,7 +178,7 @@
else if (this.months.indexOf(k.toLowerCase()) >= 0)
return k.toLowerCase();
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;
while (true) {
if (this.pos >= this.input.length) {
throw "Runaway key";
throw TypeError("Runaway key: key");
};
// а-яА-Я is Cyrillic
//console.log(this.input[this.pos]);
@ -216,10 +219,11 @@
if (this.tryMatch("=")) {
this.match("=");
var val = this.value();
key = key.trim()
return [ key, val ];
} else {
throw "... = value expected, equals sign missing:"
+ this.input.substring(this.pos);
throw TypeError("Value expected, equals sign missing: key_equals_value",
this.input.substring(this.pos));
};
};
@ -289,11 +293,11 @@
while (this.matchAt()) {
var d = this.directive();
this.match("{");
if (d == "@STRING") {
if (d.toUpperCase() == "@STRING") {
this.string();
} else if (d == "@PREAMBLE") {
} else if (d.toUpperCase() == "@PREAMBLE") {
this.preamble();
} else if (d == "@COMMENT") {
} else if (d.toUpperCase() == "@COMMENT") {
this.comment();
} else {
this.entry(d);
@ -313,25 +317,37 @@
};
/* 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 entrysep = ',';
var indent = '';
if (!compact) {
entrysep = ',\n';
indent = ' ';
}
for ( var i in json) {
out += "@" + json[i].entryType;
out += '{';
if (json[i].citationKey)
out += json[i].citationKey + ', ';
out += json[i].citationKey + entrysep;
if (json[i].entry)
out += json[i].entry ;
if (json[i].entryTags) {
var tags = '';
var tags = indent;
for (var jdx in json[i].entryTags) {
if (tags.length != 0)
tags += ', ';
tags += jdx + '= {' + json[i].entryTags[jdx] + '}';
if (tags.trim().length != 0)
tags += entrysep + indent;
tags += jdx + (compact ? '={' : ' = {') +
json[i].entryTags[jdx] + '}';
}
out += tags;
}
out += '}\n\n';
out += compact ? '}\n' : '\n}\n\n';
}
return out;