latex-kit 0.3.0 → 0.4.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 +3 -3
- package/boilerplates.d.ts +8 -0
- package/boilerplates.js +225 -0
- package/build.d.ts +6 -0
- package/build.js +12 -8
- package/check.js +2 -2
- package/cli.js +14 -3
- package/commands.js +6 -0
- package/config.d.ts +3 -1
- package/config.js +1 -1
- package/diff.d.ts +25 -0
- package/diff.js +112 -0
- package/esm/boilerplates.d.ts +8 -0
- package/esm/boilerplates.js +220 -0
- package/esm/build.d.ts +6 -0
- package/esm/build.js +13 -9
- package/esm/check.js +2 -2
- package/esm/cli.js +14 -3
- package/esm/commands.js +7 -1
- package/esm/config.d.ts +3 -1
- package/esm/config.js +1 -1
- package/esm/diff.d.ts +25 -0
- package/esm/diff.js +108 -0
- package/esm/import.d.ts +24 -0
- package/esm/import.js +113 -0
- package/esm/index.d.ts +3 -0
- package/esm/index.js +3 -0
- package/esm/project-commands.d.ts +10 -0
- package/esm/project-commands.js +163 -2
- package/esm/project.d.ts +4 -1
- package/esm/project.js +15 -1
- package/esm/render.js +3 -5
- package/esm/scaffold.d.ts +27 -0
- package/esm/scaffold.js +108 -0
- package/esm/usage.js +20 -5
- package/import.d.ts +24 -0
- package/import.js +118 -0
- package/index.d.ts +3 -0
- package/index.js +3 -0
- package/package.json +5 -5
- package/project-commands.d.ts +10 -0
- package/project-commands.js +167 -0
- package/project.d.ts +4 -1
- package/project.js +14 -0
- package/render.js +2 -4
- package/scaffold.d.ts +27 -0
- package/scaffold.js +115 -0
- package/usage.js +20 -5
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Files written by `latex-kit init` and `latex-kit new`. Every name, address
|
|
3
|
+
* and amount is a placeholder.
|
|
4
|
+
*/
|
|
5
|
+
export function projectFiles(version) {
|
|
6
|
+
return {
|
|
7
|
+
'package.json': `${JSON.stringify({
|
|
8
|
+
name: 'documents',
|
|
9
|
+
private: true,
|
|
10
|
+
scripts: {
|
|
11
|
+
check: 'latex-kit check',
|
|
12
|
+
draft: 'latex-kit build',
|
|
13
|
+
final: 'latex-kit build --final',
|
|
14
|
+
blank: 'latex-kit build --variant blank',
|
|
15
|
+
typegen: 'latex-kit typegen',
|
|
16
|
+
},
|
|
17
|
+
devDependencies: { 'latex-kit': `^${version}` },
|
|
18
|
+
}, null, 2)}\n`,
|
|
19
|
+
'.gitignore': 'node_modules/\nout/\n',
|
|
20
|
+
'latex-kit.config.ts': `import { defineConfig } from 'latex-kit';
|
|
21
|
+
|
|
22
|
+
export default defineConfig({
|
|
23
|
+
locale: 'en-US',
|
|
24
|
+
});
|
|
25
|
+
`,
|
|
26
|
+
'README.md': `# Documents
|
|
27
|
+
|
|
28
|
+
Built with [latex-kit](https://github.com/constructive-io/latex-kit).
|
|
29
|
+
|
|
30
|
+
| Folder | |
|
|
31
|
+
|---|---|
|
|
32
|
+
| \`templates/<name>/template.tex\` | A real \`.tex\` file with \`\\field{…}\`, \`\\when\`, \`\\each\` |
|
|
33
|
+
| \`templates/<name>/schema.ts\` | The data the template needs (zod) |
|
|
34
|
+
| \`snippets/\` | Shared pieces: \`\\snippet{letterhead}\` |
|
|
35
|
+
| \`clauses/\` | Versioned clauses: \`\\clause{id}\`, \`\\clauseref{id}\` |
|
|
36
|
+
| \`data/\` | Shared records, used with \`{ "$ref": "orgs/my-company" }\` |
|
|
37
|
+
| \`documents/\` | One record per document |
|
|
38
|
+
| \`out/\` | Generated .tex, PDFs and manifests (not committed) |
|
|
39
|
+
|
|
40
|
+
\`\`\`bash
|
|
41
|
+
npx latex-kit check # no TeX needed
|
|
42
|
+
npx latex-kit build # drafts
|
|
43
|
+
npx latex-kit build --final # final PDFs; mints document IDs once
|
|
44
|
+
npx latex-kit new document thank-you --template letter
|
|
45
|
+
\`\`\`
|
|
46
|
+
`,
|
|
47
|
+
'templates/letter/template.tex': `\\documentclass[11pt]{article}
|
|
48
|
+
\\usepackage{fontspec}
|
|
49
|
+
\\usepackage[margin=25mm]{geometry}
|
|
50
|
+
\\usepackage{latex-kit}
|
|
51
|
+
\\pagestyle{empty}
|
|
52
|
+
\\setlength{\\parindent}{0pt}
|
|
53
|
+
|
|
54
|
+
\\begin{document}
|
|
55
|
+
\\snippet{letterhead}
|
|
56
|
+
|
|
57
|
+
\\vspace{2em}
|
|
58
|
+
\\field{date|date:long}
|
|
59
|
+
|
|
60
|
+
\\vspace{1em}
|
|
61
|
+
\\field{to.name}\\\\
|
|
62
|
+
\\when{to.address}{\\field{to.address.line1}\\\\
|
|
63
|
+
\\field{to.address.city}}
|
|
64
|
+
|
|
65
|
+
\\vspace{1em}
|
|
66
|
+
Dear \\field{to.name},
|
|
67
|
+
|
|
68
|
+
\\field{body|paragraphs}
|
|
69
|
+
|
|
70
|
+
\\vspace{2em}
|
|
71
|
+
Sincerely,\\\\[3em]
|
|
72
|
+
\\field{from.signer.name}\\\\
|
|
73
|
+
\\field{from.signer.title|default:""}
|
|
74
|
+
|
|
75
|
+
\\vfill
|
|
76
|
+
{\\small \\docid}
|
|
77
|
+
\\end{document}
|
|
78
|
+
`,
|
|
79
|
+
'templates/letter/schema.ts': `import { ISODate, Organization, Party, z } from 'latex-kit';
|
|
80
|
+
|
|
81
|
+
export const meta = { docIdPrefix: 'LTR' };
|
|
82
|
+
|
|
83
|
+
export default z.object({
|
|
84
|
+
date: ISODate,
|
|
85
|
+
from: Organization,
|
|
86
|
+
to: Party,
|
|
87
|
+
body: z.string(),
|
|
88
|
+
});
|
|
89
|
+
`,
|
|
90
|
+
'snippets/letterhead.tex': `% Shared letterhead: reads \`from\` from the document's data.
|
|
91
|
+
\\begin{flushright}
|
|
92
|
+
{\\large\\bfseries \\field{from.name}}\\\\
|
|
93
|
+
\\field{from.address.line1}\\\\
|
|
94
|
+
\\field{from.address.city}
|
|
95
|
+
\\end{flushright}
|
|
96
|
+
`,
|
|
97
|
+
'clauses/.gitkeep': '',
|
|
98
|
+
'data/orgs/my-company.yaml': `# Your organization. Documents refer to it as { $ref: orgs/my-company }.
|
|
99
|
+
name: My Company, Inc.
|
|
100
|
+
address:
|
|
101
|
+
line1: 1 Example Street
|
|
102
|
+
city: Springfield
|
|
103
|
+
signer:
|
|
104
|
+
$ref: people/me
|
|
105
|
+
`,
|
|
106
|
+
'data/people/me.yaml': `name: Your Name
|
|
107
|
+
title: Founder
|
|
108
|
+
`,
|
|
109
|
+
'documents/welcome-letter.yaml': `template: letter
|
|
110
|
+
data:
|
|
111
|
+
date: "2026-01-15"
|
|
112
|
+
from:
|
|
113
|
+
$ref: orgs/my-company
|
|
114
|
+
to:
|
|
115
|
+
name: Jordan Example
|
|
116
|
+
address:
|
|
117
|
+
line1: 2 Sample Road
|
|
118
|
+
city: Anytown
|
|
119
|
+
body: |
|
|
120
|
+
Welcome aboard! This letter was built by latex-kit from
|
|
121
|
+
documents/welcome-letter.yaml and templates/letter/template.tex.
|
|
122
|
+
|
|
123
|
+
Run latex-kit build --final to mint its document ID.
|
|
124
|
+
`,
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
export function paperFiles() {
|
|
128
|
+
return {
|
|
129
|
+
'.gitignore': 'out/\n',
|
|
130
|
+
'paper/main.tex': `\\documentclass[11pt]{article}
|
|
131
|
+
\\usepackage{fontspec}
|
|
132
|
+
\\usepackage[margin=25mm]{geometry}
|
|
133
|
+
\\usepackage{amsmath,natbib,hyperref}
|
|
134
|
+
|
|
135
|
+
\\title{Paper Title}
|
|
136
|
+
\\author{Your Name}
|
|
137
|
+
\\date{\\today}
|
|
138
|
+
|
|
139
|
+
\\begin{document}
|
|
140
|
+
\\maketitle
|
|
141
|
+
|
|
142
|
+
\\begin{abstract}
|
|
143
|
+
One paragraph that says what the paper shows.
|
|
144
|
+
\\end{abstract}
|
|
145
|
+
|
|
146
|
+
\\section{Introduction}
|
|
147
|
+
Cite related work like this~\\citep{knuth1984}.
|
|
148
|
+
|
|
149
|
+
\\section{Results}
|
|
150
|
+
\\begin{equation}
|
|
151
|
+
e^{i\\pi} + 1 = 0
|
|
152
|
+
\\end{equation}
|
|
153
|
+
|
|
154
|
+
\\bibliographystyle{plainnat}
|
|
155
|
+
\\bibliography{refs}
|
|
156
|
+
\\end{document}
|
|
157
|
+
`,
|
|
158
|
+
'paper/refs.bib': `@book{knuth1984,
|
|
159
|
+
author = {Donald E. Knuth},
|
|
160
|
+
title = {The {\\TeX}book},
|
|
161
|
+
publisher = {Addison-Wesley},
|
|
162
|
+
year = {1984}
|
|
163
|
+
}
|
|
164
|
+
`,
|
|
165
|
+
'.github/workflows/paper.yml': `name: Paper
|
|
166
|
+
|
|
167
|
+
on:
|
|
168
|
+
push:
|
|
169
|
+
branches: [main]
|
|
170
|
+
pull_request:
|
|
171
|
+
|
|
172
|
+
jobs:
|
|
173
|
+
pdf:
|
|
174
|
+
runs-on: ubuntu-latest
|
|
175
|
+
steps:
|
|
176
|
+
- uses: actions/checkout@v4
|
|
177
|
+
with:
|
|
178
|
+
fetch-depth: 0 # the build date comes from the last commit
|
|
179
|
+
- uses: constructive-io/latex-kit/action@v1
|
|
180
|
+
with:
|
|
181
|
+
file: paper/main.tex
|
|
182
|
+
verify: 'true'
|
|
183
|
+
`,
|
|
184
|
+
'README.md': `# Paper
|
|
185
|
+
|
|
186
|
+
\`\`\`bash
|
|
187
|
+
npx latex-kit render paper/main.tex --verify # → out/main.pdf, byte-identical on every build
|
|
188
|
+
\`\`\`
|
|
189
|
+
|
|
190
|
+
CI builds the PDF on every push and attaches it to the run.
|
|
191
|
+
`,
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
export function templateFiles(name) {
|
|
195
|
+
return {
|
|
196
|
+
[`templates/${name}/template.tex`]: `\\documentclass[11pt]{article}
|
|
197
|
+
\\usepackage{fontspec}
|
|
198
|
+
\\usepackage[margin=25mm]{geometry}
|
|
199
|
+
\\usepackage{latex-kit}
|
|
200
|
+
|
|
201
|
+
\\begin{document}
|
|
202
|
+
\\section*{\\field{title}}
|
|
203
|
+
|
|
204
|
+
Prepared for \\field{recipient.name} on \\field{date|date:long}.
|
|
205
|
+
|
|
206
|
+
\\docid
|
|
207
|
+
\\end{document}
|
|
208
|
+
`,
|
|
209
|
+
[`templates/${name}/schema.ts`]: `import { ISODate, Party, z } from 'latex-kit';
|
|
210
|
+
|
|
211
|
+
export const meta = { docIdPrefix: '${name.toUpperCase().replace(/[^A-Z0-9]+/g, '-').slice(0, 12)}' };
|
|
212
|
+
|
|
213
|
+
export default z.object({
|
|
214
|
+
title: z.string(),
|
|
215
|
+
date: ISODate,
|
|
216
|
+
recipient: Party,
|
|
217
|
+
});
|
|
218
|
+
`,
|
|
219
|
+
};
|
|
220
|
+
}
|
package/esm/build.d.ts
CHANGED
|
@@ -13,10 +13,16 @@ export interface BuildOptions {
|
|
|
13
13
|
verify?: boolean;
|
|
14
14
|
/** Stop after writing the .tex (no TeX run). */
|
|
15
15
|
texOnly?: boolean;
|
|
16
|
+
/** Use this ID when the record has none, instead of minting one (for redlines). */
|
|
17
|
+
docIdFallback?: string;
|
|
18
|
+
/** Write output here instead of out/<document>/. */
|
|
19
|
+
outDir?: string;
|
|
16
20
|
onOutput?: (chunk: string) => void;
|
|
17
21
|
}
|
|
18
22
|
export interface BuildResult {
|
|
19
23
|
name: string;
|
|
24
|
+
/** The template the document uses. */
|
|
25
|
+
template: string;
|
|
20
26
|
variant: Variant;
|
|
21
27
|
tex: string;
|
|
22
28
|
pdf: string | null;
|
package/esm/build.js
CHANGED
|
@@ -3,7 +3,7 @@ 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,
|
|
6
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'fs';
|
|
7
7
|
import { basename, extname, join, relative, resolve } from 'path';
|
|
8
8
|
export class BuildError extends Error {
|
|
9
9
|
diagnostics;
|
|
@@ -74,14 +74,17 @@ async function run(job) {
|
|
|
74
74
|
template: template.name,
|
|
75
75
|
allowMissing: variant === 'draft' ? !strict : undefined,
|
|
76
76
|
loadSnippet,
|
|
77
|
+
loadClause: project.loadClause,
|
|
77
78
|
});
|
|
78
79
|
diagnostics.push(...evaluated.diagnostics);
|
|
79
80
|
if (hasErrors(diagnostics))
|
|
80
81
|
throw new BuildError(`${job.name}: cannot build ${variant}`, diagnostics);
|
|
81
82
|
// snippets count as inputs for the build date too
|
|
82
|
-
const
|
|
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 };
|
|
83
86
|
// 2. write the .tex next to latex-kit.sty
|
|
84
|
-
const outDir = join(project.outDir, job.name);
|
|
87
|
+
const outDir = options.outDir ?? join(project.outDir, job.name);
|
|
85
88
|
mkdirSync(outDir, { recursive: true });
|
|
86
89
|
const job_ = variant === 'final' ? job.name : `${job.name}-${variant}`;
|
|
87
90
|
const texPath = join(outDir, `${job_}.tex`);
|
|
@@ -91,6 +94,7 @@ async function run(job) {
|
|
|
91
94
|
writeFileSync(join(outDir, 'latex-kit.sty'), LATEX_KIT_STY);
|
|
92
95
|
const result = {
|
|
93
96
|
name: job.name,
|
|
97
|
+
template: template.name,
|
|
94
98
|
variant,
|
|
95
99
|
tex: texPath,
|
|
96
100
|
pdf: null,
|
|
@@ -132,10 +136,9 @@ async function run(job) {
|
|
|
132
136
|
result.pdf = rendered.pdf;
|
|
133
137
|
result.sha256 = rendered.sha256;
|
|
134
138
|
if (options.verify) {
|
|
135
|
-
|
|
136
|
-
const again = await render({ ...renderOpts, outDir: relative(project.root,
|
|
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) });
|
|
137
141
|
result.verified = again.sha256 === rendered.sha256;
|
|
138
|
-
rmSync(verifyDir, { recursive: true, force: true });
|
|
139
142
|
}
|
|
140
143
|
// 4. keep the ID: only after a successful final build
|
|
141
144
|
if (minted && job.mintInto) {
|
|
@@ -143,7 +146,7 @@ async function run(job) {
|
|
|
143
146
|
result.minted = true;
|
|
144
147
|
}
|
|
145
148
|
// 5. manifest: what went in, what came out
|
|
146
|
-
const inputFiles = [...new Set([...files, ...
|
|
149
|
+
const inputFiles = [...new Set([...files, ...extra])].filter(existsSync).sort();
|
|
147
150
|
writeFileSync(join(outDir, `${job_}.manifest.json`), JSON.stringify({
|
|
148
151
|
document: job.name,
|
|
149
152
|
template: template.name,
|
|
@@ -156,6 +159,7 @@ async function run(job) {
|
|
|
156
159
|
inputs: inputFiles.map((f) => ({ file: relative(project.root, f), sha256: sha256(readFileSync(f)) })),
|
|
157
160
|
tex: { file: relative(project.root, texPath), sha256: sha256(tex) },
|
|
158
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)) })),
|
|
159
163
|
fields: evaluated.fields.map((f) => ({ path: f.path, missing: f.missing })),
|
|
160
164
|
}, null, 2) + '\n');
|
|
161
165
|
return result;
|
|
@@ -179,7 +183,7 @@ export async function buildDocument(project, file, options = {}) {
|
|
|
179
183
|
diagnostics.push(diag(sev, `data.${i.path}: ${i.message}`, at));
|
|
180
184
|
}
|
|
181
185
|
}
|
|
182
|
-
const needsId = (variant === 'final' || variant === 'signed') && !doc.record.docId;
|
|
186
|
+
const needsId = (variant === 'final' || variant === 'signed') && !doc.record.docId && !options.docIdFallback;
|
|
183
187
|
if (needsId && !['.json', '.yaml', '.yml'].includes(extname(file).toLowerCase())) {
|
|
184
188
|
const suggestion = mintDocId({ prefix: template.meta.docIdPrefix ?? template.name, epoch: Math.floor(Date.now() / 1000) });
|
|
185
189
|
diagnostics.push(diag('error', `a ${variant} build needs a document ID, and latex-kit does not edit code. Add: docId: '${suggestion}'`, at));
|
|
@@ -192,7 +196,7 @@ export async function buildDocument(project, file, options = {}) {
|
|
|
192
196
|
variant,
|
|
193
197
|
data,
|
|
194
198
|
locale: doc.record.locale ?? template.meta.locale ?? project.config.locale,
|
|
195
|
-
docId: doc.record.docId,
|
|
199
|
+
docId: doc.record.docId ?? options.docIdFallback,
|
|
196
200
|
inputs: [file, ...doc.refFiles],
|
|
197
201
|
diagnostics,
|
|
198
202
|
options,
|
package/esm/check.js
CHANGED
|
@@ -13,7 +13,7 @@ export async function checkProject(project) {
|
|
|
13
13
|
if (!t.jsonSchema) {
|
|
14
14
|
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
15
|
}
|
|
16
|
-
diagnostics.push(...analyzeTemplate(t.compiled, { schema: t.jsonSchema ?? undefined, loadSnippet: project.loadSnippet }).diagnostics);
|
|
16
|
+
diagnostics.push(...analyzeTemplate(t.compiled, { schema: t.jsonSchema ?? undefined, loadSnippet: project.loadSnippet, loadClause: project.loadClause }).diagnostics);
|
|
17
17
|
}
|
|
18
18
|
catch (e) {
|
|
19
19
|
diagnostics.push(diag('error', e.message, { file: project.rel(project.path(project.config.templates, name)), line: 1, column: 1 }));
|
|
@@ -49,6 +49,6 @@ export async function checkProject(project) {
|
|
|
49
49
|
/** Every field a template reads, including through snippets. */
|
|
50
50
|
export async function templateFields(project, name) {
|
|
51
51
|
const t = await project.template(name);
|
|
52
|
-
const r = analyzeTemplate(t.compiled, { schema: t.jsonSchema ?? undefined, loadSnippet: project.loadSnippet });
|
|
52
|
+
const r = analyzeTemplate(t.compiled, { schema: t.jsonSchema ?? undefined, loadSnippet: project.loadSnippet, loadClause: project.loadClause });
|
|
53
53
|
return { fields: r.fields, diagnostics: r.diagnostics };
|
|
54
54
|
}
|
package/esm/cli.js
CHANGED
|
@@ -1,15 +1,26 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
+
import { setModuleAliases } from '@latex-kit/schema';
|
|
2
3
|
import { CLI } from 'inquirerer';
|
|
4
|
+
import { dirname } from 'path';
|
|
3
5
|
import { commands, version } from './commands';
|
|
6
|
+
// Project files import 'latex-kit' (and zod through it). Point those names at
|
|
7
|
+
// this CLI's own copies, so a project works without its own node_modules and
|
|
8
|
+
// every file shares one zod.
|
|
9
|
+
const schemaDir = dirname(require.resolve('@latex-kit/schema'));
|
|
10
|
+
setModuleAliases({
|
|
11
|
+
'latex-kit': __dirname,
|
|
12
|
+
'@latex-kit/schema': schemaDir,
|
|
13
|
+
zod: dirname(require.resolve('zod/package.json', { paths: [schemaDir] })),
|
|
14
|
+
});
|
|
4
15
|
const app = new CLI(commands, {
|
|
5
16
|
noTty: !process.stdin.isTTY,
|
|
6
17
|
// inquirerer answers --version itself; make it report latex-kit
|
|
7
18
|
version: version(),
|
|
8
19
|
minimistOpts: {
|
|
9
20
|
alias: { h: 'help', v: 'version', o: 'out', e: 'engine' },
|
|
10
|
-
boolean: ['help', 'version', 'local', 'verify', 'json', 'final', 'strict', 'tex-only'],
|
|
11
|
-
string: ['out', 'engine', 'image', 'root', 'platform', 'source-date-epoch', 'variant', 'template'],
|
|
12
|
-
default: { strict: undefined },
|
|
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'],
|
|
23
|
+
default: { strict: undefined, schema: true },
|
|
13
24
|
},
|
|
14
25
|
});
|
|
15
26
|
app.run().catch((error) => {
|
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, fieldsCommand, styCommand, typegenCommand } from './project-commands';
|
|
6
|
+
import { buildCommand, checkCommand, cleanCommand, diffCommand, fieldsCommand, importCommand, initCommand, 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. */
|
|
@@ -18,10 +18,16 @@ export function version() {
|
|
|
18
18
|
return 'unknown';
|
|
19
19
|
}
|
|
20
20
|
const COMMANDS = {
|
|
21
|
+
init: initCommand,
|
|
22
|
+
new: newCommand,
|
|
21
23
|
build: buildCommand,
|
|
24
|
+
watch: watchCommand,
|
|
25
|
+
clean: cleanCommand,
|
|
22
26
|
check: checkCommand,
|
|
23
27
|
fields: fieldsCommand,
|
|
24
28
|
typegen: typegenCommand,
|
|
29
|
+
import: importCommand,
|
|
30
|
+
diff: diffCommand,
|
|
25
31
|
sty: styCommand,
|
|
26
32
|
render: renderCommand,
|
|
27
33
|
doctor: doctorCommand,
|
package/esm/config.d.ts
CHANGED
|
@@ -5,6 +5,8 @@ export interface LatexKitConfig {
|
|
|
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`. */
|
|
9
|
+
clauses?: string;
|
|
8
10
|
/** Shared records that references point at: `<data>/<collection>/<id>.json`. Default `data`. */
|
|
9
11
|
data?: string;
|
|
10
12
|
/** Document records: `<documents>/<name>.json|yaml|ts`. Default `documents`. */
|
|
@@ -18,7 +20,7 @@ export interface LatexKitConfig {
|
|
|
18
20
|
image?: string;
|
|
19
21
|
platform?: string | null;
|
|
20
22
|
}
|
|
21
|
-
export type ResolvedConfig = Required<Pick<LatexKitConfig, 'templates' | 'snippets' | 'data' | 'documents' | 'out' | 'locale'>> & Pick<LatexKitConfig, 'engine' | 'backend' | 'image' | 'platform'> & {
|
|
23
|
+
export type ResolvedConfig = Required<Pick<LatexKitConfig, 'templates' | 'snippets' | 'clauses' | 'data' | 'documents' | 'out' | 'locale'>> & Pick<LatexKitConfig, 'engine' | 'backend' | 'image' | 'platform'> & {
|
|
22
24
|
file: string | null;
|
|
23
25
|
};
|
|
24
26
|
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', data: 'data', documents: 'documents', out: 'out', locale: 'en-US' };
|
|
8
|
+
const DEFAULTS = { templates: 'templates', snippets: 'snippets', clauses: 'clauses', 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
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { Engine } from '@latex-kit/render';
|
|
2
|
+
import { Variant } from '@latex-kit/template';
|
|
3
|
+
import { Project } from './project';
|
|
4
|
+
export interface DiffOptions {
|
|
5
|
+
variant?: Variant;
|
|
6
|
+
engine?: Engine;
|
|
7
|
+
backend?: 'docker' | 'local';
|
|
8
|
+
image?: string;
|
|
9
|
+
platform?: string | null;
|
|
10
|
+
}
|
|
11
|
+
export interface DiffResult {
|
|
12
|
+
/** The redline PDF. */
|
|
13
|
+
pdf: string;
|
|
14
|
+
/** latexdiff's marked-up .tex. */
|
|
15
|
+
tex: string;
|
|
16
|
+
old: string;
|
|
17
|
+
new: string;
|
|
18
|
+
}
|
|
19
|
+
/** Redline two .tex files inside the project. */
|
|
20
|
+
export declare function diffFiles(project: Project, oldFile: string, newFile: string, options?: DiffOptions): Promise<DiffResult>;
|
|
21
|
+
/**
|
|
22
|
+
* Redline a document against an earlier commit: build it as it was at `ref`
|
|
23
|
+
* (in a temporary git worktree) and as it is now, then latexdiff the two.
|
|
24
|
+
*/
|
|
25
|
+
export declare function diffDocument(project: Project, file: string, ref: string, options?: DiffOptions): Promise<DiffResult>;
|
package/esm/diff.js
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { render, runLatexdiff } from '@latex-kit/render';
|
|
2
|
+
import { LATEX_KIT_STY } from '@latex-kit/template';
|
|
3
|
+
import { execFileSync } from 'child_process';
|
|
4
|
+
import { copyFileSync, existsSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'fs';
|
|
5
|
+
import { tmpdir } from 'os';
|
|
6
|
+
import { basename, dirname, join, relative } from 'path';
|
|
7
|
+
import { buildDocument, BuildError } from './build';
|
|
8
|
+
import { Project } from './project';
|
|
9
|
+
function git(cwd, args) {
|
|
10
|
+
return execFileSync('git', args, { cwd, encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe'] }).trim();
|
|
11
|
+
}
|
|
12
|
+
/** latexdiff old → new in `dir`, then render the redline. */
|
|
13
|
+
async function redline(project, oldTex, newTex, dir, name, texInputs, options) {
|
|
14
|
+
mkdirSync(dir, { recursive: true });
|
|
15
|
+
writeFileSync(join(dir, 'latex-kit.sty'), LATEX_KIT_STY);
|
|
16
|
+
const tex = join(dir, `${name}.tex`);
|
|
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 });
|
|
19
|
+
const r = await render({
|
|
20
|
+
file: tex,
|
|
21
|
+
root: project.root,
|
|
22
|
+
outDir: rel(dir),
|
|
23
|
+
engine: options.engine ?? project.config.engine ?? 'lualatex',
|
|
24
|
+
backend: options.backend ?? project.config.backend,
|
|
25
|
+
image: options.image ?? project.config.image,
|
|
26
|
+
platform: options.platform !== undefined ? options.platform : project.config.platform,
|
|
27
|
+
texInputs: [rel(dir), ...texInputs.map(rel)],
|
|
28
|
+
});
|
|
29
|
+
return { pdf: r.pdf, tex, old: oldTex, new: newTex };
|
|
30
|
+
}
|
|
31
|
+
/** Redline two .tex files inside the project. */
|
|
32
|
+
export async function diffFiles(project, oldFile, newFile, options = {}) {
|
|
33
|
+
for (const f of [oldFile, newFile])
|
|
34
|
+
if (!existsSync(f))
|
|
35
|
+
throw new Error(`no such file: ${project.rel(f)}`);
|
|
36
|
+
const name = `${basename(newFile, '.tex')}-vs-${basename(oldFile, '.tex')}`;
|
|
37
|
+
const dir = join(project.outDir, 'diff', name);
|
|
38
|
+
mkdirSync(dir, { recursive: true });
|
|
39
|
+
// copies inside out/, so the files are always visible to Docker
|
|
40
|
+
const oldCopy = join(dir, 'old.tex');
|
|
41
|
+
const newCopy = join(dir, 'new.tex');
|
|
42
|
+
copyFileSync(oldFile, oldCopy);
|
|
43
|
+
copyFileSync(newFile, newCopy);
|
|
44
|
+
return redline(project, oldCopy, newCopy, dir, name, [dirname(newFile)], options);
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Redline a document against an earlier commit: build it as it was at `ref`
|
|
48
|
+
* (in a temporary git worktree) and as it is now, then latexdiff the two.
|
|
49
|
+
*/
|
|
50
|
+
export async function diffDocument(project, file, ref, options = {}) {
|
|
51
|
+
const variant = options.variant ?? 'final';
|
|
52
|
+
let top;
|
|
53
|
+
try {
|
|
54
|
+
top = git(project.root, ['rev-parse', '--show-toplevel']);
|
|
55
|
+
}
|
|
56
|
+
catch {
|
|
57
|
+
throw new Error('diff --against needs the project to be in a git repository');
|
|
58
|
+
}
|
|
59
|
+
const relDoc = relative(top, file);
|
|
60
|
+
const relProject = relative(top, project.root);
|
|
61
|
+
let sha;
|
|
62
|
+
try {
|
|
63
|
+
sha = git(top, ['rev-parse', '--short', `${ref}^{commit}`]);
|
|
64
|
+
}
|
|
65
|
+
catch {
|
|
66
|
+
throw new Error(`unknown git ref: ${ref}`);
|
|
67
|
+
}
|
|
68
|
+
try {
|
|
69
|
+
git(top, ['cat-file', '-e', `${sha}:${relDoc}`]);
|
|
70
|
+
}
|
|
71
|
+
catch {
|
|
72
|
+
throw new Error(`${relDoc} does not exist at ${ref}`);
|
|
73
|
+
}
|
|
74
|
+
const name = project.documentName(file);
|
|
75
|
+
const dir = join(project.outDir, name, 'diff');
|
|
76
|
+
const common = { variant, texOnly: true, strict: false, docIdFallback: 'UNASSIGNED' };
|
|
77
|
+
const current = await buildDocument(project, file, { ...common, outDir: join(dir, 'new') }).catch((e) => {
|
|
78
|
+
if (e instanceof BuildError)
|
|
79
|
+
throw new BuildError(`the current ${name} does not build: ${e.message}`, e.diagnostics);
|
|
80
|
+
throw e;
|
|
81
|
+
});
|
|
82
|
+
const tree = mkdtempSync(join(tmpdir(), 'latex-kit-diff-'));
|
|
83
|
+
try {
|
|
84
|
+
git(top, ['worktree', 'add', '--detach', '--quiet', tree, sha]);
|
|
85
|
+
const oldProject = await Project.open(join(tree, relProject));
|
|
86
|
+
const old = await buildDocument(oldProject, join(tree, relDoc), { ...common, outDir: join(dir, 'old') }).catch((e) => {
|
|
87
|
+
if (e instanceof BuildError)
|
|
88
|
+
throw new BuildError(`${name} at ${ref} (${sha}) does not build: ${e.message}`, e.diagnostics);
|
|
89
|
+
throw e;
|
|
90
|
+
});
|
|
91
|
+
const template = await project.template(current.template);
|
|
92
|
+
return await redline(project, old.tex, current.tex, dir, `${name}-vs-${sha}`, [template.dir], options);
|
|
93
|
+
}
|
|
94
|
+
finally {
|
|
95
|
+
try {
|
|
96
|
+
git(top, ['worktree', 'remove', '--force', tree]);
|
|
97
|
+
}
|
|
98
|
+
catch {
|
|
99
|
+
rmSync(tree, { recursive: true, force: true });
|
|
100
|
+
try {
|
|
101
|
+
git(top, ['worktree', 'prune']);
|
|
102
|
+
}
|
|
103
|
+
catch {
|
|
104
|
+
// nothing left to clean
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
package/esm/import.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { Converted } from '@latex-kit/template';
|
|
2
|
+
import { Project } from './project';
|
|
3
|
+
export interface ImportOptions {
|
|
4
|
+
/** Template name. Default: the file name in kebab-case. */
|
|
5
|
+
name?: string;
|
|
6
|
+
/** Replace an existing template. */
|
|
7
|
+
force?: boolean;
|
|
8
|
+
backend?: 'docker' | 'local';
|
|
9
|
+
/** Write a starter schema.ts from the fields found. Default true. */
|
|
10
|
+
schema?: boolean;
|
|
11
|
+
}
|
|
12
|
+
export interface ImportResult {
|
|
13
|
+
name: string;
|
|
14
|
+
template: string;
|
|
15
|
+
schema: string | null;
|
|
16
|
+
media: string | null;
|
|
17
|
+
converted: Converted[];
|
|
18
|
+
skipped: Converted[];
|
|
19
|
+
}
|
|
20
|
+
export declare function templateNameFor(file: string): string;
|
|
21
|
+
/** A zod schema source with every converted field as a string, nested by path. */
|
|
22
|
+
export declare function starterSchema(paths: string[]): string;
|
|
23
|
+
/** Convert a document (Word, Markdown, HTML, …, or an older .tex) into a template. */
|
|
24
|
+
export declare function importDocument(project: Project, file: string, options?: ImportOptions): Promise<ImportResult>;
|