@latex-kit/template 0.3.0 → 0.4.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 +30 -2
- package/analyze.d.ts +6 -2
- package/analyze.js +22 -1
- package/clause.d.ts +4 -0
- package/compile.d.ts +13 -0
- package/compile.js +25 -0
- package/component.d.ts +29 -0
- package/component.js +8 -0
- package/esm/analyze.d.ts +6 -2
- package/esm/analyze.js +22 -1
- package/esm/clause.d.ts +4 -0
- package/esm/compile.d.ts +13 -0
- package/esm/compile.js +25 -0
- package/esm/component.d.ts +29 -0
- package/esm/component.js +5 -0
- package/esm/evaluate.d.ts +16 -0
- package/esm/evaluate.js +79 -5
- package/esm/index.d.ts +2 -0
- package/esm/index.js +2 -0
- package/esm/signature.d.ts +36 -0
- package/esm/signature.js +54 -0
- package/esm/sty.d.ts +1 -1
- package/esm/sty.js +33 -1
- package/evaluate.d.ts +16 -0
- package/evaluate.js +78 -4
- package/index.d.ts +2 -0
- package/index.js +2 -0
- package/package.json +2 -2
- package/signature.d.ts +36 -0
- package/signature.js +59 -0
- package/sty.d.ts +1 -1
- package/sty.js +33 -1
package/README.md
CHANGED
|
@@ -53,6 +53,8 @@ Document ID: \docid
|
|
|
53
53
|
| `\docid` | The document's ID, minted once and stored on the record |
|
|
54
54
|
| `\clause{id}` | Places a numbered clause from the library (`clauses/<id>.tex`), filled with the same data |
|
|
55
55
|
| `\clauseref{id}` | Prints that clause's number. A reference to a clause the document doesn't contain fails a final build |
|
|
56
|
+
| `\signature{party}` | A signature block for a party or signer (see below) |
|
|
57
|
+
| `\component{name}{path}` | Calls a TypeScript component with the value at `path` (empty: the whole record) |
|
|
56
58
|
|
|
57
59
|
Special values: `$variant`, `$locale`, `$docid`, `$template`.
|
|
58
60
|
|
|
@@ -72,7 +74,33 @@ subject to Section~\clauseref{nda/exclusions}.
|
|
|
72
74
|
|
|
73
75
|
Each placed clause becomes `\lkclause{title}{label}` (a numbered section by
|
|
74
76
|
default; redefine `\lkclause` to change the style). Every build's manifest
|
|
75
|
-
lists the clause IDs and versions it used.
|
|
77
|
+
lists the clause IDs and versions it used. A clause can also be TypeScript
|
|
78
|
+
(`defineClause` in [`@latex-kit/components`](../components)): a template
|
|
79
|
+
text `body`, or a `render(data, ctx)` function.
|
|
80
|
+
|
|
81
|
+
### Signatures
|
|
82
|
+
|
|
83
|
+
`\signature{discloser}` prints `\lksignature{org}{name}{title}{signature}{date}`.
|
|
84
|
+
A party with a `signer` (or `kind: "organization"`) is an organization: its
|
|
85
|
+
name in bold, then the signer. Anything else with a `name` signs for itself.
|
|
86
|
+
|
|
87
|
+
| Variant | Signature line | Date line |
|
|
88
|
+
|---|---|---|
|
|
89
|
+
| `draft`, `final` | empty | empty |
|
|
90
|
+
| `blank` | empty; organization, name and title are labelled blanks | empty |
|
|
91
|
+
| `signed` | the signer's image (`signatureImage` option), else `/s/ Name` | the date from `signed: { discloser: { date } }` |
|
|
92
|
+
|
|
93
|
+
Two blocks fit side by side: `\signature{a}\hfill\signature{b}`. Change the
|
|
94
|
+
look with `\lksignaturewidth`, `\lksignatureheight`, or by redefining
|
|
95
|
+
`\lksignature`.
|
|
96
|
+
|
|
97
|
+
### Components
|
|
98
|
+
|
|
99
|
+
`\component{name}{path}` calls a function from the `components` option with
|
|
100
|
+
the value at `path` and a context (variant, locale, `format()`,
|
|
101
|
+
`signature()`, `missing()`). It returns `tex`-tagged LaTeX, inserted as is,
|
|
102
|
+
or a string, which is escaped. Errors are reported at the macro. The
|
|
103
|
+
built-in components and helpers are in [`@latex-kit/components`](../components).
|
|
76
104
|
|
|
77
105
|
### Conditions
|
|
78
106
|
|
|
@@ -109,7 +137,7 @@ Dates, numbers and money follow the record's locale (`ja-JP`, `de-DE`, …).
|
|
|
109
137
|
| `draft` | filled; a missing value prints a red `missing: path` box | DRAFT watermark, date and git commit |
|
|
110
138
|
| `blank` | every field is a labelled line | none |
|
|
111
139
|
| `final` | filled; a missing value is an error | none |
|
|
112
|
-
| `signed` | like `final`;
|
|
140
|
+
| `signed` | like `final`; `\signature` blocks are signed | none |
|
|
113
141
|
|
|
114
142
|
## Usage
|
|
115
143
|
|
package/analyze.d.ts
CHANGED
|
@@ -3,8 +3,8 @@ import { CompiledTemplate } from './compile';
|
|
|
3
3
|
import { Diagnostic } from './diagnostics';
|
|
4
4
|
import { JsonSchema } from './jsonschema';
|
|
5
5
|
export interface FieldRef {
|
|
6
|
-
/** How the template reads it
|
|
7
|
-
kind: 'field' | 'raw' | 'when' | 'each';
|
|
6
|
+
/** How the template reads it. */
|
|
7
|
+
kind: 'field' | 'raw' | 'when' | 'each' | 'signature' | 'component';
|
|
8
8
|
/** The path as written, e.g. `item.price`. */
|
|
9
9
|
path: string;
|
|
10
10
|
/** The path from the data root, e.g. `items[].price`. */
|
|
@@ -19,6 +19,8 @@ export interface AnalyzeOptions {
|
|
|
19
19
|
schema?: JsonSchema;
|
|
20
20
|
loadSnippet?: (name: string, from: string) => CompiledTemplate | null;
|
|
21
21
|
loadClause?: (id: string) => LoadedClause | null;
|
|
22
|
+
/** Names \component can use. Without it component names are not checked. */
|
|
23
|
+
components?: string[];
|
|
22
24
|
}
|
|
23
25
|
export interface AnalyzeResult {
|
|
24
26
|
fields: FieldRef[];
|
|
@@ -29,6 +31,8 @@ export interface AnalyzeResult {
|
|
|
29
31
|
version?: string;
|
|
30
32
|
file: string;
|
|
31
33
|
}[];
|
|
34
|
+
/** Components the template calls. */
|
|
35
|
+
components: string[];
|
|
32
36
|
diagnostics: Diagnostic[];
|
|
33
37
|
}
|
|
34
38
|
/**
|
package/analyze.js
CHANGED
|
@@ -15,6 +15,7 @@ function analyzeTemplate(template, options = {}) {
|
|
|
15
15
|
const fields = [];
|
|
16
16
|
const snippets = [];
|
|
17
17
|
const clauses = [];
|
|
18
|
+
const components = [];
|
|
18
19
|
const refs = [];
|
|
19
20
|
const stack = [template.file];
|
|
20
21
|
const resolve = (path, scopes, loc) => {
|
|
@@ -131,6 +132,26 @@ function analyzeTemplate(template, options = {}) {
|
|
|
131
132
|
case 'clauseref':
|
|
132
133
|
refs.push({ id: node.id, loc: node.loc });
|
|
133
134
|
break;
|
|
135
|
+
case 'signature': {
|
|
136
|
+
const { absolute, schema } = resolve(node.path, scopes, node.loc);
|
|
137
|
+
fields.push({ kind: 'signature', path: node.path, absolute, filters: [], ...node.loc });
|
|
138
|
+
if (root && schema !== undefined && (0, jsonschema_1.schemaAt)(schema, ['name'], root) === undefined) {
|
|
139
|
+
diagnostics.push((0, diagnostics_1.diag)('error', `\\signature{${node.path}}: ${absolute} has no name in the schema, so it is not a party or signer`, node.loc));
|
|
140
|
+
}
|
|
141
|
+
break;
|
|
142
|
+
}
|
|
143
|
+
case 'component': {
|
|
144
|
+
if (!components.includes(node.name))
|
|
145
|
+
components.push(node.name);
|
|
146
|
+
if (options.components && !options.components.includes(node.name)) {
|
|
147
|
+
diagnostics.push((0, diagnostics_1.diag)('error', `unknown component ${node.name} (known: ${[...options.components].sort().join(', ') || 'none'})`, node.loc));
|
|
148
|
+
}
|
|
149
|
+
if (node.path) {
|
|
150
|
+
const { absolute } = resolve(node.path, scopes, node.loc);
|
|
151
|
+
fields.push({ kind: 'component', path: node.path, absolute, filters: [node.name], ...node.loc });
|
|
152
|
+
}
|
|
153
|
+
break;
|
|
154
|
+
}
|
|
134
155
|
}
|
|
135
156
|
}
|
|
136
157
|
};
|
|
@@ -140,5 +161,5 @@ function analyzeTemplate(template, options = {}) {
|
|
|
140
161
|
diagnostics.push((0, diagnostics_1.diag)('error', `\\clauseref{${r.id}}: the template never places \\clause{${r.id}}`, r.loc));
|
|
141
162
|
}
|
|
142
163
|
}
|
|
143
|
-
return { fields, snippets, clauses, diagnostics };
|
|
164
|
+
return { fields, snippets, clauses, components, diagnostics };
|
|
144
165
|
}
|
package/clause.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { CompiledTemplate } from './compile';
|
|
2
|
+
import type { Component } from './component';
|
|
2
3
|
export interface ClauseMeta {
|
|
3
4
|
title?: string;
|
|
4
5
|
version?: string;
|
|
@@ -6,7 +7,10 @@ export interface ClauseMeta {
|
|
|
6
7
|
/** A clause from the library, compiled, with its header. */
|
|
7
8
|
export interface LoadedClause extends ClauseMeta {
|
|
8
9
|
id: string;
|
|
10
|
+
/** The clause text (empty for a clause written as a TypeScript function). */
|
|
9
11
|
template: CompiledTemplate;
|
|
12
|
+
/** A TypeScript clause's body, called with the record's data. */
|
|
13
|
+
render?: Component;
|
|
10
14
|
}
|
|
11
15
|
export interface ClauseUse {
|
|
12
16
|
id: string;
|
package/compile.d.ts
CHANGED
|
@@ -6,6 +6,8 @@ import { FieldSpec } from './filters';
|
|
|
6
6
|
* Bare names keep templates readable; \snippet (not \partial, which is the
|
|
7
7
|
* math ∂) and \clauseref (not \ref) avoid LaTeX's own commands. \clause
|
|
8
8
|
* inserts a numbered clause from the clause library; \clauseref prints its number.
|
|
9
|
+
* \signature prints a party's signature block; \component calls a TypeScript
|
|
10
|
+
* component with a value.
|
|
9
11
|
*/
|
|
10
12
|
export declare const MACROS: {
|
|
11
13
|
readonly field: 1;
|
|
@@ -18,6 +20,8 @@ export declare const MACROS: {
|
|
|
18
20
|
readonly docid: 0;
|
|
19
21
|
readonly clause: 1;
|
|
20
22
|
readonly clauseref: 1;
|
|
23
|
+
readonly signature: 1;
|
|
24
|
+
readonly component: 2;
|
|
21
25
|
};
|
|
22
26
|
export type MacroName = keyof typeof MACROS;
|
|
23
27
|
export type Node = {
|
|
@@ -61,6 +65,15 @@ export type Node = {
|
|
|
61
65
|
kind: 'clauseref';
|
|
62
66
|
id: string;
|
|
63
67
|
loc: Location;
|
|
68
|
+
} | {
|
|
69
|
+
kind: 'signature';
|
|
70
|
+
path: string;
|
|
71
|
+
loc: Location;
|
|
72
|
+
} | {
|
|
73
|
+
kind: 'component';
|
|
74
|
+
name: string;
|
|
75
|
+
path: string;
|
|
76
|
+
loc: Location;
|
|
64
77
|
};
|
|
65
78
|
export interface CompiledTemplate {
|
|
66
79
|
file: string;
|
package/compile.js
CHANGED
|
@@ -13,6 +13,8 @@ const path_1 = require("./path");
|
|
|
13
13
|
* Bare names keep templates readable; \snippet (not \partial, which is the
|
|
14
14
|
* math ∂) and \clauseref (not \ref) avoid LaTeX's own commands. \clause
|
|
15
15
|
* inserts a numbered clause from the clause library; \clauseref prints its number.
|
|
16
|
+
* \signature prints a party's signature block; \component calls a TypeScript
|
|
17
|
+
* component with a value.
|
|
16
18
|
*/
|
|
17
19
|
exports.MACROS = {
|
|
18
20
|
field: 1,
|
|
@@ -25,6 +27,8 @@ exports.MACROS = {
|
|
|
25
27
|
docid: 0,
|
|
26
28
|
clause: 1,
|
|
27
29
|
clauseref: 1,
|
|
30
|
+
signature: 1,
|
|
31
|
+
component: 2,
|
|
28
32
|
};
|
|
29
33
|
const parser = (0, unified_latex_util_parse_1.getParser)({
|
|
30
34
|
macros: Object.fromEntries(Object.entries(exports.MACROS).map(([name, n]) => [name, { signature: Array(n).fill('m').join(' ') }])),
|
|
@@ -232,6 +236,27 @@ function compileTemplate(text, file) {
|
|
|
232
236
|
pos = text.startsWith('{}', macroEnd) ? macroEnd + 2 : macroEnd;
|
|
233
237
|
break;
|
|
234
238
|
}
|
|
239
|
+
case 'signature': {
|
|
240
|
+
const path = argText(0);
|
|
241
|
+
if (!(0, path_1.isPath)(path))
|
|
242
|
+
diagnostics.push((0, diagnostics_1.diag)('error', `\\signature{${path}}: expected the path of a party, like discloser`, loc));
|
|
243
|
+
else
|
|
244
|
+
nodes.push({ kind: 'signature', path, loc });
|
|
245
|
+
pos = macroEnd;
|
|
246
|
+
break;
|
|
247
|
+
}
|
|
248
|
+
case 'component': {
|
|
249
|
+
const name = argText(0);
|
|
250
|
+
const path = argText(1);
|
|
251
|
+
if (!isRelativeName(name))
|
|
252
|
+
diagnostics.push((0, diagnostics_1.diag)('error', `\\component{${name}}: use a name like line-items or tables/fees`, loc));
|
|
253
|
+
else if (path !== '' && !(0, path_1.isPath)(path))
|
|
254
|
+
diagnostics.push((0, diagnostics_1.diag)('error', `\\component{${name}}{${path}}: invalid path (leave it empty for the whole record)`, loc));
|
|
255
|
+
else
|
|
256
|
+
nodes.push({ kind: 'component', name, path, loc });
|
|
257
|
+
pos = macroEnd;
|
|
258
|
+
break;
|
|
259
|
+
}
|
|
235
260
|
case 'clauseref': {
|
|
236
261
|
const id = argText(0);
|
|
237
262
|
if (!isRelativeName(id))
|
package/component.d.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { RawLatex } from '@latex-kit/escape';
|
|
2
|
+
/** What a component gets besides its value. */
|
|
3
|
+
export interface ComponentContext {
|
|
4
|
+
variant: 'draft' | 'blank' | 'final' | 'signed';
|
|
5
|
+
locale: string;
|
|
6
|
+
docId?: string;
|
|
7
|
+
template?: string;
|
|
8
|
+
/** The whole record's data. */
|
|
9
|
+
data: unknown;
|
|
10
|
+
/** The path the template passed (`\component{name}{path}`); '' for the whole record. */
|
|
11
|
+
path: string;
|
|
12
|
+
/**
|
|
13
|
+
* Format a value with the template filters, as `\field` does:
|
|
14
|
+
* `format(1200, 'money:USD')`, `format(d, 'date:long')`. Returns plain
|
|
15
|
+
* text; put it through `tex` (or escapeLatex) like any other value.
|
|
16
|
+
*/
|
|
17
|
+
format(value: unknown, filters: string): string;
|
|
18
|
+
/** The signature block `\signature` prints for a party. */
|
|
19
|
+
signature(party: unknown, path?: string): RawLatex;
|
|
20
|
+
/** A red "missing" marker in drafts; an error in final and signed builds. */
|
|
21
|
+
missing(what: string): RawLatex;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* A component turns a value into LaTeX: `\component{name}{path}` in a
|
|
25
|
+
* template calls it with the value at `path`. Return `tex`-tagged LaTeX
|
|
26
|
+
* (inserted as is) or a plain string (escaped).
|
|
27
|
+
*/
|
|
28
|
+
export type Component = (value: unknown, ctx: ComponentContext) => RawLatex | string | number | null | undefined;
|
|
29
|
+
export declare function isRawLatex(v: unknown): v is RawLatex;
|
package/component.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isRawLatex = isRawLatex;
|
|
4
|
+
const escape_1 = require("@latex-kit/escape");
|
|
5
|
+
function isRawLatex(v) {
|
|
6
|
+
return (v instanceof escape_1.RawLatex ||
|
|
7
|
+
(!!v && typeof v === 'object' && v.constructor?.name === 'RawLatex' && typeof v.value === 'string'));
|
|
8
|
+
}
|
package/esm/analyze.d.ts
CHANGED
|
@@ -3,8 +3,8 @@ import { CompiledTemplate } from './compile';
|
|
|
3
3
|
import { Diagnostic } from './diagnostics';
|
|
4
4
|
import { JsonSchema } from './jsonschema';
|
|
5
5
|
export interface FieldRef {
|
|
6
|
-
/** How the template reads it
|
|
7
|
-
kind: 'field' | 'raw' | 'when' | 'each';
|
|
6
|
+
/** How the template reads it. */
|
|
7
|
+
kind: 'field' | 'raw' | 'when' | 'each' | 'signature' | 'component';
|
|
8
8
|
/** The path as written, e.g. `item.price`. */
|
|
9
9
|
path: string;
|
|
10
10
|
/** The path from the data root, e.g. `items[].price`. */
|
|
@@ -19,6 +19,8 @@ export interface AnalyzeOptions {
|
|
|
19
19
|
schema?: JsonSchema;
|
|
20
20
|
loadSnippet?: (name: string, from: string) => CompiledTemplate | null;
|
|
21
21
|
loadClause?: (id: string) => LoadedClause | null;
|
|
22
|
+
/** Names \component can use. Without it component names are not checked. */
|
|
23
|
+
components?: string[];
|
|
22
24
|
}
|
|
23
25
|
export interface AnalyzeResult {
|
|
24
26
|
fields: FieldRef[];
|
|
@@ -29,6 +31,8 @@ export interface AnalyzeResult {
|
|
|
29
31
|
version?: string;
|
|
30
32
|
file: string;
|
|
31
33
|
}[];
|
|
34
|
+
/** Components the template calls. */
|
|
35
|
+
components: string[];
|
|
32
36
|
diagnostics: Diagnostic[];
|
|
33
37
|
}
|
|
34
38
|
/**
|
package/esm/analyze.js
CHANGED
|
@@ -12,6 +12,7 @@ export function analyzeTemplate(template, options = {}) {
|
|
|
12
12
|
const fields = [];
|
|
13
13
|
const snippets = [];
|
|
14
14
|
const clauses = [];
|
|
15
|
+
const components = [];
|
|
15
16
|
const refs = [];
|
|
16
17
|
const stack = [template.file];
|
|
17
18
|
const resolve = (path, scopes, loc) => {
|
|
@@ -128,6 +129,26 @@ export function analyzeTemplate(template, options = {}) {
|
|
|
128
129
|
case 'clauseref':
|
|
129
130
|
refs.push({ id: node.id, loc: node.loc });
|
|
130
131
|
break;
|
|
132
|
+
case 'signature': {
|
|
133
|
+
const { absolute, schema } = resolve(node.path, scopes, node.loc);
|
|
134
|
+
fields.push({ kind: 'signature', path: node.path, absolute, filters: [], ...node.loc });
|
|
135
|
+
if (root && schema !== undefined && schemaAt(schema, ['name'], root) === undefined) {
|
|
136
|
+
diagnostics.push(diag('error', `\\signature{${node.path}}: ${absolute} has no name in the schema, so it is not a party or signer`, node.loc));
|
|
137
|
+
}
|
|
138
|
+
break;
|
|
139
|
+
}
|
|
140
|
+
case 'component': {
|
|
141
|
+
if (!components.includes(node.name))
|
|
142
|
+
components.push(node.name);
|
|
143
|
+
if (options.components && !options.components.includes(node.name)) {
|
|
144
|
+
diagnostics.push(diag('error', `unknown component ${node.name} (known: ${[...options.components].sort().join(', ') || 'none'})`, node.loc));
|
|
145
|
+
}
|
|
146
|
+
if (node.path) {
|
|
147
|
+
const { absolute } = resolve(node.path, scopes, node.loc);
|
|
148
|
+
fields.push({ kind: 'component', path: node.path, absolute, filters: [node.name], ...node.loc });
|
|
149
|
+
}
|
|
150
|
+
break;
|
|
151
|
+
}
|
|
131
152
|
}
|
|
132
153
|
}
|
|
133
154
|
};
|
|
@@ -137,5 +158,5 @@ export function analyzeTemplate(template, options = {}) {
|
|
|
137
158
|
diagnostics.push(diag('error', `\\clauseref{${r.id}}: the template never places \\clause{${r.id}}`, r.loc));
|
|
138
159
|
}
|
|
139
160
|
}
|
|
140
|
-
return { fields, snippets, clauses, diagnostics };
|
|
161
|
+
return { fields, snippets, clauses, components, diagnostics };
|
|
141
162
|
}
|
package/esm/clause.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { CompiledTemplate } from './compile';
|
|
2
|
+
import type { Component } from './component';
|
|
2
3
|
export interface ClauseMeta {
|
|
3
4
|
title?: string;
|
|
4
5
|
version?: string;
|
|
@@ -6,7 +7,10 @@ export interface ClauseMeta {
|
|
|
6
7
|
/** A clause from the library, compiled, with its header. */
|
|
7
8
|
export interface LoadedClause extends ClauseMeta {
|
|
8
9
|
id: string;
|
|
10
|
+
/** The clause text (empty for a clause written as a TypeScript function). */
|
|
9
11
|
template: CompiledTemplate;
|
|
12
|
+
/** A TypeScript clause's body, called with the record's data. */
|
|
13
|
+
render?: Component;
|
|
10
14
|
}
|
|
11
15
|
export interface ClauseUse {
|
|
12
16
|
id: string;
|
package/esm/compile.d.ts
CHANGED
|
@@ -6,6 +6,8 @@ import { FieldSpec } from './filters';
|
|
|
6
6
|
* Bare names keep templates readable; \snippet (not \partial, which is the
|
|
7
7
|
* math ∂) and \clauseref (not \ref) avoid LaTeX's own commands. \clause
|
|
8
8
|
* inserts a numbered clause from the clause library; \clauseref prints its number.
|
|
9
|
+
* \signature prints a party's signature block; \component calls a TypeScript
|
|
10
|
+
* component with a value.
|
|
9
11
|
*/
|
|
10
12
|
export declare const MACROS: {
|
|
11
13
|
readonly field: 1;
|
|
@@ -18,6 +20,8 @@ export declare const MACROS: {
|
|
|
18
20
|
readonly docid: 0;
|
|
19
21
|
readonly clause: 1;
|
|
20
22
|
readonly clauseref: 1;
|
|
23
|
+
readonly signature: 1;
|
|
24
|
+
readonly component: 2;
|
|
21
25
|
};
|
|
22
26
|
export type MacroName = keyof typeof MACROS;
|
|
23
27
|
export type Node = {
|
|
@@ -61,6 +65,15 @@ export type Node = {
|
|
|
61
65
|
kind: 'clauseref';
|
|
62
66
|
id: string;
|
|
63
67
|
loc: Location;
|
|
68
|
+
} | {
|
|
69
|
+
kind: 'signature';
|
|
70
|
+
path: string;
|
|
71
|
+
loc: Location;
|
|
72
|
+
} | {
|
|
73
|
+
kind: 'component';
|
|
74
|
+
name: string;
|
|
75
|
+
path: string;
|
|
76
|
+
loc: Location;
|
|
64
77
|
};
|
|
65
78
|
export interface CompiledTemplate {
|
|
66
79
|
file: string;
|
package/esm/compile.js
CHANGED
|
@@ -8,6 +8,8 @@ import { isName, isPath } from './path';
|
|
|
8
8
|
* Bare names keep templates readable; \snippet (not \partial, which is the
|
|
9
9
|
* math ∂) and \clauseref (not \ref) avoid LaTeX's own commands. \clause
|
|
10
10
|
* inserts a numbered clause from the clause library; \clauseref prints its number.
|
|
11
|
+
* \signature prints a party's signature block; \component calls a TypeScript
|
|
12
|
+
* component with a value.
|
|
11
13
|
*/
|
|
12
14
|
export const MACROS = {
|
|
13
15
|
field: 1,
|
|
@@ -20,6 +22,8 @@ export const MACROS = {
|
|
|
20
22
|
docid: 0,
|
|
21
23
|
clause: 1,
|
|
22
24
|
clauseref: 1,
|
|
25
|
+
signature: 1,
|
|
26
|
+
component: 2,
|
|
23
27
|
};
|
|
24
28
|
const parser = getParser({
|
|
25
29
|
macros: Object.fromEntries(Object.entries(MACROS).map(([name, n]) => [name, { signature: Array(n).fill('m').join(' ') }])),
|
|
@@ -227,6 +231,27 @@ export function compileTemplate(text, file) {
|
|
|
227
231
|
pos = text.startsWith('{}', macroEnd) ? macroEnd + 2 : macroEnd;
|
|
228
232
|
break;
|
|
229
233
|
}
|
|
234
|
+
case 'signature': {
|
|
235
|
+
const path = argText(0);
|
|
236
|
+
if (!isPath(path))
|
|
237
|
+
diagnostics.push(diag('error', `\\signature{${path}}: expected the path of a party, like discloser`, loc));
|
|
238
|
+
else
|
|
239
|
+
nodes.push({ kind: 'signature', path, loc });
|
|
240
|
+
pos = macroEnd;
|
|
241
|
+
break;
|
|
242
|
+
}
|
|
243
|
+
case 'component': {
|
|
244
|
+
const name = argText(0);
|
|
245
|
+
const path = argText(1);
|
|
246
|
+
if (!isRelativeName(name))
|
|
247
|
+
diagnostics.push(diag('error', `\\component{${name}}: use a name like line-items or tables/fees`, loc));
|
|
248
|
+
else if (path !== '' && !isPath(path))
|
|
249
|
+
diagnostics.push(diag('error', `\\component{${name}}{${path}}: invalid path (leave it empty for the whole record)`, loc));
|
|
250
|
+
else
|
|
251
|
+
nodes.push({ kind: 'component', name, path, loc });
|
|
252
|
+
pos = macroEnd;
|
|
253
|
+
break;
|
|
254
|
+
}
|
|
230
255
|
case 'clauseref': {
|
|
231
256
|
const id = argText(0);
|
|
232
257
|
if (!isRelativeName(id))
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { RawLatex } from '@latex-kit/escape';
|
|
2
|
+
/** What a component gets besides its value. */
|
|
3
|
+
export interface ComponentContext {
|
|
4
|
+
variant: 'draft' | 'blank' | 'final' | 'signed';
|
|
5
|
+
locale: string;
|
|
6
|
+
docId?: string;
|
|
7
|
+
template?: string;
|
|
8
|
+
/** The whole record's data. */
|
|
9
|
+
data: unknown;
|
|
10
|
+
/** The path the template passed (`\component{name}{path}`); '' for the whole record. */
|
|
11
|
+
path: string;
|
|
12
|
+
/**
|
|
13
|
+
* Format a value with the template filters, as `\field` does:
|
|
14
|
+
* `format(1200, 'money:USD')`, `format(d, 'date:long')`. Returns plain
|
|
15
|
+
* text; put it through `tex` (or escapeLatex) like any other value.
|
|
16
|
+
*/
|
|
17
|
+
format(value: unknown, filters: string): string;
|
|
18
|
+
/** The signature block `\signature` prints for a party. */
|
|
19
|
+
signature(party: unknown, path?: string): RawLatex;
|
|
20
|
+
/** A red "missing" marker in drafts; an error in final and signed builds. */
|
|
21
|
+
missing(what: string): RawLatex;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* A component turns a value into LaTeX: `\component{name}{path}` in a
|
|
25
|
+
* template calls it with the value at `path`. Return `tex`-tagged LaTeX
|
|
26
|
+
* (inserted as is) or a plain string (escaped).
|
|
27
|
+
*/
|
|
28
|
+
export type Component = (value: unknown, ctx: ComponentContext) => RawLatex | string | number | null | undefined;
|
|
29
|
+
export declare function isRawLatex(v: unknown): v is RawLatex;
|
package/esm/component.js
ADDED
package/esm/evaluate.d.ts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import { ClauseUse, LoadedClause } from './clause';
|
|
2
2
|
import { CompiledTemplate } from './compile';
|
|
3
|
+
import { Component } from './component';
|
|
3
4
|
import { truthy } from './condition';
|
|
4
5
|
import { Diagnostic } from './diagnostics';
|
|
6
|
+
import { SignerInfo } from './signature';
|
|
5
7
|
export type Variant = 'draft' | 'blank' | 'final' | 'signed';
|
|
6
8
|
export declare const VARIANTS: Variant[];
|
|
7
9
|
export interface EvaluateOptions {
|
|
@@ -20,6 +22,18 @@ export interface EvaluateOptions {
|
|
|
20
22
|
loadSnippet?: (name: string, from: string) => CompiledTemplate | null;
|
|
21
23
|
/** Resolve \clause{id} from the clause library. */
|
|
22
24
|
loadClause?: (id: string) => LoadedClause | null;
|
|
25
|
+
/** Components for \component{name}{path}, by name. */
|
|
26
|
+
components?: Record<string, Component>;
|
|
27
|
+
/**
|
|
28
|
+
* Signed builds: a local signature image for a signer, as a path TeX can
|
|
29
|
+
* read, or null to print `/s/ Name`.
|
|
30
|
+
*/
|
|
31
|
+
signatureImage?: (signer: SignerInfo) => string | null;
|
|
32
|
+
/** The record's `signed` map: signing date (and image) per party path. */
|
|
33
|
+
signed?: Record<string, {
|
|
34
|
+
date?: string;
|
|
35
|
+
signature?: string;
|
|
36
|
+
}>;
|
|
23
37
|
}
|
|
24
38
|
export interface FieldUse {
|
|
25
39
|
path: string;
|
|
@@ -35,6 +49,8 @@ export interface EvaluateResult {
|
|
|
35
49
|
fields: FieldUse[];
|
|
36
50
|
/** Library clauses placed in the document, in order, with versions. */
|
|
37
51
|
clauses: ClauseUse[];
|
|
52
|
+
/** Components the document called, by name. */
|
|
53
|
+
components: string[];
|
|
38
54
|
}
|
|
39
55
|
export declare function evaluateTemplate(template: CompiledTemplate, options: EvaluateOptions): EvaluateResult;
|
|
40
56
|
export { truthy };
|
package/esm/evaluate.js
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import { escapeLatex, LatexEscapeError, RawLatex } from '@latex-kit/escape';
|
|
2
2
|
import { clauseLabel } from './clause';
|
|
3
|
+
import { isRawLatex } from './component';
|
|
3
4
|
import { evaluateCondition, truthy } from './condition';
|
|
4
5
|
import { diag } from './diagnostics';
|
|
5
|
-
import { applyFilters, FilterError, isMissing } from './filters';
|
|
6
|
+
import { applyFilters, FilterError, isMissing, parseFieldSpec } from './filters';
|
|
6
7
|
import { splitPath } from './path';
|
|
8
|
+
import { signatureBlock, SignatureError } from './signature';
|
|
7
9
|
export const VARIANTS = ['draft', 'blank', 'final', 'signed'];
|
|
8
10
|
function own(obj, key) {
|
|
9
11
|
if (obj === null || typeof obj !== 'object')
|
|
@@ -12,9 +14,7 @@ function own(obj, key) {
|
|
|
12
14
|
return /^\d+$/.test(key) ? obj[Number(key)] : undefined;
|
|
13
15
|
return Object.prototype.hasOwnProperty.call(obj, key) ? obj[key] : undefined;
|
|
14
16
|
}
|
|
15
|
-
|
|
16
|
-
return v instanceof RawLatex || (!!v && typeof v === 'object' && v.constructor?.name === 'RawLatex' && typeof v.value === 'string');
|
|
17
|
-
}
|
|
17
|
+
const isRaw = isRawLatex;
|
|
18
18
|
export function evaluateTemplate(template, options) {
|
|
19
19
|
const variant = options.variant;
|
|
20
20
|
const locale = options.locale ?? 'en-US';
|
|
@@ -25,6 +25,8 @@ export function evaluateTemplate(template, options) {
|
|
|
25
25
|
const stack = [template.file];
|
|
26
26
|
const clauses = [];
|
|
27
27
|
const refs = [];
|
|
28
|
+
const usedComponents = new Set();
|
|
29
|
+
const signedPaths = new Set();
|
|
28
30
|
const lookup = (path) => {
|
|
29
31
|
const [head, ...rest] = splitPath(path);
|
|
30
32
|
let value;
|
|
@@ -102,6 +104,51 @@ export function evaluateTemplate(template, options) {
|
|
|
102
104
|
diagnostics.push(diag('error', `${what} is ${kind}`, loc));
|
|
103
105
|
return '';
|
|
104
106
|
};
|
|
107
|
+
const signature = (value, path, loc) => {
|
|
108
|
+
try {
|
|
109
|
+
signedPaths.add(path);
|
|
110
|
+
const signing = options.signed && Object.prototype.hasOwnProperty.call(options.signed, path) ? options.signed[path] : undefined;
|
|
111
|
+
return signatureBlock(value, { variant, locale, path, signing, missing: (what) => missing(what, loc), signatureImage: options.signatureImage });
|
|
112
|
+
}
|
|
113
|
+
catch (e) {
|
|
114
|
+
if (!(e instanceof SignatureError || e instanceof LatexEscapeError))
|
|
115
|
+
throw e;
|
|
116
|
+
diagnostics.push(diag('error', `\\signature{${path}}: ${e.message}`, loc));
|
|
117
|
+
return '';
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
/** Call a component (or a TypeScript clause) and turn its result into LaTeX. */
|
|
121
|
+
const callComponent = (fn, value, path, what, loc) => {
|
|
122
|
+
const ctx = {
|
|
123
|
+
variant,
|
|
124
|
+
locale,
|
|
125
|
+
docId: options.docId,
|
|
126
|
+
template: options.template,
|
|
127
|
+
data: options.data,
|
|
128
|
+
path,
|
|
129
|
+
format: (v, filters) => {
|
|
130
|
+
const spec = parseFieldSpec(`value|${filters}`);
|
|
131
|
+
return String(applyFilters(v, spec.filters, { locale }).value ?? '');
|
|
132
|
+
},
|
|
133
|
+
signature: (party, p = path) => new RawLatex(signature(party, p, loc)),
|
|
134
|
+
missing: (w) => new RawLatex(missing(w, loc)),
|
|
135
|
+
};
|
|
136
|
+
try {
|
|
137
|
+
const r = fn(value, ctx);
|
|
138
|
+
if (r === null || r === undefined)
|
|
139
|
+
return '';
|
|
140
|
+
if (isRaw(r))
|
|
141
|
+
return r.value;
|
|
142
|
+
if (typeof r === 'string' || typeof r === 'number')
|
|
143
|
+
return escapeLatex(String(r));
|
|
144
|
+
diagnostics.push(diag('error', `${what} returned ${typeof r}; return tex\`…\` or a string`, loc));
|
|
145
|
+
return '';
|
|
146
|
+
}
|
|
147
|
+
catch (e) {
|
|
148
|
+
diagnostics.push(diag('error', `${what}: ${e.message}`, loc));
|
|
149
|
+
return '';
|
|
150
|
+
}
|
|
151
|
+
};
|
|
105
152
|
const render = (nodes) => {
|
|
106
153
|
let out = '';
|
|
107
154
|
for (const node of nodes) {
|
|
@@ -211,6 +258,8 @@ export function evaluateTemplate(template, options) {
|
|
|
211
258
|
out += `\\lkclause{${clause.title ? escapeLatex(clause.title) : ''}}{${clauseLabel(node.id)}}`;
|
|
212
259
|
stack.push(clause.template.file);
|
|
213
260
|
out += render(clause.template.nodes);
|
|
261
|
+
if (clause.render)
|
|
262
|
+
out += callComponent(clause.render, options.data, '', `clause ${node.id}`, node.loc);
|
|
214
263
|
stack.pop();
|
|
215
264
|
break;
|
|
216
265
|
}
|
|
@@ -218,6 +267,26 @@ export function evaluateTemplate(template, options) {
|
|
|
218
267
|
refs.push({ id: node.id, loc: node.loc });
|
|
219
268
|
out += `\\ref{${clauseLabel(node.id)}}`;
|
|
220
269
|
break;
|
|
270
|
+
case 'signature': {
|
|
271
|
+
const value = variant === 'blank' ? undefined : lookup(node.path);
|
|
272
|
+
fields.push({ path: node.path, ...node.loc, missing: variant !== 'blank' && isMissing(value) });
|
|
273
|
+
out += signature(value, node.path, node.loc);
|
|
274
|
+
break;
|
|
275
|
+
}
|
|
276
|
+
case 'component': {
|
|
277
|
+
const fn = options.components?.[node.name];
|
|
278
|
+
if (!fn) {
|
|
279
|
+
const known = Object.keys(options.components ?? {}).sort();
|
|
280
|
+
diagnostics.push(diag('error', `unknown component ${node.name}${known.length ? ` (known: ${known.join(', ')})` : ''}`, node.loc));
|
|
281
|
+
break;
|
|
282
|
+
}
|
|
283
|
+
usedComponents.add(node.name);
|
|
284
|
+
const value = variant === 'blank' ? undefined : node.path ? lookup(node.path) : options.data;
|
|
285
|
+
if (node.path)
|
|
286
|
+
fields.push({ path: node.path, ...node.loc, missing: variant !== 'blank' && isMissing(value) });
|
|
287
|
+
out += callComponent(fn, value, node.path, `\\component{${node.name}}`, node.loc);
|
|
288
|
+
break;
|
|
289
|
+
}
|
|
221
290
|
}
|
|
222
291
|
}
|
|
223
292
|
return out;
|
|
@@ -230,6 +299,11 @@ export function evaluateTemplate(template, options) {
|
|
|
230
299
|
diagnostics.push(diag(allowMissing ? 'warning' : 'error', msg, r.loc));
|
|
231
300
|
}
|
|
232
301
|
}
|
|
233
|
-
|
|
302
|
+
for (const key of Object.keys(options.signed ?? {})) {
|
|
303
|
+
if (!signedPaths.has(key)) {
|
|
304
|
+
diagnostics.push(diag('warning', `signed.${key}: the document has no \\signature{${key}}`, { file: template.file, line: 1, column: 1 }));
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
return { tex, diagnostics, fields, clauses, components: [...usedComponents].sort() };
|
|
234
308
|
}
|
|
235
309
|
export { truthy };
|
package/esm/index.d.ts
CHANGED
|
@@ -2,6 +2,7 @@ export * from './analyze';
|
|
|
2
2
|
export * from './clause';
|
|
3
3
|
export * from './codemod';
|
|
4
4
|
export * from './compile';
|
|
5
|
+
export * from './component';
|
|
5
6
|
export * from './condition';
|
|
6
7
|
export * from './diagnostics';
|
|
7
8
|
export * from './document';
|
|
@@ -9,4 +10,5 @@ export * from './evaluate';
|
|
|
9
10
|
export * from './filters';
|
|
10
11
|
export * from './jsonschema';
|
|
11
12
|
export * from './path';
|
|
13
|
+
export * from './signature';
|
|
12
14
|
export * from './sty';
|
package/esm/index.js
CHANGED
|
@@ -2,6 +2,7 @@ export * from './analyze';
|
|
|
2
2
|
export * from './clause';
|
|
3
3
|
export * from './codemod';
|
|
4
4
|
export * from './compile';
|
|
5
|
+
export * from './component';
|
|
5
6
|
export * from './condition';
|
|
6
7
|
export * from './diagnostics';
|
|
7
8
|
export * from './document';
|
|
@@ -9,4 +10,5 @@ export * from './evaluate';
|
|
|
9
10
|
export * from './filters';
|
|
10
11
|
export * from './jsonschema';
|
|
11
12
|
export * from './path';
|
|
13
|
+
export * from './signature';
|
|
12
14
|
export * from './sty';
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/** The signer a signature image is looked up for. */
|
|
2
|
+
export interface SignerInfo {
|
|
3
|
+
name: string;
|
|
4
|
+
/** The signer's `signature` field: an image file name in the signatures folder. */
|
|
5
|
+
image?: string;
|
|
6
|
+
}
|
|
7
|
+
export interface SignatureOptions {
|
|
8
|
+
variant: 'draft' | 'blank' | 'final' | 'signed';
|
|
9
|
+
locale: string;
|
|
10
|
+
/** The data path of the party, for blank labels and messages. */
|
|
11
|
+
path: string;
|
|
12
|
+
/** Text for a missing value: a red marker in drafts, an error otherwise. */
|
|
13
|
+
missing: (what: string) => string;
|
|
14
|
+
/** How this party signed, from the record's `signed` map. */
|
|
15
|
+
signing?: {
|
|
16
|
+
date?: string;
|
|
17
|
+
signature?: string;
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* Signed builds only: a signature image for this signer, as a path TeX can
|
|
21
|
+
* read, or null to print `/s/ Name`.
|
|
22
|
+
*/
|
|
23
|
+
signatureImage?: (signer: SignerInfo) => string | null;
|
|
24
|
+
}
|
|
25
|
+
export declare class SignatureError extends Error {
|
|
26
|
+
name: string;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* A signature block for a party: `\lksignature{org}{name}{title}{signature}{date}`.
|
|
30
|
+
*
|
|
31
|
+
* A party with a `signer` (or `kind: organization`) is an organization: its
|
|
32
|
+
* name in bold, then the signer's name and title. Anything else signs for
|
|
33
|
+
* itself. The signed variant prints the signer's local signature image when
|
|
34
|
+
* there is one, else `/s/ Name`, and the date from the record's `signed` map.
|
|
35
|
+
*/
|
|
36
|
+
export declare function signatureBlock(value: unknown, o: SignatureOptions): string;
|
package/esm/signature.js
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { escapeLatex } from '@latex-kit/escape';
|
|
2
|
+
import { FILTERS } from './filters';
|
|
3
|
+
export class SignatureError extends Error {
|
|
4
|
+
name = 'SignatureError';
|
|
5
|
+
}
|
|
6
|
+
const isObject = (v) => !!v && typeof v === 'object' && !Array.isArray(v);
|
|
7
|
+
const text = (v) => (typeof v === 'string' && v.trim() !== '' ? v : undefined);
|
|
8
|
+
/** Image paths must be plain relative names, so they can never inject LaTeX. */
|
|
9
|
+
const SAFE_IMAGE_PATH = /^[A-Za-z0-9_./-]+$/;
|
|
10
|
+
/**
|
|
11
|
+
* A signature block for a party: `\lksignature{org}{name}{title}{signature}{date}`.
|
|
12
|
+
*
|
|
13
|
+
* A party with a `signer` (or `kind: organization`) is an organization: its
|
|
14
|
+
* name in bold, then the signer's name and title. Anything else signs for
|
|
15
|
+
* itself. The signed variant prints the signer's local signature image when
|
|
16
|
+
* there is one, else `/s/ Name`, and the date from the record's `signed` map.
|
|
17
|
+
*/
|
|
18
|
+
export function signatureBlock(value, o) {
|
|
19
|
+
const p = o.path;
|
|
20
|
+
if (o.variant === 'blank') {
|
|
21
|
+
return `\\lksignature{\\lkblank{${p}.name}}{\\lkblank{${p}.signer.name}}{\\lkblank{${p}.signer.title}}{}{}`;
|
|
22
|
+
}
|
|
23
|
+
if (value === undefined || value === null)
|
|
24
|
+
return o.missing(p);
|
|
25
|
+
if (!isObject(value))
|
|
26
|
+
throw new SignatureError(`${p} is not a party (expected an object with a name)`);
|
|
27
|
+
const signerObj = isObject(value.signer) ? value.signer : null;
|
|
28
|
+
const isOrg = !!signerObj || value.kind === 'organization';
|
|
29
|
+
const signer = signerObj ?? (isOrg ? null : value);
|
|
30
|
+
const orgName = isOrg ? text(value.name) : undefined;
|
|
31
|
+
const org = isOrg ? (orgName ? escapeLatex(orgName) : o.missing(`${p}.name`)) : '';
|
|
32
|
+
const signerPath = signerObj ? `${p}.signer` : p;
|
|
33
|
+
const name = signer ? text(signer.name) : undefined;
|
|
34
|
+
const nameTex = name ? escapeLatex(name) : o.missing(isOrg && !signer ? `${p}.signer` : `${signerPath}.name`);
|
|
35
|
+
const title = signer ? text(signer.title) : undefined;
|
|
36
|
+
const titleTex = title ? escapeLatex(title) : '';
|
|
37
|
+
let sign = '';
|
|
38
|
+
let date = '';
|
|
39
|
+
if (o.variant === 'signed' && name) {
|
|
40
|
+
const file = o.signatureImage?.({ name, image: text(o.signing?.signature) ?? text(signer?.signature) }) ?? null;
|
|
41
|
+
if (file) {
|
|
42
|
+
if (!SAFE_IMAGE_PATH.test(file))
|
|
43
|
+
throw new SignatureError(`signature image path "${file}" may only use letters, digits, . _ - and /`);
|
|
44
|
+
sign = `\\lksignatureimage{${file}}`;
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
sign = `/s/~${escapeLatex(name)}`;
|
|
48
|
+
}
|
|
49
|
+
const signed = text(o.signing?.date);
|
|
50
|
+
if (signed)
|
|
51
|
+
date = escapeLatex(String(FILTERS.date.apply(signed, 'long', { locale: o.locale }, {})));
|
|
52
|
+
}
|
|
53
|
+
return `\\lksignature{${org}}{${nameTex}}{${titleTex}}{${sign}}{${date}}`;
|
|
54
|
+
}
|
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.
|
|
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.4 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\\RequirePackage{graphicx}\n% tables from latex-kit components (table(), line items)\n\\RequirePackage{array,tabularx,booktabs}\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 signature block: \\lksignature{organization}{name}{title}{signature}{date}.\n% The organization line is left out when empty (a person signing for\n% themselves). The signature sits on the line: an image or /s/ text in signed\n% builds, nothing otherwise. Two blocks fit side by side with \\hfill.\n\\newlength\\lksignaturewidth\n\\setlength\\lksignaturewidth{0.45\\linewidth}\n\\newlength\\lksignatureheight\n\\setlength\\lksignatureheight{2.2em}\n\\providecommand\\lksignatureimage[1]{\\includegraphics[height=\\lksignatureheight,keepaspectratio]{#1}}\n\\providecommand\\lksignature[5]{%\n \\begin{minipage}[t]{\\lksignaturewidth}\\raggedright\\setlength\\parindent{0pt}%\n \\ifx\\relax#1\\relax\\else\\textbf{#1}\\par\\fi\n \\vspace{\\dimexpr\\lksignatureheight+0.4em\\relax}%\n \\rlap{\\smash{\\raisebox{1pt}{#4}}}\\rule{\\linewidth}{0.4pt}\\par\n #2\\par\n \\ifx\\relax#3\\relax\\else#3\\par\\fi\n \\vspace{0.5em}%\n Date:~\\ifx\\relax#5\\relax\\rule{0.55\\linewidth}{0.4pt}\\else#5\\fi\n \\end{minipage}}\n\n% An exhibit: a new page headed \"Exhibit A\", then its title.\n\\newcounter{lkexhibit}\n\\renewcommand\\thelkexhibit{\\Alph{lkexhibit}}\n\\providecommand\\lkexhibit[2]{%\n \\clearpage\\refstepcounter{lkexhibit}\\label{#2}%\n \\begin{center}{\\large\\bfseries Exhibit \\thelkexhibit}\\ifx\\relax#1\\relax\\else\\\\[0.3em]{\\bfseries #1}\\fi\\end{center}\\par}\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 \\newcommand\\signature[1]{\\lksignature{\\lkblank{#1.name}}{\\lkblank{#1.signer.name}}{\\lkblank{#1.signer.title}}{}{}}\n \\newcommand\\component[2]{\\par\\lkblank{component #1}\\par}\n\\fi\n\n\\endinput\n";
|
package/esm/sty.js
CHANGED
|
@@ -12,7 +12,7 @@ export const LATEX_KIT_STY = `% latex-kit.sty: support for latex-kit templates.
|
|
|
12
12
|
%
|
|
13
13
|
% https://github.com/constructive-io/latex-kit (MIT)
|
|
14
14
|
\\NeedsTeXFormat{LaTeX2e}
|
|
15
|
-
\\ProvidesPackage{latex-kit}[2026/11/25 v0.
|
|
15
|
+
\\ProvidesPackage{latex-kit}[2026/11/25 v0.4 latex-kit template support]
|
|
16
16
|
|
|
17
17
|
\\newif\\iflk@rendered
|
|
18
18
|
\\newif\\iflk@draft
|
|
@@ -24,6 +24,9 @@ export const LATEX_KIT_STY = `% latex-kit.sty: support for latex-kit templates.
|
|
|
24
24
|
\\ProcessOptions\\relax
|
|
25
25
|
|
|
26
26
|
\\RequirePackage{xcolor}
|
|
27
|
+
\\RequirePackage{graphicx}
|
|
28
|
+
% tables from latex-kit components (table(), line items)
|
|
29
|
+
\\RequirePackage{array,tabularx,booktabs}
|
|
27
30
|
\\definecolor{lkmeta}{gray}{0.55}
|
|
28
31
|
\\providecommand\\lkstamp{}
|
|
29
32
|
|
|
@@ -36,6 +39,33 @@ export const LATEX_KIT_STY = `% latex-kit.sty: support for latex-kit templates.
|
|
|
36
39
|
% section; with no title, a bold number. Redefine it to change the style.
|
|
37
40
|
\\providecommand\\lkclause[2]{\\ifx\\relax#1\\relax\\par\\refstepcounter{section}\\label{#2}\\noindent\\textbf{\\thesection.}~\\else\\section{#1}\\label{#2}\\fi}
|
|
38
41
|
|
|
42
|
+
% A signature block: \\lksignature{organization}{name}{title}{signature}{date}.
|
|
43
|
+
% The organization line is left out when empty (a person signing for
|
|
44
|
+
% themselves). The signature sits on the line: an image or /s/ text in signed
|
|
45
|
+
% builds, nothing otherwise. Two blocks fit side by side with \\hfill.
|
|
46
|
+
\\newlength\\lksignaturewidth
|
|
47
|
+
\\setlength\\lksignaturewidth{0.45\\linewidth}
|
|
48
|
+
\\newlength\\lksignatureheight
|
|
49
|
+
\\setlength\\lksignatureheight{2.2em}
|
|
50
|
+
\\providecommand\\lksignatureimage[1]{\\includegraphics[height=\\lksignatureheight,keepaspectratio]{#1}}
|
|
51
|
+
\\providecommand\\lksignature[5]{%
|
|
52
|
+
\\begin{minipage}[t]{\\lksignaturewidth}\\raggedright\\setlength\\parindent{0pt}%
|
|
53
|
+
\\ifx\\relax#1\\relax\\else\\textbf{#1}\\par\\fi
|
|
54
|
+
\\vspace{\\dimexpr\\lksignatureheight+0.4em\\relax}%
|
|
55
|
+
\\rlap{\\smash{\\raisebox{1pt}{#4}}}\\rule{\\linewidth}{0.4pt}\\par
|
|
56
|
+
#2\\par
|
|
57
|
+
\\ifx\\relax#3\\relax\\else#3\\par\\fi
|
|
58
|
+
\\vspace{0.5em}%
|
|
59
|
+
Date:~\\ifx\\relax#5\\relax\\rule{0.55\\linewidth}{0.4pt}\\else#5\\fi
|
|
60
|
+
\\end{minipage}}
|
|
61
|
+
|
|
62
|
+
% An exhibit: a new page headed "Exhibit A", then its title.
|
|
63
|
+
\\newcounter{lkexhibit}
|
|
64
|
+
\\renewcommand\\thelkexhibit{\\Alph{lkexhibit}}
|
|
65
|
+
\\providecommand\\lkexhibit[2]{%
|
|
66
|
+
\\clearpage\\refstepcounter{lkexhibit}\\label{#2}%
|
|
67
|
+
\\begin{center}{\\large\\bfseries Exhibit \\thelkexhibit}\\ifx\\relax#1\\relax\\else\\\\[0.3em]{\\bfseries #1}\\fi\\end{center}\\par}
|
|
68
|
+
|
|
39
69
|
% A value the data does not have yet (draft builds only).
|
|
40
70
|
\\newcommand\\lkmissing[1]{\\leavevmode{\\color{red}\\fbox{\\scriptsize\\ttfamily missing: \\detokenize{#1}}}}
|
|
41
71
|
|
|
@@ -61,6 +91,8 @@ export const LATEX_KIT_STY = `% latex-kit.sty: support for latex-kit templates.
|
|
|
61
91
|
\\newcommand\\snippet[1]{\\InputIfFileExists{#1}{}{\\lkblank{snippet #1}}}
|
|
62
92
|
\\newcommand\\clause[1]{\\par\\lkblank{clause #1}\\par}
|
|
63
93
|
\\newcommand\\clauseref[1]{\\lkblank{ref #1}}
|
|
94
|
+
\\newcommand\\signature[1]{\\lksignature{\\lkblank{#1.name}}{\\lkblank{#1.signer.name}}{\\lkblank{#1.signer.title}}{}{}}
|
|
95
|
+
\\newcommand\\component[2]{\\par\\lkblank{component #1}\\par}
|
|
64
96
|
\\fi
|
|
65
97
|
|
|
66
98
|
\\endinput
|
package/evaluate.d.ts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import { ClauseUse, LoadedClause } from './clause';
|
|
2
2
|
import { CompiledTemplate } from './compile';
|
|
3
|
+
import { Component } from './component';
|
|
3
4
|
import { truthy } from './condition';
|
|
4
5
|
import { Diagnostic } from './diagnostics';
|
|
6
|
+
import { SignerInfo } from './signature';
|
|
5
7
|
export type Variant = 'draft' | 'blank' | 'final' | 'signed';
|
|
6
8
|
export declare const VARIANTS: Variant[];
|
|
7
9
|
export interface EvaluateOptions {
|
|
@@ -20,6 +22,18 @@ export interface EvaluateOptions {
|
|
|
20
22
|
loadSnippet?: (name: string, from: string) => CompiledTemplate | null;
|
|
21
23
|
/** Resolve \clause{id} from the clause library. */
|
|
22
24
|
loadClause?: (id: string) => LoadedClause | null;
|
|
25
|
+
/** Components for \component{name}{path}, by name. */
|
|
26
|
+
components?: Record<string, Component>;
|
|
27
|
+
/**
|
|
28
|
+
* Signed builds: a local signature image for a signer, as a path TeX can
|
|
29
|
+
* read, or null to print `/s/ Name`.
|
|
30
|
+
*/
|
|
31
|
+
signatureImage?: (signer: SignerInfo) => string | null;
|
|
32
|
+
/** The record's `signed` map: signing date (and image) per party path. */
|
|
33
|
+
signed?: Record<string, {
|
|
34
|
+
date?: string;
|
|
35
|
+
signature?: string;
|
|
36
|
+
}>;
|
|
23
37
|
}
|
|
24
38
|
export interface FieldUse {
|
|
25
39
|
path: string;
|
|
@@ -35,6 +49,8 @@ export interface EvaluateResult {
|
|
|
35
49
|
fields: FieldUse[];
|
|
36
50
|
/** Library clauses placed in the document, in order, with versions. */
|
|
37
51
|
clauses: ClauseUse[];
|
|
52
|
+
/** Components the document called, by name. */
|
|
53
|
+
components: string[];
|
|
38
54
|
}
|
|
39
55
|
export declare function evaluateTemplate(template: CompiledTemplate, options: EvaluateOptions): EvaluateResult;
|
|
40
56
|
export { truthy };
|
package/evaluate.js
CHANGED
|
@@ -4,11 +4,13 @@ exports.truthy = exports.VARIANTS = void 0;
|
|
|
4
4
|
exports.evaluateTemplate = evaluateTemplate;
|
|
5
5
|
const escape_1 = require("@latex-kit/escape");
|
|
6
6
|
const clause_1 = require("./clause");
|
|
7
|
+
const component_1 = require("./component");
|
|
7
8
|
const condition_1 = require("./condition");
|
|
8
9
|
Object.defineProperty(exports, "truthy", { enumerable: true, get: function () { return condition_1.truthy; } });
|
|
9
10
|
const diagnostics_1 = require("./diagnostics");
|
|
10
11
|
const filters_1 = require("./filters");
|
|
11
12
|
const path_1 = require("./path");
|
|
13
|
+
const signature_1 = require("./signature");
|
|
12
14
|
exports.VARIANTS = ['draft', 'blank', 'final', 'signed'];
|
|
13
15
|
function own(obj, key) {
|
|
14
16
|
if (obj === null || typeof obj !== 'object')
|
|
@@ -17,9 +19,7 @@ function own(obj, key) {
|
|
|
17
19
|
return /^\d+$/.test(key) ? obj[Number(key)] : undefined;
|
|
18
20
|
return Object.prototype.hasOwnProperty.call(obj, key) ? obj[key] : undefined;
|
|
19
21
|
}
|
|
20
|
-
|
|
21
|
-
return v instanceof escape_1.RawLatex || (!!v && typeof v === 'object' && v.constructor?.name === 'RawLatex' && typeof v.value === 'string');
|
|
22
|
-
}
|
|
22
|
+
const isRaw = component_1.isRawLatex;
|
|
23
23
|
function evaluateTemplate(template, options) {
|
|
24
24
|
const variant = options.variant;
|
|
25
25
|
const locale = options.locale ?? 'en-US';
|
|
@@ -30,6 +30,8 @@ function evaluateTemplate(template, options) {
|
|
|
30
30
|
const stack = [template.file];
|
|
31
31
|
const clauses = [];
|
|
32
32
|
const refs = [];
|
|
33
|
+
const usedComponents = new Set();
|
|
34
|
+
const signedPaths = new Set();
|
|
33
35
|
const lookup = (path) => {
|
|
34
36
|
const [head, ...rest] = (0, path_1.splitPath)(path);
|
|
35
37
|
let value;
|
|
@@ -107,6 +109,51 @@ function evaluateTemplate(template, options) {
|
|
|
107
109
|
diagnostics.push((0, diagnostics_1.diag)('error', `${what} is ${kind}`, loc));
|
|
108
110
|
return '';
|
|
109
111
|
};
|
|
112
|
+
const signature = (value, path, loc) => {
|
|
113
|
+
try {
|
|
114
|
+
signedPaths.add(path);
|
|
115
|
+
const signing = options.signed && Object.prototype.hasOwnProperty.call(options.signed, path) ? options.signed[path] : undefined;
|
|
116
|
+
return (0, signature_1.signatureBlock)(value, { variant, locale, path, signing, missing: (what) => missing(what, loc), signatureImage: options.signatureImage });
|
|
117
|
+
}
|
|
118
|
+
catch (e) {
|
|
119
|
+
if (!(e instanceof signature_1.SignatureError || e instanceof escape_1.LatexEscapeError))
|
|
120
|
+
throw e;
|
|
121
|
+
diagnostics.push((0, diagnostics_1.diag)('error', `\\signature{${path}}: ${e.message}`, loc));
|
|
122
|
+
return '';
|
|
123
|
+
}
|
|
124
|
+
};
|
|
125
|
+
/** Call a component (or a TypeScript clause) and turn its result into LaTeX. */
|
|
126
|
+
const callComponent = (fn, value, path, what, loc) => {
|
|
127
|
+
const ctx = {
|
|
128
|
+
variant,
|
|
129
|
+
locale,
|
|
130
|
+
docId: options.docId,
|
|
131
|
+
template: options.template,
|
|
132
|
+
data: options.data,
|
|
133
|
+
path,
|
|
134
|
+
format: (v, filters) => {
|
|
135
|
+
const spec = (0, filters_1.parseFieldSpec)(`value|${filters}`);
|
|
136
|
+
return String((0, filters_1.applyFilters)(v, spec.filters, { locale }).value ?? '');
|
|
137
|
+
},
|
|
138
|
+
signature: (party, p = path) => new escape_1.RawLatex(signature(party, p, loc)),
|
|
139
|
+
missing: (w) => new escape_1.RawLatex(missing(w, loc)),
|
|
140
|
+
};
|
|
141
|
+
try {
|
|
142
|
+
const r = fn(value, ctx);
|
|
143
|
+
if (r === null || r === undefined)
|
|
144
|
+
return '';
|
|
145
|
+
if (isRaw(r))
|
|
146
|
+
return r.value;
|
|
147
|
+
if (typeof r === 'string' || typeof r === 'number')
|
|
148
|
+
return (0, escape_1.escapeLatex)(String(r));
|
|
149
|
+
diagnostics.push((0, diagnostics_1.diag)('error', `${what} returned ${typeof r}; return tex\`…\` or a string`, loc));
|
|
150
|
+
return '';
|
|
151
|
+
}
|
|
152
|
+
catch (e) {
|
|
153
|
+
diagnostics.push((0, diagnostics_1.diag)('error', `${what}: ${e.message}`, loc));
|
|
154
|
+
return '';
|
|
155
|
+
}
|
|
156
|
+
};
|
|
110
157
|
const render = (nodes) => {
|
|
111
158
|
let out = '';
|
|
112
159
|
for (const node of nodes) {
|
|
@@ -216,6 +263,8 @@ function evaluateTemplate(template, options) {
|
|
|
216
263
|
out += `\\lkclause{${clause.title ? (0, escape_1.escapeLatex)(clause.title) : ''}}{${(0, clause_1.clauseLabel)(node.id)}}`;
|
|
217
264
|
stack.push(clause.template.file);
|
|
218
265
|
out += render(clause.template.nodes);
|
|
266
|
+
if (clause.render)
|
|
267
|
+
out += callComponent(clause.render, options.data, '', `clause ${node.id}`, node.loc);
|
|
219
268
|
stack.pop();
|
|
220
269
|
break;
|
|
221
270
|
}
|
|
@@ -223,6 +272,26 @@ function evaluateTemplate(template, options) {
|
|
|
223
272
|
refs.push({ id: node.id, loc: node.loc });
|
|
224
273
|
out += `\\ref{${(0, clause_1.clauseLabel)(node.id)}}`;
|
|
225
274
|
break;
|
|
275
|
+
case 'signature': {
|
|
276
|
+
const value = variant === 'blank' ? undefined : lookup(node.path);
|
|
277
|
+
fields.push({ path: node.path, ...node.loc, missing: variant !== 'blank' && (0, filters_1.isMissing)(value) });
|
|
278
|
+
out += signature(value, node.path, node.loc);
|
|
279
|
+
break;
|
|
280
|
+
}
|
|
281
|
+
case 'component': {
|
|
282
|
+
const fn = options.components?.[node.name];
|
|
283
|
+
if (!fn) {
|
|
284
|
+
const known = Object.keys(options.components ?? {}).sort();
|
|
285
|
+
diagnostics.push((0, diagnostics_1.diag)('error', `unknown component ${node.name}${known.length ? ` (known: ${known.join(', ')})` : ''}`, node.loc));
|
|
286
|
+
break;
|
|
287
|
+
}
|
|
288
|
+
usedComponents.add(node.name);
|
|
289
|
+
const value = variant === 'blank' ? undefined : node.path ? lookup(node.path) : options.data;
|
|
290
|
+
if (node.path)
|
|
291
|
+
fields.push({ path: node.path, ...node.loc, missing: variant !== 'blank' && (0, filters_1.isMissing)(value) });
|
|
292
|
+
out += callComponent(fn, value, node.path, `\\component{${node.name}}`, node.loc);
|
|
293
|
+
break;
|
|
294
|
+
}
|
|
226
295
|
}
|
|
227
296
|
}
|
|
228
297
|
return out;
|
|
@@ -235,5 +304,10 @@ function evaluateTemplate(template, options) {
|
|
|
235
304
|
diagnostics.push((0, diagnostics_1.diag)(allowMissing ? 'warning' : 'error', msg, r.loc));
|
|
236
305
|
}
|
|
237
306
|
}
|
|
238
|
-
|
|
307
|
+
for (const key of Object.keys(options.signed ?? {})) {
|
|
308
|
+
if (!signedPaths.has(key)) {
|
|
309
|
+
diagnostics.push((0, diagnostics_1.diag)('warning', `signed.${key}: the document has no \\signature{${key}}`, { file: template.file, line: 1, column: 1 }));
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
return { tex, diagnostics, fields, clauses, components: [...usedComponents].sort() };
|
|
239
313
|
}
|
package/index.d.ts
CHANGED
|
@@ -2,6 +2,7 @@ export * from './analyze';
|
|
|
2
2
|
export * from './clause';
|
|
3
3
|
export * from './codemod';
|
|
4
4
|
export * from './compile';
|
|
5
|
+
export * from './component';
|
|
5
6
|
export * from './condition';
|
|
6
7
|
export * from './diagnostics';
|
|
7
8
|
export * from './document';
|
|
@@ -9,4 +10,5 @@ export * from './evaluate';
|
|
|
9
10
|
export * from './filters';
|
|
10
11
|
export * from './jsonschema';
|
|
11
12
|
export * from './path';
|
|
13
|
+
export * from './signature';
|
|
12
14
|
export * from './sty';
|
package/index.js
CHANGED
|
@@ -18,6 +18,7 @@ __exportStar(require("./analyze"), exports);
|
|
|
18
18
|
__exportStar(require("./clause"), exports);
|
|
19
19
|
__exportStar(require("./codemod"), exports);
|
|
20
20
|
__exportStar(require("./compile"), exports);
|
|
21
|
+
__exportStar(require("./component"), exports);
|
|
21
22
|
__exportStar(require("./condition"), exports);
|
|
22
23
|
__exportStar(require("./diagnostics"), exports);
|
|
23
24
|
__exportStar(require("./document"), exports);
|
|
@@ -25,4 +26,5 @@ __exportStar(require("./evaluate"), exports);
|
|
|
25
26
|
__exportStar(require("./filters"), exports);
|
|
26
27
|
__exportStar(require("./jsonschema"), exports);
|
|
27
28
|
__exportStar(require("./path"), exports);
|
|
29
|
+
__exportStar(require("./signature"), exports);
|
|
28
30
|
__exportStar(require("./sty"), exports);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@latex-kit/template",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.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": "b169d7b7a018ec26e5099e021b3529dcf6c006fa"
|
|
46
46
|
}
|
package/signature.d.ts
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/** The signer a signature image is looked up for. */
|
|
2
|
+
export interface SignerInfo {
|
|
3
|
+
name: string;
|
|
4
|
+
/** The signer's `signature` field: an image file name in the signatures folder. */
|
|
5
|
+
image?: string;
|
|
6
|
+
}
|
|
7
|
+
export interface SignatureOptions {
|
|
8
|
+
variant: 'draft' | 'blank' | 'final' | 'signed';
|
|
9
|
+
locale: string;
|
|
10
|
+
/** The data path of the party, for blank labels and messages. */
|
|
11
|
+
path: string;
|
|
12
|
+
/** Text for a missing value: a red marker in drafts, an error otherwise. */
|
|
13
|
+
missing: (what: string) => string;
|
|
14
|
+
/** How this party signed, from the record's `signed` map. */
|
|
15
|
+
signing?: {
|
|
16
|
+
date?: string;
|
|
17
|
+
signature?: string;
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* Signed builds only: a signature image for this signer, as a path TeX can
|
|
21
|
+
* read, or null to print `/s/ Name`.
|
|
22
|
+
*/
|
|
23
|
+
signatureImage?: (signer: SignerInfo) => string | null;
|
|
24
|
+
}
|
|
25
|
+
export declare class SignatureError extends Error {
|
|
26
|
+
name: string;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* A signature block for a party: `\lksignature{org}{name}{title}{signature}{date}`.
|
|
30
|
+
*
|
|
31
|
+
* A party with a `signer` (or `kind: organization`) is an organization: its
|
|
32
|
+
* name in bold, then the signer's name and title. Anything else signs for
|
|
33
|
+
* itself. The signed variant prints the signer's local signature image when
|
|
34
|
+
* there is one, else `/s/ Name`, and the date from the record's `signed` map.
|
|
35
|
+
*/
|
|
36
|
+
export declare function signatureBlock(value: unknown, o: SignatureOptions): string;
|
package/signature.js
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SignatureError = void 0;
|
|
4
|
+
exports.signatureBlock = signatureBlock;
|
|
5
|
+
const escape_1 = require("@latex-kit/escape");
|
|
6
|
+
const filters_1 = require("./filters");
|
|
7
|
+
class SignatureError extends Error {
|
|
8
|
+
name = 'SignatureError';
|
|
9
|
+
}
|
|
10
|
+
exports.SignatureError = SignatureError;
|
|
11
|
+
const isObject = (v) => !!v && typeof v === 'object' && !Array.isArray(v);
|
|
12
|
+
const text = (v) => (typeof v === 'string' && v.trim() !== '' ? v : undefined);
|
|
13
|
+
/** Image paths must be plain relative names, so they can never inject LaTeX. */
|
|
14
|
+
const SAFE_IMAGE_PATH = /^[A-Za-z0-9_./-]+$/;
|
|
15
|
+
/**
|
|
16
|
+
* A signature block for a party: `\lksignature{org}{name}{title}{signature}{date}`.
|
|
17
|
+
*
|
|
18
|
+
* A party with a `signer` (or `kind: organization`) is an organization: its
|
|
19
|
+
* name in bold, then the signer's name and title. Anything else signs for
|
|
20
|
+
* itself. The signed variant prints the signer's local signature image when
|
|
21
|
+
* there is one, else `/s/ Name`, and the date from the record's `signed` map.
|
|
22
|
+
*/
|
|
23
|
+
function signatureBlock(value, o) {
|
|
24
|
+
const p = o.path;
|
|
25
|
+
if (o.variant === 'blank') {
|
|
26
|
+
return `\\lksignature{\\lkblank{${p}.name}}{\\lkblank{${p}.signer.name}}{\\lkblank{${p}.signer.title}}{}{}`;
|
|
27
|
+
}
|
|
28
|
+
if (value === undefined || value === null)
|
|
29
|
+
return o.missing(p);
|
|
30
|
+
if (!isObject(value))
|
|
31
|
+
throw new SignatureError(`${p} is not a party (expected an object with a name)`);
|
|
32
|
+
const signerObj = isObject(value.signer) ? value.signer : null;
|
|
33
|
+
const isOrg = !!signerObj || value.kind === 'organization';
|
|
34
|
+
const signer = signerObj ?? (isOrg ? null : value);
|
|
35
|
+
const orgName = isOrg ? text(value.name) : undefined;
|
|
36
|
+
const org = isOrg ? (orgName ? (0, escape_1.escapeLatex)(orgName) : o.missing(`${p}.name`)) : '';
|
|
37
|
+
const signerPath = signerObj ? `${p}.signer` : p;
|
|
38
|
+
const name = signer ? text(signer.name) : undefined;
|
|
39
|
+
const nameTex = name ? (0, escape_1.escapeLatex)(name) : o.missing(isOrg && !signer ? `${p}.signer` : `${signerPath}.name`);
|
|
40
|
+
const title = signer ? text(signer.title) : undefined;
|
|
41
|
+
const titleTex = title ? (0, escape_1.escapeLatex)(title) : '';
|
|
42
|
+
let sign = '';
|
|
43
|
+
let date = '';
|
|
44
|
+
if (o.variant === 'signed' && name) {
|
|
45
|
+
const file = o.signatureImage?.({ name, image: text(o.signing?.signature) ?? text(signer?.signature) }) ?? null;
|
|
46
|
+
if (file) {
|
|
47
|
+
if (!SAFE_IMAGE_PATH.test(file))
|
|
48
|
+
throw new SignatureError(`signature image path "${file}" may only use letters, digits, . _ - and /`);
|
|
49
|
+
sign = `\\lksignatureimage{${file}}`;
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
sign = `/s/~${(0, escape_1.escapeLatex)(name)}`;
|
|
53
|
+
}
|
|
54
|
+
const signed = text(o.signing?.date);
|
|
55
|
+
if (signed)
|
|
56
|
+
date = (0, escape_1.escapeLatex)(String(filters_1.FILTERS.date.apply(signed, 'long', { locale: o.locale }, {})));
|
|
57
|
+
}
|
|
58
|
+
return `\\lksignature{${org}}{${nameTex}}{${titleTex}}{${sign}}{${date}}`;
|
|
59
|
+
}
|
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.
|
|
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.4 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\\RequirePackage{graphicx}\n% tables from latex-kit components (table(), line items)\n\\RequirePackage{array,tabularx,booktabs}\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 signature block: \\lksignature{organization}{name}{title}{signature}{date}.\n% The organization line is left out when empty (a person signing for\n% themselves). The signature sits on the line: an image or /s/ text in signed\n% builds, nothing otherwise. Two blocks fit side by side with \\hfill.\n\\newlength\\lksignaturewidth\n\\setlength\\lksignaturewidth{0.45\\linewidth}\n\\newlength\\lksignatureheight\n\\setlength\\lksignatureheight{2.2em}\n\\providecommand\\lksignatureimage[1]{\\includegraphics[height=\\lksignatureheight,keepaspectratio]{#1}}\n\\providecommand\\lksignature[5]{%\n \\begin{minipage}[t]{\\lksignaturewidth}\\raggedright\\setlength\\parindent{0pt}%\n \\ifx\\relax#1\\relax\\else\\textbf{#1}\\par\\fi\n \\vspace{\\dimexpr\\lksignatureheight+0.4em\\relax}%\n \\rlap{\\smash{\\raisebox{1pt}{#4}}}\\rule{\\linewidth}{0.4pt}\\par\n #2\\par\n \\ifx\\relax#3\\relax\\else#3\\par\\fi\n \\vspace{0.5em}%\n Date:~\\ifx\\relax#5\\relax\\rule{0.55\\linewidth}{0.4pt}\\else#5\\fi\n \\end{minipage}}\n\n% An exhibit: a new page headed \"Exhibit A\", then its title.\n\\newcounter{lkexhibit}\n\\renewcommand\\thelkexhibit{\\Alph{lkexhibit}}\n\\providecommand\\lkexhibit[2]{%\n \\clearpage\\refstepcounter{lkexhibit}\\label{#2}%\n \\begin{center}{\\large\\bfseries Exhibit \\thelkexhibit}\\ifx\\relax#1\\relax\\else\\\\[0.3em]{\\bfseries #1}\\fi\\end{center}\\par}\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 \\newcommand\\signature[1]{\\lksignature{\\lkblank{#1.name}}{\\lkblank{#1.signer.name}}{\\lkblank{#1.signer.title}}{}{}}\n \\newcommand\\component[2]{\\par\\lkblank{component #1}\\par}\n\\fi\n\n\\endinput\n";
|
package/sty.js
CHANGED
|
@@ -15,7 +15,7 @@ exports.LATEX_KIT_STY = `% latex-kit.sty: support for latex-kit templates.
|
|
|
15
15
|
%
|
|
16
16
|
% https://github.com/constructive-io/latex-kit (MIT)
|
|
17
17
|
\\NeedsTeXFormat{LaTeX2e}
|
|
18
|
-
\\ProvidesPackage{latex-kit}[2026/11/25 v0.
|
|
18
|
+
\\ProvidesPackage{latex-kit}[2026/11/25 v0.4 latex-kit template support]
|
|
19
19
|
|
|
20
20
|
\\newif\\iflk@rendered
|
|
21
21
|
\\newif\\iflk@draft
|
|
@@ -27,6 +27,9 @@ exports.LATEX_KIT_STY = `% latex-kit.sty: support for latex-kit templates.
|
|
|
27
27
|
\\ProcessOptions\\relax
|
|
28
28
|
|
|
29
29
|
\\RequirePackage{xcolor}
|
|
30
|
+
\\RequirePackage{graphicx}
|
|
31
|
+
% tables from latex-kit components (table(), line items)
|
|
32
|
+
\\RequirePackage{array,tabularx,booktabs}
|
|
30
33
|
\\definecolor{lkmeta}{gray}{0.55}
|
|
31
34
|
\\providecommand\\lkstamp{}
|
|
32
35
|
|
|
@@ -39,6 +42,33 @@ exports.LATEX_KIT_STY = `% latex-kit.sty: support for latex-kit templates.
|
|
|
39
42
|
% section; with no title, a bold number. Redefine it to change the style.
|
|
40
43
|
\\providecommand\\lkclause[2]{\\ifx\\relax#1\\relax\\par\\refstepcounter{section}\\label{#2}\\noindent\\textbf{\\thesection.}~\\else\\section{#1}\\label{#2}\\fi}
|
|
41
44
|
|
|
45
|
+
% A signature block: \\lksignature{organization}{name}{title}{signature}{date}.
|
|
46
|
+
% The organization line is left out when empty (a person signing for
|
|
47
|
+
% themselves). The signature sits on the line: an image or /s/ text in signed
|
|
48
|
+
% builds, nothing otherwise. Two blocks fit side by side with \\hfill.
|
|
49
|
+
\\newlength\\lksignaturewidth
|
|
50
|
+
\\setlength\\lksignaturewidth{0.45\\linewidth}
|
|
51
|
+
\\newlength\\lksignatureheight
|
|
52
|
+
\\setlength\\lksignatureheight{2.2em}
|
|
53
|
+
\\providecommand\\lksignatureimage[1]{\\includegraphics[height=\\lksignatureheight,keepaspectratio]{#1}}
|
|
54
|
+
\\providecommand\\lksignature[5]{%
|
|
55
|
+
\\begin{minipage}[t]{\\lksignaturewidth}\\raggedright\\setlength\\parindent{0pt}%
|
|
56
|
+
\\ifx\\relax#1\\relax\\else\\textbf{#1}\\par\\fi
|
|
57
|
+
\\vspace{\\dimexpr\\lksignatureheight+0.4em\\relax}%
|
|
58
|
+
\\rlap{\\smash{\\raisebox{1pt}{#4}}}\\rule{\\linewidth}{0.4pt}\\par
|
|
59
|
+
#2\\par
|
|
60
|
+
\\ifx\\relax#3\\relax\\else#3\\par\\fi
|
|
61
|
+
\\vspace{0.5em}%
|
|
62
|
+
Date:~\\ifx\\relax#5\\relax\\rule{0.55\\linewidth}{0.4pt}\\else#5\\fi
|
|
63
|
+
\\end{minipage}}
|
|
64
|
+
|
|
65
|
+
% An exhibit: a new page headed "Exhibit A", then its title.
|
|
66
|
+
\\newcounter{lkexhibit}
|
|
67
|
+
\\renewcommand\\thelkexhibit{\\Alph{lkexhibit}}
|
|
68
|
+
\\providecommand\\lkexhibit[2]{%
|
|
69
|
+
\\clearpage\\refstepcounter{lkexhibit}\\label{#2}%
|
|
70
|
+
\\begin{center}{\\large\\bfseries Exhibit \\thelkexhibit}\\ifx\\relax#1\\relax\\else\\\\[0.3em]{\\bfseries #1}\\fi\\end{center}\\par}
|
|
71
|
+
|
|
42
72
|
% A value the data does not have yet (draft builds only).
|
|
43
73
|
\\newcommand\\lkmissing[1]{\\leavevmode{\\color{red}\\fbox{\\scriptsize\\ttfamily missing: \\detokenize{#1}}}}
|
|
44
74
|
|
|
@@ -64,6 +94,8 @@ exports.LATEX_KIT_STY = `% latex-kit.sty: support for latex-kit templates.
|
|
|
64
94
|
\\newcommand\\snippet[1]{\\InputIfFileExists{#1}{}{\\lkblank{snippet #1}}}
|
|
65
95
|
\\newcommand\\clause[1]{\\par\\lkblank{clause #1}\\par}
|
|
66
96
|
\\newcommand\\clauseref[1]{\\lkblank{ref #1}}
|
|
97
|
+
\\newcommand\\signature[1]{\\lksignature{\\lkblank{#1.name}}{\\lkblank{#1.signer.name}}{\\lkblank{#1.signer.title}}{}{}}
|
|
98
|
+
\\newcommand\\component[2]{\\par\\lkblank{component #1}\\par}
|
|
67
99
|
\\fi
|
|
68
100
|
|
|
69
101
|
\\endinput
|