Skip to content

Commit

Permalink
Conversion to pipeline
Browse files Browse the repository at this point in the history
  • Loading branch information
FlamingTempura committed Sep 13, 2024
1 parent 1c74c10 commit 3fcb778
Show file tree
Hide file tree
Showing 56 changed files with 1,656 additions and 1,623 deletions.
847 changes: 419 additions & 428 deletions bibtex-tidy.js

Large diffs are not rendered by default.

827 changes: 393 additions & 434 deletions bin/bibtex-tidy

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/bundle.js

Large diffs are not rendered by default.

82 changes: 82 additions & 0 deletions src/ASTProxy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { formatValue } from "./format";
import { type OptionsNormalized, normalizeOptions } from "./optionUtils";
import type {
BlockNode,
EntryNode,
FieldNode,
RootNode,
} from "./parsers/bibtexParser";
import { parseLaTeX } from "./parsers/latexParser";

export class ASTProxy {
constructor(
private ast: RootNode,
private tidyOptions: OptionsNormalized = normalizeOptions({}),
) {}

public root(): RootNode {
return this.ast;
}

public fields(): FieldNode[] {
return this.entries().flatMap((entry) => entry.fields);
}

public entries(): EntryNode[] {
return this.ast.children
.filter((node): node is BlockNode => node.type === "block")
.map((block) => block.block)
.filter((entry): entry is EntryNode => entry?.type === "entry");
}

public invalidateField(field: FieldNode): void {
this.renderValueLookup.delete(field);
}

private fieldLookup = new Map<EntryNode, Map<string, FieldNode>>();
private lookupField(
entry: EntryNode,
fieldLc: string,
): FieldNode | undefined {
let fieldNode = this.fieldLookup.get(entry)?.get(fieldLc);
if (fieldNode === undefined) {
fieldNode = entry.fields.find(
(field) => field.name.toLocaleLowerCase() === fieldLc,
);
}
return fieldNode;
}

private renderValueLookup = new Map<FieldNode, string>();

public lookupRenderedEntryValue(entry: EntryNode, fieldname: string): string;
public lookupRenderedEntryValue(field: FieldNode): string;
public lookupRenderedEntryValue(
node: EntryNode | FieldNode,
fieldName?: string,
): string {
const field =
node.type === "entry"
? this.lookupField(node, (fieldName ?? "").toLocaleLowerCase())
: node;

if (!field) {
return "";
}
let value = this.renderValueLookup.get(field);
if (value === undefined) {
const entryValue = formatValue(field, this.tidyOptions) ?? "";
value = parseLaTeX(entryValue).renderAsText();
this.renderValueLookup.set(field, value);
}
return value;
}

public lookupRenderedEntryValues(entry: EntryNode): Map<string, string> {
const values = new Map<string, string>();
for (const field of entry.fields) {
values.set(field.name, this.lookupRenderedEntryValue(field));
}
return values;
}
}
121 changes: 0 additions & 121 deletions src/cache.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/duplicates.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { equal } from "node:assert";
import { describe, it } from "node:test";
import { getEntries } from ".";
import { ASTProxy } from "./cache";
import { ASTProxy } from "./ASTProxy";
import { checkForDuplicates } from "./duplicates";
import { parseBibTeX } from "./parsers/bibtexParser";

Expand Down
16 changes: 9 additions & 7 deletions src/duplicates.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { ASTProxy } from "./cache";
import type { ASTProxy } from "./ASTProxy";
import type { DuplicateRule, MergeStrategy } from "./optionUtils";
import type { EntryNode } from "./parsers/bibtexParser";
import { parseNameList } from "./parsers/nameFieldParser";
import type { Warning } from "./tidy";
import type { Warning } from "./types";
import { alphaNum } from "./utils";

export function checkForDuplicates(
Expand Down Expand Up @@ -55,7 +55,7 @@ export function checkForDuplicates(
}

case "doi": {
const doi = alphaNum(cache.lookupEntryValue(entry, "doi"));
const doi = alphaNum(cache.lookupRenderedEntryValue(entry, "doi"));
if (!doi) continue;
duplicateOf = dois.get(doi);
if (!duplicateOf) {
Expand All @@ -67,10 +67,10 @@ export function checkForDuplicates(
}

case "citation": {
const ttl = cache.lookupEntryValue(entry, "title");
const aut = cache.lookupEntryValue(entry, "author");
const ttl = cache.lookupRenderedEntryValue(entry, "title");
const aut = cache.lookupRenderedEntryValue(entry, "author");
// Author/title can be identical for numbered reports https://github.com/FlamingTempura/bibtex-tidy/issues/364
const num = cache.lookupEntryValue(entry, "number");
const num = cache.lookupRenderedEntryValue(entry, "number");
if (!ttl || !aut) continue;
const cit: string = [
alphaNum(parseNameList(aut)[0]?.last ?? aut),
Expand All @@ -87,7 +87,9 @@ export function checkForDuplicates(
}

case "abstract": {
const abstract = alphaNum(cache.lookupEntryValue(entry, "abstract"));
const abstract = alphaNum(
cache.lookupRenderedEntryValue(entry, "abstract"),
);
const abs = abstract.slice(0, 100);
if (!abs) continue;
duplicateOf = abstracts.get(abs);
Expand Down
2 changes: 1 addition & 1 deletion src/format.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { doubleEnclose } from "./modifiers/encloseBracesModifier";
import type { OptionsNormalized } from "./optionUtils";
import type {
BlockNode,
Expand All @@ -7,6 +6,7 @@ import type {
RootNode,
TextNode,
} from "./parsers/bibtexParser";
import { doubleEnclose } from "./transforms/encloseBraces";

import { unwrapText, wrapText } from "./utils";

Expand Down
2 changes: 1 addition & 1 deletion src/generateKeys.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ASTProxy } from "./cache";
import type { ASTProxy } from "./ASTProxy";
import type { EntryNode } from "./parsers/bibtexParser";
import {
type EntryKeyTemplateToken,
Expand Down
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { type Warning, type BibTeXTidyResult, getEntries, tidy } from "./tidy";
export { getEntries, tidy } from "./tidy";
export type { Warning, BibTeXTidyResult } from "./types";
69 changes: 0 additions & 69 deletions src/modifiers/abbreviateMonthsModifier.ts

This file was deleted.

9 changes: 0 additions & 9 deletions src/modifiers/encodeUrlsModifier.ts

This file was deleted.

Loading

0 comments on commit 3fcb778

Please sign in to comment.