latex-kit 0.2.0 → 0.3.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/project.js ADDED
@@ -0,0 +1,129 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Project = void 0;
4
+ const schema_1 = require("@latex-kit/schema");
5
+ const template_1 = require("@latex-kit/template");
6
+ const fs_1 = require("fs");
7
+ const path_1 = require("path");
8
+ const config_1 = require("./config");
9
+ const SCHEMA_FILES = ['schema.ts', 'schema.mts', 'schema.js', 'schema.mjs', 'schema.cjs'];
10
+ /** A latex-kit project: a folder with templates, data and documents. */
11
+ class Project {
12
+ root;
13
+ config;
14
+ templates = new Map();
15
+ snippets = new Map();
16
+ constructor(root, config) {
17
+ this.root = root;
18
+ this.config = config;
19
+ }
20
+ static async open(root = process.cwd()) {
21
+ const abs = (0, path_1.resolve)(root);
22
+ return new Project(abs, await (0, config_1.loadConfig)(abs));
23
+ }
24
+ path(...parts) {
25
+ return (0, path_1.join)(this.root, ...parts);
26
+ }
27
+ rel(file) {
28
+ return (0, path_1.relative)(process.cwd(), file) || '.';
29
+ }
30
+ get templatesDir() {
31
+ return this.path(this.config.templates);
32
+ }
33
+ get dataDir() {
34
+ return this.path(this.config.data);
35
+ }
36
+ get documentsDir() {
37
+ return this.path(this.config.documents);
38
+ }
39
+ get outDir() {
40
+ return this.path(this.config.out);
41
+ }
42
+ /** Template names: folders under templates/ that contain template.tex. */
43
+ templateNames() {
44
+ if (!(0, fs_1.existsSync)(this.templatesDir))
45
+ return [];
46
+ return (0, fs_1.readdirSync)(this.templatesDir)
47
+ .filter((n) => !n.startsWith('.') && !n.startsWith('_'))
48
+ .filter((n) => (0, fs_1.existsSync)((0, path_1.join)(this.templatesDir, n, 'template.tex')))
49
+ .sort();
50
+ }
51
+ /** Document record files under documents/ (any depth). */
52
+ documentFiles() {
53
+ const out = [];
54
+ const walk = (dir) => {
55
+ if (!(0, fs_1.existsSync)(dir))
56
+ return;
57
+ for (const name of (0, fs_1.readdirSync)(dir).sort()) {
58
+ if (name.startsWith('.') || name.startsWith('_'))
59
+ continue;
60
+ const p = (0, path_1.join)(dir, name);
61
+ if ((0, fs_1.statSync)(p).isDirectory())
62
+ walk(p);
63
+ else if (schema_1.DATA_EXTENSIONS.includes((0, path_1.extname)(name).toLowerCase()) && !name.endsWith('.d.ts'))
64
+ out.push(p);
65
+ }
66
+ };
67
+ walk(this.documentsDir);
68
+ return out;
69
+ }
70
+ /** A document's name: its path under documents/ without extension, `/` → `-`. */
71
+ documentName(file) {
72
+ const rel = (0, path_1.relative)(this.documentsDir, file);
73
+ const base = rel.startsWith('..') || (0, path_1.isAbsolute)(rel) ? (0, path_1.basename)(file) : rel;
74
+ return base.slice(0, -(0, path_1.extname)(base).length).split(/[\\/]/).join('-');
75
+ }
76
+ template(name) {
77
+ let t = this.templates.get(name);
78
+ if (!t) {
79
+ t = this.loadTemplate(name);
80
+ this.templates.set(name, t);
81
+ }
82
+ return t;
83
+ }
84
+ async loadTemplate(name) {
85
+ if (!/^[A-Za-z0-9_-]+$/.test(name))
86
+ throw new Error(`invalid template name "${name}"`);
87
+ const dir = (0, path_1.join)(this.templatesDir, name);
88
+ const texFile = (0, path_1.join)(dir, 'template.tex');
89
+ if (!(0, fs_1.existsSync)(texFile)) {
90
+ const known = this.templateNames();
91
+ throw new Error(`no template "${name}" (expected ${this.rel(texFile)}${known.length ? `; templates: ${known.join(', ')}` : ''})`);
92
+ }
93
+ const compiled = (0, template_1.compileTemplate)((0, fs_1.readFileSync)(texFile, 'utf8'), this.rel(texFile));
94
+ const schemaFile = SCHEMA_FILES.map((f) => (0, path_1.join)(dir, f)).find((f) => (0, fs_1.existsSync)(f)) ?? null;
95
+ let schema = null;
96
+ let meta = {};
97
+ let jsonSchema = null;
98
+ if (schemaFile) {
99
+ ({ schema, meta } = await (0, schema_1.loadTemplateSchema)(schemaFile));
100
+ jsonSchema = (0, schema_1.toJsonSchema)(schema);
101
+ }
102
+ return { name, dir, texFile, compiled, schemaFile, schema, jsonSchema, meta };
103
+ }
104
+ /**
105
+ * \snippet{name}: first next to the file that uses it, then in snippets/.
106
+ * `.tex` is added when the name has no extension. Never outside the project.
107
+ */
108
+ loadSnippet = (name, from) => {
109
+ const file = name.endsWith('.tex') ? name : `${name}.tex`;
110
+ const candidates = [(0, path_1.resolve)((0, path_1.dirname)((0, path_1.resolve)(from)), file), (0, path_1.join)(this.path(this.config.snippets), file)];
111
+ for (const c of candidates) {
112
+ const rel = (0, path_1.relative)(this.root, c);
113
+ if (rel.startsWith('..') || (0, path_1.isAbsolute)(rel) || !(0, fs_1.existsSync)(c))
114
+ continue;
115
+ let compiled = this.snippets.get(c);
116
+ if (!compiled) {
117
+ compiled = (0, template_1.compileTemplate)((0, fs_1.readFileSync)(c, 'utf8'), this.rel(c));
118
+ this.snippets.set(c, compiled);
119
+ }
120
+ return compiled;
121
+ }
122
+ return null;
123
+ };
124
+ /** Absolute paths of every snippet loaded so far. */
125
+ snippetFiles() {
126
+ return [...this.snippets.keys()];
127
+ }
128
+ }
129
+ exports.Project = Project;
package/usage.js CHANGED
@@ -6,8 +6,15 @@ 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
+ Project commands (run in a folder with templates/, data/, documents/)
10
+ build [documents…] Fill templates with records and build PDFs
11
+ check Check templates, schemas, records and IDs without TeX
12
+ fields <template> List every field a template reads
13
+ typegen Write JSON Schema and .d.ts for each template's data
14
+ sty [dir] Write latex-kit.sty so editors can preview templates
15
+
16
+ Other commands
17
+ render <file.tex> Build a plain .tex file with latexmk in a pinned image
11
18
  doctor Check Docker, the image and local TeX
