paperlint 18.1.0 → 18.2.1
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/README.md +2 -2
- package/dist/adapters/latex/document-class.d.ts +28 -5
- package/dist/adapters/latex/document-class.d.ts.map +1 -1
- package/dist/adapters/latex/document-class.js +208 -21
- package/dist/adapters/latex/document-class.js.map +1 -1
- package/dist/adapters/latex/index.d.ts +4 -4
- package/dist/adapters/latex/index.d.ts.map +1 -1
- package/dist/adapters/latex/index.js +5 -2
- package/dist/adapters/latex/index.js.map +1 -1
- package/dist/adapters/latex/parse.d.ts.map +1 -1
- package/dist/adapters/latex/parse.js +5 -0
- package/dist/adapters/latex/parse.js.map +1 -1
- package/dist/adapters/latex/prose.d.ts.map +1 -1
- package/dist/adapters/latex/prose.js +20 -4
- package/dist/adapters/latex/prose.js.map +1 -1
- package/dist/domain/body-pages.d.ts.map +1 -1
- package/dist/domain/body-pages.js +4 -2
- package/dist/domain/body-pages.js.map +1 -1
- package/dist/domain/tex-document.d.ts +52 -4
- package/dist/domain/tex-document.d.ts.map +1 -1
- package/dist/domain/tex-document.js +9 -0
- package/dist/domain/tex-document.js.map +1 -1
- package/dist/eslint-rules/latex-language.d.ts +10 -0
- package/dist/eslint-rules/latex-language.d.ts.map +1 -1
- package/dist/eslint-rules/latex-language.js +41 -2
- package/dist/eslint-rules/latex-language.js.map +1 -1
- package/dist/ports/latex.d.ts +13 -1
- package/dist/ports/latex.d.ts.map +1 -1
- package/dist/presets.d.ts +15 -0
- package/dist/presets.d.ts.map +1 -1
- package/dist/presets.js +9 -0
- package/dist/presets.js.map +1 -1
- package/dist/tex-requirements.d.ts +2 -0
- package/dist/tex-requirements.d.ts.map +1 -1
- package/dist/tex-requirements.js +7 -2
- package/dist/tex-requirements.js.map +1 -1
- package/dist/tex-venue-rules.d.ts +36 -3
- package/dist/tex-venue-rules.d.ts.map +1 -1
- package/dist/tex-venue-rules.js +181 -33
- package/dist/tex-venue-rules.js.map +1 -1
- package/docs/rules/cycle/port-done.md +3 -0
- package/docs/rules/tex/negated-sentences.md +1 -1
- package/docs/rules/tex/template.md +96 -1
- package/docs/rules.md +19 -14
- package/docs/venues.md +51 -12
- package/eslint-rules/latex-language.test.mjs +64 -1
- package/eslint-rules/latex-language.ts +44 -2
- package/eslint.config.mjs +1 -0
- package/fixtures/build-e2e/neurips/PIPELINE-STATUS.md +3 -0
- package/fixtures/build-e2e/neurips/expect.json +20 -0
- package/fixtures/build-e2e/neurips/paper.tex +60 -0
- package/fixtures/build-e2e/neurips/paperlint.json +1 -0
- package/fixtures/build-e2e/neurips-nostyle/PIPELINE-STATUS.md +3 -0
- package/fixtures/build-e2e/neurips-nostyle/expect.json +26 -0
- package/fixtures/build-e2e/neurips-nostyle/paper.tex +56 -0
- package/fixtures/build-e2e/neurips-nostyle/paperlint.json +1 -0
- package/fixtures/build-e2e/neurips-over/PIPELINE-STATUS.md +3 -0
- package/fixtures/build-e2e/neurips-over/expect.json +24 -0
- package/fixtures/build-e2e/neurips-over/paper.tex +56 -0
- package/fixtures/build-e2e/neurips-over/paperlint.json +1 -0
- package/fixtures/real-papers.test.ts +17 -6
- package/package.json +3 -1
- package/presets/aaai-2027.jsonc +126 -0
- package/presets/aaai-2027.md +127 -0
- package/presets/aaai-press.jsonc +167 -0
- package/presets/aaai-press.md +126 -0
- package/presets/aidc-2026.jsonc +11 -11
- package/presets/icml-2027.jsonc +111 -0
- package/presets/icml-2027.md +130 -0
- package/presets/icml-style.jsonc +125 -0
- package/presets/icml-style.md +77 -0
- package/presets/neurips-2026.jsonc +123 -0
- package/presets/neurips-2026.md +137 -0
- package/presets/neurips-style.jsonc +113 -0
- package/presets/neurips-style.md +85 -0
- package/presets/venue-profile.schema.json +6 -0
- package/skills/build-label-page/scripts/label-page-browser.test.ts +275 -114
- package/src/adapters/latex/document-class.ts +292 -20
- package/src/adapters/latex/index.test.ts +310 -0
- package/src/adapters/latex/index.ts +12 -3
- package/src/adapters/latex/parse.ts +5 -0
- package/src/adapters/latex/prose.ts +27 -4
- package/src/cli.test.ts +6 -1
- package/src/domain/body-pages.test.ts +22 -6
- package/src/domain/body-pages.ts +4 -2
- package/src/domain/tex-document.ts +64 -5
- package/src/folder-venue-rule.test.ts +51 -0
- package/src/new-paper.test.ts +2 -2
- package/src/paper-settings-commands.test.ts +1 -1
- package/src/ports/latex.ts +14 -0
- package/src/preset-card-rule.test.ts +44 -27
- package/src/presets.test.ts +177 -1
- package/src/presets.ts +27 -0
- package/src/register-bands.test.ts +2 -2
- package/src/rule-cases.test.ts +52 -0
- package/src/tex-requirements.harness.ts +17 -3
- package/src/tex-requirements.ts +13 -2
- package/src/tex-venue-rules.test.ts +1020 -0
- package/src/tex-venue-rules.ts +248 -38
- package/src/venue-rules.test.ts +369 -2
|
@@ -1,21 +1,29 @@
|
|
|
1
1
|
/** The paper's `\documentclass`, a preset's template, and setting a class in a source. */
|
|
2
2
|
import {
|
|
3
3
|
documentClassLine,
|
|
4
|
+
optionsInEffect,
|
|
5
|
+
readWhole,
|
|
4
6
|
venueClass,
|
|
5
7
|
type ClassCandidate,
|
|
6
8
|
type ClassLine,
|
|
7
9
|
type DocumentClass,
|
|
10
|
+
type PackageLoad,
|
|
11
|
+
type PassedOptions,
|
|
12
|
+
type StylePackage,
|
|
8
13
|
} from "../../domain/tex-document.ts";
|
|
14
|
+
import { documentBodyOf, includesOf } from "./includes.ts";
|
|
9
15
|
import {
|
|
10
16
|
collapse,
|
|
17
|
+
isNode,
|
|
11
18
|
macroPlace,
|
|
12
19
|
mandatory,
|
|
13
20
|
optional,
|
|
14
21
|
textOf,
|
|
22
|
+
visited,
|
|
15
23
|
type Node,
|
|
16
24
|
type Macro,
|
|
17
25
|
} from "./nodes.ts";
|
|
18
|
-
import { parseLatex, type ParsedTex } from "./parse.ts";
|
|
26
|
+
import { liveRoot, parseLatex, type ParsedTex } from "./parse.ts";
|
|
19
27
|
|
|
20
28
|
const isDocumentClass = (n: Node): n is Macro =>
|
|
21
29
|
n.type === "macro" && n.content === "documentclass";
|
|
@@ -53,31 +61,110 @@ const optionText = (src: string, part: readonly Node[]): string =>
|
|
|
53
61
|
.join(""),
|
|
54
62
|
);
|
|
55
63
|
|
|
64
|
+
/** A node at the top of the tree, with its index there: what stands before what. */
|
|
65
|
+
interface AtTop {
|
|
66
|
+
readonly node: Macro;
|
|
67
|
+
readonly index: number;
|
|
68
|
+
}
|
|
69
|
+
|
|
56
70
|
/** Every `\documentclass` at the top of the tree, in source order. */
|
|
57
|
-
const classNodes = (t: ParsedTex): readonly
|
|
58
|
-
t.root.content.
|
|
71
|
+
const classNodes = (t: ParsedTex): readonly AtTop[] =>
|
|
72
|
+
t.root.content.flatMap((node, index) =>
|
|
73
|
+
isDocumentClass(node) ? [{ node, index }] : [],
|
|
74
|
+
);
|
|
75
|
+
|
|
76
|
+
/** Whether nodes hold a macro, inside braces too: what TeX reads there is the macro's expansion. */
|
|
77
|
+
const holdsMacro = (nodes: readonly Node[]): boolean =>
|
|
78
|
+
nodes.some(
|
|
79
|
+
(n) => n.type === "macro" || (n.type === "group" && holdsMacro(n.content)),
|
|
80
|
+
);
|
|
81
|
+
|
|
82
|
+
/** A list of options (`a, b={c,d}`), each as written, and the ones of them built by a macro. */
|
|
83
|
+
function optionList(
|
|
84
|
+
src: string,
|
|
85
|
+
nodes: readonly Node[],
|
|
86
|
+
): Pick<PassedOptions, "options" | "unread"> {
|
|
87
|
+
const parts = splitWhere(nodes, isComma)
|
|
88
|
+
.map((part) => ({ text: optionText(src, part), unread: holdsMacro(part) }))
|
|
89
|
+
.filter((o) => o.text !== "");
|
|
90
|
+
return {
|
|
91
|
+
options: parts.map((o) => o.text),
|
|
92
|
+
unread: parts.filter((o) => o.unread).map((o) => o.text),
|
|
93
|
+
};
|
|
94
|
+
}
|
|
59
95
|
|
|
60
|
-
/**
|
|
61
|
-
|
|
96
|
+
/** One `\PassOptionsToClass{options}{class}`: the class it names, its options, its index at the top. */
|
|
97
|
+
interface Passed extends PassedOptions {
|
|
98
|
+
readonly cls: string;
|
|
99
|
+
readonly index: number;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Every `\PassOptionsToClass` at the top of the tree: the options it gives a class count for a
|
|
104
|
+
* `\documentclass` of that class after it, as LaTeX reads them. One missing an argument passes
|
|
105
|
+
* nothing to any class.
|
|
106
|
+
*/
|
|
107
|
+
const passedOf = (t: ParsedTex): readonly Passed[] =>
|
|
108
|
+
t.root.content.flatMap((n, index) => {
|
|
109
|
+
if (n.type !== "macro" || n.content !== "PassOptionsToClass") return [];
|
|
110
|
+
const [opts, cls] = mandatory(n);
|
|
111
|
+
if (opts === undefined || cls === undefined) return [];
|
|
112
|
+
const list = optionList(t.src, opts.content);
|
|
113
|
+
const name = collapse(textOf(cls.content));
|
|
114
|
+
return [{ ...list, cls: name, place: macroPlace(n), index }];
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* A `\documentclass` node as a class line: empty; a class built by a macro, unread; or the class
|
|
119
|
+
* with its options as read, and the options `passed` to that class before the line.
|
|
120
|
+
*/
|
|
121
|
+
function classLine(
|
|
122
|
+
src: string,
|
|
123
|
+
{ node, index }: AtTop,
|
|
124
|
+
passed: readonly Passed[],
|
|
125
|
+
): ClassCandidate {
|
|
62
126
|
const place = macroPlace(node);
|
|
63
|
-
const
|
|
127
|
+
const name = mandatory(node)[0]?.content;
|
|
128
|
+
if (name !== undefined && holdsMacro(name)) return { kind: "unread", place };
|
|
129
|
+
const cls = collapse(textOf(name));
|
|
64
130
|
if (cls === "") return { kind: "empty", place };
|
|
65
|
-
const
|
|
66
|
-
.
|
|
67
|
-
.
|
|
68
|
-
|
|
131
|
+
const before = passed
|
|
132
|
+
.filter((p) => p.cls === cls && p.index < index)
|
|
133
|
+
.map(({ options, unread, place: where }) => ({
|
|
134
|
+
options,
|
|
135
|
+
unread,
|
|
136
|
+
place: where,
|
|
137
|
+
}));
|
|
138
|
+
const own = optionList(src, optional(node)?.content ?? []);
|
|
139
|
+
return { kind: "class", cls, ...own, passed: before, place };
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/** A `\documentclass` anywhere in the tree: in another macro's argument, a definition's body. */
|
|
143
|
+
const classAnywhere = (t: ParsedTex): boolean =>
|
|
144
|
+
visited(t.root, () => false).some((v) => isNode(v) && isDocumentClass(v));
|
|
145
|
+
|
|
146
|
+
/** An `\input` or `\include` in the preamble, left as written: a file this reader was not given. */
|
|
147
|
+
function includesInPreamble(t: ParsedTex): boolean {
|
|
148
|
+
const body = documentBodyOf(t)?.start ?? Number.POSITIVE_INFINITY;
|
|
149
|
+
return includesOf(t).some((i) => i.span.start < body);
|
|
69
150
|
}
|
|
70
151
|
|
|
71
152
|
/**
|
|
72
|
-
* The `\documentclass` at the top of the tree: missing;
|
|
73
|
-
*
|
|
74
|
-
*
|
|
75
|
-
*
|
|
153
|
+
* The `\documentclass` at the top of the tree: missing; unseen, when there is none at the top but
|
|
154
|
+
* one may stand where this reader does not look (`classAnywhere`, `includesInPreamble`); empty
|
|
155
|
+
* (`\documentclass{}`); unread (`\documentclass{\cls}`); the class with each of its options as
|
|
156
|
+
* written (`foo={a,b}` stays one option, braces kept), whitespace collapsed, its place spanning the
|
|
157
|
+
* macro and its arguments; or, when there are several (behind a TeX switch), each of them as a
|
|
158
|
+
* candidate.
|
|
76
159
|
*/
|
|
77
160
|
export function documentClassOf(t: ParsedTex): ClassLine {
|
|
78
|
-
const
|
|
161
|
+
const passed = passedOf(t);
|
|
162
|
+
const lines = classNodes(t).map((n) => classLine(t.src, n, passed));
|
|
79
163
|
const [first, second, ...rest] = lines;
|
|
80
|
-
if (first === undefined)
|
|
164
|
+
if (first === undefined)
|
|
165
|
+
return classAnywhere(t) || includesInPreamble(t)
|
|
166
|
+
? { kind: "unseen" }
|
|
167
|
+
: { kind: "missing" };
|
|
81
168
|
return second === undefined
|
|
82
169
|
? first
|
|
83
170
|
: { kind: "ambiguous", candidates: [first, second, ...rest] };
|
|
@@ -98,6 +185,188 @@ export function parseTemplate(text: string): DocumentClass | null {
|
|
|
98
185
|
return only.type === "string" ? { cls: only.content, options: [] } : null;
|
|
99
186
|
}
|
|
100
187
|
|
|
188
|
+
const isPackageLine = (n: Node): n is Macro =>
|
|
189
|
+
n.type === "macro" &&
|
|
190
|
+
(n.content === "usepackage" ||
|
|
191
|
+
n.content === "RequirePackage" ||
|
|
192
|
+
n.content === "RequirePackageWithOptions");
|
|
193
|
+
|
|
194
|
+
/** A comma list of names (`a, b`), each whitespace-collapsed, empty ones left out. */
|
|
195
|
+
const namesIn = (nodes: readonly Node[] | undefined): readonly string[] =>
|
|
196
|
+
splitWhere(nodes ?? [], isComma)
|
|
197
|
+
.map((part) => collapse(textOf(part)))
|
|
198
|
+
.filter(Boolean);
|
|
199
|
+
|
|
200
|
+
/** Options as LaTeX hands them on: each as written, and the ones built by a macro. */
|
|
201
|
+
type Options = Pick<PassedOptions, "options" | "unread">;
|
|
202
|
+
|
|
203
|
+
const NO_OPTIONS: Options = { options: [], unread: [] };
|
|
204
|
+
|
|
205
|
+
/** Several lists of options, one after the other. */
|
|
206
|
+
const joined = (lists: readonly Options[]): Options => ({
|
|
207
|
+
options: lists.flatMap((l) => l.options),
|
|
208
|
+
unread: lists.flatMap((l) => l.unread),
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
/** One `\PassOptionsToPackage{options}{packages}`: the packages it names, its options, its index. */
|
|
212
|
+
interface PassedToPackage extends Options {
|
|
213
|
+
readonly packages: readonly string[];
|
|
214
|
+
readonly index: number;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Every `\PassOptionsToPackage` at the top of the tree, none after `\end{document}` (`liveRoot`):
|
|
219
|
+
* LaTeX adds its options to each package's own list (`\@pass@ptions`, latex.ltx), so they count for
|
|
220
|
+
* a load of one of its packages after it. One missing an argument passes nothing.
|
|
221
|
+
*/
|
|
222
|
+
const passedToPackagesOf = (t: ParsedTex): readonly PassedToPackage[] =>
|
|
223
|
+
liveRoot(t).content.flatMap((n, index) => {
|
|
224
|
+
if (n.type !== "macro" || n.content !== "PassOptionsToPackage") return [];
|
|
225
|
+
const [opts, pkgs] = mandatory(n);
|
|
226
|
+
if (opts === undefined || pkgs === undefined) return [];
|
|
227
|
+
const list = optionList(t.src, opts.content);
|
|
228
|
+
return [{ ...list, packages: namesIn(pkgs.content), index }];
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* The global options in effect at `index`: those of the `\documentclass` lines before it. LaTeX
|
|
233
|
+
* keeps the class line's own options as `\@classoptionslist`, which every package's
|
|
234
|
+
* `\ProcessOptions` reads; options given with `\PassOptionsToClass` are the class's alone. Several
|
|
235
|
+
* lines behind a TeX switch: each one's, as any of them may be the one that builds.
|
|
236
|
+
*/
|
|
237
|
+
const globalAt = (t: ParsedTex, index: number): Options =>
|
|
238
|
+
joined(
|
|
239
|
+
classNodes(t)
|
|
240
|
+
.filter((c) => c.index < index)
|
|
241
|
+
.map((c) => optionList(t.src, optional(c.node)?.content ?? [])),
|
|
242
|
+
);
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* One package line, at `index` at the top: each package it names, with the options LaTeX hands it
|
|
246
|
+
* — the global ones, then those passed to it before the line, then the line's own — and its place.
|
|
247
|
+
* `\RequirePackageWithOptions` gives the package the options of the file that loads it instead of
|
|
248
|
+
* its own list: in a paper's preamble, none, so the global ones alone.
|
|
249
|
+
*/
|
|
250
|
+
function packageLoads(
|
|
251
|
+
src: string,
|
|
252
|
+
{ node, index }: AtTop,
|
|
253
|
+
{
|
|
254
|
+
passed,
|
|
255
|
+
global,
|
|
256
|
+
}: { readonly passed: readonly PassedToPackage[]; readonly global: Options },
|
|
257
|
+
): readonly PackageLoad[] {
|
|
258
|
+
const place = macroPlace(node);
|
|
259
|
+
const withOptions = node.content === "RequirePackageWithOptions";
|
|
260
|
+
const own = withOptions
|
|
261
|
+
? NO_OPTIONS
|
|
262
|
+
: optionList(src, optional(node)?.content ?? []);
|
|
263
|
+
const before = (name: string): readonly Options[] =>
|
|
264
|
+
withOptions
|
|
265
|
+
? []
|
|
266
|
+
: passed.filter((p) => p.index < index && p.packages.includes(name));
|
|
267
|
+
// A package loaded from a folder (`sty/aaai2027`) is the package of its file's base name: LaTeX
|
|
268
|
+
// parses the path and names the package by the base (`\@onefilewithoptions`, latex.ltx).
|
|
269
|
+
return namesIn(mandatory(node)[0]?.content)
|
|
270
|
+
.map((path) => path.slice(path.lastIndexOf("/") + 1))
|
|
271
|
+
.map((name) => ({
|
|
272
|
+
name,
|
|
273
|
+
...joined([global, ...before(name), own]),
|
|
274
|
+
place,
|
|
275
|
+
}));
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/**
|
|
279
|
+
* Every package the `\usepackage`, `\RequirePackage` and `\RequirePackageWithOptions` lines at the
|
|
280
|
+
* top of the tree load, in source order, none after `\end{document}` (TeX stops there): the
|
|
281
|
+
* preamble's, as the class line is read, each with the options LaTeX hands it (`packageLoads`). A
|
|
282
|
+
* comment is not a line; several lines behind a TeX switch are each read. A package loaded twice keeps the options of its first load in LaTeX, and a
|
|
283
|
+
* second load asking for more stops the build («Option clash»), so each line is read as its own.
|
|
284
|
+
*/
|
|
285
|
+
export function packagesOf(t: ParsedTex): readonly PackageLoad[] {
|
|
286
|
+
const passed = passedToPackagesOf(t);
|
|
287
|
+
return liveRoot(t)
|
|
288
|
+
.content.flatMap((node, index) =>
|
|
289
|
+
isPackageLine(node) ? [{ node, index }] : [],
|
|
290
|
+
)
|
|
291
|
+
.flatMap((at) =>
|
|
292
|
+
packageLoads(t.src, at, { passed, global: globalAt(t, at.index) }),
|
|
293
|
+
);
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* Whether the preamble may load a package, or pass it options, where this reader does not see: an
|
|
298
|
+
* `\input` or `\include` there left as written (no build record yet, or a stale one), a package line
|
|
299
|
+
* or a `\PassOptionsToPackage` inside another macro's argument or a definition's body
|
|
300
|
+
* (`\newcommand{\load}{\usepackage{…}}`), one whose package name is built by a macro
|
|
301
|
+
* (`\usepackage{\style}`), or a file read through another loader (`\InputIfFileExists`, `\import`).
|
|
302
|
+
*/
|
|
303
|
+
export function packagesUnseen(t: ParsedTex): boolean {
|
|
304
|
+
const live = liveRoot(t);
|
|
305
|
+
const top = new Set<unknown>(live.content);
|
|
306
|
+
const hidden = visited(live, () => false).some(
|
|
307
|
+
(v) =>
|
|
308
|
+
isNode(v) &&
|
|
309
|
+
(isPackageLine(v) || isPassToPackage(v)) &&
|
|
310
|
+
(!top.has(v) || namedByMacro(v)),
|
|
311
|
+
);
|
|
312
|
+
return hidden || includesInPreamble(t) || otherLoaderInPreamble(t);
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
/** Macros that read a file this reader never opens: the `import` package's and the kernel's. */
|
|
316
|
+
const OTHER_LOADERS: ReadonlySet<string> = new Set([
|
|
317
|
+
"InputIfFileExists",
|
|
318
|
+
"import",
|
|
319
|
+
"subimport",
|
|
320
|
+
"inputfrom",
|
|
321
|
+
"subinputfrom",
|
|
322
|
+
]);
|
|
323
|
+
|
|
324
|
+
/**
|
|
325
|
+
* Whether the preamble reads a file through a loader `includesOf` does not follow, or names a
|
|
326
|
+
* package line through `\csname` (`\csname usepackage\endcsname{…}`).
|
|
327
|
+
*/
|
|
328
|
+
function otherLoaderInPreamble(t: ParsedTex): boolean {
|
|
329
|
+
const body = documentBodyOf(t)?.start ?? Number.POSITIVE_INFINITY;
|
|
330
|
+
const loader = visited(t.root, () => false).some(
|
|
331
|
+
(v) =>
|
|
332
|
+
isNode(v) &&
|
|
333
|
+
v.type === "macro" &&
|
|
334
|
+
OTHER_LOADERS.has(v.content) &&
|
|
335
|
+
// A node with no place (none the parser makes) reads NaN, which is before nothing.
|
|
336
|
+
Number(v.position?.start.offset) < body,
|
|
337
|
+
);
|
|
338
|
+
return loader || CSNAME_LOAD.test(t.src.slice(0, body));
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
const CSNAME_LOAD = /\\csname\s*(?:usepackage|RequirePackage)/u;
|
|
342
|
+
|
|
343
|
+
const isPassToPackage = (n: Node): n is Macro =>
|
|
344
|
+
n.type === "macro" && n.content === "PassOptionsToPackage";
|
|
345
|
+
|
|
346
|
+
/** Whether the packages a line names are built by a macro: its last mandatory argument. */
|
|
347
|
+
const namedByMacro = (m: Macro): boolean =>
|
|
348
|
+
holdsMacro(mandatory(m).at(isPassToPackage(m) ? 1 : 0)?.content ?? []);
|
|
349
|
+
|
|
350
|
+
/**
|
|
351
|
+
* A preset's `template_style` → the package it names and the options it requires: one whole
|
|
352
|
+
* `\usepackage[…]{…}` line naming one package. Anything else is null.
|
|
353
|
+
*/
|
|
354
|
+
export function parseTemplateStyle(text: string): StylePackage | null {
|
|
355
|
+
const t = parseLatex(text);
|
|
356
|
+
const nodes = t.root.content.filter(
|
|
357
|
+
(n) => n.type !== "whitespace" && n.type !== "comment",
|
|
358
|
+
);
|
|
359
|
+
const [only, ...rest] = nodes;
|
|
360
|
+
if (only === undefined || rest.length > 0 || !isPackageLine(only))
|
|
361
|
+
return null;
|
|
362
|
+
const none = { passed: [], global: NO_OPTIONS };
|
|
363
|
+
const loads = packageLoads(t.src, { node: only, index: 0 }, none);
|
|
364
|
+
const [load, ...more] = loads;
|
|
365
|
+
return load === undefined || more.length > 0
|
|
366
|
+
? null
|
|
367
|
+
: { name: load.name, options: load.options };
|
|
368
|
+
}
|
|
369
|
+
|
|
101
370
|
/** `src` with `text` in place of `[from, to)`. */
|
|
102
371
|
const splice = (src: string, from: number, to: number, text: string): string =>
|
|
103
372
|
src.slice(0, from) + text + src.slice(to);
|
|
@@ -133,12 +402,15 @@ export function replaceDocumentClass(
|
|
|
133
402
|
want: DocumentClass,
|
|
134
403
|
): string {
|
|
135
404
|
// Several lines behind a switch: which one builds is not known, so none is rewritten.
|
|
136
|
-
const [
|
|
137
|
-
if (
|
|
138
|
-
const
|
|
405
|
+
const [top, ...others] = classNodes(t);
|
|
406
|
+
if (top === undefined || others.length > 0) return t.src;
|
|
407
|
+
const { node } = top;
|
|
408
|
+
const line = classLine(t.src, top, passedOf(t));
|
|
139
409
|
if (line.kind !== "class" || line.place.kind === "unplaced") return t.src;
|
|
410
|
+
// A line with an option built by a macro: what it lacks is not known, so it is not rewritten.
|
|
411
|
+
if (!readWhole(line)) return t.src;
|
|
140
412
|
const { start, end } = line.place.span;
|
|
141
|
-
const change = venueClass(line, want);
|
|
413
|
+
const change = venueClass(optionsInEffect(line), want);
|
|
142
414
|
switch (change.kind) {
|
|
143
415
|
case "keep":
|
|
144
416
|
return t.src;
|