@latex-kit/template 0.2.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 +48 -3
- package/analyze.d.ts +14 -2
- package/analyze.js +53 -1
- package/clause.d.ts +27 -0
- package/clause.js +26 -0
- package/codemod.d.ts +25 -0
- package/codemod.js +67 -0
- package/compile.d.ts +26 -1
- package/compile.js +48 -7
- package/component.d.ts +29 -0
- package/component.js +8 -0
- package/esm/analyze.d.ts +14 -2
- package/esm/analyze.js +53 -1
- package/esm/clause.d.ts +27 -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 +26 -1
- package/esm/compile.js +47 -7
- package/esm/component.d.ts +29 -0
- package/esm/component.js +5 -0
- package/esm/evaluate.d.ts +21 -0
- package/esm/evaluate.js +115 -5
- package/esm/index.d.ts +4 -0
- package/esm/index.js +4 -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 +39 -2
- package/evaluate.d.ts +21 -0
- package/evaluate.js +114 -4
- package/index.d.ts +4 -0
- package/index.js +4 -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 +39 -2
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
|
|
|
@@ -32,6 +35,37 @@ export const LATEX_KIT_STY = `% latex-kit.sty: support for latex-kit templates.
|
|
|
32
35
|
\\setlength\\lkblankwidth{3.5cm}
|
|
33
36
|
\\newcommand\\lkblank[1]{\\leavevmode\\underline{\\makebox[\\lkblankwidth][l]{\\color{lkmeta}\\tiny\\ttfamily\\detokenize{#1}}}}
|
|
34
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
|
+
|
|
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
|
+
|
|
35
69
|
% A value the data does not have yet (draft builds only).
|
|
36
70
|
\\newcommand\\lkmissing[1]{\\leavevmode{\\color{red}\\fbox{\\scriptsize\\ttfamily missing: \\detokenize{#1}}}}
|
|
37
71
|
|
|
@@ -55,7 +89,10 @@ export const LATEX_KIT_STY = `% latex-kit.sty: support for latex-kit templates.
|
|
|
55
89
|
\\newcommand\\each[3]{{\\color{lkmeta}\\tiny[each \\detokenize{#2} in \\detokenize{#1}]}#3}
|
|
56
90
|
\\newcommand\\sep[1]{#1}
|
|
57
91
|
\\newcommand\\snippet[1]{\\InputIfFileExists{#1}{}{\\lkblank{snippet #1}}}
|
|
58
|
-
\\newcommand\\
|
|
92
|
+
\\newcommand\\clause[1]{\\par\\lkblank{clause #1}\\par}
|
|
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}
|
|
59
96
|
\\fi
|
|
60
97
|
|
|
61
98
|
\\endinput
|
package/evaluate.d.ts
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
|
+
import { ClauseUse, LoadedClause } from './clause';
|
|
1
2
|
import { CompiledTemplate } from './compile';
|
|
3
|
+
import { Component } from './component';
|
|
2
4
|
import { truthy } from './condition';
|
|
3
5
|
import { Diagnostic } from './diagnostics';
|
|
6
|
+
import { SignerInfo } from './signature';
|
|
4
7
|
export type Variant = 'draft' | 'blank' | 'final' | 'signed';
|
|
5
8
|
export declare const VARIANTS: Variant[];
|
|
6
9
|
export interface EvaluateOptions {
|
|
@@ -17,6 +20,20 @@ export interface EvaluateOptions {
|
|
|
17
20
|
allowMissing?: boolean;
|
|
18
21
|
/** Resolve \snippet{name} from the file that uses it. */
|
|
19
22
|
loadSnippet?: (name: string, from: string) => CompiledTemplate | null;
|
|
23
|
+
/** Resolve \clause{id} from the clause library. */
|
|
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
|
+
}>;
|
|
20
37
|
}
|
|
21
38
|
export interface FieldUse {
|
|
22
39
|
path: string;
|
|
@@ -30,6 +47,10 @@ export interface EvaluateResult {
|
|
|
30
47
|
diagnostics: Diagnostic[];
|
|
31
48
|
/** Every field that was filled (or found missing), in document order. */
|
|
32
49
|
fields: FieldUse[];
|
|
50
|
+
/** Library clauses placed in the document, in order, with versions. */
|
|
51
|
+
clauses: ClauseUse[];
|
|
52
|
+
/** Components the document called, by name. */
|
|
53
|
+
components: string[];
|
|
33
54
|
}
|
|
34
55
|
export declare function evaluateTemplate(template: CompiledTemplate, options: EvaluateOptions): EvaluateResult;
|
|
35
56
|
export { truthy };
|
package/evaluate.js
CHANGED
|
@@ -3,11 +3,14 @@ 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");
|
|
7
|
+
const component_1 = require("./component");
|
|
6
8
|
const condition_1 = require("./condition");
|
|
7
9
|
Object.defineProperty(exports, "truthy", { enumerable: true, get: function () { return condition_1.truthy; } });
|
|
8
10
|
const diagnostics_1 = require("./diagnostics");
|
|
9
11
|
const filters_1 = require("./filters");
|
|
10
12
|
const path_1 = require("./path");
|
|
13
|
+
const signature_1 = require("./signature");
|
|
11
14
|
exports.VARIANTS = ['draft', 'blank', 'final', 'signed'];
|
|
12
15
|
function own(obj, key) {
|
|
13
16
|
if (obj === null || typeof obj !== 'object')
|
|
@@ -16,9 +19,7 @@ function own(obj, key) {
|
|
|
16
19
|
return /^\d+$/.test(key) ? obj[Number(key)] : undefined;
|
|
17
20
|
return Object.prototype.hasOwnProperty.call(obj, key) ? obj[key] : undefined;
|
|
18
21
|
}
|
|
19
|
-
|
|
20
|
-
return v instanceof escape_1.RawLatex || (!!v && typeof v === 'object' && v.constructor?.name === 'RawLatex' && typeof v.value === 'string');
|
|
21
|
-
}
|
|
22
|
+
const isRaw = component_1.isRawLatex;
|
|
22
23
|
function evaluateTemplate(template, options) {
|
|
23
24
|
const variant = options.variant;
|
|
24
25
|
const locale = options.locale ?? 'en-US';
|
|
@@ -27,6 +28,10 @@ function evaluateTemplate(template, options) {
|
|
|
27
28
|
const fields = [];
|
|
28
29
|
const frames = [];
|
|
29
30
|
const stack = [template.file];
|
|
31
|
+
const clauses = [];
|
|
32
|
+
const refs = [];
|
|
33
|
+
const usedComponents = new Set();
|
|
34
|
+
const signedPaths = new Set();
|
|
30
35
|
const lookup = (path) => {
|
|
31
36
|
const [head, ...rest] = (0, path_1.splitPath)(path);
|
|
32
37
|
let value;
|
|
@@ -104,6 +109,51 @@ function evaluateTemplate(template, options) {
|
|
|
104
109
|
diagnostics.push((0, diagnostics_1.diag)('error', `${what} is ${kind}`, loc));
|
|
105
110
|
return '';
|
|
106
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
|
+
};
|
|
107
157
|
const render = (nodes) => {
|
|
108
158
|
let out = '';
|
|
109
159
|
for (const node of nodes) {
|
|
@@ -194,10 +244,70 @@ function evaluateTemplate(template, options) {
|
|
|
194
244
|
stack.pop();
|
|
195
245
|
break;
|
|
196
246
|
}
|
|
247
|
+
case 'clause': {
|
|
248
|
+
const clause = options.loadClause?.(node.id) ?? null;
|
|
249
|
+
if (!clause) {
|
|
250
|
+
diagnostics.push((0, diagnostics_1.diag)('error', `clause not found: ${node.id}`, node.loc));
|
|
251
|
+
break;
|
|
252
|
+
}
|
|
253
|
+
if (stack.includes(clause.template.file)) {
|
|
254
|
+
diagnostics.push((0, diagnostics_1.diag)('error', `clause ${node.id} includes itself`, node.loc));
|
|
255
|
+
break;
|
|
256
|
+
}
|
|
257
|
+
if (clauses.some((c) => c.id === node.id)) {
|
|
258
|
+
diagnostics.push((0, diagnostics_1.diag)('error', `clause ${node.id} is placed twice`, node.loc));
|
|
259
|
+
break;
|
|
260
|
+
}
|
|
261
|
+
clauses.push({ id: node.id, version: clause.version, file: clause.template.file });
|
|
262
|
+
diagnostics.push(...clause.template.diagnostics);
|
|
263
|
+
out += `\\lkclause{${clause.title ? (0, escape_1.escapeLatex)(clause.title) : ''}}{${(0, clause_1.clauseLabel)(node.id)}}`;
|
|
264
|
+
stack.push(clause.template.file);
|
|
265
|
+
out += render(clause.template.nodes);
|
|
266
|
+
if (clause.render)
|
|
267
|
+
out += callComponent(clause.render, options.data, '', `clause ${node.id}`, node.loc);
|
|
268
|
+
stack.pop();
|
|
269
|
+
break;
|
|
270
|
+
}
|
|
271
|
+
case 'clauseref':
|
|
272
|
+
refs.push({ id: node.id, loc: node.loc });
|
|
273
|
+
out += `\\ref{${(0, clause_1.clauseLabel)(node.id)}}`;
|
|
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
|
+
}
|
|
197
295
|
}
|
|
198
296
|
}
|
|
199
297
|
return out;
|
|
200
298
|
};
|
|
201
299
|
const tex = render(template.nodes);
|
|
202
|
-
|
|
300
|
+
// a reference to a clause this document does not contain would print "??"
|
|
301
|
+
for (const r of refs) {
|
|
302
|
+
if (!clauses.some((c) => c.id === r.id)) {
|
|
303
|
+
const msg = `\\clauseref{${r.id}}: clause ${r.id} is not in this document`;
|
|
304
|
+
diagnostics.push((0, diagnostics_1.diag)(allowMissing ? 'warning' : 'error', msg, r.loc));
|
|
305
|
+
}
|
|
306
|
+
}
|
|
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() };
|
|
203
313
|
}
|
package/index.d.ts
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
export * from './analyze';
|
|
2
|
+
export * from './clause';
|
|
3
|
+
export * from './codemod';
|
|
2
4
|
export * from './compile';
|
|
5
|
+
export * from './component';
|
|
3
6
|
export * from './condition';
|
|
4
7
|
export * from './diagnostics';
|
|
5
8
|
export * from './document';
|
|
@@ -7,4 +10,5 @@ export * from './evaluate';
|
|
|
7
10
|
export * from './filters';
|
|
8
11
|
export * from './jsonschema';
|
|
9
12
|
export * from './path';
|
|
13
|
+
export * from './signature';
|
|
10
14
|
export * from './sty';
|
package/index.js
CHANGED
|
@@ -15,7 +15,10 @@ 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);
|
|
21
|
+
__exportStar(require("./component"), exports);
|
|
19
22
|
__exportStar(require("./condition"), exports);
|
|
20
23
|
__exportStar(require("./diagnostics"), exports);
|
|
21
24
|
__exportStar(require("./document"), exports);
|
|
@@ -23,4 +26,5 @@ __exportStar(require("./evaluate"), exports);
|
|
|
23
26
|
__exportStar(require("./filters"), exports);
|
|
24
27
|
__exportStar(require("./jsonschema"), exports);
|
|
25
28
|
__exportStar(require("./path"), exports);
|
|
29
|
+
__exportStar(require("./signature"), exports);
|
|
26
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
|
|
|
@@ -35,6 +38,37 @@ exports.LATEX_KIT_STY = `% latex-kit.sty: support for latex-kit templates.
|
|
|
35
38
|
\\setlength\\lkblankwidth{3.5cm}
|
|
36
39
|
\\newcommand\\lkblank[1]{\\leavevmode\\underline{\\makebox[\\lkblankwidth][l]{\\color{lkmeta}\\tiny\\ttfamily\\detokenize{#1}}}}
|
|
37
40
|
|
|
41
|
+
% A clause from the library: \\lkclause{title}{label}. Numbered like a
|
|
42
|
+
% section; with no title, a bold number. Redefine it to change the style.
|
|
43
|
+
\\providecommand\\lkclause[2]{\\ifx\\relax#1\\relax\\par\\refstepcounter{section}\\label{#2}\\noindent\\textbf{\\thesection.}~\\else\\section{#1}\\label{#2}\\fi}
|
|
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
|
+
|
|
38
72
|
% A value the data does not have yet (draft builds only).
|
|
39
73
|
\\newcommand\\lkmissing[1]{\\leavevmode{\\color{red}\\fbox{\\scriptsize\\ttfamily missing: \\detokenize{#1}}}}
|
|
40
74
|
|
|
@@ -58,7 +92,10 @@ exports.LATEX_KIT_STY = `% latex-kit.sty: support for latex-kit templates.
|
|
|
58
92
|
\\newcommand\\each[3]{{\\color{lkmeta}\\tiny[each \\detokenize{#2} in \\detokenize{#1}]}#3}
|
|
59
93
|
\\newcommand\\sep[1]{#1}
|
|
60
94
|
\\newcommand\\snippet[1]{\\InputIfFileExists{#1}{}{\\lkblank{snippet #1}}}
|
|
61
|
-
\\newcommand\\
|
|
95
|
+
\\newcommand\\clause[1]{\\par\\lkblank{clause #1}\\par}
|
|
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}
|
|
62
99
|
\\fi
|
|
63
100
|
|
|
64
101
|
\\endinput
|