paperlint 4.0.0 → 4.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/ci.yml +16 -23
- package/CLAUDE.md +55 -11
- package/CONTRIBUTING.md +30 -7
- package/README.md +11 -8
- package/dist/adapters/banal/invocation.d.ts +1 -1
- package/dist/adapters/latex/document-class.d.ts +23 -0
- package/dist/adapters/latex/document-class.d.ts.map +1 -0
- package/dist/adapters/latex/document-class.js +110 -0
- package/dist/adapters/latex/document-class.js.map +1 -0
- package/dist/adapters/latex/hidden.d.ts +16 -0
- package/dist/adapters/latex/hidden.d.ts.map +1 -0
- package/dist/adapters/latex/hidden.js +49 -0
- package/dist/adapters/latex/hidden.js.map +1 -0
- package/dist/adapters/latex/includes.d.ts +12 -0
- package/dist/adapters/latex/includes.d.ts.map +1 -0
- package/dist/adapters/latex/includes.js +42 -0
- package/dist/adapters/latex/includes.js.map +1 -0
- package/dist/adapters/latex/index.d.ts +33 -0
- package/dist/adapters/latex/index.d.ts.map +1 -0
- package/dist/adapters/latex/index.js +25 -0
- package/dist/adapters/latex/index.js.map +1 -0
- package/dist/adapters/latex/nodes.d.ts +58 -0
- package/dist/adapters/latex/nodes.d.ts.map +1 -0
- package/dist/adapters/latex/nodes.js +97 -0
- package/dist/adapters/latex/nodes.js.map +1 -0
- package/dist/adapters/latex/outline.d.ts +10 -0
- package/dist/adapters/latex/outline.d.ts.map +1 -0
- package/dist/adapters/latex/outline.js +39 -0
- package/dist/adapters/latex/outline.js.map +1 -0
- package/dist/adapters/latex/parse.d.ts +42 -0
- package/dist/adapters/latex/parse.d.ts.map +1 -0
- package/dist/adapters/latex/parse.js +95 -0
- package/dist/adapters/latex/parse.js.map +1 -0
- package/dist/adapters/latex/prose.d.ts +8 -0
- package/dist/adapters/latex/prose.d.ts.map +1 -0
- package/dist/adapters/latex/prose.js +353 -0
- package/dist/adapters/latex/prose.js.map +1 -0
- package/dist/adapters/latex/rendered.d.ts +10 -0
- package/dist/adapters/latex/rendered.d.ts.map +1 -0
- package/dist/adapters/latex/rendered.js +39 -0
- package/dist/adapters/latex/rendered.js.map +1 -0
- package/dist/adapters/pdfjs/fill.d.ts.map +1 -1
- package/dist/adapters/pdfjs/fill.js +12 -12
- package/dist/adapters/pdfjs/fill.js.map +1 -1
- package/dist/build.js +4 -4
- package/dist/build.js.map +1 -1
- package/dist/claim-provenance.d.ts +34 -0
- package/dist/claim-provenance.d.ts.map +1 -0
- package/dist/claim-provenance.js +146 -0
- package/dist/claim-provenance.js.map +1 -0
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +49 -5
- package/dist/cli.js.map +1 -1
- package/dist/domain/paper-source.d.ts +58 -0
- package/dist/domain/paper-source.d.ts.map +1 -0
- package/dist/domain/paper-source.js +109 -0
- package/dist/domain/paper-source.js.map +1 -0
- package/dist/domain/sentences.d.ts +26 -0
- package/dist/domain/sentences.d.ts.map +1 -0
- package/dist/domain/sentences.js +48 -0
- package/dist/domain/sentences.js.map +1 -0
- package/dist/domain/tex-document.d.ts +119 -0
- package/dist/domain/tex-document.d.ts.map +1 -0
- package/dist/domain/tex-document.js +43 -0
- package/dist/domain/tex-document.js.map +1 -0
- package/dist/eslint-rules/paper-typography.d.ts +7 -0
- package/dist/eslint-rules/paper-typography.d.ts.map +1 -1
- package/dist/eslint-rules/paper-typography.js +53 -1
- package/dist/eslint-rules/paper-typography.js.map +1 -1
- package/dist/facts-file.d.ts.map +1 -1
- package/dist/facts-file.js +2 -2
- package/dist/facts-file.js.map +1 -1
- package/dist/new-paper.d.ts +5 -0
- package/dist/new-paper.d.ts.map +1 -1
- package/dist/new-paper.js +3 -0
- package/dist/new-paper.js.map +1 -1
- package/dist/package-dirs.d.ts +5 -0
- package/dist/package-dirs.d.ts.map +1 -0
- package/dist/package-dirs.js +14 -0
- package/dist/package-dirs.js.map +1 -0
- package/dist/ports/latex.d.ts +35 -0
- package/dist/ports/latex.d.ts.map +1 -0
- package/dist/ports/latex.js +2 -0
- package/dist/ports/latex.js.map +1 -0
- package/dist/presets.d.ts +15 -2
- package/dist/presets.d.ts.map +1 -1
- package/dist/presets.js +19 -6
- package/dist/presets.js.map +1 -1
- package/dist/register.d.ts +40 -0
- package/dist/register.d.ts.map +1 -0
- package/dist/register.js +134 -0
- package/dist/register.js.map +1 -0
- package/dist/tex-paper.d.ts +19 -0
- package/dist/tex-paper.d.ts.map +1 -0
- package/dist/tex-paper.js +105 -0
- package/dist/tex-paper.js.map +1 -0
- package/dist/tex-requirements.d.ts +11 -0
- package/dist/tex-requirements.d.ts.map +1 -1
- package/dist/tex-requirements.js +14 -8
- package/dist/tex-requirements.js.map +1 -1
- package/dist/tex-venue-rules.d.ts +88 -0
- package/dist/tex-venue-rules.d.ts.map +1 -0
- package/dist/tex-venue-rules.js +268 -0
- package/dist/tex-venue-rules.js.map +1 -0
- package/dist/venue-rules.d.ts +1 -1
- package/dist/venue-rules.d.ts.map +1 -1
- package/docs/configuration.md +3 -2
- package/docs/e2e.md +69 -40
- package/docs/install.md +1 -1
- package/docs/prior-art/README.md +1 -1
- package/docs/prior-art/package-location.md +5 -5
- package/docs/prior-art/repro/README.md +1 -1
- package/docs/prior-art/test-tooling.md +21 -10
- package/docs/rules/tex/claim-provenance.md +83 -0
- package/docs/rules/tex/missing-input.md +58 -0
- package/docs/rules/tex/register.md +77 -0
- package/docs/rules/tex/required-section.md +75 -0
- package/docs/rules/tex/template.md +74 -0
- package/docs/rules/tex/venue-leftover.md +73 -0
- package/docs/rules.md +73 -48
- package/docs/testing.md +17 -4
- package/docs/texlive-install-decision.md +1 -1
- package/docs/toolchain.md +2 -2
- package/eslint-rules/paper-typography.test.mjs +48 -1
- package/eslint-rules/paper-typography.ts +72 -1
- package/eslint.config.mjs +156 -15
- package/fixtures/build-e2e/acmart/expect.json +26 -0
- package/fixtures/build-e2e/aidc/PIPELINE-STATUS.md +3 -0
- package/fixtures/build-e2e/aidc/expect.json +14 -0
- package/fixtures/build-e2e/aidc/paper.tex +46 -0
- package/fixtures/build-e2e/aidc/paperlint.json +1 -0
- package/fixtures/build-e2e/broken/expect.json +11 -0
- package/fixtures/build-e2e/cite/expect.json +27 -0
- package/fixtures/build-e2e/empty/expect.json +12 -0
- package/fixtures/build-e2e/fallback/expect.json +53 -0
- package/fixtures/build-e2e/guards/expect.json +6 -0
- package/fixtures/build-e2e/no-source/expect.json +10 -0
- package/fixtures/build-e2e/unbalanced/expect.json +60 -0
- package/fixtures/pdf-facts/README.md +1 -1
- package/fixtures/real-markdown-paper/baseline.ts +8 -7
- package/fixtures/render-paper/build-clean.sh +3 -5
- package/package.json +14 -2
- package/{skills/submit-paper/references/venues → presets}/acm-sigconf.jsonc +4 -1
- package/{skills/submit-paper/references/venues → presets}/agenticdev.jsonc +4 -0
- package/presets/aidc.jsonc +73 -0
- package/{skills/submit-paper/references/venues → presets}/aisec.jsonc +4 -0
- package/presets/ieee-conference.jsonc +66 -0
- package/{skills/submit-paper/references/venues → presets}/realm.jsonc +8 -1
- package/{skills/submit-paper/references/venues → presets/tex}/paper-guards.tex +1 -1
- package/{skills/submit-paper/references/venues → presets}/venue-profile.schema.json +30 -2
- package/scripts/check.harness.ts +64 -10
- package/scripts/check.test.ts +60 -13
- package/scripts/check.ts +83 -30
- package/scripts/test.test.ts +1 -1
- package/scripts/test.ts +1 -1
- package/skills/cold-read-diff/SKILL.md +31 -10
- package/skills/cold-read-diff/SKILL.md.spec.ts +30 -9
- package/skills/draft-paper/SKILL.md +2 -2
- package/skills/draft-paper/SKILL.md.spec.ts +1 -1
- package/skills/grade-paper-writing/SKILL.md +109 -44
- package/skills/grade-paper-writing/SKILL.md.spec.ts +108 -43
- package/skills/harden-paper/SKILL.md +35 -36
- package/skills/harden-paper/SKILL.md.spec.ts +34 -35
- package/skills/paper-adversarial-review/SKILL.md +2 -2
- package/skills/paper-adversarial-review/SKILL.md.spec.ts +1 -1
- package/skills/paper-pipeline/PIPELINE-MAP.md +2 -2
- package/skills/paper-pipeline/SKILL.md +4 -4
- package/skills/paper-pipeline/SKILL.md.spec.ts +3 -3
- package/skills/paper-pipeline/grade-paper-writing-ablation.eval.mjs +6 -6
- package/skills/paper-pipeline/references/writing-craft.md +53 -18
- package/skills/paper-pipeline/scripts/consumer.d.mts +0 -2
- package/skills/paper-pipeline/scripts/consumer.mjs +1 -15
- package/skills/pc-panel-review/SKILL.md +36 -11
- package/skills/pc-panel-review/SKILL.md.spec.ts +35 -10
- package/skills/render-paper/SKILL.md +2 -2
- package/skills/render-paper/SKILL.md.spec.ts +1 -1
- package/skills/render-paper/check-render.sh +16 -63
- package/skills/render-paper/render-paper.harness.mjs +17 -59
- package/src/adapters/banal/index.test.ts +1 -1
- package/src/adapters/banal/invocation.ts +1 -1
- package/src/adapters/banal/output.test.ts +1 -1
- package/src/adapters/banal/run.test.ts +1 -1
- package/src/adapters/banal/xml.test.ts +1 -1
- package/src/adapters/latex/document-class.ts +155 -0
- package/src/adapters/latex/hidden.ts +70 -0
- package/src/adapters/latex/includes.ts +66 -0
- package/src/adapters/latex/index.test.ts +535 -0
- package/src/adapters/latex/index.ts +65 -0
- package/src/adapters/latex/nodes.ts +150 -0
- package/src/adapters/latex/outline.ts +64 -0
- package/src/adapters/latex/parse.ts +137 -0
- package/src/adapters/latex/prose.ts +484 -0
- package/src/adapters/latex/rendered.ts +59 -0
- package/src/adapters/pdfjs/fill.test.ts +1 -1
- package/src/adapters/pdfjs/fill.ts +18 -10
- package/src/build.harness.mjs +5 -7
- package/src/build.ts +4 -4
- package/src/claim-provenance.test.ts +352 -0
- package/src/claim-provenance.ts +217 -0
- package/src/cli.harness.mjs +1 -1
- package/src/cli.test.ts +2 -8
- package/src/cli.ts +53 -8
- package/src/domain/paper-source.test.ts +156 -0
- package/src/domain/paper-source.ts +233 -0
- package/src/domain/sentences.ts +67 -0
- package/src/domain/tex-document.test.ts +80 -0
- package/src/domain/tex-document.ts +143 -0
- package/src/engine.harness.mjs +1 -1
- package/src/facts-file.test.ts +4 -7
- package/src/facts-file.ts +2 -2
- package/src/latex-loop.harness.ts +1 -1
- package/src/new-paper.test.ts +28 -3
- package/src/new-paper.ts +8 -0
- package/src/package-dirs.ts +17 -0
- package/src/paper-settings-commands.test.ts +6 -1
- package/src/ports/latex.ts +42 -0
- package/src/presets.test.ts +65 -4
- package/src/presets.ts +38 -15
- package/src/register.test.ts +165 -0
- package/src/register.ts +177 -0
- package/src/rule-cases.test.ts +524 -0
- package/src/rule-docs.test.ts +159 -0
- package/src/tex-paper.test.ts +131 -0
- package/src/tex-paper.ts +152 -0
- package/src/tex-requirements.harness.ts +13 -23
- package/src/tex-requirements.ts +35 -8
- package/src/tex-venue-rules.test.ts +741 -0
- package/src/tex-venue-rules.ts +461 -0
- package/src/toolchain.harness.mjs +1 -1
- package/src/venue-rules.test.ts +96 -4
- package/src/venue-rules.ts +1 -1
- package/templates/paper/paper.tex +2 -1
- package/skills/harden-paper/fixtures/uncited-assertions-sample.md +0 -43
- package/skills/harden-paper/fixtures/uncited-assertions-sample.tex +0 -77
- /package/{skills/submit-paper/references/venues → presets/tex}/agenticdev.tex +0 -0
- /package/{skills/submit-paper/references/venues → presets}/tex-base.jsonc +0 -0
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
/** What the three readers share about unified-latex nodes: places, text, and the walk. */
|
|
2
|
+
import type * as Ast from "@unified-latex/unified-latex-types";
|
|
3
|
+
import { CONTINUE, SKIP, visit } from "@unified-latex/unified-latex-util-visit";
|
|
4
|
+
import type { Place, Span } from "../../domain/tex-document.ts";
|
|
5
|
+
import { KEY_SIGNATURES } from "./parse.ts";
|
|
6
|
+
|
|
7
|
+
export { collapse } from "../../domain/tex-document.ts";
|
|
8
|
+
|
|
9
|
+
/** unified-latex's node types, read-only: nothing here writes to a tree. */
|
|
10
|
+
export type Node = Readonly<Ast.Node>;
|
|
11
|
+
export type Macro = Readonly<Ast.Macro>;
|
|
12
|
+
export type Argument = Readonly<Ast.Argument>;
|
|
13
|
+
|
|
14
|
+
/** Anything the parser may have given a position: offsets of its first and past its last character. */
|
|
15
|
+
interface Positioned {
|
|
16
|
+
readonly position?: {
|
|
17
|
+
readonly start: { readonly offset: number };
|
|
18
|
+
readonly end: { readonly offset: number };
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** Where a node stands, or that it has no position. */
|
|
23
|
+
export const placeOf = (n: Positioned): Place =>
|
|
24
|
+
n.position === undefined
|
|
25
|
+
? { kind: "unplaced" }
|
|
26
|
+
: {
|
|
27
|
+
kind: "at",
|
|
28
|
+
span: { start: n.position.start.offset, end: n.position.end.offset },
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
/** What `f` makes of a place's span, or nothing for a node the parser gave no position. */
|
|
32
|
+
export const inPlace = <T>(
|
|
33
|
+
p: Place,
|
|
34
|
+
f: (span: Span) => readonly T[],
|
|
35
|
+
): readonly T[] => (p.kind === "at" ? f(p.span) : []);
|
|
36
|
+
|
|
37
|
+
/** A macro's arguments, in order: none when the parser attached none. */
|
|
38
|
+
export const argumentsOf = (m: Macro): readonly Argument[] => m.args ?? [];
|
|
39
|
+
|
|
40
|
+
/** The first mandatory (`{…}`) and optional (`[…]`) arguments' contents, and the last mandatory one's. */
|
|
41
|
+
export const mandatory = (m: Macro): readonly Argument[] =>
|
|
42
|
+
argumentsOf(m).filter((a) => a.openMark === "{");
|
|
43
|
+
export const optional = (m: Macro): Argument | undefined =>
|
|
44
|
+
argumentsOf(m).find((a) => a.openMark === "[");
|
|
45
|
+
|
|
46
|
+
/** What a string node reads as: itself, except a tie (`~`), which is a space the reader sees. */
|
|
47
|
+
export const stringText = (content: string): string =>
|
|
48
|
+
content === "~" ? " " : content;
|
|
49
|
+
|
|
50
|
+
/** Which of a macro's mandatory arguments it typesets: one of them, or none at all. */
|
|
51
|
+
type Shown =
|
|
52
|
+
| { readonly kind: "argument"; readonly at: number }
|
|
53
|
+
| { readonly kind: "none" };
|
|
54
|
+
|
|
55
|
+
const LAST: Shown = { kind: "argument", at: -1 };
|
|
56
|
+
const NONE: Shown = { kind: "none" };
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Macros that do not typeset their last mandatory argument, as data. `\texorpdfstring{shown}{bookmark}`
|
|
60
|
+
* typesets the first; a declaration (`\color{red}`, `\fontsize{10}{12}`, `\vspace{1em}`,
|
|
61
|
+
* `\setlength{\x}{0pt}`) typesets none. Every other macro typesets its last (`\textcolor{red}{x}`,
|
|
62
|
+
* `\colorbox{yellow}{x}`, `\href{url}{x}`).
|
|
63
|
+
*/
|
|
64
|
+
const SHOWN_ARGUMENT: ReadonlyMap<string, Shown> = new Map<string, Shown>([
|
|
65
|
+
["texorpdfstring", { kind: "argument", at: 0 }],
|
|
66
|
+
...["color", "fontsize", "vspace", "hspace", "setlength", "addtolength"].map(
|
|
67
|
+
(m) => [m, NONE] as const,
|
|
68
|
+
),
|
|
69
|
+
]);
|
|
70
|
+
|
|
71
|
+
/** The text a (non-key) macro typesets: see `SHOWN_ARGUMENT`. */
|
|
72
|
+
function shownText(m: Macro): string {
|
|
73
|
+
const shown = SHOWN_ARGUMENT.get(m.content) ?? LAST;
|
|
74
|
+
return shown.kind === "none"
|
|
75
|
+
? ""
|
|
76
|
+
: textOf(mandatory(m).at(shown.at)?.content);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* The text of a node list with markup dropped: strings, spaces, what groups hold, and the argument a
|
|
81
|
+
* formatting macro typesets — its last mandatory one (`\textbf{Usage}`, `\textcolor{red}{Usage}`,
|
|
82
|
+
* `\href{url}{Usage}` → `Usage`) unless `SHOWN_ARGUMENT` says otherwise. A key macro's arguments
|
|
83
|
+
* (`\label{…}`, `\cite{…}`) are not text.
|
|
84
|
+
*/
|
|
85
|
+
export const textOf = (nodes: readonly Node[] | undefined): string =>
|
|
86
|
+
(nodes ?? [])
|
|
87
|
+
.map((n) => {
|
|
88
|
+
if (n.type === "string") return stringText(n.content);
|
|
89
|
+
if (n.type === "whitespace" || n.type === "parbreak") return " ";
|
|
90
|
+
if (n.type === "group") return textOf(n.content);
|
|
91
|
+
return n.type === "macro" && !(n.content in KEY_SIGNATURES)
|
|
92
|
+
? shownText(n)
|
|
93
|
+
: "";
|
|
94
|
+
})
|
|
95
|
+
.join("");
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* A macro's place with its arguments: from its name to past the closing mark of the last argument
|
|
99
|
+
* that holds text. unified-latex positions the name and the arguments' contents, never the
|
|
100
|
+
* arguments themselves, so an empty trailing argument is not covered.
|
|
101
|
+
*/
|
|
102
|
+
export function macroPlace(m: Macro): Place {
|
|
103
|
+
const name = placeOf(m);
|
|
104
|
+
if (name.kind === "unplaced") return name;
|
|
105
|
+
const end = (m.args ?? []).reduce((e, a) => {
|
|
106
|
+
const last = a.content.at(-1)?.position?.end.offset;
|
|
107
|
+
return last === undefined ? e : last + a.closeMark.length;
|
|
108
|
+
}, name.span.end);
|
|
109
|
+
const span: Span = { start: name.span.start, end };
|
|
110
|
+
return { kind: "at", span };
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** What visit reaches: a node, an argument, or a node list (a content array, an argument list). */
|
|
114
|
+
export type Visited = Readonly<Ast.Ast> | readonly (Node | Argument)[];
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Everything visit reaches under `tree`, in document order, node lists included; `skip` prunes what
|
|
118
|
+
* it is given (a node or an argument, with the node that holds it) and everything under it. visit
|
|
119
|
+
* reports through a callback and returns nothing, so the one collection point of this adapter is here.
|
|
120
|
+
*/
|
|
121
|
+
export function visited(
|
|
122
|
+
// eslint-disable-next-line functional/prefer-immutable-types -- visit's own parameter type is mutable
|
|
123
|
+
tree: Ast.Ast,
|
|
124
|
+
skip: (v: Visited, parent: Visited | undefined) => boolean,
|
|
125
|
+
): readonly Visited[] {
|
|
126
|
+
// eslint-disable-next-line functional/prefer-immutable-types -- the one accumulator, see above
|
|
127
|
+
const seen: Visited[] = [];
|
|
128
|
+
visit(
|
|
129
|
+
tree,
|
|
130
|
+
(node, info) => {
|
|
131
|
+
// eslint-disable-next-line functional/immutable-data -- visit is callback-only; see above
|
|
132
|
+
seen.push(node);
|
|
133
|
+
return skip(node, info.parents[0]) ? SKIP : CONTINUE;
|
|
134
|
+
},
|
|
135
|
+
{ includeArrays: true },
|
|
136
|
+
);
|
|
137
|
+
return seen;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/** A node list visit reached. `Array.isArray` alone does not narrow a readonly array. */
|
|
141
|
+
export const isList = (v: Visited): v is readonly (Node | Argument)[] =>
|
|
142
|
+
Array.isArray(v);
|
|
143
|
+
|
|
144
|
+
/** A macro's argument, as visit reports it. */
|
|
145
|
+
export const isArgument = (v: Visited): v is Argument =>
|
|
146
|
+
!isList(v) && v.type === "argument";
|
|
147
|
+
|
|
148
|
+
/** A node, as opposed to a node list or an argument (which visit also reports). */
|
|
149
|
+
export const isNode = (v: Visited): v is Node =>
|
|
150
|
+
!isList(v) && v.type !== "argument" && v.type !== "root";
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/** The document's sections, where its back matter starts, and where it ends. */
|
|
2
|
+
import type * as Ast from "@unified-latex/unified-latex-types";
|
|
3
|
+
import { isDefined } from "remeda";
|
|
4
|
+
import type { Heading, Outline } from "../../domain/tex-document.ts";
|
|
5
|
+
import {
|
|
6
|
+
collapse,
|
|
7
|
+
isNode,
|
|
8
|
+
macroPlace,
|
|
9
|
+
mandatory,
|
|
10
|
+
textOf,
|
|
11
|
+
visited,
|
|
12
|
+
type Macro,
|
|
13
|
+
type Node,
|
|
14
|
+
} from "./nodes.ts";
|
|
15
|
+
import { isDefinition, isUnrendered } from "./hidden.ts";
|
|
16
|
+
import type { ParsedTex } from "./parse.ts";
|
|
17
|
+
|
|
18
|
+
/** Macros and the environment that start the back matter: the appendix and the bibliography. */
|
|
19
|
+
const BACK_MATTER_MACROS: ReadonlySet<string> = new Set([
|
|
20
|
+
"appendix",
|
|
21
|
+
"bibliography",
|
|
22
|
+
"printbibliography",
|
|
23
|
+
]);
|
|
24
|
+
|
|
25
|
+
const isBackMatter = (n: Node): boolean =>
|
|
26
|
+
(n.type === "macro" && BACK_MATTER_MACROS.has(n.content)) ||
|
|
27
|
+
(n.type === "environment" && n.env === "thebibliography");
|
|
28
|
+
|
|
29
|
+
const isSection = (n: Node): n is Macro =>
|
|
30
|
+
n.type === "macro" && n.content === "section";
|
|
31
|
+
|
|
32
|
+
const isDocument = (n: Node): n is Readonly<Ast.Environment> =>
|
|
33
|
+
n.type === "environment" && n.env === "document";
|
|
34
|
+
|
|
35
|
+
/** One `\section`/`\section*`: the last mandatory argument is its title. */
|
|
36
|
+
const headingOf = (m: Macro): Heading => ({
|
|
37
|
+
title: collapse(textOf(mandatory(m).at(-1)?.content)),
|
|
38
|
+
place: macroPlace(m),
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* The body's `\section`s (starred or not) in document order, the first back-matter node, and the
|
|
43
|
+
* end of the document. A source with no `document` environment is read whole, as a fragment. What
|
|
44
|
+
* the PDF never shows is pruned (`isUnrendered`), and so is a macro definition's body
|
|
45
|
+
* (`isDefinition`): a heading in a comment environment or in a definition is not a section.
|
|
46
|
+
*/
|
|
47
|
+
export function outlineOf(t: ParsedTex): Outline {
|
|
48
|
+
const doc = t.root.content.find(isDocument);
|
|
49
|
+
const nodes = visited(
|
|
50
|
+
doc?.content ?? t.root.content,
|
|
51
|
+
(v, parent) => isUnrendered(v, parent) || isDefinition(v),
|
|
52
|
+
).filter(isNode);
|
|
53
|
+
const backMatter =
|
|
54
|
+
nodes
|
|
55
|
+
.filter(isBackMatter)
|
|
56
|
+
.map((n) => n.position?.start.offset)
|
|
57
|
+
.filter(isDefined)[0] ?? null;
|
|
58
|
+
const docEnd = doc?.position?.end.offset;
|
|
59
|
+
return {
|
|
60
|
+
sections: nodes.filter(isSection).map(headingOf),
|
|
61
|
+
backMatter,
|
|
62
|
+
end: docEnd === undefined ? null : docEnd - 1,
|
|
63
|
+
};
|
|
64
|
+
}
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A LaTeX source → its unified-latex tree, as one `ParsedTex` that keeps the two together.
|
|
3
|
+
*
|
|
4
|
+
* ── CITATION KEYS ARE ARGUMENTS, NOT TEXT ────────────────────────────────────────
|
|
5
|
+
* unified-latex attaches arguments only to macros it has a signature for. natbib's and biblatex's
|
|
6
|
+
* citation commands have none, so `\citep{aisec2025}` came back with its key as a separate group —
|
|
7
|
+
* readable as prose. `KEY_SIGNATURES` declares them, and the other macros whose arguments name a
|
|
8
|
+
* file, a label or a URL rather than say something, so their arguments are attached and the
|
|
9
|
+
* rendered text skips them structurally. `\href` says something in one argument and names a URL in
|
|
10
|
+
* the other, so it is signed too, and `KEY_ARGUMENT` says which of its arguments is not text.
|
|
11
|
+
*/
|
|
12
|
+
import type * as Ast from "@unified-latex/unified-latex-types";
|
|
13
|
+
import { getParser } from "@unified-latex/unified-latex-util-parse";
|
|
14
|
+
import { once } from "remeda";
|
|
15
|
+
|
|
16
|
+
const signed = (
|
|
17
|
+
names: readonly string[],
|
|
18
|
+
signature: string,
|
|
19
|
+
): readonly (readonly [string, { readonly signature: string }])[] =>
|
|
20
|
+
names.map((m) => [m, { signature }] as const);
|
|
21
|
+
|
|
22
|
+
/** Citation commands: their `{…}` argument is a key, their `[…]` ones are notes the reader sees. */
|
|
23
|
+
export const CITATION_MACROS: ReadonlySet<string> = new Set([
|
|
24
|
+
"cite",
|
|
25
|
+
"citep",
|
|
26
|
+
"citet",
|
|
27
|
+
"citealp",
|
|
28
|
+
"citealt",
|
|
29
|
+
"citeauthor",
|
|
30
|
+
"citeyear",
|
|
31
|
+
"citeyearpar",
|
|
32
|
+
"Citep",
|
|
33
|
+
"Citet",
|
|
34
|
+
"nocite",
|
|
35
|
+
"parencite",
|
|
36
|
+
"textcite",
|
|
37
|
+
"autocite",
|
|
38
|
+
"footcite",
|
|
39
|
+
]);
|
|
40
|
+
|
|
41
|
+
/** Macros whose arguments are keys, labels, files or URLs — never text a reader sees. */
|
|
42
|
+
export const KEY_SIGNATURES: Readonly<
|
|
43
|
+
Record<string, { readonly signature: string }>
|
|
44
|
+
> = Object.fromEntries([
|
|
45
|
+
...signed([...CITATION_MACROS], "o o m"),
|
|
46
|
+
...signed(
|
|
47
|
+
[
|
|
48
|
+
"label",
|
|
49
|
+
"ref",
|
|
50
|
+
"eqref",
|
|
51
|
+
"pageref",
|
|
52
|
+
"autoref",
|
|
53
|
+
"cref",
|
|
54
|
+
"Cref",
|
|
55
|
+
"url",
|
|
56
|
+
"input",
|
|
57
|
+
"include",
|
|
58
|
+
"subfile",
|
|
59
|
+
"bibliography",
|
|
60
|
+
"bibliographystyle",
|
|
61
|
+
"addbibresource",
|
|
62
|
+
],
|
|
63
|
+
"m",
|
|
64
|
+
),
|
|
65
|
+
...signed(["includegraphics"], "s o o m"),
|
|
66
|
+
...signed(["usepackage", "RequirePackage", "documentclass"], "o m o"),
|
|
67
|
+
]);
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Macros that print some arguments and not others: the index of the one argument that is a key or
|
|
71
|
+
* a URL. A citation (`o o m`) prints its `[…]` notes, not its `{…}` key; `\href` (`m m`) prints its
|
|
72
|
+
* link text, not its URL.
|
|
73
|
+
*/
|
|
74
|
+
export const KEY_ARGUMENT: ReadonlyMap<string, number> = new Map([
|
|
75
|
+
...[...CITATION_MACROS].map((m) => [m, 2] as const),
|
|
76
|
+
["href", 0],
|
|
77
|
+
]);
|
|
78
|
+
|
|
79
|
+
const LINK_SIGNATURES = Object.fromEntries(signed(["href"], "m m"));
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Declarations unified-latex has no signature for: signed so their arguments (sizes, not text)
|
|
83
|
+
* attach to them instead of standing beside them as groups a reader would seem to see.
|
|
84
|
+
*/
|
|
85
|
+
const DECLARATION_SIGNATURES = Object.fromEntries(signed(["fontsize"], "m m"));
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Macros that define other macros or environments. A heading in a body is no section where it is
|
|
89
|
+
* written, so the outline skips these; the rendered text reads a body as written, since nothing here
|
|
90
|
+
* expands macros. `\def` and its kin get a signature so the name and the body attach to them; a
|
|
91
|
+
* parameter text (`\def\x#1{…}`) is not modelled.
|
|
92
|
+
*/
|
|
93
|
+
export const DEFINITION_MACROS: ReadonlySet<string> = new Set([
|
|
94
|
+
"newcommand",
|
|
95
|
+
"renewcommand",
|
|
96
|
+
"providecommand",
|
|
97
|
+
"DeclareRobustCommand",
|
|
98
|
+
"newenvironment",
|
|
99
|
+
"renewenvironment",
|
|
100
|
+
"def",
|
|
101
|
+
"gdef",
|
|
102
|
+
"edef",
|
|
103
|
+
"xdef",
|
|
104
|
+
]);
|
|
105
|
+
|
|
106
|
+
const DEF_SIGNATURES = Object.fromEntries(
|
|
107
|
+
signed(["def", "gdef", "edef", "xdef"], "m m"),
|
|
108
|
+
);
|
|
109
|
+
|
|
110
|
+
// Not exported: a `ParsedTex` carries this property, and the one way to write it is `parseLatex`.
|
|
111
|
+
const PARSED = Symbol("ParsedTex");
|
|
112
|
+
|
|
113
|
+
/** A source and ITS tree: only `parseLatex` makes one, so a tree cannot be paired with another source. */
|
|
114
|
+
export interface ParsedTex {
|
|
115
|
+
readonly src: string;
|
|
116
|
+
readonly root: Ast.Root;
|
|
117
|
+
readonly [PARSED]: true;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** One parser for the process, made on first use: building one compiles its grammar. */
|
|
121
|
+
const parser = once(() =>
|
|
122
|
+
getParser({
|
|
123
|
+
macros: {
|
|
124
|
+
...KEY_SIGNATURES,
|
|
125
|
+
...LINK_SIGNATURES,
|
|
126
|
+
...DECLARATION_SIGNATURES,
|
|
127
|
+
...DEF_SIGNATURES,
|
|
128
|
+
},
|
|
129
|
+
}),
|
|
130
|
+
);
|
|
131
|
+
|
|
132
|
+
/** A LaTeX source → its tree. */
|
|
133
|
+
export const parseLatex = (src: string): ParsedTex => ({
|
|
134
|
+
src,
|
|
135
|
+
root: parser().parse(src),
|
|
136
|
+
[PARSED]: true,
|
|
137
|
+
});
|