latex-kit 0.4.1 → 0.5.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/README.md +20 -1
- package/args.js +3 -3
- package/build.d.ts +22 -2
- package/build.js +230 -95
- package/check.js +4 -3
- package/cli.js +2 -2
- package/commands.js +1 -0
- package/config.d.ts +13 -4
- package/config.js +1 -1
- package/diff.d.ts +2 -2
- package/diff.js +3 -3
- package/esm/args.js +3 -3
- package/esm/build.d.ts +22 -2
- package/esm/build.js +231 -98
- package/esm/check.js +4 -3
- package/esm/cli.js +2 -2
- package/esm/commands.js +2 -1
- package/esm/config.d.ts +13 -4
- package/esm/config.js +1 -1
- package/esm/diff.d.ts +2 -2
- package/esm/diff.js +3 -3
- package/esm/import.js +1 -1
- package/esm/index.d.ts +6 -2
- package/esm/index.js +5 -2
- package/esm/lock.d.ts +24 -0
- package/esm/lock.js +50 -0
- package/esm/project-commands.d.ts +4 -0
- package/esm/project-commands.js +69 -5
- package/esm/project.d.ts +18 -2
- package/esm/project.js +92 -5
- package/esm/render.js +4 -2
- package/esm/usage.js +23 -5
- package/import.js +1 -1
- package/index.d.ts +6 -2
- package/index.js +24 -2
- package/lock.d.ts +24 -0
- package/lock.js +57 -0
- package/package.json +6 -5
- package/project-commands.d.ts +4 -0
- package/project-commands.js +71 -5
- package/project.d.ts +18 -2
- package/project.js +90 -3
- package/render.js +4 -2
- package/usage.js +23 -5
package/esm/build.d.ts
CHANGED
|
@@ -1,11 +1,21 @@
|
|
|
1
|
-
import { Engine } from '@latex-kit/render';
|
|
1
|
+
import { Engine, EngineChoice } from '@latex-kit/render';
|
|
2
2
|
import { Diagnostic, Variant } from '@latex-kit/template';
|
|
3
3
|
import { Project } from './project';
|
|
4
4
|
export interface BuildOptions {
|
|
5
5
|
variant?: Variant;
|
|
6
6
|
/** Fail on missing values even in drafts. Default: on when CI=true. */
|
|
7
7
|
strict?: boolean;
|
|
8
|
-
|
|
8
|
+
/** Default: the template's, the config's, else `auto`. */
|
|
9
|
+
engine?: EngineChoice;
|
|
10
|
+
/** Build in this locale instead of the record's; the output name gets `-<locale>`. */
|
|
11
|
+
locale?: string;
|
|
12
|
+
/** Rebuild even when the cached PDF matches every input. */
|
|
13
|
+
force?: boolean;
|
|
14
|
+
/**
|
|
15
|
+
* Folder of signature images for signed builds. Default: the
|
|
16
|
+
* LATEX_KIT_SIGNATURES environment variable, then the config.
|
|
17
|
+
*/
|
|
18
|
+
signatures?: string | null;
|
|
9
19
|
backend?: 'docker' | 'local';
|
|
10
20
|
image?: string;
|
|
11
21
|
platform?: string | null;
|
|
@@ -33,12 +43,22 @@ export interface BuildResult {
|
|
|
33
43
|
sourceDateEpoch: number;
|
|
34
44
|
reproducible: boolean;
|
|
35
45
|
verified?: boolean;
|
|
46
|
+
/** The engine TeX ran with (null for --tex-only). */
|
|
47
|
+
engine: Engine | null;
|
|
48
|
+
/** "chosen", or why `auto` picked it. */
|
|
49
|
+
engineReason: string | null;
|
|
50
|
+
/** The PDF was already up to date: same inputs, same image, same date. */
|
|
51
|
+
cached: boolean;
|
|
36
52
|
diagnostics: Diagnostic[];
|
|
37
53
|
}
|
|
38
54
|
export declare class BuildError extends Error {
|
|
39
55
|
readonly diagnostics: Diagnostic[];
|
|
40
56
|
constructor(message: string, diagnostics: Diagnostic[]);
|
|
41
57
|
}
|
|
58
|
+
/** A folder name for a signer: "Avery Example" → "avery-example". */
|
|
59
|
+
export declare function signerSlug(name: string): string;
|
|
60
|
+
/** The signatures folder: option, LATEX_KIT_SIGNATURES, then config. `~` is home. */
|
|
61
|
+
export declare function signaturesDir(project: Project, option?: string | null): string | null;
|
|
42
62
|
/** Build one document record. */
|
|
43
63
|
export declare function buildDocument(project: Project, file: string, options?: BuildOptions): Promise<BuildResult>;
|
|
44
64
|
/** Build a template as a blank form, without a record. */
|
package/esm/build.js
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
import { gitCommitTime, render, RenderError } from '@latex-kit/render';
|
|
1
|
+
import { detectEngine, gitCommitTime, render, RenderError } from '@latex-kit/render';
|
|
2
2
|
import { loadDocument, mintDocId, validate, writeDocId } from '@latex-kit/schema';
|
|
3
3
|
import { diag, evaluateTemplate, hasErrors, LATEX_KIT_STY, wrapDocument, } from '@latex-kit/template';
|
|
4
4
|
import { execFileSync } from 'child_process';
|
|
5
5
|
import { createHash } from 'crypto';
|
|
6
|
-
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'fs';
|
|
7
|
-
import {
|
|
6
|
+
import { copyFileSync, existsSync, mkdirSync, readdirSync, readFileSync, rmSync, statSync, writeFileSync } from 'fs';
|
|
7
|
+
import { homedir } from 'os';
|
|
8
|
+
import { basename, extname, isAbsolute, join, posix, relative, resolve, sep } from 'path';
|
|
8
9
|
export class BuildError extends Error {
|
|
9
10
|
diagnostics;
|
|
10
11
|
constructor(message, diagnostics) {
|
|
@@ -46,6 +47,42 @@ function gitShortHead(cwd) {
|
|
|
46
47
|
return 'no-git';
|
|
47
48
|
}
|
|
48
49
|
}
|
|
50
|
+
/** A folder name for a signer: "Avery Example" → "avery-example". */
|
|
51
|
+
export function signerSlug(name) {
|
|
52
|
+
return name
|
|
53
|
+
.normalize('NFKD')
|
|
54
|
+
.replace(/[\u0300-\u036f]/g, '')
|
|
55
|
+
.toLowerCase()
|
|
56
|
+
.replace(/[^a-z0-9]+/g, '-')
|
|
57
|
+
.replace(/^-|-$/g, '');
|
|
58
|
+
}
|
|
59
|
+
const IMAGE_EXTENSIONS = ['.pdf', '.png', '.jpg', '.jpeg'];
|
|
60
|
+
/** The signatures folder: option, LATEX_KIT_SIGNATURES, then config. `~` is home. */
|
|
61
|
+
export function signaturesDir(project, option) {
|
|
62
|
+
const raw = option ?? process.env.LATEX_KIT_SIGNATURES ?? project.config.signatures ?? null;
|
|
63
|
+
if (!raw)
|
|
64
|
+
return null;
|
|
65
|
+
const expanded = raw === '~' || raw.startsWith('~/') ? join(homedir(), raw.slice(1)) : raw;
|
|
66
|
+
return resolve(project.root, expanded);
|
|
67
|
+
}
|
|
68
|
+
/** Every file under a folder, for the cache key. */
|
|
69
|
+
function filesUnder(dir) {
|
|
70
|
+
const out = [];
|
|
71
|
+
const walk = (d) => {
|
|
72
|
+
if (!existsSync(d))
|
|
73
|
+
return;
|
|
74
|
+
for (const name of readdirSync(d).sort()) {
|
|
75
|
+
const p = join(d, name);
|
|
76
|
+
if (statSync(p).isDirectory())
|
|
77
|
+
walk(p);
|
|
78
|
+
else
|
|
79
|
+
out.push(p);
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
walk(dir);
|
|
83
|
+
return out;
|
|
84
|
+
}
|
|
85
|
+
const posixRel = (root, p) => relative(root, p).split(sep).join(posix.sep);
|
|
49
86
|
async function run(job) {
|
|
50
87
|
const { project, template, variant, options } = job;
|
|
51
88
|
const diagnostics = job.diagnostics;
|
|
@@ -66,103 +103,194 @@ async function run(job) {
|
|
|
66
103
|
docId = mintDocId({ prefix: template.meta.docIdPrefix ?? template.name, epoch: Math.floor(Date.now() / 1000) });
|
|
67
104
|
minted = true;
|
|
68
105
|
}
|
|
69
|
-
const evaluated = evaluateTemplate(template.compiled, {
|
|
70
|
-
data: job.data,
|
|
71
|
-
variant,
|
|
72
|
-
locale: job.locale,
|
|
73
|
-
docId,
|
|
74
|
-
template: template.name,
|
|
75
|
-
allowMissing: variant === 'draft' ? !strict : undefined,
|
|
76
|
-
loadSnippet,
|
|
77
|
-
loadClause: project.loadClause,
|
|
78
|
-
});
|
|
79
|
-
diagnostics.push(...evaluated.diagnostics);
|
|
80
|
-
if (hasErrors(diagnostics))
|
|
81
|
-
throw new BuildError(`${job.name}: cannot build ${variant}`, diagnostics);
|
|
82
|
-
// snippets count as inputs for the build date too
|
|
83
|
-
const clauseFiles = evaluated.clauses.map((c) => resolve(c.file));
|
|
84
|
-
const extra = [...snippets, ...clauseFiles];
|
|
85
|
-
const finalEpoch = extra.length ? buildEpoch([...files, ...extra]) : { epoch, reproducible };
|
|
86
|
-
// 2. write the .tex next to latex-kit.sty
|
|
87
106
|
const outDir = options.outDir ?? join(project.outDir, job.name);
|
|
88
|
-
mkdirSync(outDir, { recursive: true });
|
|
89
107
|
const job_ = variant === 'final' ? job.name : `${job.name}-${variant}`;
|
|
90
|
-
|
|
91
|
-
const
|
|
92
|
-
const
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
sourceDateEpoch: finalEpoch.epoch,
|
|
120
|
-
texInputs: [relative(project.root, outDir) || '.', relative(project.root, template.dir) || '.'],
|
|
121
|
-
onOutput: options.onOutput,
|
|
122
|
-
};
|
|
123
|
-
let rendered;
|
|
124
|
-
try {
|
|
125
|
-
rendered = await render({ ...renderOpts, outDir: relative(project.root, outDir) });
|
|
126
|
-
}
|
|
127
|
-
catch (e) {
|
|
128
|
-
if (e instanceof RenderError) {
|
|
129
|
-
throw new BuildError(`${job.name}: ${e.message}`, [
|
|
130
|
-
...diagnostics,
|
|
131
|
-
...e.diagnostics.map((d) => diag(d.severity, d.message, d.file ? { file: project.rel(join(outDir, basename(d.file))), line: d.line ?? 0, column: 0 } : undefined)),
|
|
132
|
-
]);
|
|
108
|
+
// signed builds: copy signature images next to the .tex for this build only
|
|
109
|
+
const stageDir = join(outDir, 'lk-signatures');
|
|
110
|
+
const staged = [];
|
|
111
|
+
let signatureImage;
|
|
112
|
+
const sigDir = variant === 'signed' ? signaturesDir(project, options.signatures) : null;
|
|
113
|
+
if (sigDir) {
|
|
114
|
+
const at = { file: project.rel(sigDir), line: 1, column: 1 };
|
|
115
|
+
if (!existsSync(sigDir)) {
|
|
116
|
+
diagnostics.push(diag('error', `signatures folder not found: ${project.rel(sigDir)}`, at));
|
|
117
|
+
}
|
|
118
|
+
else {
|
|
119
|
+
const inside = relative(project.root, sigDir);
|
|
120
|
+
if (!inside.startsWith('..') && !isAbsolute(inside)) {
|
|
121
|
+
diagnostics.push(diag('warning', 'the signatures folder is inside the project; keep signature images out of the repository', at));
|
|
122
|
+
}
|
|
123
|
+
signatureImage = (signer) => {
|
|
124
|
+
const names = signer.image ? [signer.image] : IMAGE_EXTENSIONS.map((e) => `${signerSlug(signer.name)}${e}`);
|
|
125
|
+
const found = names.find((n) => basename(n) === n && existsSync(join(sigDir, n)));
|
|
126
|
+
if (!found) {
|
|
127
|
+
diagnostics.push(diag('warning', `no signature image for ${signer.name} in ${project.rel(sigDir)} (looked for ${names.join(', ')}); printed /s/ ${signer.name}`, at));
|
|
128
|
+
return null;
|
|
129
|
+
}
|
|
130
|
+
mkdirSync(stageDir, { recursive: true });
|
|
131
|
+
const target = join(stageDir, found);
|
|
132
|
+
copyFileSync(join(sigDir, found), target);
|
|
133
|
+
if (!staged.includes(target))
|
|
134
|
+
staged.push(target);
|
|
135
|
+
return posixRel(project.root, target);
|
|
136
|
+
};
|
|
133
137
|
}
|
|
134
|
-
throw e;
|
|
135
138
|
}
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
if (options.verify) {
|
|
139
|
-
// same output path: some tools (Ghostscript) hash the path into the PDF ID
|
|
140
|
-
const again = await render({ ...renderOpts, outDir: relative(project.root, outDir) });
|
|
141
|
-
result.verified = again.sha256 === rendered.sha256;
|
|
139
|
+
try {
|
|
140
|
+
return await fill();
|
|
142
141
|
}
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
142
|
+
finally {
|
|
143
|
+
rmSync(stageDir, { recursive: true, force: true });
|
|
144
|
+
}
|
|
145
|
+
async function fill() {
|
|
146
|
+
const evaluated = evaluateTemplate(template.compiled, {
|
|
147
|
+
data: job.data,
|
|
148
|
+
variant,
|
|
149
|
+
locale: job.locale,
|
|
150
|
+
docId,
|
|
151
|
+
template: template.name,
|
|
152
|
+
allowMissing: variant === 'draft' ? !strict : undefined,
|
|
153
|
+
loadSnippet,
|
|
154
|
+
loadClause: project.loadClause,
|
|
155
|
+
components: project.components,
|
|
156
|
+
signatureImage,
|
|
157
|
+
signed: job.signed,
|
|
158
|
+
});
|
|
159
|
+
diagnostics.push(...evaluated.diagnostics);
|
|
160
|
+
if (hasErrors(diagnostics))
|
|
161
|
+
throw new BuildError(`${job.name}: cannot build ${variant}`, diagnostics);
|
|
162
|
+
// snippets, clauses and components count as inputs for the build date too
|
|
163
|
+
const clauseFiles = evaluated.clauses.map((c) => resolve(c.file));
|
|
164
|
+
const componentFiles = evaluated.components.map((n) => project.componentFiles[n]).filter((f) => !!f);
|
|
165
|
+
const extra = [...snippets, ...clauseFiles, ...componentFiles];
|
|
166
|
+
const finalEpoch = extra.length ? buildEpoch([...files, ...extra]) : { epoch, reproducible };
|
|
167
|
+
// 2. write the .tex next to latex-kit.sty
|
|
168
|
+
mkdirSync(outDir, { recursive: true });
|
|
169
|
+
const texPath = join(outDir, `${job_}.tex`);
|
|
170
|
+
const stamp = `${new Date(finalEpoch.epoch * 1000).toISOString().slice(0, 10)} ${gitShortHead(project.root)}`;
|
|
171
|
+
const tex = wrapDocument(evaluated.tex, { variant, stamp });
|
|
172
|
+
writeFileSync(texPath, tex);
|
|
173
|
+
writeFileSync(join(outDir, 'latex-kit.sty'), LATEX_KIT_STY);
|
|
174
|
+
const result = {
|
|
175
|
+
name: job.name,
|
|
176
|
+
template: template.name,
|
|
177
|
+
variant,
|
|
178
|
+
tex: texPath,
|
|
179
|
+
pdf: null,
|
|
180
|
+
sha256: null,
|
|
181
|
+
docId: docId ?? null,
|
|
182
|
+
minted: false,
|
|
183
|
+
sourceDateEpoch: finalEpoch.epoch,
|
|
184
|
+
reproducible: finalEpoch.reproducible,
|
|
185
|
+
engine: null,
|
|
186
|
+
engineReason: null,
|
|
187
|
+
cached: false,
|
|
188
|
+
diagnostics,
|
|
189
|
+
};
|
|
190
|
+
if (options.texOnly)
|
|
191
|
+
return result;
|
|
192
|
+
// 3. render, unless the last build had exactly these inputs
|
|
193
|
+
const choice = options.engine ?? template.meta.engine ?? project.config.engine ?? 'auto';
|
|
194
|
+
let engine;
|
|
195
|
+
let engineReason;
|
|
196
|
+
if (choice === 'auto') {
|
|
197
|
+
const d = detectEngine(tex);
|
|
198
|
+
engine = d.engine;
|
|
199
|
+
engineReason = `auto: ${d.reason}`;
|
|
200
|
+
for (const w of d.warnings)
|
|
201
|
+
diagnostics.push(diag('warning', w, { file: template.compiled.file, line: 1, column: 1 }));
|
|
202
|
+
}
|
|
203
|
+
else {
|
|
204
|
+
engine = choice;
|
|
205
|
+
engineReason = 'chosen';
|
|
206
|
+
}
|
|
207
|
+
result.engine = engine;
|
|
208
|
+
result.engineReason = engineReason;
|
|
209
|
+
const backend = options.backend ?? project.config.backend ?? 'docker';
|
|
210
|
+
const image = project.image(options.image);
|
|
211
|
+
const platform = options.platform !== undefined ? options.platform : project.config.platform;
|
|
212
|
+
const renderOpts = {
|
|
213
|
+
file: texPath,
|
|
214
|
+
root: project.root,
|
|
215
|
+
engine,
|
|
216
|
+
backend,
|
|
217
|
+
image,
|
|
218
|
+
platform,
|
|
219
|
+
sourceDateEpoch: finalEpoch.epoch,
|
|
220
|
+
texInputs: [relative(project.root, outDir) || '.', relative(project.root, template.dir) || '.'],
|
|
221
|
+
onOutput: options.onOutput,
|
|
222
|
+
};
|
|
223
|
+
// content-addressed: the PDF depends on the .tex, the .sty, the template
|
|
224
|
+
// folder (images, \\input files), signature images, the engine, the image
|
|
225
|
+
// and the build date. Same key → same bytes, so the last PDF is reused.
|
|
226
|
+
const keyInputs = [...filesUnder(template.dir), ...staged].map((f) => [posixRel(project.root, f), sha256(readFileSync(f))]);
|
|
227
|
+
const cacheKey = sha256(JSON.stringify({ v: 1, tex: sha256(tex), sty: sha256(LATEX_KIT_STY), engine, backend, image: backend === 'docker' ? (image ?? 'default') : null, platform: platform ?? null, epoch: finalEpoch.epoch, inputs: keyInputs }));
|
|
228
|
+
const manifestPath = join(outDir, `${job_}.manifest.json`);
|
|
229
|
+
const pdfPath = join(outDir, `${job_}.pdf`);
|
|
230
|
+
if (!options.force && !options.verify && existsSync(manifestPath) && existsSync(pdfPath)) {
|
|
231
|
+
try {
|
|
232
|
+
const m = JSON.parse(readFileSync(manifestPath, 'utf8'));
|
|
233
|
+
const pdfSha = sha256(readFileSync(pdfPath));
|
|
234
|
+
if (m.cacheKey === cacheKey && m.pdf?.sha256 === pdfSha) {
|
|
235
|
+
result.pdf = pdfPath;
|
|
236
|
+
result.sha256 = pdfSha;
|
|
237
|
+
result.cached = true;
|
|
238
|
+
return result;
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
catch {
|
|
242
|
+
// an unreadable manifest just means a rebuild
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
let rendered;
|
|
246
|
+
try {
|
|
247
|
+
rendered = await render({ ...renderOpts, outDir: relative(project.root, outDir) });
|
|
248
|
+
}
|
|
249
|
+
catch (e) {
|
|
250
|
+
if (e instanceof RenderError) {
|
|
251
|
+
throw new BuildError(`${job.name}: ${e.message}`, [
|
|
252
|
+
...diagnostics,
|
|
253
|
+
...e.diagnostics.map((d) => diag(d.severity, d.message, d.file ? { file: project.rel(join(outDir, basename(d.file))), line: d.line ?? 0, column: 0 } : undefined)),
|
|
254
|
+
]);
|
|
255
|
+
}
|
|
256
|
+
throw e;
|
|
257
|
+
}
|
|
258
|
+
result.pdf = rendered.pdf;
|
|
259
|
+
result.sha256 = rendered.sha256;
|
|
260
|
+
if (options.verify) {
|
|
261
|
+
// same output path: some tools (Ghostscript) hash the path into the PDF ID
|
|
262
|
+
const again = await render({ ...renderOpts, outDir: relative(project.root, outDir) });
|
|
263
|
+
result.verified = again.sha256 === rendered.sha256;
|
|
264
|
+
}
|
|
265
|
+
// 4. keep the ID: only after a successful final build
|
|
266
|
+
if (minted && job.mintInto) {
|
|
267
|
+
writeDocId(job.mintInto, docId);
|
|
268
|
+
result.minted = true;
|
|
269
|
+
}
|
|
270
|
+
// 5. manifest: what went in, what came out
|
|
271
|
+
const inputFiles = [...new Set([...files, ...extra])].filter(existsSync).sort();
|
|
272
|
+
writeFileSync(manifestPath, JSON.stringify({
|
|
273
|
+
document: job.name,
|
|
274
|
+
template: template.name,
|
|
275
|
+
variant,
|
|
276
|
+
docId: docId ?? null,
|
|
277
|
+
locale: job.locale,
|
|
278
|
+
engine,
|
|
279
|
+
engineReason,
|
|
280
|
+
image: rendered.image,
|
|
281
|
+
cacheKey,
|
|
282
|
+
sourceDateEpoch: finalEpoch.epoch,
|
|
283
|
+
inputs: inputFiles.map((f) => ({ file: relative(project.root, f), sha256: sha256(readFileSync(f)) })),
|
|
284
|
+
tex: { file: relative(project.root, texPath), sha256: sha256(tex) },
|
|
285
|
+
pdf: { file: relative(project.root, rendered.pdf), sha256: rendered.sha256 },
|
|
286
|
+
clauses: evaluated.clauses.map((c) => ({ id: c.id, version: c.version ?? null, file: relative(project.root, resolve(c.file)) })),
|
|
287
|
+
components: evaluated.components,
|
|
288
|
+
fields: evaluated.fields.map((f) => ({ path: f.path, missing: f.missing })),
|
|
289
|
+
// which signers had an image; never the images or where they live
|
|
290
|
+
signatureImages: staged.map((f) => basename(f)),
|
|
291
|
+
}, null, 2) + '\n');
|
|
292
|
+
return result;
|
|
147
293
|
}
|
|
148
|
-
// 5. manifest: what went in, what came out
|
|
149
|
-
const inputFiles = [...new Set([...files, ...extra])].filter(existsSync).sort();
|
|
150
|
-
writeFileSync(join(outDir, `${job_}.manifest.json`), JSON.stringify({
|
|
151
|
-
document: job.name,
|
|
152
|
-
template: template.name,
|
|
153
|
-
variant,
|
|
154
|
-
docId: docId ?? null,
|
|
155
|
-
locale: job.locale,
|
|
156
|
-
engine,
|
|
157
|
-
image: rendered.image,
|
|
158
|
-
sourceDateEpoch: finalEpoch.epoch,
|
|
159
|
-
inputs: inputFiles.map((f) => ({ file: relative(project.root, f), sha256: sha256(readFileSync(f)) })),
|
|
160
|
-
tex: { file: relative(project.root, texPath), sha256: sha256(tex) },
|
|
161
|
-
pdf: { file: relative(project.root, rendered.pdf), sha256: rendered.sha256 },
|
|
162
|
-
clauses: evaluated.clauses.map((c) => ({ id: c.id, version: c.version ?? null, file: relative(project.root, resolve(c.file)) })),
|
|
163
|
-
fields: evaluated.fields.map((f) => ({ path: f.path, missing: f.missing })),
|
|
164
|
-
}, null, 2) + '\n');
|
|
165
|
-
return result;
|
|
166
294
|
}
|
|
167
295
|
/** Build one document record. */
|
|
168
296
|
export async function buildDocument(project, file, options = {}) {
|
|
@@ -189,14 +317,18 @@ export async function buildDocument(project, file, options = {}) {
|
|
|
189
317
|
diagnostics.push(diag('error', `a ${variant} build needs a document ID, and latex-kit does not edit code. Add: docId: '${suggestion}'`, at));
|
|
190
318
|
throw new BuildError(`${doc.record.template}: no document ID`, diagnostics);
|
|
191
319
|
}
|
|
320
|
+
const natural = doc.record.locale ?? template.meta.locale ?? project.config.locale;
|
|
321
|
+
const name = project.documentName(file);
|
|
192
322
|
return run({
|
|
193
323
|
project,
|
|
194
324
|
template,
|
|
195
|
-
|
|
325
|
+
// another language is another PDF, next to the usual one
|
|
326
|
+
name: options.locale && options.locale !== natural ? `${name}-${options.locale}` : name,
|
|
196
327
|
variant,
|
|
197
328
|
data,
|
|
198
|
-
locale:
|
|
329
|
+
locale: options.locale ?? natural,
|
|
199
330
|
docId: doc.record.docId ?? options.docIdFallback,
|
|
331
|
+
signed: doc.record.signed,
|
|
200
332
|
inputs: [file, ...doc.refFiles],
|
|
201
333
|
diagnostics,
|
|
202
334
|
options,
|
|
@@ -206,13 +338,14 @@ export async function buildDocument(project, file, options = {}) {
|
|
|
206
338
|
/** Build a template as a blank form, without a record. */
|
|
207
339
|
export async function buildBlank(project, templateName, options = {}) {
|
|
208
340
|
const template = await project.template(templateName);
|
|
341
|
+
const natural = template.meta.locale ?? project.config.locale;
|
|
209
342
|
return run({
|
|
210
343
|
project,
|
|
211
344
|
template,
|
|
212
|
-
name: `${templateName}
|
|
345
|
+
name: options.locale && options.locale !== natural ? `${templateName}-${options.locale}` : templateName,
|
|
213
346
|
variant: 'blank',
|
|
214
347
|
data: {},
|
|
215
|
-
locale:
|
|
348
|
+
locale: options.locale ?? natural,
|
|
216
349
|
inputs: [],
|
|
217
350
|
diagnostics: [],
|
|
218
351
|
options: { ...options, variant: 'blank' },
|
package/esm/check.js
CHANGED
|
@@ -5,15 +5,16 @@ import { analyzeTemplate, diag } from '@latex-kit/template';
|
|
|
5
5
|
* against each schema, every document's data, references, and duplicate IDs.
|
|
6
6
|
*/
|
|
7
7
|
export async function checkProject(project) {
|
|
8
|
-
const diagnostics = [];
|
|
8
|
+
const diagnostics = [...project.codeDiagnostics];
|
|
9
9
|
const templates = project.templateNames();
|
|
10
|
+
const components = project.componentNames();
|
|
10
11
|
for (const name of templates) {
|
|
11
12
|
try {
|
|
12
13
|
const t = await project.template(name);
|
|
13
14
|
if (!t.jsonSchema) {
|
|
14
15
|
diagnostics.push(diag('warning', `template ${name} has no schema.ts, so field names are not checked`, { file: t.compiled.file, line: 1, column: 1 }));
|
|
15
16
|
}
|
|
16
|
-
diagnostics.push(...analyzeTemplate(t.compiled, { schema: t.jsonSchema ?? undefined, loadSnippet: project.loadSnippet, loadClause: project.loadClause }).diagnostics);
|
|
17
|
+
diagnostics.push(...analyzeTemplate(t.compiled, { schema: t.jsonSchema ?? undefined, loadSnippet: project.loadSnippet, loadClause: project.loadClause, components }).diagnostics);
|
|
17
18
|
}
|
|
18
19
|
catch (e) {
|
|
19
20
|
diagnostics.push(diag('error', e.message, { file: project.rel(project.path(project.config.templates, name)), line: 1, column: 1 }));
|
|
@@ -49,6 +50,6 @@ export async function checkProject(project) {
|
|
|
49
50
|
/** Every field a template reads, including through snippets. */
|
|
50
51
|
export async function templateFields(project, name) {
|
|
51
52
|
const t = await project.template(name);
|
|
52
|
-
const r = analyzeTemplate(t.compiled, { schema: t.jsonSchema ?? undefined, loadSnippet: project.loadSnippet, loadClause: project.loadClause });
|
|
53
|
+
const r = analyzeTemplate(t.compiled, { schema: t.jsonSchema ?? undefined, loadSnippet: project.loadSnippet, loadClause: project.loadClause, components: project.componentNames() });
|
|
53
54
|
return { fields: r.fields, diagnostics: r.diagnostics };
|
|
54
55
|
}
|
package/esm/cli.js
CHANGED
|
@@ -18,8 +18,8 @@ const app = new CLI(commands, {
|
|
|
18
18
|
version: version(),
|
|
19
19
|
minimistOpts: {
|
|
20
20
|
alias: { h: 'help', v: 'version', o: 'out', e: 'engine' },
|
|
21
|
-
boolean: ['help', 'version', 'local', 'verify', 'json', 'final', 'strict', 'tex-only', 'force', 'schema', 'paper'],
|
|
22
|
-
string: ['out', 'engine', 'image', 'root', 'platform', 'source-date-epoch', 'variant', 'template', 'name', 'against'],
|
|
21
|
+
boolean: ['help', 'version', 'local', 'verify', 'json', 'final', 'strict', 'tex-only', 'force', 'schema', 'paper', 'check'],
|
|
22
|
+
string: ['out', 'engine', 'image', 'root', 'platform', 'source-date-epoch', 'variant', 'template', 'name', 'against', 'only', 'locale', 'signatures'],
|
|
23
23
|
default: { strict: undefined, schema: true },
|
|
24
24
|
},
|
|
25
25
|
});
|
package/esm/commands.js
CHANGED
|
@@ -3,7 +3,7 @@ import { join } from 'path';
|
|
|
3
3
|
import chalk from 'yanse';
|
|
4
4
|
import { UsageError } from './args';
|
|
5
5
|
import { doctorCommand } from './doctor';
|
|
6
|
-
import { buildCommand, checkCommand, cleanCommand, diffCommand, fieldsCommand, importCommand, initCommand, newCommand, styCommand, typegenCommand, watchCommand } from './project-commands';
|
|
6
|
+
import { buildCommand, checkCommand, cleanCommand, diffCommand, fieldsCommand, importCommand, initCommand, lockCommand, newCommand, styCommand, typegenCommand, watchCommand } from './project-commands';
|
|
7
7
|
import { renderCommand } from './render';
|
|
8
8
|
import { help } from './usage';
|
|
9
9
|
/** The package version, from package.json next to the built files. */
|
|
@@ -29,6 +29,7 @@ const COMMANDS = {
|
|
|
29
29
|
import: importCommand,
|
|
30
30
|
diff: diffCommand,
|
|
31
31
|
sty: styCommand,
|
|
32
|
+
lock: lockCommand,
|
|
32
33
|
render: renderCommand,
|
|
33
34
|
doctor: doctorCommand,
|
|
34
35
|
};
|
package/esm/config.d.ts
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
|
-
import type { Backend,
|
|
1
|
+
import type { Backend, EngineChoice } from '@latex-kit/render';
|
|
2
2
|
/** latex-kit.config.ts (or .js, .mjs, .cjs, .json) at the project root. */
|
|
3
3
|
export interface LatexKitConfig {
|
|
4
4
|
/** One folder per template: `<templates>/<name>/template.tex` and optional `schema.ts`. Default `templates`. */
|
|
5
5
|
templates?: string;
|
|
6
6
|
/** Shared pieces for \snippet{name}. Default `snippets`. */
|
|
7
7
|
snippets?: string;
|
|
8
|
-
/** The clause library for \\clause{id}: `<clauses>/<id>.tex`. Default `clauses`. */
|
|
8
|
+
/** The clause library for \\clause{id}: `<clauses>/<id>.tex` or `.ts`. Default `clauses`. */
|
|
9
9
|
clauses?: string;
|
|
10
|
+
/** TypeScript components for \\component{name}{path}: `<components>/<name>.ts`. Default `components`. */
|
|
11
|
+
components?: string;
|
|
10
12
|
/** Shared records that references point at: `<data>/<collection>/<id>.json`. Default `data`. */
|
|
11
13
|
data?: string;
|
|
12
14
|
/** Document records: `<documents>/<name>.json|yaml|ts`. Default `documents`. */
|
|
@@ -15,12 +17,19 @@ export interface LatexKitConfig {
|
|
|
15
17
|
out?: string;
|
|
16
18
|
/** Default locale for dates, numbers and money. Default `en-US`. */
|
|
17
19
|
locale?: string;
|
|
18
|
-
|
|
20
|
+
/** Default `auto`: chosen from the packages each document loads. */
|
|
21
|
+
engine?: EngineChoice;
|
|
22
|
+
/**
|
|
23
|
+
* Folder with signature images for signed builds (`<name>.png|pdf|jpg`).
|
|
24
|
+
* Keep it outside the repository; `--signatures` and LATEX_KIT_SIGNATURES
|
|
25
|
+
* override it. A leading ~ is the home folder.
|
|
26
|
+
*/
|
|
27
|
+
signatures?: string;
|
|
19
28
|
backend?: Backend;
|
|
20
29
|
image?: string;
|
|
21
30
|
platform?: string | null;
|
|
22
31
|
}
|
|
23
|
-
export type ResolvedConfig = Required<Pick<LatexKitConfig, 'templates' | 'snippets' | 'clauses' | 'data' | 'documents' | 'out' | 'locale'>> & Pick<LatexKitConfig, 'engine' | 'backend' | 'image' | 'platform'> & {
|
|
32
|
+
export type ResolvedConfig = Required<Pick<LatexKitConfig, 'templates' | 'snippets' | 'clauses' | 'components' | 'data' | 'documents' | 'out' | 'locale'>> & Pick<LatexKitConfig, 'engine' | 'backend' | 'image' | 'platform' | 'signatures'> & {
|
|
24
33
|
file: string | null;
|
|
25
34
|
};
|
|
26
35
|
export declare function defineConfig(config: LatexKitConfig): LatexKitConfig;
|
package/esm/config.js
CHANGED
|
@@ -5,7 +5,7 @@ export function defineConfig(config) {
|
|
|
5
5
|
return config;
|
|
6
6
|
}
|
|
7
7
|
export const CONFIG_FILES = ['latex-kit.config.ts', 'latex-kit.config.mts', 'latex-kit.config.js', 'latex-kit.config.mjs', 'latex-kit.config.cjs', 'latex-kit.config.json'];
|
|
8
|
-
const DEFAULTS = { templates: 'templates', snippets: 'snippets', clauses: 'clauses', data: 'data', documents: 'documents', out: 'out', locale: 'en-US' };
|
|
8
|
+
const DEFAULTS = { templates: 'templates', snippets: 'snippets', clauses: 'clauses', components: 'components', data: 'data', documents: 'documents', out: 'out', locale: 'en-US' };
|
|
9
9
|
export async function loadConfig(root) {
|
|
10
10
|
for (const name of CONFIG_FILES) {
|
|
11
11
|
const file = join(resolve(root), name);
|
package/esm/diff.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { EngineChoice } from '@latex-kit/render';
|
|
2
2
|
import { Variant } from '@latex-kit/template';
|
|
3
3
|
import { Project } from './project';
|
|
4
4
|
export interface DiffOptions {
|
|
5
5
|
variant?: Variant;
|
|
6
|
-
engine?:
|
|
6
|
+
engine?: EngineChoice;
|
|
7
7
|
backend?: 'docker' | 'local';
|
|
8
8
|
image?: string;
|
|
9
9
|
platform?: string | null;
|
package/esm/diff.js
CHANGED
|
@@ -15,14 +15,14 @@ async function redline(project, oldTex, newTex, dir, name, texInputs, options) {
|
|
|
15
15
|
writeFileSync(join(dir, 'latex-kit.sty'), LATEX_KIT_STY);
|
|
16
16
|
const tex = join(dir, `${name}.tex`);
|
|
17
17
|
const rel = (p) => relative(project.root, p);
|
|
18
|
-
await runLatexdiff({ root: project.root, old: rel(oldTex), new: rel(newTex), output: rel(tex), backend: options.backend, image: options.image, platform: options.platform });
|
|
18
|
+
await runLatexdiff({ root: project.root, old: rel(oldTex), new: rel(newTex), output: rel(tex), backend: options.backend, image: project.image(options.image), platform: options.platform });
|
|
19
19
|
const r = await render({
|
|
20
20
|
file: tex,
|
|
21
21
|
root: project.root,
|
|
22
22
|
outDir: rel(dir),
|
|
23
|
-
engine: options.engine ?? project.config.engine ?? '
|
|
23
|
+
engine: options.engine ?? project.config.engine ?? 'auto',
|
|
24
24
|
backend: options.backend ?? project.config.backend,
|
|
25
|
-
image:
|
|
25
|
+
image: project.image(options.image),
|
|
26
26
|
platform: options.platform !== undefined ? options.platform : project.config.platform,
|
|
27
27
|
texInputs: [rel(dir), ...texInputs.map(rel)],
|
|
28
28
|
});
|
package/esm/import.js
CHANGED
|
@@ -87,7 +87,7 @@ export async function importDocument(project, file, options = {}) {
|
|
|
87
87
|
}
|
|
88
88
|
const mediaDir = join(dir, 'media');
|
|
89
89
|
try {
|
|
90
|
-
await runPandoc({ root: project.root, input: source, output: target, extractMedia: mediaDir, backend: options.backend });
|
|
90
|
+
await runPandoc({ root: project.root, input: source, output: target, extractMedia: mediaDir, backend: options.backend, image: project.lock?.pandocImage });
|
|
91
91
|
}
|
|
92
92
|
finally {
|
|
93
93
|
if (staged)
|
package/esm/index.d.ts
CHANGED
|
@@ -2,14 +2,18 @@
|
|
|
2
2
|
* latex-kit: typed, reproducible LaTeX documents.
|
|
3
3
|
*
|
|
4
4
|
* The CLI is `latex-kit`. This module is what project files import:
|
|
5
|
-
* `defineConfig` for latex-kit.config.ts
|
|
6
|
-
*
|
|
5
|
+
* `defineConfig` for latex-kit.config.ts; `z`, the shared types and `ref()`
|
|
6
|
+
* for schema.ts; `defineDocument` for TypeScript records; `defineComponent`,
|
|
7
|
+
* `table`, `tex` and friends for components/; `defineClause` for clauses.
|
|
7
8
|
*/
|
|
8
9
|
export * from './build';
|
|
9
10
|
export * from './check';
|
|
10
11
|
export * from './config';
|
|
11
12
|
export * from './diff';
|
|
12
13
|
export * from './import';
|
|
14
|
+
export * from './lock';
|
|
13
15
|
export * from './project';
|
|
14
16
|
export * from './scaffold';
|
|
17
|
+
export type { Cell, ClauseDefinition, Column, Component, ComponentContext, Exhibit, TableOptions } from '@latex-kit/components';
|
|
18
|
+
export { address, bilingual, BUILTIN_COMPONENTS, date, defineClause, defineComponent, escapeLatex, exhibit, exhibits, get, money, number, percent, raw, RawLatex, signatures, table, tex, } from '@latex-kit/components';
|
|
15
19
|
export * from '@latex-kit/schema';
|
package/esm/index.js
CHANGED
|
@@ -2,14 +2,17 @@
|
|
|
2
2
|
* latex-kit: typed, reproducible LaTeX documents.
|
|
3
3
|
*
|
|
4
4
|
* The CLI is `latex-kit`. This module is what project files import:
|
|
5
|
-
* `defineConfig` for latex-kit.config.ts
|
|
6
|
-
*
|
|
5
|
+
* `defineConfig` for latex-kit.config.ts; `z`, the shared types and `ref()`
|
|
6
|
+
* for schema.ts; `defineDocument` for TypeScript records; `defineComponent`,
|
|
7
|
+
* `table`, `tex` and friends for components/; `defineClause` for clauses.
|
|
7
8
|
*/
|
|
8
9
|
export * from './build';
|
|
9
10
|
export * from './check';
|
|
10
11
|
export * from './config';
|
|
11
12
|
export * from './diff';
|
|
12
13
|
export * from './import';
|
|
14
|
+
export * from './lock';
|
|
13
15
|
export * from './project';
|
|
14
16
|
export * from './scaffold';
|
|
17
|
+
export { address, bilingual, BUILTIN_COMPONENTS, date, defineClause, defineComponent, escapeLatex, exhibit, exhibits, get, money, number, percent, raw, RawLatex, signatures, table, tex, } from '@latex-kit/components';
|
|
15
18
|
export * from '@latex-kit/schema';
|
package/esm/lock.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* latex-kit.lock: the exact images a project builds with. Commit it. A newer
|
|
3
|
+
* latex-kit with a newer default image keeps building this project with the
|
|
4
|
+
* locked one, so PDFs do not change until `latex-kit lock` moves the pins.
|
|
5
|
+
*/
|
|
6
|
+
export declare const LOCK_FILE = "latex-kit.lock";
|
|
7
|
+
export interface Lock {
|
|
8
|
+
lockfileVersion: 1;
|
|
9
|
+
/** The TeX Live image, by digest. */
|
|
10
|
+
image: string;
|
|
11
|
+
/** The pandoc image `latex-kit import` uses, by digest. */
|
|
12
|
+
pandocImage: string;
|
|
13
|
+
}
|
|
14
|
+
export declare class LockError extends Error {
|
|
15
|
+
name: string;
|
|
16
|
+
}
|
|
17
|
+
/** The pins this version of latex-kit would write. */
|
|
18
|
+
export declare function currentPins(): Lock;
|
|
19
|
+
export declare function readLock(root: string): Lock | null;
|
|
20
|
+
/** Write the current pins. Returns the previous lock, if any. */
|
|
21
|
+
export declare function writeLock(root: string): {
|
|
22
|
+
lock: Lock;
|
|
23
|
+
previous: Lock | null;
|
|
24
|
+
};
|