UNPKG

open-agreements

Version:

Open-source legal template filling CLI and library

179 lines • 8.43 kB
import AdmZip from 'adm-zip'; import { DOMParser, XMLSerializer } from '@xmldom/xmldom'; import { readFileSync, writeFileSync } from 'node:fs'; import { z } from 'zod'; import { copyEntriesSkippingDirs, preserveXmlSpace } from './ooxml-parts.js'; const W_NS = 'http://schemas.openxmlformats.org/wordprocessingml/2006/main'; export const AnchoredParagraphBindingsConfigSchema = z.object({ groups: z.array(z.object({ id: z.string().min(1), start_anchor: z.string().min(1), end_anchor: z.string().min(1).optional(), end_at_document_end: z.literal(true).optional(), expected_group_matches: z.literal(1), bindings: z.array(z.object({ label: z.string().min(1), field: z.string().regex(/^[A-Za-z_][A-Za-z0-9_]*$/), expected_matches: z.literal(1), insert_after_label: z.literal(true), preserve_following_tabs: z.literal(true), /** Opt in only when following underlined tab-only runs are blank form tails. */ consume_following_underlined_tabs: z.literal(true).optional(), }).strict()).min(1), }).strict().refine((group) => Boolean(group.end_anchor) !== Boolean(group.end_at_document_end), { message: 'Specify exactly one of end_anchor or end_at_document_end', })).min(1), }).strict(); /** * Canonicalize only layout whitespace and Word's common smart-punctuation * substitutions. Matching remains case- and punctuation-sensitive otherwise. */ function canonicalMatchText(value) { return value .replace(/[\u2018\u2019]/g, "'") .replace(/[\u201C\u201D]/g, '"') .replace(/[\u2013\u2014]/g, '-') .replace(/\s+/g, ' ') .trim(); } function normalizedText(para) { const chunks = []; const walk = (node) => { if (node.nodeType !== 1) return; const el = node; if (el.namespaceURI === W_NS && el.localName === 't') chunks.push(el.textContent ?? ''); if (el.namespaceURI === W_NS && (el.localName === 'tab' || el.localName === 'br')) chunks.push(' '); for (let i = 0; i < el.childNodes.length; i++) walk(el.childNodes[i]); }; walk(para); return canonicalMatchText(chunks.join('')); } function directBodyParagraphs(doc) { const bodies = doc.getElementsByTagNameNS(W_NS, 'body'); if (bodies.length !== 1) throw new Error('anchored paragraph bindings: expected exactly one document body'); const result = []; const children = bodies[0].childNodes; for (let i = 0; i < children.length; i++) { const child = children[i]; if (child.nodeType === 1 && child.namespaceURI === W_NS && child.localName === 'p') result.push(child); } return result; } function exactAnchorIndices(paragraphs, anchor) { const expected = canonicalMatchText(anchor); const matches = []; for (let i = 0; i < paragraphs.length; i++) { if (normalizedText(paragraphs[i]) === expected) matches.push(i); } return matches; } /** Bind fields after plain labels between unique sibling body-paragraph anchors. */ export function bindAnchoredParagraphFields(inputPath, outputPath, rawConfig) { const config = AnchoredParagraphBindingsConfigSchema.parse(rawConfig); const zip = new AdmZip(inputPath); const entry = zip.getEntry('word/document.xml'); if (!entry) throw new Error('anchored paragraph bindings: word/document.xml not found'); const doc = new DOMParser().parseFromString(entry.getData().toString('utf-8'), 'text/xml'); const paragraphs = directBodyParagraphs(doc); // Validate the complete plan before mutation, so every failure is atomic. const insertions = []; for (const group of config.groups) { const starts = exactAnchorIndices(paragraphs, group.start_anchor); const ends = group.end_at_document_end ? [paragraphs.length] : exactAnchorIndices(paragraphs, group.end_anchor); if (starts.length !== group.expected_group_matches || ends.length !== group.expected_group_matches) { throw new Error(`anchored paragraph bindings '${group.id}': expected one unique start/end anchor; ` + `found start=${starts.length}, end=${ends.length}`); } const start = starts[0]; const end = ends[0]; if (end <= start) throw new Error(`anchored paragraph bindings '${group.id}': end anchor does not follow start anchor`); for (const binding of group.bindings) { const matches = []; const expectedLabel = canonicalMatchText(binding.label); for (let i = start + 1; i < end; i++) { const texts = paragraphs[i].getElementsByTagNameNS(W_NS, 't'); for (let j = 0; j < texts.length; j++) { if (canonicalMatchText(texts[j].textContent ?? '') === expectedLabel) matches.push(texts[j]); } } if (matches.length !== binding.expected_matches) { throw new Error(`anchored paragraph bindings '${group.id}' label '${binding.label}': ` + `expected one match inside boundaries; found ${matches.length}`); } if (insertions.some((insertion) => insertion.textNode === matches[0])) { throw new Error(`anchored paragraph bindings '${group.id}': duplicate binding target '${binding.label}'`); } insertions.push({ textNode: matches[0], field: binding.field, groupId: group.id, consumeFollowingUnderlinedTabs: binding.consume_following_underlined_tabs === true, }); } } for (const insertion of insertions) { const run = insertion.textNode.parentNode; if (!run || run.localName !== 'r' || run.namespaceURI !== W_NS) { throw new Error(`anchored paragraph bindings '${insertion.groupId}': label is not directly inside a run`); } const tag = doc.createElementNS(W_NS, 'w:t'); preserveXmlSpace(tag); // Labels such as "Name:" and "Title:" are authored without a trailing // space in many signature blocks. The inserted value is a new semantic // token, so keep a visible separator without disturbing the following tab // and underline layout carriers. tag.textContent = ` {${insertion.field}}`; run.insertBefore(tag, insertion.textNode.nextSibling); if (insertion.consumeFollowingUnderlinedTabs) { removeFollowingUnderlinedTabTails(run); } } const serialized = new XMLSerializer().serializeToString(doc); const outZip = new AdmZip(); copyEntriesSkippingDirs(zip, outZip, (name, data) => name === 'word/document.xml' ? Buffer.from(serialized, 'utf-8') : data); writeFileSync(outputPath, outZip.toBuffer()); } /** * Remove only underlined, textless tab runs following an anchored label/value. * Non-underlined layout tabs remain, and visible text, fields, breaks, drawings, * or any other semantic run content ends the scan. */ function removeFollowingUnderlinedTabTails(labelRun) { let sibling = labelRun.nextSibling; while (sibling) { const next = sibling.nextSibling; if (sibling.nodeType !== 1) { sibling = next; continue; } const run = sibling; if (run.namespaceURI !== W_NS || run.localName !== 'r') break; const texts = run.getElementsByTagNameNS(W_NS, 't'); const hasVisibleText = Array.from(texts).some((text) => (text.textContent ?? '').length > 0); const tabs = run.getElementsByTagNameNS(W_NS, 'tab'); const breaks = run.getElementsByTagNameNS(W_NS, 'br'); const fields = run.getElementsByTagNameNS(W_NS, 'fldChar'); const underlines = run.getElementsByTagNameNS(W_NS, 'u'); if (hasVisibleText || breaks.length > 0 || fields.length > 0) break; if (tabs.length > 0 && underlines.length > 0) { run.parentNode?.removeChild(run); } sibling = next; } } export function loadAnchoredParagraphBindingsConfig(path) { return AnchoredParagraphBindingsConfigSchema.parse(JSON.parse(readFileSync(path, 'utf-8'))); } //# sourceMappingURL=anchored-paragraph-bindings.js.map