Skip to content

Commit

Permalink
adding files
Browse files Browse the repository at this point in the history
  • Loading branch information
mikolalysenko committed Jul 18, 2013
0 parents commit 12b10f4
Show file tree
Hide file tree
Showing 6 changed files with 368 additions and 0 deletions.
16 changes: 16 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
lib-cov
*.seed
*.log
*.csv
*.dat
*.out
*.pid
*.gz

pids
logs
results

npm-debug.log
node_modules/*
*.DS_Store
22 changes: 22 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

The MIT License (MIT)

Copyright (c) 2010 Henrik Muehe

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
52 changes: 52 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
bibtex-parser
=============
A pure JavaScript BibTeX parser. Based on the following implementation by Henrik Muehe:

* https://code.google.com/p/bibtex-js/

## Example

```javascript
var bibliography = "@inproceedings{Lysenko:2010:GMC:1839778.1839781,\
author = {Lysenko, Mikola and Nelaturi, Saigopal and Shapiro, Vadim},\
title = {Group morphology with convolution algebras},\
booktitle = {Proceedings of the 14th ACM Symposium on Solid and Physical Modeling},\
series = {SPM '10},\
year = {2010},\
isbn = {978-1-60558-984-8},\
location = {Haifa, Israel},\
pages = {11--22},\
numpages = {12},\
url = {http://doi.acm.org/10.1145/1839778.1839781},\
doi = {10.1145/1839778.1839781},\
acmid = {1839781},\
publisher = {ACM},\
address = {New York, NY, USA},\
}"

var parse = require("bibtex-parser")

console.log(parse(bibliography))
```

## Install

npm install bibtex-parser

## API

```javascript
var parse = require("bibtex-parser")
```

### `parse(input)`
Parses a string as a bibtex file. The result is an object whose properties are the entries of the bibtex file, and whose values are objects with contents of the bibtex file.

* `input` is a string representing a bibtex file

**Returns** A parsed bibtex file as a JSON object

## Credits
(c) 2010 Henrik Muehe. MIT License

CommonJS port maintained by Mikola Lysenko
250 changes: 250 additions & 0 deletions parse-bibtex.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,250 @@
// Original work by Henrik Muehe (c) 2010
//
// CommonJS port by Mikola Lysenko 2013
//
//

// Issues:
// no comment handling within strings
// no string concatenation
// no variable values yet

// Grammar implemented here:
// bibtex -> (string | preamble | comment | entry)*;
// string -> '@STRING' '{' key_equals_value '}';
// preamble -> '@PREAMBLE' '{' value '}';
// comment -> '@COMMENT' '{' value '}';
// entry -> '@' key '{' key ',' key_value_list '}';
// key_value_list -> key_equals_value (',' key_equals_value)*;
// key_equals_value -> key '=' value;
// value -> value_quotes | value_braces | key;
// value_quotes -> '"' .*? '"'; // not quite
// value_braces -> '{' .*? '"'; // not quite
function BibtexParser() {
this.pos = 0;
this.input = "";

this.entries = {};
this.strings = {
JAN: "January",
FEB: "February",
MAR: "March",
APR: "April",
MAY: "May",
JUN: "June",
JUL: "July",
AUG: "August",
SEP: "September",
OCT: "October",
NOV: "November",
DEC: "December"
};
this.currentKey = "";
this.currentEntry = "";


this.setInput = function(t) {
this.input = t;
}

this.getEntries = function() {
return this.entries;
}

this.isWhitespace = function(s) {
return (s == ' ' || s == '\r' || s == '\t' || s == '\n');
}

this.match = function(s) {
this.skipWhitespace();
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);
}
this.skipWhitespace();
}

this.tryMatch = function(s) {
this.skipWhitespace();
if (this.input.substring(this.pos, this.pos+s.length) == s) {
return true;
} else {
return false;
}
this.skipWhitespace();
}

this.skipWhitespace = function() {
while (this.isWhitespace(this.input[this.pos])) {
this.pos++;
}
if (this.input[this.pos] == "%") {
while(this.input[this.pos] != "\n") {
this.pos++;
}
this.skipWhitespace();
}
}

this.value_braces = function() {
var bracecount = 0;
this.match("{");
var start = this.pos;
while(true) {
if (this.input[this.pos] == '}' && this.input[this.pos-1] != '\\') {
if (bracecount > 0) {
bracecount--;
} else {
var end = this.pos;
this.match("}");
return this.input.substring(start, end);
}
} else if (this.input[this.pos] == '{') {
bracecount++;
} else if (this.pos == this.input.length-1) {
throw "Unterminated value";
}
this.pos++;
}
}

this.value_quotes = function() {
this.match('"');
var start = this.pos;
while(true) {
if (this.input[this.pos] == '"' && this.input[this.pos-1] != '\\') {
var end = this.pos;
this.match('"');
return this.input.substring(start, end);
} else if (this.pos == this.input.length-1) {
throw "Unterminated value:" + this.input.substring(start);
}
this.pos++;
}
}

this.single_value = function() {
var start = this.pos;
if (this.tryMatch("{")) {
return this.value_braces();
} else if (this.tryMatch('"')) {
return this.value_quotes();
} else {
var k = this.key();
if (this.strings[k.toUpperCase()]) {
return this.strings[k];
} else if (k.match("^[0-9]+$")) {
return k;
} else {
throw "Value expected:" + this.input.substring(start);
}
}
}

this.value = function() {
var values = [];
values.push(this.single_value());
while (this.tryMatch("#")) {
this.match("#");
values.push(this.single_value());
}
return values.join("");
}

this.key = function() {
var start = this.pos;
while(true) {
if (this.pos == this.input.length) {
throw "Runaway key";
}

if (this.input[this.pos].match("[a-zA-Z0-9_:\\./-]")) {
this.pos++
} else {
return this.input.substring(start, this.pos).toUpperCase();
}
}
}

this.key_equals_value = function() {
var key = this.key();
if (this.tryMatch("=")) {
this.match("=");
var val = this.value();
return [ key, val ];
} else {
throw "... = value expected, equals sign missing:" + this.input.substring(this.pos);
}
}

this.key_value_list = function() {
var kv = this.key_equals_value();
this.entries[this.currentEntry][kv[0]] = kv[1];
while (this.tryMatch(",")) {
this.match(",");
// fixes problems with commas at the end of a list
if (this.tryMatch("}")) {
break;
}
kv = this.key_equals_value();
this.entries[this.currentEntry][kv[0]] = kv[1];
}
}

this.entry_body = function(d) {
this.currentEntry = this.key();
this.entries[this.currentEntry] = { entryType: d.substring(1) };
this.match(",");
this.key_value_list();
}

this.directive = function () {
this.match("@");
return "@"+this.key();
}

this.string = function () {
var kv = this.key_equals_value();
this.strings[kv[0].toUpperCase()] = kv[1];
}

this.preamble = function() {
this.value();
}

this.comment = function() {
this.value(); // this is wrong
}

this.entry = function(d) {
this.entry_body(d);
}

this.bibtex = function() {
while(this.tryMatch("@")) {
var d = this.directive().toUpperCase();
this.match("{");
if (d == "@STRING") {
this.string();
} else if (d == "@PREAMBLE") {
this.preamble();
} else if (d == "@COMMENT") {
this.comment();
} else {
this.entry(d);
}
this.match("}");
}
}
}

//Runs the parser
function doParse(input) {
var b = new BibtexParser()
b.setInput(input)
b.bibtex()
return b.entries
}

module.exports = doParse
16 changes: 16 additions & 0 deletions test/test.bib
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
@inproceedings{Lysenko:2010:GMC:1839778.1839781,
author = {Lysenko, Mikola and Nelaturi, Saigopal and Shapiro, Vadim},
title = {Group morphology with convolution algebras},
booktitle = {Proceedings of the 14th ACM Symposium on Solid and Physical Modeling},
series = {SPM '10},
year = {2010},
isbn = {978-1-60558-984-8},
location = {Haifa, Israel},
pages = {11--22},
numpages = {12},
url = {http://doi.acm.org/10.1145/1839778.1839781},
doi = {10.1145/1839778.1839781},
acmid = {1839781},
publisher = {ACM},
address = {New York, NY, USA},
}
12 changes: 12 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
"use strict"

var bib = require("fs").readFileSync(__dirname + "/test.bib").toString()
var parse = require("../parse-bibtex.js")

require("tape")("parse-bibtex", function(t) {

var parsed = parse(bib)
console.log(parsed)

t.end()
})

0 comments on commit 12b10f4

Please sign in to comment.