@latex-kit/template 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/README.md +19 -2
- package/analyze.d.ts +8 -0
- package/analyze.js +32 -1
- package/clause.d.ts +23 -0
- package/clause.js +26 -0
- package/codemod.d.ts +25 -0
- package/codemod.js +67 -0
- package/compile.d.ts +13 -1
- package/compile.js +23 -7
- package/esm/analyze.d.ts +8 -0
- package/esm/analyze.js +32 -1
- package/esm/clause.d.ts +23 -0
- package/esm/clause.js +21 -0
- package/esm/codemod.d.ts +25 -0
- package/esm/codemod.js +62 -0
- package/esm/compile.d.ts +13 -1
- package/esm/compile.js +22 -7
- package/esm/evaluate.d.ts +5 -0
- package/esm/evaluate.js +37 -1
- package/esm/index.d.ts +2 -0
- package/esm/index.js +2 -0
- package/esm/sty.d.ts +1 -1
- package/esm/sty.js +6 -1
- package/evaluate.d.ts +5 -0
- package/evaluate.js +37 -1
- package/index.d.ts +2 -0
- package/index.js +2 -0
- package/package.json +2 -2
- package/sty.d.ts +1 -1
- package/sty.js +6 -1
package/README.md
CHANGED
|
@@ -51,11 +51,28 @@ Document ID: \docid
|
|
|
51
51
|
| `\sep{…}` | Inside `\each`: printed between items, not after the last |
|
|
52
52
|
| `\snippet{name}` | Includes `name.tex` (next to the template, then `snippets/`) with the same data |
|
|
53
53
|
| `\docid` | The document's ID, minted once and stored on the record |
|
|
54
|
+
| `\clause{id}` | Places a numbered clause from the library (`clauses/<id>.tex`), filled with the same data |
|
|
55
|
+
| `\clauseref{id}` | Prints that clause's number. A reference to a clause the document doesn't contain fails a final build |
|
|
54
56
|
|
|
55
57
|
Special values: `$variant`, `$locale`, `$docid`, `$template`.
|
|
56
58
|
|
|
57
|
-
`\snippet` and `\clauseref`
|
|
58
|
-
|
|
59
|
+
`\snippet` and `\clauseref` are named so they never collide with LaTeX's
|
|
60
|
+
`\partial` and `\ref`.
|
|
61
|
+
|
|
62
|
+
### Clauses
|
|
63
|
+
|
|
64
|
+
A clause is a template fragment with a short header:
|
|
65
|
+
|
|
66
|
+
```latex
|
|
67
|
+
% title: Confidentiality
|
|
68
|
+
% version: 3
|
|
69
|
+
The Recipient will keep it confidential for \field{termMonths|words} months,
|
|
70
|
+
subject to Section~\clauseref{nda/exclusions}.
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
Each placed clause becomes `\lkclause{title}{label}` (a numbered section by
|
|
74
|
+
default; redefine `\lkclause` to change the style). Every build's manifest
|
|
75
|
+
lists the clause IDs and versions it used.
|
|
59
76
|
|
|
60
77
|
### Conditions
|
|
61
78
|
|
package/analyze.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { LoadedClause } from './clause';
|
|
1
2
|
import { CompiledTemplate } from './compile';
|
|
2
3
|
import { Diagnostic } from './diagnostics';
|
|
3
4
|
import { JsonSchema } from './jsonschema';
|
|
@@ -17,10 +18,17 @@ export interface AnalyzeOptions {
|
|
|
17
18
|
/** JSON Schema of the record's data. Without it only syntax is checked. */
|
|
18
19
|
schema?: JsonSchema;
|
|
19
20
|
loadSnippet?: (name: string, from: string) => CompiledTemplate | null;
|
|
21
|
+
loadClause?: (id: string) => LoadedClause | null;
|
|
20
22
|
}
|
|
21
23
|
export interface AnalyzeResult {
|
|
22
24
|
fields: FieldRef[];
|
|
23
25
|
snippets: string[];
|
|
26
|
+
/** Library clauses the template can place, with versions. */
|
|
27
|
+
clauses: {
|
|
28
|
+
id: string;
|
|
29
|
+
version?: string;
|
|
30
|
+
file: string;
|
|
31
|
+
}[];
|
|
24
32
|
diagnostics: Diagnostic[];
|
|
25
33
|
}
|
|
26
34
|
/**
|
package/analyze.js
CHANGED
|
@@ -14,6 +14,8 @@ function analyzeTemplate(template, options = {}) {
|
|
|
14
14
|
const diagnostics = [...template.diagnostics];
|
|
15
15
|
const fields = [];
|
|
16
16
|
const snippets = [];
|
|
17
|
+
const clauses = [];
|
|
18
|
+
const refs = [];
|
|
17
19
|
const stack = [template.file];
|
|
18
20
|
const resolve = (path, scopes, loc) => {
|
|
19
21
|
const [head, ...rest] = (0, path_1.splitPath)(path);
|
|
@@ -105,9 +107,38 @@ function analyzeTemplate(template, options = {}) {
|
|
|
105
107
|
stack.pop();
|
|
106
108
|
break;
|
|
107
109
|
}
|
|
110
|
+
case 'clause': {
|
|
111
|
+
const clause = options.loadClause?.(node.id) ?? null;
|
|
112
|
+
if (!clause) {
|
|
113
|
+
diagnostics.push((0, diagnostics_1.diag)('error', `clause not found: ${node.id}`, node.loc));
|
|
114
|
+
break;
|
|
115
|
+
}
|
|
116
|
+
if (stack.includes(clause.template.file)) {
|
|
117
|
+
diagnostics.push((0, diagnostics_1.diag)('error', `clause ${node.id} includes itself`, node.loc));
|
|
118
|
+
break;
|
|
119
|
+
}
|
|
120
|
+
if (!clauses.some((c) => c.id === node.id)) {
|
|
121
|
+
clauses.push({ id: node.id, version: clause.version, file: clause.template.file });
|
|
122
|
+
diagnostics.push(...clause.template.diagnostics);
|
|
123
|
+
if (!clause.title)
|
|
124
|
+
diagnostics.push((0, diagnostics_1.diag)('warning', `clause ${node.id} has no "% title:" line, so it prints as a bare number`, { file: clause.template.file, line: 1, column: 1 }));
|
|
125
|
+
}
|
|
126
|
+
stack.push(clause.template.file);
|
|
127
|
+
walk(clause.template.nodes, scopes);
|
|
128
|
+
stack.pop();
|
|
129
|
+
break;
|
|
130
|
+
}
|
|
131
|
+
case 'clauseref':
|
|
132
|
+
refs.push({ id: node.id, loc: node.loc });
|
|
133
|
+
break;
|
|
108
134
|
}
|
|
109
135
|
}
|
|
110
136
|
};
|
|
111
137
|
walk(template.nodes, []);
|
|
112
|
-
|
|
138
|
+
for (const r of refs) {
|
|
139
|
+
if (!clauses.some((c) => c.id === r.id)) {
|
|
140
|
+
diagnostics.push((0, diagnostics_1.diag)('error', `\\clauseref{${r.id}}: the template never places \\clause{${r.id}}`, r.loc));
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
return { fields, snippets, clauses, diagnostics };
|
|
113
144
|
}
|
package/clause.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { CompiledTemplate } from './compile';
|
|
2
|
+
export interface ClauseMeta {
|
|
3
|
+
title?: string;
|
|
4
|
+
version?: string;
|
|
5
|
+
}
|
|
6
|
+
/** A clause from the library, compiled, with its header. */
|
|
7
|
+
export interface LoadedClause extends ClauseMeta {
|
|
8
|
+
id: string;
|
|
9
|
+
template: CompiledTemplate;
|
|
10
|
+
}
|
|
11
|
+
export interface ClauseUse {
|
|
12
|
+
id: string;
|
|
13
|
+
version?: string;
|
|
14
|
+
file: string;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Read a clause file's header: the leading `%` comment lines.
|
|
18
|
+
*
|
|
19
|
+
* % title: Confidentiality
|
|
20
|
+
* % version: 3
|
|
21
|
+
*/
|
|
22
|
+
export declare function parseClauseHeader(text: string): ClauseMeta;
|
|
23
|
+
export declare const clauseLabel: (id: string) => string;
|
package/clause.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.clauseLabel = void 0;
|
|
4
|
+
exports.parseClauseHeader = parseClauseHeader;
|
|
5
|
+
/**
|
|
6
|
+
* Read a clause file's header: the leading `%` comment lines.
|
|
7
|
+
*
|
|
8
|
+
* % title: Confidentiality
|
|
9
|
+
* % version: 3
|
|
10
|
+
*/
|
|
11
|
+
function parseClauseHeader(text) {
|
|
12
|
+
const meta = {};
|
|
13
|
+
for (const line of text.split('\n')) {
|
|
14
|
+
const t = line.trim();
|
|
15
|
+
if (t === '')
|
|
16
|
+
continue;
|
|
17
|
+
if (!t.startsWith('%'))
|
|
18
|
+
break;
|
|
19
|
+
const m = /^%\s*(title|version)\s*:\s*(.*?)\s*$/i.exec(t);
|
|
20
|
+
if (m)
|
|
21
|
+
meta[m[1].toLowerCase()] = m[2];
|
|
22
|
+
}
|
|
23
|
+
return meta;
|
|
24
|
+
}
|
|
25
|
+
const clauseLabel = (id) => `clause:${id}`;
|
|
26
|
+
exports.clauseLabel = clauseLabel;
|
package/codemod.d.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export interface Converted {
|
|
2
|
+
/** The token as it appeared, e.g. `@COMPANY@LEGAL_NAME@` or `{{ party.name }}`. */
|
|
3
|
+
from: string;
|
|
4
|
+
/** What it became, e.g. `\field{company.legalName}`. */
|
|
5
|
+
to: string;
|
|
6
|
+
line: number;
|
|
7
|
+
}
|
|
8
|
+
export interface ConvertResult {
|
|
9
|
+
tex: string;
|
|
10
|
+
converted: Converted[];
|
|
11
|
+
/** Tokens that looked like placeholders but could not become a valid path. */
|
|
12
|
+
skipped: Converted[];
|
|
13
|
+
}
|
|
14
|
+
/** LEGAL_NAME → legalName, COMPANY → company, already camelCase → unchanged. */
|
|
15
|
+
export declare function camelSegment(seg: string): string;
|
|
16
|
+
/**
|
|
17
|
+
* Turn placeholder tokens from older templates or Word documents into
|
|
18
|
+
* `\field{…}`: `@GROUP@FIELD@` (the legacy generator's tokens) and
|
|
19
|
+
* `{{ path }}` (common in Word/Markdown templates, also after pandoc
|
|
20
|
+
* escaped it). Anything that cannot become a valid path is left alone and
|
|
21
|
+
* reported.
|
|
22
|
+
*/
|
|
23
|
+
export declare function convertPlaceholders(tex: string): ConvertResult;
|
|
24
|
+
/** Make sure a document loads latex-kit.sty, right before \begin{document}. */
|
|
25
|
+
export declare function ensureLatexKitPackage(tex: string): string;
|
package/codemod.js
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.camelSegment = camelSegment;
|
|
4
|
+
exports.convertPlaceholders = convertPlaceholders;
|
|
5
|
+
exports.ensureLatexKitPackage = ensureLatexKitPackage;
|
|
6
|
+
const path_1 = require("./path");
|
|
7
|
+
/** LEGAL_NAME → legalName, COMPANY → company, already camelCase → unchanged. */
|
|
8
|
+
function camelSegment(seg) {
|
|
9
|
+
const s = seg.replace(/\\_/g, '_');
|
|
10
|
+
if (!s.includes('_') && s !== s.toUpperCase())
|
|
11
|
+
return s;
|
|
12
|
+
const parts = s.toLowerCase().split('_').filter(Boolean);
|
|
13
|
+
return parts.map((p, i) => (i === 0 ? p : p[0].toUpperCase() + p.slice(1))).join('');
|
|
14
|
+
}
|
|
15
|
+
// @GROUP@FIELD@ with letters, digits and underscores (possibly escaped as \_ by pandoc)
|
|
16
|
+
const LEGACY = /@([A-Za-z](?:[A-Za-z0-9_]|\\_)*)@([A-Za-z](?:[A-Za-z0-9_]|\\_)*)@/g;
|
|
17
|
+
// {{ path }} as typed, or \{\{ path \}\} as pandoc escapes it
|
|
18
|
+
const MUSTACHE = /(?:\{\{|\\\{\\\{)\s*([^{}\\]*(?:\\_[^{}\\]*)*?)\s*(?:\}\}|\\\}\\\})/g;
|
|
19
|
+
function lineAt(text, offset) {
|
|
20
|
+
let n = 1;
|
|
21
|
+
for (let i = 0; i < offset; i++)
|
|
22
|
+
if (text.charCodeAt(i) === 10)
|
|
23
|
+
n++;
|
|
24
|
+
return n;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Turn placeholder tokens from older templates or Word documents into
|
|
28
|
+
* `\field{…}`: `@GROUP@FIELD@` (the legacy generator's tokens) and
|
|
29
|
+
* `{{ path }}` (common in Word/Markdown templates, also after pandoc
|
|
30
|
+
* escaped it). Anything that cannot become a valid path is left alone and
|
|
31
|
+
* reported.
|
|
32
|
+
*/
|
|
33
|
+
function convertPlaceholders(tex) {
|
|
34
|
+
const converted = [];
|
|
35
|
+
const skipped = [];
|
|
36
|
+
let out = tex.replace(LEGACY, (whole, group, field, offset) => {
|
|
37
|
+
const path = `${camelSegment(group)}.${camelSegment(field)}`;
|
|
38
|
+
const item = { from: whole, to: `\\field{${path}}`, line: lineAt(tex, offset) };
|
|
39
|
+
if (!(0, path_1.isPath)(path)) {
|
|
40
|
+
skipped.push(item);
|
|
41
|
+
return whole;
|
|
42
|
+
}
|
|
43
|
+
converted.push(item);
|
|
44
|
+
return item.to;
|
|
45
|
+
});
|
|
46
|
+
const before = out;
|
|
47
|
+
out = out.replace(MUSTACHE, (whole, inner, offset) => {
|
|
48
|
+
const path = inner.replace(/\\_/g, '_').trim();
|
|
49
|
+
const item = { from: whole, to: `\\field{${path}}`, line: lineAt(before, offset) };
|
|
50
|
+
if (!(0, path_1.isPath)(path)) {
|
|
51
|
+
skipped.push(item);
|
|
52
|
+
return whole;
|
|
53
|
+
}
|
|
54
|
+
converted.push(item);
|
|
55
|
+
return item.to;
|
|
56
|
+
});
|
|
57
|
+
return { tex: out, converted, skipped };
|
|
58
|
+
}
|
|
59
|
+
/** Make sure a document loads latex-kit.sty, right before \begin{document}. */
|
|
60
|
+
function ensureLatexKitPackage(tex) {
|
|
61
|
+
if (/\\usepackage\s*(?:\[[^\]]*\])?\s*\{[^}]*\blatex-kit\b[^}]*\}/.test(tex))
|
|
62
|
+
return tex;
|
|
63
|
+
const at = tex.indexOf('\\begin{document}');
|
|
64
|
+
if (at === -1)
|
|
65
|
+
return tex;
|
|
66
|
+
return `${tex.slice(0, at)}\\usepackage{latex-kit}\n${tex.slice(at)}`;
|
|
67
|
+
}
|
package/compile.d.ts
CHANGED
|
@@ -4,7 +4,8 @@ import { FieldSpec } from './filters';
|
|
|
4
4
|
/**
|
|
5
5
|
* The template language's macros and how many {…} arguments each takes.
|
|
6
6
|
* Bare names keep templates readable; \snippet (not \partial, which is the
|
|
7
|
-
* math ∂) and \clauseref (not \ref) avoid LaTeX's own commands.
|
|
7
|
+
* math ∂) and \clauseref (not \ref) avoid LaTeX's own commands. \clause
|
|
8
|
+
* inserts a numbered clause from the clause library; \clauseref prints its number.
|
|
8
9
|
*/
|
|
9
10
|
export declare const MACROS: {
|
|
10
11
|
readonly field: 1;
|
|
@@ -15,6 +16,7 @@ export declare const MACROS: {
|
|
|
15
16
|
readonly sep: 1;
|
|
16
17
|
readonly snippet: 1;
|
|
17
18
|
readonly docid: 0;
|
|
19
|
+
readonly clause: 1;
|
|
18
20
|
readonly clauseref: 1;
|
|
19
21
|
};
|
|
20
22
|
export type MacroName = keyof typeof MACROS;
|
|
@@ -51,6 +53,14 @@ export type Node = {
|
|
|
51
53
|
} | {
|
|
52
54
|
kind: 'docid';
|
|
53
55
|
loc: Location;
|
|
56
|
+
} | {
|
|
57
|
+
kind: 'clause';
|
|
58
|
+
id: string;
|
|
59
|
+
loc: Location;
|
|
60
|
+
} | {
|
|
61
|
+
kind: 'clauseref';
|
|
62
|
+
id: string;
|
|
63
|
+
loc: Location;
|
|
54
64
|
};
|
|
55
65
|
export interface CompiledTemplate {
|
|
56
66
|
file: string;
|
|
@@ -59,4 +69,6 @@ export interface CompiledTemplate {
|
|
|
59
69
|
/** Syntax problems found while compiling. */
|
|
60
70
|
diagnostics: Diagnostic[];
|
|
61
71
|
}
|
|
72
|
+
/** `parts/header`, `nda/confidentiality`: relative, no `..`, no empty segments. */
|
|
73
|
+
export declare function isRelativeName(name: string): boolean;
|
|
62
74
|
export declare function compileTemplate(text: string, file: string): CompiledTemplate;
|
package/compile.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.MACROS = void 0;
|
|
4
|
+
exports.isRelativeName = isRelativeName;
|
|
4
5
|
exports.compileTemplate = compileTemplate;
|
|
5
6
|
const unified_latex_util_parse_1 = require("@unified-latex/unified-latex-util-parse");
|
|
6
7
|
const condition_1 = require("./condition");
|
|
@@ -10,7 +11,8 @@ const path_1 = require("./path");
|
|
|
10
11
|
/**
|
|
11
12
|
* The template language's macros and how many {…} arguments each takes.
|
|
12
13
|
* Bare names keep templates readable; \snippet (not \partial, which is the
|
|
13
|
-
* math ∂) and \clauseref (not \ref) avoid LaTeX's own commands.
|
|
14
|
+
* math ∂) and \clauseref (not \ref) avoid LaTeX's own commands. \clause
|
|
15
|
+
* inserts a numbered clause from the clause library; \clauseref prints its number.
|
|
14
16
|
*/
|
|
15
17
|
exports.MACROS = {
|
|
16
18
|
field: 1,
|
|
@@ -21,11 +23,16 @@ exports.MACROS = {
|
|
|
21
23
|
sep: 1,
|
|
22
24
|
snippet: 1,
|
|
23
25
|
docid: 0,
|
|
26
|
+
clause: 1,
|
|
24
27
|
clauseref: 1,
|
|
25
28
|
};
|
|
26
29
|
const parser = (0, unified_latex_util_parse_1.getParser)({
|
|
27
30
|
macros: Object.fromEntries(Object.entries(exports.MACROS).map(([name, n]) => [name, { signature: Array(n).fill('m').join(' ') }])),
|
|
28
31
|
});
|
|
32
|
+
/** `parts/header`, `nda/confidentiality`: relative, no `..`, no empty segments. */
|
|
33
|
+
function isRelativeName(name) {
|
|
34
|
+
return /^[A-Za-z0-9_./-]+$/.test(name) && !name.split('/').some((s) => s === '..' || s === '' || s === '.');
|
|
35
|
+
}
|
|
29
36
|
/** Find our macros with unified-latex, so comments, verbatim and \verb are skipped. */
|
|
30
37
|
function findMacros(text) {
|
|
31
38
|
const out = [];
|
|
@@ -204,14 +211,18 @@ function compileTemplate(text, file) {
|
|
|
204
211
|
pos = macroEnd;
|
|
205
212
|
break;
|
|
206
213
|
}
|
|
207
|
-
case 'snippet':
|
|
214
|
+
case 'snippet':
|
|
215
|
+
case 'clause': {
|
|
208
216
|
const name = argText(0);
|
|
209
|
-
if (
|
|
210
|
-
diagnostics.push((0, diagnostics_1.diag)('error',
|
|
217
|
+
if (!isRelativeName(name)) {
|
|
218
|
+
diagnostics.push((0, diagnostics_1.diag)('error', `\\${occ.name}{${name}}: use a relative name like ${occ.name === 'clause' ? 'nda/confidentiality' : 'parts/header'}`, loc));
|
|
211
219
|
}
|
|
212
|
-
else {
|
|
220
|
+
else if (occ.name === 'snippet') {
|
|
213
221
|
nodes.push({ kind: 'snippet', name, loc });
|
|
214
222
|
}
|
|
223
|
+
else {
|
|
224
|
+
nodes.push({ kind: 'clause', id: name, loc });
|
|
225
|
+
}
|
|
215
226
|
pos = macroEnd;
|
|
216
227
|
break;
|
|
217
228
|
}
|
|
@@ -221,10 +232,15 @@ function compileTemplate(text, file) {
|
|
|
221
232
|
pos = text.startsWith('{}', macroEnd) ? macroEnd + 2 : macroEnd;
|
|
222
233
|
break;
|
|
223
234
|
}
|
|
224
|
-
case 'clauseref':
|
|
225
|
-
|
|
235
|
+
case 'clauseref': {
|
|
236
|
+
const id = argText(0);
|
|
237
|
+
if (!isRelativeName(id))
|
|
238
|
+
diagnostics.push((0, diagnostics_1.diag)('error', `\\clauseref{${id}}: use the clause's name, like nda/confidentiality`, loc));
|
|
239
|
+
else
|
|
240
|
+
nodes.push({ kind: 'clauseref', id, loc });
|
|
226
241
|
pos = macroEnd;
|
|
227
242
|
break;
|
|
243
|
+
}
|
|
228
244
|
}
|
|
229
245
|
}
|
|
230
246
|
catch (e) {
|
package/esm/analyze.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { LoadedClause } from './clause';
|
|
1
2
|
import { CompiledTemplate } from './compile';
|
|
2
3
|
import { Diagnostic } from './diagnostics';
|
|
3
4
|
import { JsonSchema } from './jsonschema';
|
|
@@ -17,10 +18,17 @@ export interface AnalyzeOptions {
|
|
|
17
18
|
/** JSON Schema of the record's data. Without it only syntax is checked. */
|
|
18
19
|
schema?: JsonSchema;
|
|
19
20
|
loadSnippet?: (name: string, from: string) => CompiledTemplate | null;
|
|
21
|
+
loadClause?: (id: string) => LoadedClause | null;
|
|
20
22
|
}
|
|
21
23
|
export interface AnalyzeResult {
|
|
22
24
|
fields: FieldRef[];
|
|
23
25
|
snippets: string[];
|
|
26
|
+
/** Library clauses the template can place, with versions. */
|
|
27
|
+
clauses: {
|
|
28
|
+
id: string;
|
|
29
|
+
version?: string;
|
|
30
|
+
file: string;
|
|
31
|
+
}[];
|
|
24
32
|
diagnostics: Diagnostic[];
|
|
25
33
|
}
|
|
26
34
|
/**
|
package/esm/analyze.js
CHANGED
|
@@ -11,6 +11,8 @@ export function analyzeTemplate(template, options = {}) {
|
|
|
11
11
|
const diagnostics = [...template.diagnostics];
|
|
12
12
|
const fields = [];
|
|
13
13
|
const snippets = [];
|
|
14
|
+
const clauses = [];
|
|
15
|
+
const refs = [];
|
|
14
16
|
const stack = [template.file];
|
|
15
17
|
const resolve = (path, scopes, loc) => {
|
|
16
18
|
const [head, ...rest] = splitPath(path);
|
|
@@ -102,9 +104,38 @@ export function analyzeTemplate(template, options = {}) {
|
|
|
102
104
|
stack.pop();
|
|
103
105
|
break;
|
|
104
106
|
}
|
|
107
|
+
case 'clause': {
|
|
108
|
+
const clause = options.loadClause?.(node.id) ?? null;
|
|
109
|
+
if (!clause) {
|
|
110
|
+
diagnostics.push(diag('error', `clause not found: ${node.id}`, node.loc));
|
|
111
|
+
break;
|
|
112
|
+
}
|
|
113
|
+
if (stack.includes(clause.template.file)) {
|
|
114
|
+
diagnostics.push(diag('error', `clause ${node.id} includes itself`, node.loc));
|
|
115
|
+
break;
|
|
116
|
+
}
|
|
117
|
+
if (!clauses.some((c) => c.id === node.id)) {
|
|
118
|
+
clauses.push({ id: node.id, version: clause.version, file: clause.template.file });
|
|
119
|
+
diagnostics.push(...clause.template.diagnostics);
|
|
120
|
+
if (!clause.title)
|
|
121
|
+
diagnostics.push(diag('warning', `clause ${node.id} has no "% title:" line, so it prints as a bare number`, { file: clause.template.file, line: 1, column: 1 }));
|
|
122
|
+
}
|
|
123
|
+
stack.push(clause.template.file);
|
|
124
|
+
walk(clause.template.nodes, scopes);
|
|
125
|
+
stack.pop();
|
|
126
|
+
break;
|
|
127
|
+
}
|
|
128
|
+
case 'clauseref':
|
|
129
|
+
refs.push({ id: node.id, loc: node.loc });
|
|
130
|
+
break;
|
|
105
131
|
}
|
|
106
132
|
}
|
|
107
133
|
};
|
|
108
134
|
walk(template.nodes, []);
|
|
109
|
-
|
|
135
|
+
for (const r of refs) {
|
|
136
|
+
if (!clauses.some((c) => c.id === r.id)) {
|
|
137
|
+
diagnostics.push(diag('error', `\\clauseref{${r.id}}: the template never places \\clause{${r.id}}`, r.loc));
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
return { fields, snippets, clauses, diagnostics };
|
|
110
141
|
}
|
package/esm/clause.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { CompiledTemplate } from './compile';
|
|
2
|
+
export interface ClauseMeta {
|
|
3
|
+
title?: string;
|
|
4
|
+
version?: string;
|
|
5
|
+
}
|
|
6
|
+
/** A clause from the library, compiled, with its header. */
|
|
7
|
+
export interface LoadedClause extends ClauseMeta {
|
|
8
|
+
id: string;
|
|
9
|
+
template: CompiledTemplate;
|
|
10
|
+
}
|
|
11
|
+
export interface ClauseUse {
|
|
12
|
+
id: string;
|
|
13
|
+
version?: string;
|
|
14
|
+
file: string;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Read a clause file's header: the leading `%` comment lines.
|
|
18
|
+
*
|
|
19
|
+
* % title: Confidentiality
|
|
20
|
+
* % version: 3
|
|
21
|
+
*/
|
|
22
|
+
export declare function parseClauseHeader(text: string): ClauseMeta;
|
|
23
|
+
export declare const clauseLabel: (id: string) => string;
|
package/esm/clause.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Read a clause file's header: the leading `%` comment lines.
|
|
3
|
+
*
|
|
4
|
+
* % title: Confidentiality
|
|
5
|
+
* % version: 3
|
|
6
|
+
*/
|
|
7
|
+
export function parseClauseHeader(text) {
|
|
8
|
+
const meta = {};
|
|
9
|
+
for (const line of text.split('\n')) {
|
|
10
|
+
const t = line.trim();
|
|
11
|
+
if (t === '')
|
|
12
|
+
continue;
|
|
13
|
+
if (!t.startsWith('%'))
|
|
14
|
+
break;
|
|
15
|
+
const m = /^%\s*(title|version)\s*:\s*(.*?)\s*$/i.exec(t);
|
|
16
|
+
if (m)
|
|
17
|
+
meta[m[1].toLowerCase()] = m[2];
|
|
18
|
+
}
|
|
19
|
+
return meta;
|
|
20
|
+
}
|
|
21
|
+
export const clauseLabel = (id) => `clause:${id}`;
|
package/esm/codemod.d.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export interface Converted {
|
|
2
|
+
/** The token as it appeared, e.g. `@COMPANY@LEGAL_NAME@` or `{{ party.name }}`. */
|
|
3
|
+
from: string;
|
|
4
|
+
/** What it became, e.g. `\field{company.legalName}`. */
|
|
5
|
+
to: string;
|
|
6
|
+
line: number;
|
|
7
|
+
}
|
|
8
|
+
export interface ConvertResult {
|
|
9
|
+
tex: string;
|
|
10
|
+
converted: Converted[];
|
|
11
|
+
/** Tokens that looked like placeholders but could not become a valid path. */
|
|
12
|
+
skipped: Converted[];
|
|
13
|
+
}
|
|
14
|
+
/** LEGAL_NAME → legalName, COMPANY → company, already camelCase → unchanged. */
|
|
15
|
+
export declare function camelSegment(seg: string): string;
|
|
16
|
+
/**
|
|
17
|
+
* Turn placeholder tokens from older templates or Word documents into
|
|
18
|
+
* `\field{…}`: `@GROUP@FIELD@` (the legacy generator's tokens) and
|
|
19
|
+
* `{{ path }}` (common in Word/Markdown templates, also after pandoc
|
|
20
|
+
* escaped it). Anything that cannot become a valid path is left alone and
|
|
21
|
+
* reported.
|
|
22
|
+
*/
|
|
23
|
+
export declare function convertPlaceholders(tex: string): ConvertResult;
|
|
24
|
+
/** Make sure a document loads latex-kit.sty, right before \begin{document}. */
|
|
25
|
+
export declare function ensureLatexKitPackage(tex: string): string;
|
package/esm/codemod.js
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { isPath } from './path';
|
|
2
|
+
/** LEGAL_NAME → legalName, COMPANY → company, already camelCase → unchanged. */
|
|
3
|
+
export function camelSegment(seg) {
|
|
4
|
+
const s = seg.replace(/\\_/g, '_');
|
|
5
|
+
if (!s.includes('_') && s !== s.toUpperCase())
|
|
6
|
+
return s;
|
|
7
|
+
const parts = s.toLowerCase().split('_').filter(Boolean);
|
|
8
|
+
return parts.map((p, i) => (i === 0 ? p : p[0].toUpperCase() + p.slice(1))).join('');
|
|
9
|
+
}
|
|
10
|
+
// @GROUP@FIELD@ with letters, digits and underscores (possibly escaped as \_ by pandoc)
|
|
11
|
+
const LEGACY = /@([A-Za-z](?:[A-Za-z0-9_]|\\_)*)@([A-Za-z](?:[A-Za-z0-9_]|\\_)*)@/g;
|
|
12
|
+
// {{ path }} as typed, or \{\{ path \}\} as pandoc escapes it
|
|
13
|
+
const MUSTACHE = /(?:\{\{|\\\{\\\{)\s*([^{}\\]*(?:\\_[^{}\\]*)*?)\s*(?:\}\}|\\\}\\\})/g;
|
|
14
|
+
function lineAt(text, offset) {
|
|
15
|
+
let n = 1;
|
|
16
|
+
for (let i = 0; i < offset; i++)
|
|
17
|
+
if (text.charCodeAt(i) === 10)
|
|
18
|
+
n++;
|
|
19
|
+
return n;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Turn placeholder tokens from older templates or Word documents into
|
|
23
|
+
* `\field{…}`: `@GROUP@FIELD@` (the legacy generator's tokens) and
|
|
24
|
+
* `{{ path }}` (common in Word/Markdown templates, also after pandoc
|
|
25
|
+
* escaped it). Anything that cannot become a valid path is left alone and
|
|
26
|
+
* reported.
|
|
27
|
+
*/
|
|
28
|
+
export function convertPlaceholders(tex) {
|
|
29
|
+
const converted = [];
|
|
30
|
+
const skipped = [];
|
|
31
|
+
let out = tex.replace(LEGACY, (whole, group, field, offset) => {
|
|
32
|
+
const path = `${camelSegment(group)}.${camelSegment(field)}`;
|
|
33
|
+
const item = { from: whole, to: `\\field{${path}}`, line: lineAt(tex, offset) };
|
|
34
|
+
if (!isPath(path)) {
|
|
35
|
+
skipped.push(item);
|
|
36
|
+
return whole;
|
|
37
|
+
}
|
|
38
|
+
converted.push(item);
|
|
39
|
+
return item.to;
|
|
40
|
+
});
|
|
41
|
+
const before = out;
|
|
42
|
+
out = out.replace(MUSTACHE, (whole, inner, offset) => {
|
|
43
|
+
const path = inner.replace(/\\_/g, '_').trim();
|
|
44
|
+
const item = { from: whole, to: `\\field{${path}}`, line: lineAt(before, offset) };
|
|
45
|
+
if (!isPath(path)) {
|
|
46
|
+
skipped.push(item);
|
|
47
|
+
return whole;
|
|
48
|
+
}
|
|
49
|
+
converted.push(item);
|
|
50
|
+
return item.to;
|
|
51
|
+
});
|
|
52
|
+
return { tex: out, converted, skipped };
|
|
53
|
+
}
|
|
54
|
+
/** Make sure a document loads latex-kit.sty, right before \begin{document}. */
|
|
55
|
+
export function ensureLatexKitPackage(tex) {
|
|
56
|
+
if (/\\usepackage\s*(?:\[[^\]]*\])?\s*\{[^}]*\blatex-kit\b[^}]*\}/.test(tex))
|
|
57
|
+
return tex;
|
|
58
|
+
const at = tex.indexOf('\\begin{document}');
|
|
59
|
+
if (at === -1)
|
|
60
|
+
return tex;
|
|
61
|
+
return `${tex.slice(0, at)}\\usepackage{latex-kit}\n${tex.slice(at)}`;
|
|
62
|
+
}
|
package/esm/compile.d.ts
CHANGED
|
@@ -4,7 +4,8 @@ import { FieldSpec } from './filters';
|
|
|
4
4
|
/**
|
|
5
5
|
* The template language's macros and how many {…} arguments each takes.
|
|
6
6
|
* Bare names keep templates readable; \snippet (not \partial, which is the
|
|
7
|
-
* math ∂) and \clauseref (not \ref) avoid LaTeX's own commands.
|
|
7
|
+
* math ∂) and \clauseref (not \ref) avoid LaTeX's own commands. \clause
|
|
8
|
+
* inserts a numbered clause from the clause library; \clauseref prints its number.
|
|
8
9
|
*/
|
|
9
10
|
export declare const MACROS: {
|
|
10
11
|
readonly field: 1;
|
|
@@ -15,6 +16,7 @@ export declare const MACROS: {
|
|
|
15
16
|
readonly sep: 1;
|
|
16
17
|
readonly snippet: 1;
|
|
17
18
|
readonly docid: 0;
|
|
19
|
+
readonly clause: 1;
|
|
18
20
|
readonly clauseref: 1;
|
|
19
21
|
};
|
|
20
22
|
export type MacroName = keyof typeof MACROS;
|
|
@@ -51,6 +53,14 @@ export type Node = {
|
|
|
51
53
|
} | {
|
|
52
54
|
kind: 'docid';
|
|
53
55
|
loc: Location;
|
|
56
|
+
} | {
|
|
57
|
+
kind: 'clause';
|
|
58
|
+
id: string;
|
|
59
|
+
loc: Location;
|
|
60
|
+
} | {
|
|
61
|
+
kind: 'clauseref';
|
|
62
|
+
id: string;
|
|
63
|
+
loc: Location;
|
|
54
64
|
};
|
|
55
65
|
export interface CompiledTemplate {
|
|
56
66
|
file: string;
|
|
@@ -59,4 +69,6 @@ export interface CompiledTemplate {
|
|
|
59
69
|
/** Syntax problems found while compiling. */
|
|
60
70
|
diagnostics: Diagnostic[];
|
|
61
71
|
}
|
|
72
|
+
/** `parts/header`, `nda/confidentiality`: relative, no `..`, no empty segments. */
|
|
73
|
+
export declare function isRelativeName(name: string): boolean;
|
|
62
74
|
export declare function compileTemplate(text: string, file: string): CompiledTemplate;
|
package/esm/compile.js
CHANGED
|
@@ -6,7 +6,8 @@ import { isName, isPath } from './path';
|
|
|
6
6
|
/**
|
|
7
7
|
* The template language's macros and how many {…} arguments each takes.
|
|
8
8
|
* Bare names keep templates readable; \snippet (not \partial, which is the
|
|
9
|
-
* math ∂) and \clauseref (not \ref) avoid LaTeX's own commands.
|
|
9
|
+
* math ∂) and \clauseref (not \ref) avoid LaTeX's own commands. \clause
|
|
10
|
+
* inserts a numbered clause from the clause library; \clauseref prints its number.
|
|
10
11
|
*/
|
|
11
12
|
export const MACROS = {
|
|
12
13
|
field: 1,
|
|
@@ -17,11 +18,16 @@ export const MACROS = {
|
|
|
17
18
|
sep: 1,
|
|
18
19
|
snippet: 1,
|
|
19
20
|
docid: 0,
|
|
21
|
+
clause: 1,
|
|
20
22
|
clauseref: 1,
|
|
21
23
|
};
|
|
22
24
|
const parser = getParser({
|
|
23
25
|
macros: Object.fromEntries(Object.entries(MACROS).map(([name, n]) => [name, { signature: Array(n).fill('m').join(' ') }])),
|
|
24
26
|
});
|
|
27
|
+
/** `parts/header`, `nda/confidentiality`: relative, no `..`, no empty segments. */
|
|
28
|
+
export function isRelativeName(name) {
|
|
29
|
+
return /^[A-Za-z0-9_./-]+$/.test(name) && !name.split('/').some((s) => s === '..' || s === '' || s === '.');
|
|
30
|
+
}
|
|
25
31
|
/** Find our macros with unified-latex, so comments, verbatim and \verb are skipped. */
|
|
26
32
|
function findMacros(text) {
|
|
27
33
|
const out = [];
|
|
@@ -200,14 +206,18 @@ export function compileTemplate(text, file) {
|
|
|
200
206
|
pos = macroEnd;
|
|
201
207
|
break;
|
|
202
208
|
}
|
|
203
|
-
case 'snippet':
|
|
209
|
+
case 'snippet':
|
|
210
|
+
case 'clause': {
|
|
204
211
|
const name = argText(0);
|
|
205
|
-
if (
|
|
206
|
-
diagnostics.push(diag('error',
|
|
212
|
+
if (!isRelativeName(name)) {
|
|
213
|
+
diagnostics.push(diag('error', `\\${occ.name}{${name}}: use a relative name like ${occ.name === 'clause' ? 'nda/confidentiality' : 'parts/header'}`, loc));
|
|
207
214
|
}
|
|
208
|
-
else {
|
|
215
|
+
else if (occ.name === 'snippet') {
|
|
209
216
|
nodes.push({ kind: 'snippet', name, loc });
|
|
210
217
|
}
|
|
218
|
+
else {
|
|
219
|
+
nodes.push({ kind: 'clause', id: name, loc });
|
|
220
|
+
}
|
|
211
221
|
pos = macroEnd;
|
|
212
222
|
break;
|
|
213
223
|
}
|
|
@@ -217,10 +227,15 @@ export function compileTemplate(text, file) {
|
|
|
217
227
|
pos = text.startsWith('{}', macroEnd) ? macroEnd + 2 : macroEnd;
|
|
218
228
|
break;
|
|
219
229
|
}
|
|
220
|
-
case 'clauseref':
|
|
221
|
-
|
|
230
|
+
case 'clauseref': {
|
|
231
|
+
const id = argText(0);
|
|
232
|
+
if (!isRelativeName(id))
|
|
233
|
+
diagnostics.push(diag('error', `\\clauseref{${id}}: use the clause's name, like nda/confidentiality`, loc));
|
|
234
|
+
else
|
|
235
|
+
nodes.push({ kind: 'clauseref', id, loc });
|
|
222
236
|
pos = macroEnd;
|
|
223
237
|
break;
|
|
238
|
+
}
|
|
224
239
|
}
|
|
225
240
|
}
|
|
226
241
|
catch (e) {
|
package/esm/evaluate.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { ClauseUse, LoadedClause } from './clause';
|
|
1
2
|
import { CompiledTemplate } from './compile';
|
|
2
3
|
import { truthy } from './condition';
|
|
3
4
|
import { Diagnostic } from './diagnostics';
|
|
@@ -17,6 +18,8 @@ export interface EvaluateOptions {
|
|
|
17
18
|
allowMissing?: boolean;
|
|
18
19
|
/** Resolve \snippet{name} from the file that uses it. */
|
|
19
20
|
loadSnippet?: (name: string, from: string) => CompiledTemplate | null;
|
|
21
|
+
/** Resolve \clause{id} from the clause library. */
|
|
22
|
+
loadClause?: (id: string) => LoadedClause | null;
|
|
20
23
|
}
|
|
21
24
|
export interface FieldUse {
|
|
22
25
|
path: string;
|
|
@@ -30,6 +33,8 @@ export interface EvaluateResult {
|
|
|
30
33
|
diagnostics: Diagnostic[];
|
|
31
34
|
/** Every field that was filled (or found missing), in document order. */
|
|
32
35
|
fields: FieldUse[];
|
|
36
|
+
/** Library clauses placed in the document, in order, with versions. */
|
|
37
|
+
clauses: ClauseUse[];
|
|
33
38
|
}
|
|
34
39
|
export declare function evaluateTemplate(template: CompiledTemplate, options: EvaluateOptions): EvaluateResult;
|
|
35
40
|
export { truthy };
|
package/esm/evaluate.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { escapeLatex, LatexEscapeError, RawLatex } from '@latex-kit/escape';
|
|
2
|
+
import { clauseLabel } from './clause';
|
|
2
3
|
import { evaluateCondition, truthy } from './condition';
|
|
3
4
|
import { diag } from './diagnostics';
|
|
4
5
|
import { applyFilters, FilterError, isMissing } from './filters';
|
|
@@ -22,6 +23,8 @@ export function evaluateTemplate(template, options) {
|
|
|
22
23
|
const fields = [];
|
|
23
24
|
const frames = [];
|
|
24
25
|
const stack = [template.file];
|
|
26
|
+
const clauses = [];
|
|
27
|
+
const refs = [];
|
|
25
28
|
const lookup = (path) => {
|
|
26
29
|
const [head, ...rest] = splitPath(path);
|
|
27
30
|
let value;
|
|
@@ -189,11 +192,44 @@ export function evaluateTemplate(template, options) {
|
|
|
189
192
|
stack.pop();
|
|
190
193
|
break;
|
|
191
194
|
}
|
|
195
|
+
case 'clause': {
|
|
196
|
+
const clause = options.loadClause?.(node.id) ?? null;
|
|
197
|
+
if (!clause) {
|
|
198
|
+
diagnostics.push(diag('error', `clause not found: ${node.id}`, node.loc));
|
|
199
|
+
break;
|
|
200
|
+
}
|
|
201
|
+
if (stack.includes(clause.template.file)) {
|
|
202
|
+
diagnostics.push(diag('error', `clause ${node.id} includes itself`, node.loc));
|
|
203
|
+
break;
|
|
204
|
+
}
|
|
205
|
+
if (clauses.some((c) => c.id === node.id)) {
|
|
206
|
+
diagnostics.push(diag('error', `clause ${node.id} is placed twice`, node.loc));
|
|
207
|
+
break;
|
|
208
|
+
}
|
|
209
|
+
clauses.push({ id: node.id, version: clause.version, file: clause.template.file });
|
|
210
|
+
diagnostics.push(...clause.template.diagnostics);
|
|
211
|
+
out += `\\lkclause{${clause.title ? escapeLatex(clause.title) : ''}}{${clauseLabel(node.id)}}`;
|
|
212
|
+
stack.push(clause.template.file);
|
|
213
|
+
out += render(clause.template.nodes);
|
|
214
|
+
stack.pop();
|
|
215
|
+
break;
|
|
216
|
+
}
|
|
217
|
+
case 'clauseref':
|
|
218
|
+
refs.push({ id: node.id, loc: node.loc });
|
|
219
|
+
out += `\\ref{${clauseLabel(node.id)}}`;
|
|
220
|
+
break;
|
|
192
221
|
}
|
|
193
222
|
}
|
|
194
223
|
return out;
|
|
195
224
|
};
|
|
196
225
|
const tex = render(template.nodes);
|
|
197
|
-
|
|
226
|
+
// a reference to a clause this document does not contain would print "??"
|
|
227
|
+
for (const r of refs) {
|
|
228
|
+
if (!clauses.some((c) => c.id === r.id)) {
|
|
229
|
+
const msg = `\\clauseref{${r.id}}: clause ${r.id} is not in this document`;
|
|
230
|
+
diagnostics.push(diag(allowMissing ? 'warning' : 'error', msg, r.loc));
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
return { tex, diagnostics, fields, clauses };
|
|
198
234
|
}
|
|
199
235
|
export { truthy };
|
package/esm/index.d.ts
CHANGED
package/esm/index.js
CHANGED
package/esm/sty.d.ts
CHANGED
|
@@ -3,4 +3,4 @@
|
|
|
3
3
|
* every generated document; `latex-kit sty <dir>` writes it for previewing
|
|
4
4
|
* templates in an editor.
|
|
5
5
|
*/
|
|
6
|
-
export declare const LATEX_KIT_STY = "% latex-kit.sty: support for latex-kit templates.\n%\n% Load it in a template with \\usepackage{latex-kit}. Compiled directly, a\n% template previews as a blank form: every \\field becomes a labelled line.\n% When latex-kit renders a document it passes the `rendered` option and the\n% variant (draft, blank, final, signed); the template macros are gone by then.\n%\n% https://github.com/constructive-io/latex-kit (MIT)\n\\NeedsTeXFormat{LaTeX2e}\n\\ProvidesPackage{latex-kit}[2026/11/25 v0.3 latex-kit template support]\n\n\\newif\\iflk@rendered\n\\newif\\iflk@draft\n\\DeclareOption{rendered}{\\lk@renderedtrue}\n\\DeclareOption{draft}{\\lk@drafttrue}\n\\DeclareOption{blank}{}\n\\DeclareOption{final}{}\n\\DeclareOption{signed}{}\n\\ProcessOptions\\relax\n\n\\RequirePackage{xcolor}\n\\definecolor{lkmeta}{gray}{0.55}\n\\providecommand\\lkstamp{}\n\n% A labelled blank line: \\lkblank{party.name}\n\\newlength\\lkblankwidth\n\\setlength\\lkblankwidth{3.5cm}\n\\newcommand\\lkblank[1]{\\leavevmode\\underline{\\makebox[\\lkblankwidth][l]{\\color{lkmeta}\\tiny\\ttfamily\\detokenize{#1}}}}\n\n% A value the data does not have yet (draft builds only).\n\\newcommand\\lkmissing[1]{\\leavevmode{\\color{red}\\fbox{\\scriptsize\\ttfamily missing: \\detokenize{#1}}}}\n\n\\iflk@draft\n \\RequirePackage{draftwatermark}\n \\SetWatermarkText{DRAFT}\n \\SetWatermarkScale{1}\n \\SetWatermarkColor[gray]{0.9}\n \\RequirePackage{eso-pic}\n \\AddToShipoutPictureFG{%\n \\AtPageLowerLeft{\\raisebox{6mm}{\\hspace{10mm}\\color{lkmeta}\\scriptsize\\ttfamily DRAFT \\lkstamp}}}\n\\fi\n\n\\iflk@rendered\\else\n % Previewing the template itself: show its structure.\n \\newcommand\\field[1]{\\lkblank{#1}}\n \\newcommand\\raw[1]{\\lkblank{#1}}\n \\newcommand\\docid{\\lkblank{docid}}\n \\newcommand\\when[2]{{\\color{lkmeta}\\tiny[if \\detokenize{#1}]}#2}\n \\newcommand\\otherwise[1]{{\\color{lkmeta}\\tiny[otherwise]}#1}\n \\newcommand\\each[3]{{\\color{lkmeta}\\tiny[each \\detokenize{#2} in \\detokenize{#1}]}#3}\n \\newcommand\\sep[1]{#1}\n \\newcommand\\snippet[1]{\\InputIfFileExists{#1}{}{\\lkblank{snippet #1}}}\n \\newcommand\\
|
|
6
|
+
export declare const LATEX_KIT_STY = "% latex-kit.sty: support for latex-kit templates.\n%\n% Load it in a template with \\usepackage{latex-kit}. Compiled directly, a\n% template previews as a blank form: every \\field becomes a labelled line.\n% When latex-kit renders a document it passes the `rendered` option and the\n% variant (draft, blank, final, signed); the template macros are gone by then.\n%\n% https://github.com/constructive-io/latex-kit (MIT)\n\\NeedsTeXFormat{LaTeX2e}\n\\ProvidesPackage{latex-kit}[2026/11/25 v0.3 latex-kit template support]\n\n\\newif\\iflk@rendered\n\\newif\\iflk@draft\n\\DeclareOption{rendered}{\\lk@renderedtrue}\n\\DeclareOption{draft}{\\lk@drafttrue}\n\\DeclareOption{blank}{}\n\\DeclareOption{final}{}\n\\DeclareOption{signed}{}\n\\ProcessOptions\\relax\n\n\\RequirePackage{xcolor}\n\\definecolor{lkmeta}{gray}{0.55}\n\\providecommand\\lkstamp{}\n\n% A labelled blank line: \\lkblank{party.name}\n\\newlength\\lkblankwidth\n\\setlength\\lkblankwidth{3.5cm}\n\\newcommand\\lkblank[1]{\\leavevmode\\underline{\\makebox[\\lkblankwidth][l]{\\color{lkmeta}\\tiny\\ttfamily\\detokenize{#1}}}}\n\n% A clause from the library: \\lkclause{title}{label}. Numbered like a\n% section; with no title, a bold number. Redefine it to change the style.\n\\providecommand\\lkclause[2]{\\ifx\\relax#1\\relax\\par\\refstepcounter{section}\\label{#2}\\noindent\\textbf{\\thesection.}~\\else\\section{#1}\\label{#2}\\fi}\n\n% A value the data does not have yet (draft builds only).\n\\newcommand\\lkmissing[1]{\\leavevmode{\\color{red}\\fbox{\\scriptsize\\ttfamily missing: \\detokenize{#1}}}}\n\n\\iflk@draft\n \\RequirePackage{draftwatermark}\n \\SetWatermarkText{DRAFT}\n \\SetWatermarkScale{1}\n \\SetWatermarkColor[gray]{0.9}\n \\RequirePackage{eso-pic}\n \\AddToShipoutPictureFG{%\n \\AtPageLowerLeft{\\raisebox{6mm}{\\hspace{10mm}\\color{lkmeta}\\scriptsize\\ttfamily DRAFT \\lkstamp}}}\n\\fi\n\n\\iflk@rendered\\else\n % Previewing the template itself: show its structure.\n \\newcommand\\field[1]{\\lkblank{#1}}\n \\newcommand\\raw[1]{\\lkblank{#1}}\n \\newcommand\\docid{\\lkblank{docid}}\n \\newcommand\\when[2]{{\\color{lkmeta}\\tiny[if \\detokenize{#1}]}#2}\n \\newcommand\\otherwise[1]{{\\color{lkmeta}\\tiny[otherwise]}#1}\n \\newcommand\\each[3]{{\\color{lkmeta}\\tiny[each \\detokenize{#2} in \\detokenize{#1}]}#3}\n \\newcommand\\sep[1]{#1}\n \\newcommand\\snippet[1]{\\InputIfFileExists{#1}{}{\\lkblank{snippet #1}}}\n \\newcommand\\clause[1]{\\par\\lkblank{clause #1}\\par}\n \\newcommand\\clauseref[1]{\\lkblank{ref #1}}\n\\fi\n\n\\endinput\n";
|
package/esm/sty.js
CHANGED
|
@@ -32,6 +32,10 @@ export const LATEX_KIT_STY = `% latex-kit.sty: support for latex-kit templates.
|
|
|
32
32
|
\\setlength\\lkblankwidth{3.5cm}
|
|
33
33
|
\\newcommand\\lkblank[1]{\\leavevmode\\underline{\\makebox[\\lkblankwidth][l]{\\color{lkmeta}\\tiny\\ttfamily\\detokenize{#1}}}}
|
|
34
34
|
|
|
35
|
+
% A clause from the library: \\lkclause{title}{label}. Numbered like a
|
|
36
|
+
% section; with no title, a bold number. Redefine it to change the style.
|
|
37
|
+
\\providecommand\\lkclause[2]{\\ifx\\relax#1\\relax\\par\\refstepcounter{section}\\label{#2}\\noindent\\textbf{\\thesection.}~\\else\\section{#1}\\label{#2}\\fi}
|
|
38
|
+
|
|
35
39
|
% A value the data does not have yet (draft builds only).
|
|
36
40
|
\\newcommand\\lkmissing[1]{\\leavevmode{\\color{red}\\fbox{\\scriptsize\\ttfamily missing: \\detokenize{#1}}}}
|
|
37
41
|
|
|
@@ -55,7 +59,8 @@ export const LATEX_KIT_STY = `% latex-kit.sty: support for latex-kit templates.
|
|
|
55
59
|
\\newcommand\\each[3]{{\\color{lkmeta}\\tiny[each \\detokenize{#2} in \\detokenize{#1}]}#3}
|
|
56
60
|
\\newcommand\\sep[1]{#1}
|
|
57
61
|
\\newcommand\\snippet[1]{\\InputIfFileExists{#1}{}{\\lkblank{snippet #1}}}
|
|
58
|
-
\\newcommand\\
|
|
62
|
+
\\newcommand\\clause[1]{\\par\\lkblank{clause #1}\\par}
|
|
63
|
+
\\newcommand\\clauseref[1]{\\lkblank{ref #1}}
|
|
59
64
|
\\fi
|
|
60
65
|
|
|
61
66
|
\\endinput
|
package/evaluate.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { ClauseUse, LoadedClause } from './clause';
|
|
1
2
|
import { CompiledTemplate } from './compile';
|
|
2
3
|
import { truthy } from './condition';
|
|
3
4
|
import { Diagnostic } from './diagnostics';
|
|
@@ -17,6 +18,8 @@ export interface EvaluateOptions {
|
|
|
17
18
|
allowMissing?: boolean;
|
|
18
19
|
/** Resolve \snippet{name} from the file that uses it. */
|
|
19
20
|
loadSnippet?: (name: string, from: string) => CompiledTemplate | null;
|
|
21
|
+
/** Resolve \clause{id} from the clause library. */
|
|
22
|
+
loadClause?: (id: string) => LoadedClause | null;
|
|
20
23
|
}
|
|
21
24
|
export interface FieldUse {
|
|
22
25
|
path: string;
|
|
@@ -30,6 +33,8 @@ export interface EvaluateResult {
|
|
|
30
33
|
diagnostics: Diagnostic[];
|
|
31
34
|
/** Every field that was filled (or found missing), in document order. */
|
|
32
35
|
fields: FieldUse[];
|
|
36
|
+
/** Library clauses placed in the document, in order, with versions. */
|
|
37
|
+
clauses: ClauseUse[];
|
|
33
38
|
}
|
|
34
39
|
export declare function evaluateTemplate(template: CompiledTemplate, options: EvaluateOptions): EvaluateResult;
|
|
35
40
|
export { truthy };
|
package/evaluate.js
CHANGED
|
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.truthy = exports.VARIANTS = void 0;
|
|
4
4
|
exports.evaluateTemplate = evaluateTemplate;
|
|
5
5
|
const escape_1 = require("@latex-kit/escape");
|
|
6
|
+
const clause_1 = require("./clause");
|
|
6
7
|
const condition_1 = require("./condition");
|
|
7
8
|
Object.defineProperty(exports, "truthy", { enumerable: true, get: function () { return condition_1.truthy; } });
|
|
8
9
|
const diagnostics_1 = require("./diagnostics");
|
|
@@ -27,6 +28,8 @@ function evaluateTemplate(template, options) {
|
|
|
27
28
|
const fields = [];
|
|
28
29
|
const frames = [];
|
|
29
30
|
const stack = [template.file];
|
|
31
|
+
const clauses = [];
|
|
32
|
+
const refs = [];
|
|
30
33
|
const lookup = (path) => {
|
|
31
34
|
const [head, ...rest] = (0, path_1.splitPath)(path);
|
|
32
35
|
let value;
|
|
@@ -194,10 +197,43 @@ function evaluateTemplate(template, options) {
|
|
|
194
197
|
stack.pop();
|
|
195
198
|
break;
|
|
196
199
|
}
|
|
200
|
+
case 'clause': {
|
|
201
|
+
const clause = options.loadClause?.(node.id) ?? null;
|
|
202
|
+
if (!clause) {
|
|
203
|
+
diagnostics.push((0, diagnostics_1.diag)('error', `clause not found: ${node.id}`, node.loc));
|
|
204
|
+
break;
|
|
205
|
+
}
|
|
206
|
+
if (stack.includes(clause.template.file)) {
|
|
207
|
+
diagnostics.push((0, diagnostics_1.diag)('error', `clause ${node.id} includes itself`, node.loc));
|
|
208
|
+
break;
|
|
209
|
+
}
|
|
210
|
+
if (clauses.some((c) => c.id === node.id)) {
|
|
211
|
+
diagnostics.push((0, diagnostics_1.diag)('error', `clause ${node.id} is placed twice`, node.loc));
|
|
212
|
+
break;
|
|
213
|
+
}
|
|
214
|
+
clauses.push({ id: node.id, version: clause.version, file: clause.template.file });
|
|
215
|
+
diagnostics.push(...clause.template.diagnostics);
|
|
216
|
+
out += `\\lkclause{${clause.title ? (0, escape_1.escapeLatex)(clause.title) : ''}}{${(0, clause_1.clauseLabel)(node.id)}}`;
|
|
217
|
+
stack.push(clause.template.file);
|
|
218
|
+
out += render(clause.template.nodes);
|
|
219
|
+
stack.pop();
|
|
220
|
+
break;
|
|
221
|
+
}
|
|
222
|
+
case 'clauseref':
|
|
223
|
+
refs.push({ id: node.id, loc: node.loc });
|
|
224
|
+
out += `\\ref{${(0, clause_1.clauseLabel)(node.id)}}`;
|
|
225
|
+
break;
|
|
197
226
|
}
|
|
198
227
|
}
|
|
199
228
|
return out;
|
|
200
229
|
};
|
|
201
230
|
const tex = render(template.nodes);
|
|
202
|
-
|
|
231
|
+
// a reference to a clause this document does not contain would print "??"
|
|
232
|
+
for (const r of refs) {
|
|
233
|
+
if (!clauses.some((c) => c.id === r.id)) {
|
|
234
|
+
const msg = `\\clauseref{${r.id}}: clause ${r.id} is not in this document`;
|
|
235
|
+
diagnostics.push((0, diagnostics_1.diag)(allowMissing ? 'warning' : 'error', msg, r.loc));
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
return { tex, diagnostics, fields, clauses };
|
|
203
239
|
}
|
package/index.d.ts
CHANGED
package/index.js
CHANGED
|
@@ -15,6 +15,8 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
__exportStar(require("./analyze"), exports);
|
|
18
|
+
__exportStar(require("./clause"), exports);
|
|
19
|
+
__exportStar(require("./codemod"), exports);
|
|
18
20
|
__exportStar(require("./compile"), exports);
|
|
19
21
|
__exportStar(require("./condition"), exports);
|
|
20
22
|
__exportStar(require("./diagnostics"), exports);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@latex-kit/template",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "The latex-kit template language: fill-in macros inside real .tex files, checked and escaped",
|
|
5
5
|
"author": "Constructive <developers@constructive.io>",
|
|
6
6
|
"main": "index.js",
|
|
@@ -42,5 +42,5 @@
|
|
|
42
42
|
"@latex-kit/escape": "0.3.0",
|
|
43
43
|
"@unified-latex/unified-latex-util-parse": "^1.8.4"
|
|
44
44
|
},
|
|
45
|
-
"gitHead": "
|
|
45
|
+
"gitHead": "023ede48131a32e9250ec48b0dde17e71c6a1536"
|
|
46
46
|
}
|
package/sty.d.ts
CHANGED
|
@@ -3,4 +3,4 @@
|
|
|
3
3
|
* every generated document; `latex-kit sty <dir>` writes it for previewing
|
|
4
4
|
* templates in an editor.
|
|
5
5
|
*/
|
|
6
|
-
export declare const LATEX_KIT_STY = "% latex-kit.sty: support for latex-kit templates.\n%\n% Load it in a template with \\usepackage{latex-kit}. Compiled directly, a\n% template previews as a blank form: every \\field becomes a labelled line.\n% When latex-kit renders a document it passes the `rendered` option and the\n% variant (draft, blank, final, signed); the template macros are gone by then.\n%\n% https://github.com/constructive-io/latex-kit (MIT)\n\\NeedsTeXFormat{LaTeX2e}\n\\ProvidesPackage{latex-kit}[2026/11/25 v0.3 latex-kit template support]\n\n\\newif\\iflk@rendered\n\\newif\\iflk@draft\n\\DeclareOption{rendered}{\\lk@renderedtrue}\n\\DeclareOption{draft}{\\lk@drafttrue}\n\\DeclareOption{blank}{}\n\\DeclareOption{final}{}\n\\DeclareOption{signed}{}\n\\ProcessOptions\\relax\n\n\\RequirePackage{xcolor}\n\\definecolor{lkmeta}{gray}{0.55}\n\\providecommand\\lkstamp{}\n\n% A labelled blank line: \\lkblank{party.name}\n\\newlength\\lkblankwidth\n\\setlength\\lkblankwidth{3.5cm}\n\\newcommand\\lkblank[1]{\\leavevmode\\underline{\\makebox[\\lkblankwidth][l]{\\color{lkmeta}\\tiny\\ttfamily\\detokenize{#1}}}}\n\n% A value the data does not have yet (draft builds only).\n\\newcommand\\lkmissing[1]{\\leavevmode{\\color{red}\\fbox{\\scriptsize\\ttfamily missing: \\detokenize{#1}}}}\n\n\\iflk@draft\n \\RequirePackage{draftwatermark}\n \\SetWatermarkText{DRAFT}\n \\SetWatermarkScale{1}\n \\SetWatermarkColor[gray]{0.9}\n \\RequirePackage{eso-pic}\n \\AddToShipoutPictureFG{%\n \\AtPageLowerLeft{\\raisebox{6mm}{\\hspace{10mm}\\color{lkmeta}\\scriptsize\\ttfamily DRAFT \\lkstamp}}}\n\\fi\n\n\\iflk@rendered\\else\n % Previewing the template itself: show its structure.\n \\newcommand\\field[1]{\\lkblank{#1}}\n \\newcommand\\raw[1]{\\lkblank{#1}}\n \\newcommand\\docid{\\lkblank{docid}}\n \\newcommand\\when[2]{{\\color{lkmeta}\\tiny[if \\detokenize{#1}]}#2}\n \\newcommand\\otherwise[1]{{\\color{lkmeta}\\tiny[otherwise]}#1}\n \\newcommand\\each[3]{{\\color{lkmeta}\\tiny[each \\detokenize{#2} in \\detokenize{#1}]}#3}\n \\newcommand\\sep[1]{#1}\n \\newcommand\\snippet[1]{\\InputIfFileExists{#1}{}{\\lkblank{snippet #1}}}\n \\newcommand\\
|
|
6
|
+
export declare const LATEX_KIT_STY = "% latex-kit.sty: support for latex-kit templates.\n%\n% Load it in a template with \\usepackage{latex-kit}. Compiled directly, a\n% template previews as a blank form: every \\field becomes a labelled line.\n% When latex-kit renders a document it passes the `rendered` option and the\n% variant (draft, blank, final, signed); the template macros are gone by then.\n%\n% https://github.com/constructive-io/latex-kit (MIT)\n\\NeedsTeXFormat{LaTeX2e}\n\\ProvidesPackage{latex-kit}[2026/11/25 v0.3 latex-kit template support]\n\n\\newif\\iflk@rendered\n\\newif\\iflk@draft\n\\DeclareOption{rendered}{\\lk@renderedtrue}\n\\DeclareOption{draft}{\\lk@drafttrue}\n\\DeclareOption{blank}{}\n\\DeclareOption{final}{}\n\\DeclareOption{signed}{}\n\\ProcessOptions\\relax\n\n\\RequirePackage{xcolor}\n\\definecolor{lkmeta}{gray}{0.55}\n\\providecommand\\lkstamp{}\n\n% A labelled blank line: \\lkblank{party.name}\n\\newlength\\lkblankwidth\n\\setlength\\lkblankwidth{3.5cm}\n\\newcommand\\lkblank[1]{\\leavevmode\\underline{\\makebox[\\lkblankwidth][l]{\\color{lkmeta}\\tiny\\ttfamily\\detokenize{#1}}}}\n\n% A clause from the library: \\lkclause{title}{label}. Numbered like a\n% section; with no title, a bold number. Redefine it to change the style.\n\\providecommand\\lkclause[2]{\\ifx\\relax#1\\relax\\par\\refstepcounter{section}\\label{#2}\\noindent\\textbf{\\thesection.}~\\else\\section{#1}\\label{#2}\\fi}\n\n% A value the data does not have yet (draft builds only).\n\\newcommand\\lkmissing[1]{\\leavevmode{\\color{red}\\fbox{\\scriptsize\\ttfamily missing: \\detokenize{#1}}}}\n\n\\iflk@draft\n \\RequirePackage{draftwatermark}\n \\SetWatermarkText{DRAFT}\n \\SetWatermarkScale{1}\n \\SetWatermarkColor[gray]{0.9}\n \\RequirePackage{eso-pic}\n \\AddToShipoutPictureFG{%\n \\AtPageLowerLeft{\\raisebox{6mm}{\\hspace{10mm}\\color{lkmeta}\\scriptsize\\ttfamily DRAFT \\lkstamp}}}\n\\fi\n\n\\iflk@rendered\\else\n % Previewing the template itself: show its structure.\n \\newcommand\\field[1]{\\lkblank{#1}}\n \\newcommand\\raw[1]{\\lkblank{#1}}\n \\newcommand\\docid{\\lkblank{docid}}\n \\newcommand\\when[2]{{\\color{lkmeta}\\tiny[if \\detokenize{#1}]}#2}\n \\newcommand\\otherwise[1]{{\\color{lkmeta}\\tiny[otherwise]}#1}\n \\newcommand\\each[3]{{\\color{lkmeta}\\tiny[each \\detokenize{#2} in \\detokenize{#1}]}#3}\n \\newcommand\\sep[1]{#1}\n \\newcommand\\snippet[1]{\\InputIfFileExists{#1}{}{\\lkblank{snippet #1}}}\n \\newcommand\\clause[1]{\\par\\lkblank{clause #1}\\par}\n \\newcommand\\clauseref[1]{\\lkblank{ref #1}}\n\\fi\n\n\\endinput\n";
|
package/sty.js
CHANGED
|
@@ -35,6 +35,10 @@ exports.LATEX_KIT_STY = `% latex-kit.sty: support for latex-kit templates.
|
|
|
35
35
|
\\setlength\\lkblankwidth{3.5cm}
|
|
36
36
|
\\newcommand\\lkblank[1]{\\leavevmode\\underline{\\makebox[\\lkblankwidth][l]{\\color{lkmeta}\\tiny\\ttfamily\\detokenize{#1}}}}
|
|
37
37
|
|
|
38
|
+
% A clause from the library: \\lkclause{title}{label}. Numbered like a
|
|
39
|
+
% section; with no title, a bold number. Redefine it to change the style.
|
|
40
|
+
\\providecommand\\lkclause[2]{\\ifx\\relax#1\\relax\\par\\refstepcounter{section}\\label{#2}\\noindent\\textbf{\\thesection.}~\\else\\section{#1}\\label{#2}\\fi}
|
|
41
|
+
|
|
38
42
|
% A value the data does not have yet (draft builds only).
|
|
39
43
|
\\newcommand\\lkmissing[1]{\\leavevmode{\\color{red}\\fbox{\\scriptsize\\ttfamily missing: \\detokenize{#1}}}}
|
|
40
44
|
|
|
@@ -58,7 +62,8 @@ exports.LATEX_KIT_STY = `% latex-kit.sty: support for latex-kit templates.
|
|
|
58
62
|
\\newcommand\\each[3]{{\\color{lkmeta}\\tiny[each \\detokenize{#2} in \\detokenize{#1}]}#3}
|
|
59
63
|
\\newcommand\\sep[1]{#1}
|
|
60
64
|
\\newcommand\\snippet[1]{\\InputIfFileExists{#1}{}{\\lkblank{snippet #1}}}
|
|
61
|
-
\\newcommand\\
|
|
65
|
+
\\newcommand\\clause[1]{\\par\\lkblank{clause #1}\\par}
|
|
66
|
+
\\newcommand\\clauseref[1]{\\lkblank{ref #1}}
|
|
62
67
|
\\fi
|
|
63
68
|
|
|
64
69
|
\\endinput
|