Skip to content

Commit 6faec7b

Browse files
fix: don't brace abbreviated months
1 parent 3dc7e48 commit 6faec7b

File tree

7 files changed

+38
-91
lines changed

7 files changed

+38
-91
lines changed

bibtex-tidy.d.ts

Lines changed: 0 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -226,94 +226,6 @@ export type BibTeXTidyOptions = {
226226
};
227227
export type Options = Omit<BibTeXTidyOptions, "help" | "version" | "quiet" | "backup">;
228228
export type DuplicateRule = Exclude<BibTeXTidyOptions["duplicates"], boolean | undefined>[number];
229-
declare class RootNode {
230-
children: (TextNode | BlockNode)[];
231-
type: "root";
232-
constructor(children?: (TextNode | BlockNode)[]);
233-
}
234-
declare class TextNode {
235-
parent: RootNode;
236-
text: string;
237-
type: "text";
238-
constructor(parent: RootNode, text: string);
239-
}
240-
declare class BlockNode {
241-
parent: RootNode;
242-
type: "block";
243-
command: string;
244-
block?: CommentNode | PreambleNode | StringNode | EntryNode;
245-
constructor(parent: RootNode);
246-
}
247-
declare class CommentNode {
248-
parent: BlockNode;
249-
raw: string;
250-
braces: number;
251-
parens: number;
252-
type: "comment";
253-
constructor(parent: BlockNode, raw: string, braces: number, parens: number);
254-
}
255-
declare class PreambleNode {
256-
parent: BlockNode;
257-
raw: string;
258-
braces: number;
259-
parens: number;
260-
type: "preamble";
261-
constructor(parent: BlockNode, raw: string, braces: number, parens: number);
262-
}
263-
declare class StringNode {
264-
parent: BlockNode;
265-
raw: string;
266-
braces: number;
267-
parens: number;
268-
type: "string";
269-
constructor(parent: BlockNode, raw: string, braces: number, parens: number);
270-
}
271-
declare class EntryNode {
272-
parent: BlockNode;
273-
wrapType: "{" | "(";
274-
type: "entry";
275-
key?: string;
276-
keyEnded?: boolean;
277-
fields: FieldNode[];
278-
constructor(parent: BlockNode, wrapType: "{" | "(");
279-
}
280-
declare class FieldNode {
281-
parent: EntryNode;
282-
name: string;
283-
type: "field";
284-
/** Each value is concatenated */
285-
value: ConcatNode;
286-
constructor(parent: EntryNode, name?: string);
287-
}
288-
declare class ConcatNode {
289-
parent: FieldNode;
290-
type: "concat";
291-
concat: (LiteralNode | BracedNode | QuotedNode)[];
292-
canConsumeValue: boolean;
293-
constructor(parent: FieldNode);
294-
}
295-
declare class LiteralNode {
296-
parent: ConcatNode;
297-
value: string;
298-
type: "literal";
299-
constructor(parent: ConcatNode, value: string);
300-
}
301-
declare class BracedNode {
302-
parent: ConcatNode;
303-
type: "braced";
304-
value: string;
305-
/** Used to count opening and closing braces */
306-
depth: number;
307-
constructor(parent: ConcatNode);
308-
}
309-
declare class QuotedNode {
310-
parent: ConcatNode;
311-
type: "quoted";
312-
value: string;
313-
/** Used to count opening and closing braces */
314-
depth: number;
315-
constructor(parent: ConcatNode);
316-
}
317229
export type Warning = ({
318230
code: "MISSING_KEY";
319231
} | {
@@ -328,6 +240,5 @@ export type BibTeXTidyResult = {
328240
count: number;
329241
};
330242
export declare function tidy(input: string, options_?: Options): BibTeXTidyResult;
331-
export declare function getEntries(ast: RootNode): EntryNode[];
332243

333244
export {};

bibtex-tidy.js

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bin/bibtex-tidy

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/bundle.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/transforms/abbreviateMonths.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,15 @@ import type { ASTProxy } from "../ASTProxy";
22
import { type FieldNode, LiteralNode } from "../parsers/bibtexParser";
33
import type { Transform } from "../types";
44

5-
const monthAliases: Record<string, string[]> = {
5+
/**
6+
* It’s generally recommended to store months in BibTeX files using the macros jan, feb,
7+
* and so on. Whether they’re recognised depends on the particular BibTeX style, but most
8+
* of them do support those macros. Biber also supports these macros.
9+
* - https://www.bibtex.com/f/month-field/
10+
* - https://texdoc.org/serve/biber.pdf/0
11+
*/
12+
13+
export const monthAliases: Record<string, string[]> = {
614
jan: ["1", "jan", "january"],
715
feb: ["2", "feb", "february"],
816
mar: ["3", "mar", "march"],

src/transforms/preferCurly.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
import type { Transform } from "../types";
2+
import { monthAliases } from "./abbreviateMonths";
23

34
export function createPreferCurlyTransform(): Transform {
45
return {
56
name: "prefer-curly",
67
apply: (ast) => {
78
for (const field of ast.fields()) {
9+
if (
10+
field.name.toLowerCase() === "month" &&
11+
monthAliases[ast.lookupRenderedEntryValue(field)]
12+
) {
13+
continue;
14+
}
815
for (const child of field.value.concat) {
916
child.type = "braced";
1017
}

test/curly.spec.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,18 @@ test("curly (enforce braced values)", async () => {
3131
const tidied = await bibtexTidy(input, { curly: true });
3232
strictEqual(tidied.bibtex, output);
3333
});
34+
35+
test("do not brace abbreviated months", async () => {
36+
const input = bibtex`@article{foo, title={Foo}, month = mar }`;
37+
38+
const expected = bibtex`
39+
@article{foo,
40+
title = {Foo},
41+
month = mar
42+
}
43+
`;
44+
45+
const tidied = await bibtexTidy(input, { curly: true });
46+
strictEqual(tidied.bibtex, expected);
47+
});
48+
//bibtex-tidy --curly --numeric --months --tab --align=13 --duplicates=key --no-escape --sort-fields --no-remove-dupe-fields YOUR_FILE.bib

0 commit comments

Comments
 (0)