12
19
 
13
20
  Options
@@ -33,6 +40,31 @@ Options
33
40
 
34
41
  Only lualatex is byte-reproducible in the default image.
35
42
  `;
43
+ const BUILD = `latex-kit build [documents…] [options]
44
+
45
+ Fills each document's template with its data and builds a PDF in
46
+ out/<document>/. Without arguments, builds every record in documents/.
47
+
48
+ Variants
49
+ --variant draft (default) DRAFT watermark and stamp; missing values show
50
+ as red markers (they fail instead when CI=true or --strict)
51
+ --variant final No marks. Missing values fail. Mints a document ID on the
52
+ --final first build and saves it in the record (JSON/YAML)
53
+ --variant signed Like final; templates can test \\when{$variant == "signed"}
54
+ --variant blank Every field is a labelled line. With no documents, builds
55
+ each template (or --template <name>) as an empty form
56
+
57
+ Options
58
+ --template <name> Only documents that use this template
59
+ --strict Fail on missing values in drafts too
60
+ -e, --engine <name> lualatex (default), xelatex or pdflatex
61
+ --local Use latexmk on PATH instead of Docker
62
+ --image <ref> Docker image (default: pinned pyramation/latex)
63
+ --verify Build twice and fail unless the PDFs are identical
64
+ --tex-only Write the .tex without running TeX
65
+ --json JSON results on stdout
66
+ --root <dir> Project folder (default: current directory)
67
+ `;
36
68
  const DOCTOR = `latex-kit doctor [--image <ref>] [--json]
37
69
 
38
70
  Reports the Node version, whether Docker is running, whether the image is
@@ -40,5 +72,5 @@ pulled and whether it runs natively on this machine, and whether latexmk
40
72
  is available for --local builds. Exits 1 if rendering cannot work.
41
73
  `;
42
74
  function help(command) {
43
- console.log(command === 'render' ? RENDER : command === 'doctor' ? DOCTOR : MAIN);
75
+ console.log(command === 'render' ? RENDER : command === 'doctor' ? DOCTOR : command === 'build' ? BUILD : MAIN);
44
76
  }