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/README.md
CHANGED
|
@@ -29,6 +29,25 @@ npm install -g latex-kit
|
|
|
29
29
|
|
|
30
30
|
## Usage
|
|
31
31
|
|
|
32
|
+
A project (templates, data, records) is described in the
|
|
33
|
+
[repository README](https://github.com/constructive-io/latex-kit#a-project):
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
latex-kit init my-documents && cd my-documents
|
|
37
|
+
latex-kit check # no TeX needed
|
|
38
|
+
latex-kit build # drafts
|
|
39
|
+
latex-kit build --final --only 'nda-*' # final PDFs for some documents
|
|
40
|
+
latex-kit build --variant signed --signatures ~/signatures
|
|
41
|
+
latex-kit build --locale ja-JP # another language, next to the usual PDFs
|
|
42
|
+
latex-kit lock # pin the images in latex-kit.lock
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
`latex-kit help build` lists every flag. Project files import helpers from
|
|
46
|
+
this package: `defineConfig`, `defineDocument`, `defineComponent`,
|
|
47
|
+
`defineClause`, `table`, `tex`, `ref`, `z` and the shared types.
|
|
48
|
+
|
|
49
|
+
Plain `.tex` files:
|
|
50
|
+
|
|
32
51
|
```bash
|
|
33
52
|
latex-kit doctor # is Docker ready?
|
|
34
53
|
latex-kit render examples/letter/letter.tex # → out/letter.pdf
|
|
@@ -42,7 +61,7 @@ latex-kit render letter.tex --json # machine-readable summary
|
|
|
42
61
|
| Flag | Default | |
|
|
43
62
|
|---|---|---|
|
|
44
63
|
| `-o, --out <dir>` | `out` | Output directory, relative to `--root` |
|
|
45
|
-
| `-e, --engine <name>` | `
|
|
64
|
+
| `-e, --engine <name>` | `auto` | `auto` (from the packages the file loads), `lualatex`, `xelatex`, `pdflatex`, `pdfps` (PSTricks) |
|
|
46
65
|
| `--root <dir>` | current directory | Directory TeX can see |
|
|
47
66
|
| `--image <ref>` | pinned `ghcr.io/constructive-io/latex-kit` (TeX Live 2026) | Docker image |
|
|
48
67
|
| `--platform <os/arch>` | native | Docker `--platform` |
|
package/args.js
CHANGED
|
@@ -9,9 +9,9 @@ class UsageError extends Error {
|
|
|
9
9
|
exports.UsageError = UsageError;
|
|
10
10
|
/** Turn `latex-kit render` flags into render options. Pure, for testing. */
|
|
11
11
|
function renderOptionsFromArgv(argv) {
|
|
12
|
-
const engine = (argv.engine ?? '
|
|
13
|
-
if (!render_1.ENGINES.includes(engine)) {
|
|
14
|
-
throw new UsageError(`--engine must be one of ${render_1.ENGINES.join(', ')}`);
|
|
12
|
+
const engine = (argv.engine ?? 'auto');
|
|
13
|
+
if (engine !== 'auto' && !render_1.ENGINES.includes(engine)) {
|
|
14
|
+
throw new UsageError(`--engine must be auto or one of ${render_1.ENGINES.join(', ')}`);
|
|
15
15
|
}
|
|
16
16
|
let sourceDateEpoch;
|
|
17
17
|
if (argv['source-date-epoch'] !== undefined) {
|
package/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/build.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.BuildError = void 0;
|
|
4
|
+
exports.signerSlug = signerSlug;
|
|
5
|
+
exports.signaturesDir = signaturesDir;
|
|
4
6
|
exports.buildDocument = buildDocument;
|
|
5
7
|
exports.buildBlank = buildBlank;
|
|
6
8
|
const render_1 = require("@latex-kit/render");
|
|
@@ -9,6 +11,7 @@ const template_1 = require("@latex-kit/template");
|
|
|
9
11
|
const child_process_1 = require("child_process");
|
|
10
12
|
const crypto_1 = require("crypto");
|
|
11
13
|
const fs_1 = require("fs");
|
|
14
|
+
const os_1 = require("os");
|
|
12
15
|
const path_1 = require("path");
|
|
13
16
|
class BuildError extends Error {
|
|
14
17
|
diagnostics;
|
|
@@ -52,6 +55,42 @@ function gitShortHead(cwd) {
|
|
|
52
55
|
return 'no-git';
|
|
53
56
|
}
|
|
54
57
|
}
|
|
58
|
+
/** A folder name for a signer: "Avery Example" → "avery-example". */
|
|
59
|
+
function signerSlug(name) {
|
|
60
|
+
return name
|
|
61
|
+
.normalize('NFKD')
|
|
62
|
+
.replace(/[\u0300-\u036f]/g, '')
|
|
63
|
+
.toLowerCase()
|
|
64
|
+
.replace(/[^a-z0-9]+/g, '-')
|
|
65
|
+
.replace(/^-|-$/g, '');
|
|
66
|
+
}
|
|
67
|
+
const IMAGE_EXTENSIONS = ['.pdf', '.png', '.jpg', '.jpeg'];
|
|
68
|
+
/** The signatures folder: option, LATEX_KIT_SIGNATURES, then config. `~` is home. */
|
|
69
|
+
function signaturesDir(project, option) {
|
|
70
|
+
const raw = option ?? process.env.LATEX_KIT_SIGNATURES ?? project.config.signatures ?? null;
|
|
71
|
+
if (!raw)
|
|
72
|
+
return null;
|
|
73
|
+
const expanded = raw === '~' || raw.startsWith('~/') ? (0, path_1.join)((0, os_1.homedir)(), raw.slice(1)) : raw;
|
|
74
|
+
return (0, path_1.resolve)(project.root, expanded);
|
|
75
|
+
}
|
|
76
|
+
/** Every file under a folder, for the cache key. */
|
|
77
|
+
function filesUnder(dir) {
|
|
78
|
+
const out = [];
|
|
79
|
+
const walk = (d) => {
|
|
80
|
+
if (!(0, fs_1.existsSync)(d))
|
|
81
|
+
return;
|
|
82
|
+
for (const name of (0, fs_1.readdirSync)(d).sort()) {
|
|
83
|
+
const p = (0, path_1.join)(d, name);
|
|
84
|
+
if ((0, fs_1.statSync)(p).isDirectory())
|
|
85
|
+
walk(p);
|
|
86
|
+
else
|
|
87
|
+
out.push(p);
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
walk(dir);
|
|
91
|
+
return out;
|
|
92
|
+
}
|
|
93
|
+
const posixRel = (root, p) => (0, path_1.relative)(root, p).split(path_1.sep).join(path_1.posix.sep);
|
|
55
94
|
async function run(job) {
|
|
56
95
|
const { project, template, variant, options } = job;
|
|
57
96
|
const diagnostics = job.diagnostics;
|
|
@@ -72,103 +111,194 @@ async function run(job) {
|
|
|
72
111
|
docId = (0, schema_1.mintDocId)({ prefix: template.meta.docIdPrefix ?? template.name, epoch: Math.floor(Date.now() / 1000) });
|
|
73
112
|
minted = true;
|
|
74
113
|
}
|
|
75
|
-
const evaluated = (0, template_1.evaluateTemplate)(template.compiled, {
|
|
76
|
-
data: job.data,
|
|
77
|
-
variant,
|
|
78
|
-
locale: job.locale,
|
|
79
|
-
docId,
|
|
80
|
-
template: template.name,
|
|
81
|
-
allowMissing: variant === 'draft' ? !strict : undefined,
|
|
82
|
-
loadSnippet,
|
|
83
|
-
loadClause: project.loadClause,
|
|
84
|
-
});
|
|
85
|
-
diagnostics.push(...evaluated.diagnostics);
|
|
86
|
-
if ((0, template_1.hasErrors)(diagnostics))
|
|
87
|
-
throw new BuildError(`${job.name}: cannot build ${variant}`, diagnostics);
|
|
88
|
-
// snippets count as inputs for the build date too
|
|
89
|
-
const clauseFiles = evaluated.clauses.map((c) => (0, path_1.resolve)(c.file));
|
|
90
|
-
const extra = [...snippets, ...clauseFiles];
|
|
91
|
-
const finalEpoch = extra.length ? buildEpoch([...files, ...extra]) : { epoch, reproducible };
|
|
92
|
-
// 2. write the .tex next to latex-kit.sty
|
|
93
114
|
const outDir = options.outDir ?? (0, path_1.join)(project.outDir, job.name);
|
|
94
|
-
(0, fs_1.mkdirSync)(outDir, { recursive: true });
|
|
95
115
|
const job_ = variant === 'final' ? job.name : `${job.name}-${variant}`;
|
|
96
|
-
|
|
97
|
-
const
|
|
98
|
-
const
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
sourceDateEpoch: finalEpoch.epoch,
|
|
126
|
-
texInputs: [(0, path_1.relative)(project.root, outDir) || '.', (0, path_1.relative)(project.root, template.dir) || '.'],
|
|
127
|
-
onOutput: options.onOutput,
|
|
128
|
-
};
|
|
129
|
-
let rendered;
|
|
130
|
-
try {
|
|
131
|
-
rendered = await (0, render_1.render)({ ...renderOpts, outDir: (0, path_1.relative)(project.root, outDir) });
|
|
132
|
-
}
|
|
133
|
-
catch (e) {
|
|
134
|
-
if (e instanceof render_1.RenderError) {
|
|
135
|
-
throw new BuildError(`${job.name}: ${e.message}`, [
|
|
136
|
-
...diagnostics,
|
|
137
|
-
...e.diagnostics.map((d) => (0, template_1.diag)(d.severity, d.message, d.file ? { file: project.rel((0, path_1.join)(outDir, (0, path_1.basename)(d.file))), line: d.line ?? 0, column: 0 } : undefined)),
|
|
138
|
-
]);
|
|
116
|
+
// signed builds: copy signature images next to the .tex for this build only
|
|
117
|
+
const stageDir = (0, path_1.join)(outDir, 'lk-signatures');
|
|
118
|
+
const staged = [];
|
|
119
|
+
let signatureImage;
|
|
120
|
+
const sigDir = variant === 'signed' ? signaturesDir(project, options.signatures) : null;
|
|
121
|
+
if (sigDir) {
|
|
122
|
+
const at = { file: project.rel(sigDir), line: 1, column: 1 };
|
|
123
|
+
if (!(0, fs_1.existsSync)(sigDir)) {
|
|
124
|
+
diagnostics.push((0, template_1.diag)('error', `signatures folder not found: ${project.rel(sigDir)}`, at));
|
|
125
|
+
}
|
|
126
|
+
else {
|
|
127
|
+
const inside = (0, path_1.relative)(project.root, sigDir);
|
|
128
|
+
if (!inside.startsWith('..') && !(0, path_1.isAbsolute)(inside)) {
|
|
129
|
+
diagnostics.push((0, template_1.diag)('warning', 'the signatures folder is inside the project; keep signature images out of the repository', at));
|
|
130
|
+
}
|
|
131
|
+
signatureImage = (signer) => {
|
|
132
|
+
const names = signer.image ? [signer.image] : IMAGE_EXTENSIONS.map((e) => `${signerSlug(signer.name)}${e}`);
|
|
133
|
+
const found = names.find((n) => (0, path_1.basename)(n) === n && (0, fs_1.existsSync)((0, path_1.join)(sigDir, n)));
|
|
134
|
+
if (!found) {
|
|
135
|
+
diagnostics.push((0, template_1.diag)('warning', `no signature image for ${signer.name} in ${project.rel(sigDir)} (looked for ${names.join(', ')}); printed /s/ ${signer.name}`, at));
|
|
136
|
+
return null;
|
|
137
|
+
}
|
|
138
|
+
(0, fs_1.mkdirSync)(stageDir, { recursive: true });
|
|
139
|
+
const target = (0, path_1.join)(stageDir, found);
|
|
140
|
+
(0, fs_1.copyFileSync)((0, path_1.join)(sigDir, found), target);
|
|
141
|
+
if (!staged.includes(target))
|
|
142
|
+
staged.push(target);
|
|
143
|
+
return posixRel(project.root, target);
|
|
144
|
+
};
|
|
139
145
|
}
|
|
140
|
-
throw e;
|
|
141
146
|
}
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
if (options.verify) {
|
|
145
|
-
// same output path: some tools (Ghostscript) hash the path into the PDF ID
|
|
146
|
-
const again = await (0, render_1.render)({ ...renderOpts, outDir: (0, path_1.relative)(project.root, outDir) });
|
|
147
|
-
result.verified = again.sha256 === rendered.sha256;
|
|
147
|
+
try {
|
|
148
|
+
return await fill();
|
|
148
149
|
}
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
150
|
+
finally {
|
|
151
|
+
(0, fs_1.rmSync)(stageDir, { recursive: true, force: true });
|
|
152
|
+
}
|
|
153
|
+
async function fill() {
|
|
154
|
+
const evaluated = (0, template_1.evaluateTemplate)(template.compiled, {
|
|
155
|
+
data: job.data,
|
|
156
|
+
variant,
|
|
157
|
+
locale: job.locale,
|
|
158
|
+
docId,
|
|
159
|
+
template: template.name,
|
|
160
|
+
allowMissing: variant === 'draft' ? !strict : undefined,
|
|
161
|
+
loadSnippet,
|
|
162
|
+
loadClause: project.loadClause,
|
|
163
|
+
components: project.components,
|
|
164
|
+
signatureImage,
|
|
165
|
+
signed: job.signed,
|
|
166
|
+
});
|
|
167
|
+
diagnostics.push(...evaluated.diagnostics);
|
|
168
|
+
if ((0, template_1.hasErrors)(diagnostics))
|
|
169
|
+
throw new BuildError(`${job.name}: cannot build ${variant}`, diagnostics);
|
|
170
|
+
// snippets, clauses and components count as inputs for the build date too
|
|
171
|
+
const clauseFiles = evaluated.clauses.map((c) => (0, path_1.resolve)(c.file));
|
|
172
|
+
const componentFiles = evaluated.components.map((n) => project.componentFiles[n]).filter((f) => !!f);
|
|
173
|
+
const extra = [...snippets, ...clauseFiles, ...componentFiles];
|
|
174
|
+
const finalEpoch = extra.length ? buildEpoch([...files, ...extra]) : { epoch, reproducible };
|
|
175
|
+
// 2. write the .tex next to latex-kit.sty
|
|
176
|
+
(0, fs_1.mkdirSync)(outDir, { recursive: true });
|
|
177
|
+
const texPath = (0, path_1.join)(outDir, `${job_}.tex`);
|
|
178
|
+
const stamp = `${new Date(finalEpoch.epoch * 1000).toISOString().slice(0, 10)} ${gitShortHead(project.root)}`;
|
|
179
|
+
const tex = (0, template_1.wrapDocument)(evaluated.tex, { variant, stamp });
|
|
180
|
+
(0, fs_1.writeFileSync)(texPath, tex);
|
|
181
|
+
(0, fs_1.writeFileSync)((0, path_1.join)(outDir, 'latex-kit.sty'), template_1.LATEX_KIT_STY);
|
|
182
|
+
const result = {
|
|
183
|
+
name: job.name,
|
|
184
|
+
template: template.name,
|
|
185
|
+
variant,
|
|
186
|
+
tex: texPath,
|
|
187
|
+
pdf: null,
|
|
188
|
+
sha256: null,
|
|
189
|
+
docId: docId ?? null,
|
|
190
|
+
minted: false,
|
|
191
|
+
sourceDateEpoch: finalEpoch.epoch,
|
|
192
|
+
reproducible: finalEpoch.reproducible,
|
|
193
|
+
engine: null,
|
|
194
|
+
engineReason: null,
|
|
195
|
+
cached: false,
|
|
196
|
+
diagnostics,
|
|
197
|
+
};
|
|
198
|
+
if (options.texOnly)
|
|
199
|
+
return result;
|
|
200
|
+
// 3. render, unless the last build had exactly these inputs
|
|
201
|
+
const choice = options.engine ?? template.meta.engine ?? project.config.engine ?? 'auto';
|
|
202
|
+
let engine;
|
|
203
|
+
let engineReason;
|
|
204
|
+
if (choice === 'auto') {
|
|
205
|
+
const d = (0, render_1.detectEngine)(tex);
|
|
206
|
+
engine = d.engine;
|
|
207
|
+
engineReason = `auto: ${d.reason}`;
|
|
208
|
+
for (const w of d.warnings)
|
|
209
|
+
diagnostics.push((0, template_1.diag)('warning', w, { file: template.compiled.file, line: 1, column: 1 }));
|
|
210
|
+
}
|
|
211
|
+
else {
|
|
212
|
+
engine = choice;
|
|
213
|
+
engineReason = 'chosen';
|
|
214
|
+
}
|
|
215
|
+
result.engine = engine;
|
|
216
|
+
result.engineReason = engineReason;
|
|
217
|
+
const backend = options.backend ?? project.config.backend ?? 'docker';
|
|
218
|
+
const image = project.image(options.image);
|
|
219
|
+
const platform = options.platform !== undefined ? options.platform : project.config.platform;
|
|
220
|
+
const renderOpts = {
|
|
221
|
+
file: texPath,
|
|
222
|
+
root: project.root,
|
|
223
|
+
engine,
|
|
224
|
+
backend,
|
|
225
|
+
image,
|
|
226
|
+
platform,
|
|
227
|
+
sourceDateEpoch: finalEpoch.epoch,
|
|
228
|
+
texInputs: [(0, path_1.relative)(project.root, outDir) || '.', (0, path_1.relative)(project.root, template.dir) || '.'],
|
|
229
|
+
onOutput: options.onOutput,
|
|
230
|
+
};
|
|
231
|
+
// content-addressed: the PDF depends on the .tex, the .sty, the template
|
|
232
|
+
// folder (images, \\input files), signature images, the engine, the image
|
|
233
|
+
// and the build date. Same key → same bytes, so the last PDF is reused.
|
|
234
|
+
const keyInputs = [...filesUnder(template.dir), ...staged].map((f) => [posixRel(project.root, f), sha256((0, fs_1.readFileSync)(f))]);
|
|
235
|
+
const cacheKey = sha256(JSON.stringify({ v: 1, tex: sha256(tex), sty: sha256(template_1.LATEX_KIT_STY), engine, backend, image: backend === 'docker' ? (image ?? 'default') : null, platform: platform ?? null, epoch: finalEpoch.epoch, inputs: keyInputs }));
|
|
236
|
+
const manifestPath = (0, path_1.join)(outDir, `${job_}.manifest.json`);
|
|
237
|
+
const pdfPath = (0, path_1.join)(outDir, `${job_}.pdf`);
|
|
238
|
+
if (!options.force && !options.verify && (0, fs_1.existsSync)(manifestPath) && (0, fs_1.existsSync)(pdfPath)) {
|
|
239
|
+
try {
|
|
240
|
+
const m = JSON.parse((0, fs_1.readFileSync)(manifestPath, 'utf8'));
|
|
241
|
+
const pdfSha = sha256((0, fs_1.readFileSync)(pdfPath));
|
|
242
|
+
if (m.cacheKey === cacheKey && m.pdf?.sha256 === pdfSha) {
|
|
243
|
+
result.pdf = pdfPath;
|
|
244
|
+
result.sha256 = pdfSha;
|
|
245
|
+
result.cached = true;
|
|
246
|
+
return result;
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
catch {
|
|
250
|
+
// an unreadable manifest just means a rebuild
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
let rendered;
|
|
254
|
+
try {
|
|
255
|
+
rendered = await (0, render_1.render)({ ...renderOpts, outDir: (0, path_1.relative)(project.root, outDir) });
|
|
256
|
+
}
|
|
257
|
+
catch (e) {
|
|
258
|
+
if (e instanceof render_1.RenderError) {
|
|
259
|
+
throw new BuildError(`${job.name}: ${e.message}`, [
|
|
260
|
+
...diagnostics,
|
|
261
|
+
...e.diagnostics.map((d) => (0, template_1.diag)(d.severity, d.message, d.file ? { file: project.rel((0, path_1.join)(outDir, (0, path_1.basename)(d.file))), line: d.line ?? 0, column: 0 } : undefined)),
|
|
262
|
+
]);
|
|
263
|
+
}
|
|
264
|
+
throw e;
|
|
265
|
+
}
|
|
266
|
+
result.pdf = rendered.pdf;
|
|
267
|
+
result.sha256 = rendered.sha256;
|
|
268
|
+
if (options.verify) {
|
|
269
|
+
// same output path: some tools (Ghostscript) hash the path into the PDF ID
|
|
270
|
+
const again = await (0, render_1.render)({ ...renderOpts, outDir: (0, path_1.relative)(project.root, outDir) });
|
|
271
|
+
result.verified = again.sha256 === rendered.sha256;
|
|
272
|
+
}
|
|
273
|
+
// 4. keep the ID: only after a successful final build
|
|
274
|
+
if (minted && job.mintInto) {
|
|
275
|
+
(0, schema_1.writeDocId)(job.mintInto, docId);
|
|
276
|
+
result.minted = true;
|
|
277
|
+
}
|
|
278
|
+
// 5. manifest: what went in, what came out
|
|
279
|
+
const inputFiles = [...new Set([...files, ...extra])].filter(fs_1.existsSync).sort();
|
|
280
|
+
(0, fs_1.writeFileSync)(manifestPath, JSON.stringify({
|
|
281
|
+
document: job.name,
|
|
282
|
+
template: template.name,
|
|
283
|
+
variant,
|
|
284
|
+
docId: docId ?? null,
|
|
285
|
+
locale: job.locale,
|
|
286
|
+
engine,
|
|
287
|
+
engineReason,
|
|
288
|
+
image: rendered.image,
|
|
289
|
+
cacheKey,
|
|
290
|
+
sourceDateEpoch: finalEpoch.epoch,
|
|
291
|
+
inputs: inputFiles.map((f) => ({ file: (0, path_1.relative)(project.root, f), sha256: sha256((0, fs_1.readFileSync)(f)) })),
|
|
292
|
+
tex: { file: (0, path_1.relative)(project.root, texPath), sha256: sha256(tex) },
|
|
293
|
+
pdf: { file: (0, path_1.relative)(project.root, rendered.pdf), sha256: rendered.sha256 },
|
|
294
|
+
clauses: evaluated.clauses.map((c) => ({ id: c.id, version: c.version ?? null, file: (0, path_1.relative)(project.root, (0, path_1.resolve)(c.file)) })),
|
|
295
|
+
components: evaluated.components,
|
|
296
|
+
fields: evaluated.fields.map((f) => ({ path: f.path, missing: f.missing })),
|
|
297
|
+
// which signers had an image; never the images or where they live
|
|
298
|
+
signatureImages: staged.map((f) => (0, path_1.basename)(f)),
|
|
299
|
+
}, null, 2) + '\n');
|
|
300
|
+
return result;
|
|
153
301
|
}
|
|
154
|
-
// 5. manifest: what went in, what came out
|
|
155
|
-
const inputFiles = [...new Set([...files, ...extra])].filter(fs_1.existsSync).sort();
|
|
156
|
-
(0, fs_1.writeFileSync)((0, path_1.join)(outDir, `${job_}.manifest.json`), JSON.stringify({
|
|
157
|
-
document: job.name,
|
|
158
|
-
template: template.name,
|
|
159
|
-
variant,
|
|
160
|
-
docId: docId ?? null,
|
|
161
|
-
locale: job.locale,
|
|
162
|
-
engine,
|
|
163
|
-
image: rendered.image,
|
|
164
|
-
sourceDateEpoch: finalEpoch.epoch,
|
|
165
|
-
inputs: inputFiles.map((f) => ({ file: (0, path_1.relative)(project.root, f), sha256: sha256((0, fs_1.readFileSync)(f)) })),
|
|
166
|
-
tex: { file: (0, path_1.relative)(project.root, texPath), sha256: sha256(tex) },
|
|
167
|
-
pdf: { file: (0, path_1.relative)(project.root, rendered.pdf), sha256: rendered.sha256 },
|
|
168
|
-
clauses: evaluated.clauses.map((c) => ({ id: c.id, version: c.version ?? null, file: (0, path_1.relative)(project.root, (0, path_1.resolve)(c.file)) })),
|
|
169
|
-
fields: evaluated.fields.map((f) => ({ path: f.path, missing: f.missing })),
|
|
170
|
-
}, null, 2) + '\n');
|
|
171
|
-
return result;
|
|
172
302
|
}
|
|
173
303
|
/** Build one document record. */
|
|
174
304
|
async function buildDocument(project, file, options = {}) {
|
|
@@ -195,14 +325,18 @@ async function buildDocument(project, file, options = {}) {
|
|
|
195
325
|
diagnostics.push((0, template_1.diag)('error', `a ${variant} build needs a document ID, and latex-kit does not edit code. Add: docId: '${suggestion}'`, at));
|
|
196
326
|
throw new BuildError(`${doc.record.template}: no document ID`, diagnostics);
|
|
197
327
|
}
|
|
328
|
+
const natural = doc.record.locale ?? template.meta.locale ?? project.config.locale;
|
|
329
|
+
const name = project.documentName(file);
|
|
198
330
|
return run({
|
|
199
331
|
project,
|
|
200
332
|
template,
|
|
201
|
-
|
|
333
|
+
// another language is another PDF, next to the usual one
|
|
334
|
+
name: options.locale && options.locale !== natural ? `${name}-${options.locale}` : name,
|
|
202
335
|
variant,
|
|
203
336
|
data,
|
|
204
|
-
locale:
|
|
337
|
+
locale: options.locale ?? natural,
|
|
205
338
|
docId: doc.record.docId ?? options.docIdFallback,
|
|
339
|
+
signed: doc.record.signed,
|
|
206
340
|
inputs: [file, ...doc.refFiles],
|
|
207
341
|
diagnostics,
|
|
208
342
|
options,
|
|
@@ -212,13 +346,14 @@ async function buildDocument(project, file, options = {}) {
|
|
|
212
346
|
/** Build a template as a blank form, without a record. */
|
|
213
347
|
async function buildBlank(project, templateName, options = {}) {
|
|
214
348
|
const template = await project.template(templateName);
|
|
349
|
+
const natural = template.meta.locale ?? project.config.locale;
|
|
215
350
|
return run({
|
|
216
351
|
project,
|
|
217
352
|
template,
|
|
218
|
-
name: `${templateName}
|
|
353
|
+
name: options.locale && options.locale !== natural ? `${templateName}-${options.locale}` : templateName,
|
|
219
354
|
variant: 'blank',
|
|
220
355
|
data: {},
|
|
221
|
-
locale:
|
|
356
|
+
locale: options.locale ?? natural,
|
|
222
357
|
inputs: [],
|
|
223
358
|
diagnostics: [],
|
|
224
359
|
options: { ...options, variant: 'blank' },
|
package/check.js
CHANGED
|
@@ -9,15 +9,16 @@ const template_1 = require("@latex-kit/template");
|
|
|
9
9
|
* against each schema, every document's data, references, and duplicate IDs.
|
|
10
10
|
*/
|
|
11
11
|
async function checkProject(project) {
|
|
12
|
-
const diagnostics = [];
|
|
12
|
+
const diagnostics = [...project.codeDiagnostics];
|
|
13
13
|
const templates = project.templateNames();
|
|
14
|
+
const components = project.componentNames();
|
|
14
15
|
for (const name of templates) {
|
|
15
16
|
try {
|
|
16
17
|
const t = await project.template(name);
|
|
17
18
|
if (!t.jsonSchema) {
|
|
18
19
|
diagnostics.push((0, template_1.diag)('warning', `template ${name} has no schema.ts, so field names are not checked`, { file: t.compiled.file, line: 1, column: 1 }));
|
|
19
20
|
}
|
|
20
|
-
diagnostics.push(...(0, template_1.analyzeTemplate)(t.compiled, { schema: t.jsonSchema ?? undefined, loadSnippet: project.loadSnippet, loadClause: project.loadClause }).diagnostics);
|
|
21
|
+
diagnostics.push(...(0, template_1.analyzeTemplate)(t.compiled, { schema: t.jsonSchema ?? undefined, loadSnippet: project.loadSnippet, loadClause: project.loadClause, components }).diagnostics);
|
|
21
22
|
}
|
|
22
23
|
catch (e) {
|
|
23
24
|
diagnostics.push((0, template_1.diag)('error', e.message, { file: project.rel(project.path(project.config.templates, name)), line: 1, column: 1 }));
|
|
@@ -53,6 +54,6 @@ async function checkProject(project) {
|
|
|
53
54
|
/** Every field a template reads, including through snippets. */
|
|
54
55
|
async function templateFields(project, name) {
|
|
55
56
|
const t = await project.template(name);
|
|
56
|
-
const r = (0, template_1.analyzeTemplate)(t.compiled, { schema: t.jsonSchema ?? undefined, loadSnippet: project.loadSnippet, loadClause: project.loadClause });
|
|
57
|
+
const r = (0, template_1.analyzeTemplate)(t.compiled, { schema: t.jsonSchema ?? undefined, loadSnippet: project.loadSnippet, loadClause: project.loadClause, components: project.componentNames() });
|
|
57
58
|
return { fields: r.fields, diagnostics: r.diagnostics };
|
|
58
59
|
}
|
package/cli.js
CHANGED
|
@@ -20,8 +20,8 @@ const app = new inquirerer_1.CLI(commands_1.commands, {
|
|
|
20
20
|
version: (0, commands_1.version)(),
|
|
21
21
|
minimistOpts: {
|
|
22
22
|
alias: { h: 'help', v: 'version', o: 'out', e: 'engine' },
|
|
23
|
-
boolean: ['help', 'version', 'local', 'verify', 'json', 'final', 'strict', 'tex-only', 'force', 'schema', 'paper'],
|
|
24
|
-
string: ['out', 'engine', 'image', 'root', 'platform', 'source-date-epoch', 'variant', 'template', 'name', 'against'],
|
|
23
|
+
boolean: ['help', 'version', 'local', 'verify', 'json', 'final', 'strict', 'tex-only', 'force', 'schema', 'paper', 'check'],
|
|
24
|
+
string: ['out', 'engine', 'image', 'root', 'platform', 'source-date-epoch', 'variant', 'template', 'name', 'against', 'only', 'locale', 'signatures'],
|
|
25
25
|
default: { strict: undefined, schema: true },
|
|
26
26
|
},
|
|
27
27
|
});
|
package/commands.js
CHANGED
package/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/config.js
CHANGED
|
@@ -10,7 +10,7 @@ function defineConfig(config) {
|
|
|
10
10
|
return config;
|
|
11
11
|
}
|
|
12
12
|
exports.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'];
|
|
13
|
-
const DEFAULTS = { templates: 'templates', snippets: 'snippets', clauses: 'clauses', data: 'data', documents: 'documents', out: 'out', locale: 'en-US' };
|
|
13
|
+
const DEFAULTS = { templates: 'templates', snippets: 'snippets', clauses: 'clauses', components: 'components', data: 'data', documents: 'documents', out: 'out', locale: 'en-US' };
|
|
14
14
|
async function loadConfig(root) {
|
|
15
15
|
for (const name of exports.CONFIG_FILES) {
|
|
16
16
|
const file = (0, path_1.join)((0, path_1.resolve)(root), name);
|
package/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/diff.js
CHANGED
|
@@ -19,14 +19,14 @@ async function redline(project, oldTex, newTex, dir, name, texInputs, options) {
|
|
|
19
19
|
(0, fs_1.writeFileSync)((0, path_1.join)(dir, 'latex-kit.sty'), template_1.LATEX_KIT_STY);
|
|
20
20
|
const tex = (0, path_1.join)(dir, `${name}.tex`);
|
|
21
21
|
const rel = (p) => (0, path_1.relative)(project.root, p);
|
|
22
|
-
await (0, render_1.runLatexdiff)({ root: project.root, old: rel(oldTex), new: rel(newTex), output: rel(tex), backend: options.backend, image: options.image, platform: options.platform });
|
|
22
|
+
await (0, render_1.runLatexdiff)({ root: project.root, old: rel(oldTex), new: rel(newTex), output: rel(tex), backend: options.backend, image: project.image(options.image), platform: options.platform });
|
|
23
23
|
const r = await (0, render_1.render)({
|
|
24
24
|
file: tex,
|
|
25
25
|
root: project.root,
|
|
26
26
|
outDir: rel(dir),
|
|
27
|
-
engine: options.engine ?? project.config.engine ?? '
|
|
27
|
+
engine: options.engine ?? project.config.engine ?? 'auto',
|
|
28
28
|
backend: options.backend ?? project.config.backend,
|
|
29
|
-
image:
|
|
29
|
+
image: project.image(options.image),
|
|
30
30
|
platform: options.platform !== undefined ? options.platform : project.config.platform,
|
|
31
31
|
texInputs: [rel(dir), ...texInputs.map(rel)],
|
|
32
32
|
});
|
package/esm/args.js
CHANGED
|
@@ -4,9 +4,9 @@ export class UsageError extends Error {
|
|
|
4
4
|
}
|
|
5
5
|
/** Turn `latex-kit render` flags into render options. Pure, for testing. */
|
|
6
6
|
export function renderOptionsFromArgv(argv) {
|
|
7
|
-
const engine = (argv.engine ?? '
|
|
8
|
-
if (!ENGINES.includes(engine)) {
|
|
9
|
-
throw new UsageError(`--engine must be one of ${ENGINES.join(', ')}`);
|
|
7
|
+
const engine = (argv.engine ?? 'auto');
|
|
8
|
+
if (engine !== 'auto' && !ENGINES.includes(engine)) {
|
|
9
|
+
throw new UsageError(`--engine must be auto or one of ${ENGINES.join(', ')}`);
|
|
10
10
|
}
|
|
11
11
|
let sourceDateEpoch;
|
|
12
12
|
if (argv['source-date-epoch'] !== undefined) {
|