diff --git a/SAMPLE.js b/SAMPLE.js index e3f0023..6a8df5b 100644 --- a/SAMPLE.js +++ b/SAMPLE.js @@ -2,10 +2,12 @@ var bibtexParse = require('bibtex-parse-js'); var sample = bibtexParse.toJSON('@article{sample1,title={sample title}}'); -# Will conolse log: +/* +# Will console log: # # [ { citationKey: 'SAMPLE1', # entryType: 'ARTICLE', # entryTags: { TITLE: 'sample title' } } ] # +*/ console.log(sample); diff --git a/bibtexParse.js b/bibtexParse.js index 4bac1ac..0f52dd2 100644 --- a/bibtexParse.js +++ b/bibtexParse.js @@ -173,12 +173,12 @@ return this.value_quotes(); } else { var k = this.key(); - if (k.match("^[0-9]+$")) - return k; + if (k.match("^[0-9]+\n*$")) + return parseInt(k); else if (this.months.indexOf(k.toLowerCase()) >= 0) return k.toLowerCase(); else - throw "Value expected: single_value" + this.input.substring(start) + ' for key: ' + k; + throw "Value expected: single_value " + this.input.substring(start) + ' for key: ' + k; }; }; @@ -190,7 +190,11 @@ this.match("#"); values.push(this.single_value()); }; - return values.join(""); + if(values.length == 1) { + return values[0]; + } else { + return values.join(""); + } }; this.key = function(optional) { @@ -238,8 +242,16 @@ break; } ; - kv = this.key_equals_value(); - this.currentEntry['entryTags'][kv[0]] = kv[1]; + try { + kv = this.key_equals_value(); + this.currentEntry['entryTags'][kv[0]] = kv[1]; + } catch (e) { + if(("" + e).includes("Value expected, equals sign missing: key_equals_value")) { + console.warn("Warning: ignoring line"); + } else { + throw new Error(e); + } + } }; }; @@ -302,7 +314,15 @@ } else { this.entry(d); } - this.match("}"); + try { + this.match("}"); + } catch (e) { + if(("" + e).includes("Token mismatch: match")) { + console.warn(e + ", ignoring line"); + } else { + throw new Error(e); + } + } }; this.alernativeCitationKey(); @@ -311,6 +331,75 @@ exports.toJSON = function(bibtex) { var b = new BibtexParser(); + function cleanBibtex(bibtex_code) { + // searches for lines that should begin with @, but dont, and add @ for resiliency + var at_regex = /(\s+|^)(\w+\{)/ig; + bibtex_code = bibtex_code.replace(at_regex, (match) => `${match[1].replace(/[\n\r\R]*/, "")}\n@${match.replace(/[\n\r\R]*/, "")}`); + + // remove spaces before @ at beginning of line + var space_regex = /^\s*@/g; + bibtex_code = bibtex_code.replace(space_regex, "@"); + + var search_missing_commas_regex_stage_one = /(.*=.*)/g; + bibtex_code = bibtex_code.replace(search_missing_commas_regex_stage_one, "$1,"); + + var search_missing_commas_regex_stage_two = /(,\s*)+\s*$/g; + bibtex_code = bibtex_code.replace(search_missing_commas_regex_stage_two, ",\n"); + + var find_unfinished_double_quotes = /(=\s*"[^"]*?),+/g; + bibtex_code = bibtex_code.replace(find_unfinished_double_quotes, `$1",`); + + var find_unfinished_bracket_quotes = /(=\s*\{[^\}]*?),+/g; + bibtex_code = bibtex_code.replace(find_unfinished_bracket_quotes, `$1},`); + + var find_missing_quotation = /(.*=)([^\{"'].*?[^\}"']),/g; + bibtex_code = bibtex_code.replace(find_missing_quotation, `$1{$2},`); + + var find_missing_bracket_quote_start = /(.*=)([^\{].*?\}),/g; + bibtex_code = bibtex_code.replace(find_missing_bracket_quote_start, `$1{$2,`); + + var find_missing_article_type = /^\s*(?<=@)([a-zA-Z0-9_]*)/g; + if(bibtex_code.match(find_missing_article_type)) { + bibtex_code = bibtex_code.replace(find_missing_article_type, `@article{$1`); + console.log("Found no article type, added @article"); + } + + var remove_double_commas = /,,+/g + bibtex_code = bibtex_code.replace(remove_double_commas, ",\n"); + + console.log(bibtex_code); + + var lines = bibtex_code.split(/[\n\r]/); + + function line_could_be_valid_syntax (line) { + if(line.match("^\s*@")) { + return true; + } else if(line.match("=")) { + return true; + } else if(line.match("}")) { + return true; + } else if(line.match(/^\s*$/)) { + return true; + } else { + return false; + } + } + + var new_lines = []; + + for (var i = 0; i < lines.length; i++) { + if(line_could_be_valid_syntax(lines[i])) { + new_lines.push(lines[i]); + } else { + console.warn("The line " + lines[i] + " does not seem to contain a valid bibtex line"); + } + } + + return new_lines.join("\n"); + } + + bibtex = cleanBibtex(bibtex); + b.setInput(bibtex); b.bibtex(); return b.entries; diff --git a/example_site/index.html b/example_site/index.html new file mode 100644 index 0000000..e5853cc --- /dev/null +++ b/example_site/index.html @@ -0,0 +1,166 @@ + + +
+