latex-kit 0.2.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.
Files changed (54) hide show
  1. package/README.md +3 -3
  2. package/boilerplates.d.ts +8 -0
  3. package/boilerplates.js +225 -0
  4. package/build.d.ts +45 -0
  5. package/build.js +226 -0
  6. package/check.d.ts +17 -0
  7. package/check.js +58 -0
  8. package/cli.d.ts +2 -0
  9. package/cli.js +31 -0
  10. package/commands.d.ts +1 -0
  11. package/commands.js +51 -15
  12. package/config.d.ts +28 -0
  13. package/config.js +30 -0
  14. package/diff.d.ts +25 -0
  15. package/diff.js +112 -0
  16. package/esm/boilerplates.d.ts +8 -0
  17. package/esm/boilerplates.js +220 -0
  18. package/esm/build.d.ts +45 -0
  19. package/esm/build.js +220 -0
  20. package/esm/check.d.ts +17 -0
  21. package/esm/check.js +54 -0
  22. package/esm/cli.d.ts +2 -0
  23. package/esm/cli.js +29 -0
  24. package/esm/commands.d.ts +1 -0
  25. package/esm/commands.js +49 -16
  26. package/esm/config.d.ts +28 -0
  27. package/esm/config.js +25 -0
  28. package/esm/diff.d.ts +25 -0
  29. package/esm/diff.js +108 -0
  30. package/esm/import.d.ts +24 -0
  31. package/esm/import.js +113 -0
  32. package/esm/index.d.ts +15 -2
  33. package/esm/index.js +15 -15
  34. package/esm/project-commands.d.ts +16 -0
  35. package/esm/project-commands.js +324 -0
  36. package/esm/project.d.ts +46 -0
  37. package/esm/project.js +139 -0
  38. package/esm/render.js +3 -5
  39. package/esm/scaffold.d.ts +27 -0
  40. package/esm/scaffold.js +108 -0
  41. package/esm/usage.js +53 -6
  42. package/import.d.ts +24 -0
  43. package/import.js +118 -0
  44. package/index.d.ts +15 -2
  45. package/index.js +29 -15
  46. package/package.json +8 -6
  47. package/project-commands.d.ts +16 -0
  48. package/project-commands.js +340 -0
  49. package/project.d.ts +46 -0
  50. package/project.js +143 -0
  51. package/render.js +2 -4
  52. package/scaffold.d.ts +27 -0
  53. package/scaffold.js +115 -0
  54. package/usage.js +53 -6
