Skip to content

Commit

Permalink
Resolve string entries
Browse files Browse the repository at this point in the history
  • Loading branch information
Maarten committed May 25, 2017
1 parent 7b2c748 commit 187428e
Show file tree
Hide file tree
Showing 17 changed files with 350 additions and 197 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# bibtex-js

Library for parsing BibTeX.
Library for parsing BibTeX .bib files 📚.

This module literally just parses a BibTex file and doesn't do any fancy TeX string processing yet. If you want to actually work with a bibliography, look for [Bibliography.js](https://github.com/digitalheir/bibliography-js).
This module literally just parses a BibTex file and doesn't do any TeX string processing (i.e., `{\"o}` is not translated to `ö`). If you want to actually work with a bibliography, look for [Bibliography.js](https://github.com/digitalheir/bibliography-js).

Also, many internal BibTeX functions are not implemented simply because I don't need them personally. Pull requests are welcome.
Also, many internal BibTeX functions are not implemented yet, simply because I don't need them personally. Pull requests are welcome.

## License
MIT
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
"ts-node": "^3.0.4",
"tslint": "^5.3.2",
"typescript": "^2.3.3",
"webpack": "^2.3.3"
"webpack": "^2.3.3",
"yargs": "latest"
},
"scripts": {
"build": "npm run clean && npm run build:min",
Expand Down
12 changes: 5 additions & 7 deletions src/bibfile/BibEntry.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import {OuterQuotedString} from "./string/QuotedString";
import {OuterBracedString} from "./string/BracedString";
import {OuterQuotedString, QuotedString} from "./string/QuotedString";
import {BracedString, OuterBracedString} from "./string/BracedString";
import {flatten, isArray, isNumber, isString, mustBeArray, mustBeString} from "../util";
import {BibStringComponent} from "./BibStringItem";
import {BibStringComponent} from "./string/BibStringItem";
import {isStringRef, StringRef} from "./string/StringRef";
import {isNum} from "../lexer/NumericToken";

export class BibEntry {
readonly type: string;
Expand Down Expand Up @@ -76,14 +75,13 @@ export function parseStringComponent(braceDepth: number, obj: any): BibStringCom
if (!isArray(obj.data)) {
throw new Error("Expect array for data: " + JSON.stringify(obj));
}
return new BibStringComponent(obj.type, braceDepth, flatten(obj.data).map(e => parseStringComponent(braceDepth+1, e)));
// TODO
return new BracedString(braceDepth, flatten(obj.data).map(e => parseStringComponent(braceDepth+1, e)));
case "quotedstring":
if (!isArray(obj.data)) {
throw new Error("Expect array for data: " + JSON.stringify(obj));
}
const flattened = flatten(obj.data);
return new BibStringComponent(obj.type, braceDepth, flattened.map(e => parseStringComponent(braceDepth, e)));
return new QuotedString(braceDepth, flattened.map(e => parseStringComponent(braceDepth, e)));
default:
throw new Error("Unexpected complex string type: " + obj.type);
}
Expand Down
14 changes: 10 additions & 4 deletions src/bibfile/BibFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {isKeyVal} from "./KeyVal";
import {BibEntry, FieldValue, isBibEntry, parseEntryFields} from "./BibEntry";
import {BibComment, CommentEntry, flattenPlainText, isBibComment} from "./BibComment";
import {isPreamble, Preamble, newPreambleNode} from "./BibPreamble";
import {newStringNode, StringEntry} from "./string/StringEntry";
import {newStringNode, resolveStrings, StringEntry} from "./string/StringEntry";


export type NonBibComment = BibEntry | CommentEntry | StringEntry | Preamble;
Expand Down Expand Up @@ -43,6 +43,10 @@ export class BibFile {
readonly preamble$: string;

readonly strings: { [k: string]: FieldValue };
/**
* `strings`, but with all references resolved
*/
readonly strings$: { [k: string]: FieldValue };


constructor(content: (NonBibComment | BibComment)[]) {
Expand All @@ -60,11 +64,10 @@ export class BibFile {
this.entries.forEach((entry: BibEntry) => {
const key = entry._id.toLowerCase();
/**
* BibTEX
* will complain if two entries have the same internal key, even if they aren’t capitalized in the same
* BibTEX will complain if two entries have the same internal key, even if they aren’t capitalized in the same
* way. For instance, you cannot have two entries named Example and example.
* In the same way, if you cite both example and Example, BibTEX will complain. Indeed, it would
* have to include the same entry twice, which probably is not what you want;
* have to include the same entry twice, which probably is not what you want
*/
if (!!entryMap[key]) throw new Error("Entry with id " + key + " was defined more than once");
entryMap[key] = entry;
Expand All @@ -82,6 +85,9 @@ export class BibFile {
}
);
this.strings = strings;
this.strings$ = resolveStrings(strings);

console.log("Parsefd")
}

getEntry(id: string): BibEntry | undefined {
Expand Down
32 changes: 0 additions & 32 deletions src/bibfile/BibStringItem.ts

This file was deleted.

Empty file removed src/bibfile/parseBibNode.ts
Empty file.
47 changes: 47 additions & 0 deletions src/bibfile/string/BibStringItem.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import {StringRef} from "./StringRef";
import {BracedString, OuterBracedString} from "./BracedString";
import {OuterQuotedString, QuotedString} from "./QuotedString";


export type BibStringDatum = (BracedString | QuotedString | OuterQuotedString | OuterBracedString | string | number | StringRef);
export type BibStringData = BibStringDatum[];

export class BibStringComponent {
readonly data: BibStringData;
readonly type: string;

/**
* The brace depth of an item is the number of braces surrounding it (surrounding the field with braces instead of quotes does not modify the brace depth)
*/
readonly braceDepth: number;

constructor(type: string, braceDepth: number, data: BibStringData) {
this.type = type;
this.braceDepth = braceDepth;
this.data = data;
}


}

export function isBibStringComponent(x: any): x is BibStringComponent {
return typeof x.braceDepth === "number" && typeof x.type === "string";
}

// TODO
// /**
// * A special character is a
// part of a field starting with a left brace being at brace depth 0 immediately followed with a backslash,
// and ending with the corresponding right brace. For instance, in the above example, there is no special
// character, since \LaTeX is at depth 2. It should be noticed that anything in a special character is
// considered as being at brace depth 0, even if it is placed between another pair of braces.
// */
// export class SpecialCharacter extends BibStringComponent {
// constructor(data: BibStringData) {
// super("specialCharacter", 0, data);
// }
//
// copyWithResolvedStringReferences(alreadyResolved, refs): BibStringComponent {
// return new SpecialCharacter(resolveStringReferences(this,(alreadyResolved, refs));
// }
// }
20 changes: 12 additions & 8 deletions src/bibfile/string/BracedString.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
import {BibStringComponent, BibStringData} from "../BibStringItem";

import {BibStringComponent, BibStringData} from "./BibStringItem";

export class BracedString extends BibStringComponent {
readonly type = "bracedstring";

constructor(braceDepth: number, data: BibStringComponent[]) {
constructor(braceDepth: number, data: BibStringData) {
super("bracedstring", braceDepth, data);
}

}
export class OuterBracedString extends BibStringComponent {
readonly type = "bracedstringwrapper";

constructor(data: BibStringData) {
super("bracedstring", 0, data);
super("bracedstringwrapper", 0, data);
}
}

export function isOuterBracedString(x: any): x is OuterBracedString {
return x.type === "bracedstringwrapper";
}

export function isBracedString(x: any): x is BracedString {
return x.type === "bracedstring";
}

// TODO extends?
// export class DefiniteBracedString extends BibStringComponent {
// readonly type = "bracedstring";
Expand Down
12 changes: 0 additions & 12 deletions src/bibfile/string/ComplexString.ts

This file was deleted.

7 changes: 6 additions & 1 deletion src/bibfile/string/QuotedString.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {BibStringComponent, BibStringData} from "../BibStringItem";
import {BibStringComponent, BibStringData, resolveStringReferences} from "./BibStringItem";
import {FieldValue} from "../BibEntry";

export class QuotedString extends BibStringComponent {
constructor(braceDepth: number, data: BibStringData) {
Expand All @@ -17,6 +18,10 @@ export function isOuterQuotedString(x: any): x is OuterQuotedString {
return x.type === "quotedstringwrapper";
}

export function isQuotedString(x: any): x is OuterQuotedString {
return x.type === "quotedstring";
}

// export class DefiniteOuterQuotedString extends BibStringComponent {
// readonly data: DefiniteStringy[];
//
Expand Down
Loading

0 comments on commit 187428e

Please sign in to comment.