package/scaffold.js ADDED
@@ -0,0 +1,115 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.writeFiles = writeFiles;
4
+ exports.initProject = initProject;
5
+ exports.newTemplate = newTemplate;
6
+ exports.skeleton = skeleton;
7
+ exports.newDocument = newDocument;
8
+ const schema_1 = require("@latex-kit/schema");
9
+ const fs_1 = require("fs");
10
+ const path_1 = require("path");
11
+ const boilerplates_1 = require("./boilerplates");
12
+ const config_1 = require("./config");
13
+ /** Write files under `dir`. Existing files are kept unless `force`. */
14
+ function writeFiles(dir, files, force = false) {
15
+ const written = [];
16
+ const skipped = [];
17
+ for (const [rel, content] of Object.entries(files)) {
18
+ const file = (0, path_1.join)(dir, rel);
19
+ if ((0, fs_1.existsSync)(file) && !force) {
20
+ skipped.push(file);
21
+ continue;
22
+ }
23
+ (0, fs_1.mkdirSync)((0, path_1.dirname)(file), { recursive: true });
24
+ (0, fs_1.writeFileSync)(file, content);
25
+ written.push(file);
26
+ }
27
+ return { written, skipped };
28
+ }
29
+ /** Scaffold a latex-kit project (or a paper) in `dir`. */
30
+ function initProject(dir, options) {
31
+ (0, fs_1.mkdirSync)(dir, { recursive: true });
32
+ if (!options.paper && !options.force) {
33
+ const existing = config_1.CONFIG_FILES.find((f) => (0, fs_1.existsSync)((0, path_1.join)(dir, f)));
34
+ if (existing)
35
+ throw new Error(`${(0, path_1.join)(dir, existing)} already exists; use --force to add the starter files anyway`);
36
+ }
37
+ return writeFiles(dir, options.paper ? (0, boilerplates_1.paperFiles)() : (0, boilerplates_1.projectFiles)(options.version), options.force);
38
+ }
39
+ function newTemplate(project, name, force = false) {
40
+ if (!/^[a-z0-9][a-z0-9_-]*$/.test(name))
41
+ throw new Error(`invalid template name "${name}" (use lowercase letters, digits and -)`);
42
+ if ((0, fs_1.existsSync)((0, path_1.join)(project.templatesDir, name)) && !force)
43
+ throw new Error(`template ${name} already exists`);
44
+ const files = (0, boilerplates_1.templateFiles)(name);
45
+ const mapped = {};
46
+ for (const [rel, content] of Object.entries(files))
47
+ mapped[(0, path_1.join)(project.config.templates, rel.replace(/^templates\//, ''))] = content;
48
+ return writeFiles(project.root, mapped, force);
49
+ }
50
+ /** A placeholder value for a JSON Schema: required fields only, typed. */
51
+ function skeleton(schema, root, depth = 0) {
52
+ if (!schema || depth > 12)
53
+ return '';
54
+ if (typeof schema.$ref === 'string') {
55
+ const m = /^#\/(\$defs|definitions)\/(.+)$/.exec(schema.$ref);
56
+ const target = m ? root[m[1]]?.[m[2]] : undefined;
57
+ return skeleton(target, root, depth + 1);
58
+ }
59
+ if ('default' in schema)
60
+ return schema.default;
61
+ if ('const' in schema)
62
+ return schema.const;
63
+ if (Array.isArray(schema.enum))
64
+ return schema.enum[0];
65
+ const alt = (schema.anyOf ?? schema.oneOf);
66
+ if (alt?.length)
67
+ return skeleton(alt.find((a) => a.type !== 'null') ?? alt[0], root, depth + 1);
68
+ const type = Array.isArray(schema.type) ? schema.type[0] : schema.type;
69
+ switch (type) {
70
+ case 'object': {
71
+ const props = (schema.properties ?? {});
72
+ const required = (schema.required ?? []);
73
+ const out = {};
74
+ for (const key of required)
75
+ out[key] = skeleton(props[key], root, depth + 1);
76
+ return out;
77
+ }
78
+ case 'array':
79
+ return [];
80
+ case 'number':
81
+ case 'integer':
82
+ return 0;
83
+ case 'boolean':
84
+ return false;
85
+ case 'string':
86
+ return typeof schema.pattern === 'string' && schema.pattern.includes('\\d{4}-\\d{2}-\\d{2}') ? '2026-01-01' : '';
87
+ default:
88
+ return '';
89
+ }
90
+ }
91
+ /**
92
+ * documents/<name>.json for a template: every required field with a
93
+ * placeholder, and `$schema` pointing at the generated JSON Schema so editors
94
+ * autocomplete and validate it.
95
+ */
96
+ async function newDocument(project, name, templateName, force = false) {
97
+ if (!/^[a-z0-9][a-z0-9_-]*$/.test(name))
98
+ throw new Error(`invalid document name "${name}" (use lowercase letters, digits and -)`);
99
+ const existing = (0, fs_1.existsSync)(project.documentsDir) ? (0, fs_1.readdirSync)(project.documentsDir).find((f) => f.replace(/\.[^.]+$/, '') === name) : undefined;
100
+ if (existing && !force)
101
+ throw new Error(`documents/${existing} already exists`);
102
+ const template = await project.template(templateName);
103
+ const file = (0, path_1.join)(project.documentsDir, `${name}.json`);
104
+ let schemaFile = null;
105
+ const record = {};
106
+ if (template.jsonSchema) {
107
+ schemaFile = (0, path_1.join)(template.dir, `${templateName}.schema.json`);
108
+ (0, fs_1.writeFileSync)(schemaFile, JSON.stringify((0, schema_1.documentJsonSchema)(templateName, template.jsonSchema), null, 2) + '\n');
109
+ record.$schema = (0, path_1.relative)(project.documentsDir, schemaFile).split('\\').join('/');
110
+ }
111
+ record.template = templateName;
112
+ record.data = template.jsonSchema ? skeleton(template.jsonSchema, template.jsonSchema) : {};
113
+ const r = writeFiles((0, path_1.dirname)(file), { [`${name}.json`]: JSON.stringify(record, null, 2) + '\n' }, force);
114
+ return { ...r, schemaFile };
115
+ }
package/usage.js CHANGED
@@ -6,8 +6,30 @@ const MAIN = `latex-kit — build typed, reproducible LaTeX documents
6
6
  Usage
7
7
  latex-kit <command> [options]
8
8
 
9
- Commands
10
- render <file.tex> Build a PDF with latexmk in a pinned Docker image
9
+ Start
10
+ init [dir] A new project: a letter template, data and a record
11
+ init [dir] --paper A paper: main.tex, refs.bib and a GitHub Actions workflow
12
+ new template <name> A template folder with template.tex and schema.ts
13
+ new document <name> --template <t>
14
+ A record with every required field, linked to its schema
15
+
16
+ Project commands (run in a folder with templates/, data/, documents/)
17
+ build [documents…] Fill templates with records and build PDFs
18
+ watch [documents…] Rebuild drafts whenever a template, clause or record changes
19
+ clean Delete the out/ folder
20
+ check Check templates, schemas, records and IDs without TeX
21
+ fields <template> List every field a template reads
22
+ typegen Write JSON Schema and .d.ts for each template's data
23
+ sty [dir] Write latex-kit.sty so editors can preview templates
24
+ import <file> Turn a Word/Markdown/HTML document (or an older .tex)
25
+ into a template; {{x}} and @GROUP@FIELD@ become \\field
26
+ diff <document> --against <ref>
27
+ Redline PDF of a document against an earlier commit
28
+ diff <old.tex> <new.tex>
29
+ Redline PDF of any two .tex files
30
+
31
+ Other commands
32
+ render <file.tex> Build a plain .tex file with latexmk in a pinned image
11
33
  doctor Check Docker, the image and local TeX
12
34
 
13
35
  Options
@@ -22,10 +44,10 @@ always produces the same PDF bytes.
22
44
 
23
45
  Options
24
46
  -o, --out <dir> Output directory, relative to --root (default: out)
25
- -e, --engine <name> lualatex (default), xelatex or pdflatex
47
+ -e, --engine <name> lualatex (default), xelatex, pdflatex or pdfps
26
48
  --root <dir> Directory TeX can see (default: current directory)
27
- --image <ref> Docker image (default: pinned pyramation/latex digest)
28
- --platform <os/arch> Docker --platform (default: linux/amd64 for the default image)
49
+ --image <ref> Docker image (default: pinned ghcr.io/constructive-io/latex-kit)
50
+ --platform <os/arch> Docker --platform (default: native)
29
51
  --local Use latexmk on PATH instead of Docker (not pinned)
30
52
  --source-date-epoch <s> Build timestamp in seconds since 1970
31
53
  --verify Build twice and fail if the PDFs differ
@@ -33,6 +55,31 @@ Options
33
55
 
34
56
  Only lualatex is byte-reproducible in the default image.
35
57
  `;
58
+ const BUILD = `latex-kit build [documents…] [options]
59
+
60
+ Fills each document's template with its data and builds a PDF in
61
+ out/<document>/. Without arguments, builds every record in documents/.
62
+
63
+ Variants
64
+ --variant draft (default) DRAFT watermark and stamp; missing values show
65
+ as red markers (they fail instead when CI=true or --strict)
66
+ --variant final No marks. Missing values fail. Mints a document ID on the
67
+ --final first build and saves it in the record (JSON/YAML)
68
+ --variant signed Like final; templates can test \\when{$variant == "signed"}
69
+ --variant blank Every field is a labelled line. With no documents, builds
70
+ each template (or --template <name>) as an empty form
71
+
72
+ Options
73
+ --template <name> Only documents that use this template
74
+ --strict Fail on missing values in drafts too
75
+ -e, --engine <name> lualatex (default), xelatex, pdflatex or pdfps
76
+ --local Use latexmk on PATH instead of Docker
77
+ --image <ref> Docker image (default: pinned ghcr.io/constructive-io/latex-kit)
78
+ --verify Build twice and fail unless the PDFs are identical
79
+ --tex-only Write the .tex without running TeX
80
+ --json JSON results on stdout
81
+ --root <dir> Project folder (default: current directory)
82
+ `;
36
83
  const DOCTOR = `latex-kit doctor [--image <ref>] [--json]
37
84
 
38
85
  Reports the Node version, whether Docker is running, whether the image is
@@ -40,5 +87,5 @@ pulled and whether it runs natively on this machine, and whether latexmk
40
87
  is available for --local builds. Exits 1 if rendering cannot work.
41
88
  `;
42
89
  function help(command) {
43
- console.log(command === 'render' ? RENDER : command === 'doctor' ? DOCTOR : MAIN);
90
+ console.log(command === 'render' ? RENDER : command === 'doctor' ? DOCTOR : command === 'build' ? BUILD : MAIN);
44
91
  }