auto-hwpx 0.1.0 → 0.1.2

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/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "auto-hwpx",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "license": "MIT",
7
7
  "description": "TypeScript utility for replacing text and image placeholders in HWPX files.",
8
8
  "bin": {
9
- "auto-hwpx": "./dist/cli.js"
9
+ "auto-hwpx": "./dist/cli-v2.js"
10
10
  },
11
11
  "main": "./dist/index.js",
12
12
  "types": "./dist/index.d.ts",
@@ -36,16 +36,13 @@
36
36
  "typecheck": "tsc -p tsconfig.json",
37
37
  "test": "vitest run",
38
38
  "test:watch": "vitest",
39
- "start": "node --import tsx src/cli.ts",
40
- "demo": "node --import tsx scripts/demo.ts",
39
+ "start": "node --import tsx src/cli-v2.ts",
40
+ "demo": "./scripts/run-cli-demo-v2.sh",
41
41
  "prepublishOnly": "npm run test && npm run build"
42
42
  },
43
43
  "dependencies": {
44
- "@xmldom/xmldom": "^0.8.10",
45
44
  "commander": "^13.1.0",
46
- "image-size": "^2.0.2",
47
- "jszip": "^3.10.1",
48
- "xpath": "^0.0.34"
45
+ "jszip": "^3.10.1"
49
46
  },
50
47
  "devDependencies": {
51
48
  "@types/node": "^22.15.18",
package/dist/cli.d.ts DELETED
@@ -1,3 +0,0 @@
1
- #!/usr/bin/env node
2
- export {};
3
- //# sourceMappingURL=cli.d.ts.map
package/dist/cli.d.ts.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"cli.d.ts","sourceRoot":"","sources":["../src/cli.ts"],"names":[],"mappings":""}
package/dist/cli.js DELETED
@@ -1,68 +0,0 @@
1
- #!/usr/bin/env node
2
- import { promises as fs } from "node:fs";
3
- import { Command } from "commander";
4
- import { replaceHwpxPlaceholders } from "./render-hwpx.js";
5
- function isPlainObject(value) {
6
- if (typeof value !== "object" || value === null || Array.isArray(value)) {
7
- return false;
8
- }
9
- return Object.values(value).every((item) => typeof item === "string");
10
- }
11
- async function parseJsonMapInput(raw, label) {
12
- let source = raw;
13
- try {
14
- await fs.access(raw);
15
- source = await fs.readFile(raw, "utf8");
16
- }
17
- catch {
18
- // Not a file path. Treat as inline JSON text.
19
- }
20
- let parsed;
21
- try {
22
- parsed = JSON.parse(source);
23
- }
24
- catch (error) {
25
- throw new Error(`Invalid ${label} JSON: ${String(error)}`);
26
- }
27
- if (!isPlainObject(parsed)) {
28
- throw new Error(`${label} must be a JSON object of string values.`);
29
- }
30
- return parsed;
31
- }
32
- async function main() {
33
- const program = new Command();
34
- program
35
- .name("auto-hwpx")
36
- .description("Replace text and image placeholders in a .hwpx file.")
37
- .requiredOption("--input <path>", "Input .hwpx file path")
38
- .requiredOption("--output <path>", "Output .hwpx file path")
39
- .option("--text-json <jsonOrPath>", "Text replacement JSON object or path", "{}")
40
- .option("--images-json <jsonOrPath>", "Image replacement JSON object or path", "{}")
41
- .option("--image-template <path>", "Template .hwpx containing a donor hp:pic run");
42
- program.parse(process.argv);
43
- const options = program.opts();
44
- const [textReplacements, imageReplacements] = await Promise.all([
45
- parseJsonMapInput(options.textJson, "--text-json"),
46
- parseJsonMapInput(options.imagesJson, "--images-json"),
47
- ]);
48
- const result = await replaceHwpxPlaceholders({
49
- inputPath: options.input,
50
- outputPath: options.output,
51
- replacements: {
52
- text: textReplacements,
53
- images: imageReplacements,
54
- },
55
- imageTemplatePath: options.imageTemplate,
56
- });
57
- process.stdout.write(`${JSON.stringify({
58
- outputPath: options.output,
59
- textReplacements: result.textReplacements,
60
- imageReplacements: result.imageReplacements,
61
- unresolvedPlaceholders: result.unresolvedPlaceholders,
62
- }, null, 2)}\n`);
63
- }
64
- main().catch((error) => {
65
- process.stderr.write(`${error instanceof Error ? error.message : String(error)}\n`);
66
- process.exitCode = 1;
67
- });
68
- //# sourceMappingURL=cli.js.map
package/dist/cli.js.map DELETED
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BAAsB,EAAE,MAAM,CAAC,sBAAsB;KACtD,EACD,IAAI,EACJ,CAAC,CACF,IAAI,CACN,CAAC;AACJ,CAAC;AAED,IAAI,EAAE,CAAC,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE;IACrB,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,GAAG,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;IACpF,OAAO,CAAC,QAAQ,GAAG,CAAC,CAAC;AACvB,CAAC,CAAC,CAAC"}
@@ -1,16 +0,0 @@
1
- export interface ReplaceHwpxInput {
2
- inputPath: string;
3
- outputPath: string;
4
- replacements: {
5
- text: Record<string, string>;
6
- images: Record<string, string>;
7
- };
8
- imageTemplatePath?: string;
9
- }
10
- export interface ReplaceHwpxResult {
11
- textReplacements: number;
12
- imageReplacements: number;
13
- unresolvedPlaceholders: string[];
14
- }
15
- export declare function replaceHwpxPlaceholders(input: ReplaceHwpxInput): Promise<ReplaceHwpxResult>;
16
- //# sourceMappingURL=render-hwpx.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"render-hwpx.d.ts","sourceRoot":"","sources":["../src/render-hwpx.ts"],"names":[],"mappings":"AAOA,MAAM,WAAW,gBAAgB;IAC/B,SAAS,EAAE,MAAM,CAAC;IAClB,UAAU,EAAE,MAAM,CAAC;IACnB,YAAY,EAAE;QACZ,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;QAC7B,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;KAChC,CAAC;IACF,iBAAiB,CAAC,EAAE,MAAM,CAAC;CAC5B;AAED,MAAM,WAAW,iBAAiB;IAChC,gBAAgB,EAAE,MAAM,CAAC;IACzB,iBAAiB,EAAE,MAAM,CAAC;IAC1B,sBAAsB,EAAE,MAAM,EAAE,CAAC;CAClC;AAmgBD,wBAAsB,uBAAuB,CAC3C,KAAK,EAAE,gBAAgB,GACtB,OAAO,CAAC,iBAAiB,CAAC,CA0J5B"}
@@ -1,518 +0,0 @@
1
- import { promises as fs } from "node:fs";
2
- import path from "node:path";
3
- import JSZip from "jszip";
4
- import { DOMParser, XMLSerializer } from "@xmldom/xmldom";
5
- import { imageSize } from "image-size";
6
- import xpath from "xpath";
7
- const DEFAULT_TEMPLATE_PATH = path.resolve(process.cwd(), "hwpx-examples/with-img.hwpx");
8
- const XML_NS = {
9
- hp: "http://www.hancom.co.kr/hwpml/2011/paragraph",
10
- hc: "http://www.hancom.co.kr/hwpml/2011/core",
11
- opf: "http://www.idpf.org/2007/opf/",
12
- };
13
- const select = xpath.useNamespaces(XML_NS);
14
- const XML_DECLARATION_RE = /^<\?xml[^>]+?>/;
15
- const PLACEHOLDER_RE = /(?:&lt;|<)@([^=@>\s]+)(?:=([^@]*?))?@(?:&gt;|>)/g;
16
- const IMAGE_PLACEHOLDER_RE = /^<@([^=@>\s]+)(?:=([^@]*?))?@>$/;
17
- function parseXml(xmlText) {
18
- return new DOMParser({
19
- errorHandler: {
20
- warning: () => undefined,
21
- error: (message) => {
22
- throw new Error(`XML parse error: ${message}`);
23
- },
24
- fatalError: (message) => {
25
- throw new Error(`XML fatal parse error: ${message}`);
26
- },
27
- },
28
- }).parseFromString(xmlText, "application/xml");
29
- }
30
- function serializeXml(doc, originalText) {
31
- const serialized = new XMLSerializer().serializeToString(doc);
32
- if (XML_DECLARATION_RE.test(serialized)) {
33
- return serialized;
34
- }
35
- const originalDecl = originalText.match(XML_DECLARATION_RE)?.[0];
36
- if (originalDecl) {
37
- return `${originalDecl}${serialized}`;
38
- }
39
- return serialized;
40
- }
41
- function escapeXmlText(text) {
42
- return text
43
- .replaceAll("&", "&amp;")
44
- .replaceAll("<", "&lt;")
45
- .replaceAll(">", "&gt;")
46
- .replaceAll('"', "&quot;")
47
- .replaceAll("'", "&apos;");
48
- }
49
- function isTextEntry(fileName) {
50
- return (fileName.endsWith(".xml") ||
51
- fileName.endsWith(".txt") ||
52
- fileName.endsWith(".hpf"));
53
- }
54
- function isSectionEntry(fileName) {
55
- return /^Contents\/section\d+\.xml$/.test(fileName);
56
- }
57
- function decodePlaceholderToken(text) {
58
- return text.replaceAll("&lt;", "<").replaceAll("&gt;", ">");
59
- }
60
- function replaceTextPlaceholders(params) {
61
- const { input, textValues, imageKeys, escapeOutput, unresolved } = params;
62
- let replaced = 0;
63
- const output = input.replaceAll(PLACEHOLDER_RE, (fullMatch, key, defaultValue) => {
64
- if (imageKeys.has(key)) {
65
- return fullMatch;
66
- }
67
- const explicitValue = textValues[key];
68
- if (explicitValue !== undefined) {
69
- replaced += 1;
70
- return escapeOutput ? escapeXmlText(explicitValue) : explicitValue;
71
- }
72
- if (defaultValue !== undefined) {
73
- replaced += 1;
74
- return escapeOutput ? escapeXmlText(defaultValue) : defaultValue;
75
- }
76
- unresolved.add(key);
77
- return fullMatch;
78
- });
79
- return { output, replaced };
80
- }
81
- function firstElementByXPath(expr, contextNode) {
82
- const found = select(expr, contextNode);
83
- if (found.length === 0) {
84
- return null;
85
- }
86
- return found[0];
87
- }
88
- function listElementsByXPath(expr, contextNode) {
89
- return select(expr, contextNode).map((node) => node);
90
- }
91
- function parseIntAttr(element, attrName) {
92
- if (!element) {
93
- return null;
94
- }
95
- const raw = element.getAttribute(attrName);
96
- if (!raw) {
97
- return null;
98
- }
99
- const parsed = Number.parseInt(raw, 10);
100
- return Number.isFinite(parsed) ? parsed : null;
101
- }
102
- function setIntAttr(element, attrName, value) {
103
- if (!element) {
104
- return;
105
- }
106
- element.setAttribute(attrName, String(Math.max(1, Math.round(value))));
107
- }
108
- function detectImageTypeAndSize(buffer) {
109
- const details = imageSize(buffer);
110
- if (!details.width || !details.height || !details.type) {
111
- throw new Error("Unable to detect image dimensions and format.");
112
- }
113
- const normalizedType = details.type.toLowerCase();
114
- const extensionMap = {
115
- jpg: "jpg",
116
- jpeg: "jpeg",
117
- png: "png",
118
- gif: "gif",
119
- bmp: "bmp",
120
- tiff: "tiff",
121
- tif: "tif",
122
- svg: "svg",
123
- };
124
- const mediaTypeMap = {
125
- jpg: "image/jpeg",
126
- jpeg: "image/jpeg",
127
- png: "image/png",
128
- gif: "image/gif",
129
- bmp: "image/bmp",
130
- tiff: "image/tiff",
131
- tif: "image/tiff",
132
- svg: "image/svg+xml",
133
- };
134
- const extension = extensionMap[normalizedType];
135
- const mediaType = mediaTypeMap[normalizedType];
136
- if (!extension || !mediaType) {
137
- throw new Error(`Unsupported image type: ${details.type}`);
138
- }
139
- return {
140
- extension,
141
- mediaType,
142
- width: details.width,
143
- height: details.height,
144
- };
145
- }
146
- function calculateDisplaySize(params) {
147
- // HWPX image dimensions in the sample map closely to 75 HWP units per pixel.
148
- const unitPerPixel = 75;
149
- const originalWidth = Math.max(1, Math.round(params.pixelWidth * unitPerPixel));
150
- const originalHeight = Math.max(1, Math.round(params.pixelHeight * unitPerPixel));
151
- const maxWidth = Math.max(1, Math.round(params.maxWidth));
152
- const maxHeight = Math.max(1, Math.round(params.maxHeight));
153
- const scale = Math.min(maxWidth / originalWidth, maxHeight / originalHeight);
154
- const clampedScale = Number.isFinite(scale) && scale > 0 ? scale : 1;
155
- return {
156
- width: Math.max(1, Math.round(originalWidth * clampedScale)),
157
- height: Math.max(1, Math.round(originalHeight * clampedScale)),
158
- };
159
- }
160
- function getRunParagraph(runElement) {
161
- let current = runElement.parentNode;
162
- while (current) {
163
- if (current.nodeType === current.ELEMENT_NODE) {
164
- const element = current;
165
- if (element.localName === "p" && element.prefix === "hp") {
166
- return element;
167
- }
168
- }
169
- current = current.parentNode;
170
- }
171
- return null;
172
- }
173
- function getParentTableCell(runElement) {
174
- let current = runElement.parentNode;
175
- while (current) {
176
- if (current.nodeType === current.ELEMENT_NODE) {
177
- const element = current;
178
- if (element.localName === "tc" && element.prefix === "hp") {
179
- return element;
180
- }
181
- }
182
- current = current.parentNode;
183
- }
184
- return null;
185
- }
186
- function resolvePlaceholderBox(runElement) {
187
- const cell = getParentTableCell(runElement);
188
- if (cell) {
189
- const cellSize = firstElementByXPath("./hp:cellSz", cell);
190
- const cellMargin = firstElementByXPath("./hp:cellMargin", cell);
191
- const width = parseIntAttr(cellSize, "width");
192
- const height = parseIntAttr(cellSize, "height");
193
- if (width && height) {
194
- const marginLeft = parseIntAttr(cellMargin, "left") ?? 0;
195
- const marginRight = parseIntAttr(cellMargin, "right") ?? 0;
196
- const marginTop = parseIntAttr(cellMargin, "top") ?? 0;
197
- const marginBottom = parseIntAttr(cellMargin, "bottom") ?? 0;
198
- return {
199
- width: Math.max(1, width - marginLeft - marginRight),
200
- height: Math.max(1, height - marginTop - marginBottom),
201
- };
202
- }
203
- }
204
- const paragraph = getRunParagraph(runElement);
205
- if (paragraph) {
206
- const lineSeg = firstElementByXPath("./hp:linesegarray/hp:lineseg", paragraph);
207
- const width = parseIntAttr(lineSeg, "horzsize");
208
- const height = parseIntAttr(lineSeg, "textheight");
209
- if (width && height) {
210
- return { width, height };
211
- }
212
- }
213
- return { width: 26280, height: 40980 };
214
- }
215
- function setPictureGeometry(picElement, imageResource, displaySize) {
216
- const originalWidth = imageResource.originalWidth;
217
- const originalHeight = imageResource.originalHeight;
218
- const width = displaySize.width;
219
- const height = displaySize.height;
220
- const orgSz = firstElementByXPath("./hp:orgSz", picElement);
221
- setIntAttr(orgSz, "width", width);
222
- setIntAttr(orgSz, "height", height);
223
- const sz = firstElementByXPath("./hp:sz", picElement);
224
- setIntAttr(sz, "width", width);
225
- setIntAttr(sz, "height", height);
226
- const rotationInfo = firstElementByXPath("./hp:rotationInfo", picElement);
227
- setIntAttr(rotationInfo, "centerX", Math.round(width / 2));
228
- setIntAttr(rotationInfo, "centerY", Math.round(height / 2));
229
- const imgRef = firstElementByXPath("./hc:img", picElement);
230
- if (!imgRef) {
231
- throw new Error("Template picture is missing hc:img node.");
232
- }
233
- imgRef.setAttribute("binaryItemIDRef", imageResource.itemId);
234
- const point0 = firstElementByXPath("./hp:imgRect/hc:pt0", picElement);
235
- const point1 = firstElementByXPath("./hp:imgRect/hc:pt1", picElement);
236
- const point2 = firstElementByXPath("./hp:imgRect/hc:pt2", picElement);
237
- const point3 = firstElementByXPath("./hp:imgRect/hc:pt3", picElement);
238
- if (!point0 || !point1 || !point2 || !point3) {
239
- throw new Error("Template picture is missing imgRect points.");
240
- }
241
- point0.setAttribute("x", "0");
242
- point0.setAttribute("y", "0");
243
- point1.setAttribute("x", String(width));
244
- point1.setAttribute("y", "0");
245
- point2.setAttribute("x", String(width));
246
- point2.setAttribute("y", String(height));
247
- point3.setAttribute("x", "0");
248
- point3.setAttribute("y", String(height));
249
- const clip = firstElementByXPath("./hp:imgClip", picElement);
250
- if (clip) {
251
- clip.setAttribute("left", "0");
252
- clip.setAttribute("top", "0");
253
- clip.setAttribute("right", String(originalWidth));
254
- clip.setAttribute("bottom", String(originalHeight));
255
- }
256
- const dimension = firstElementByXPath("./hp:imgDim", picElement);
257
- if (dimension) {
258
- dimension.setAttribute("dimwidth", String(originalWidth));
259
- dimension.setAttribute("dimheight", String(originalHeight));
260
- }
261
- }
262
- function buildRunFromTemplate(params) {
263
- const doc = parseXml(`<root xmlns:hp="${XML_NS.hp}" xmlns:hc="${XML_NS.hc}">${params.templateRunXml}</root>`);
264
- const runElement = firstElementByXPath("/root/hp:run", doc);
265
- if (!runElement) {
266
- throw new Error("Unable to parse template run XML.");
267
- }
268
- if (params.charPrIDRef) {
269
- runElement.setAttribute("charPrIDRef", params.charPrIDRef);
270
- }
271
- const pic = firstElementByXPath("./hp:pic", runElement);
272
- if (!pic) {
273
- throw new Error("Template run does not contain hp:pic.");
274
- }
275
- pic.setAttribute("id", String(params.picId));
276
- pic.setAttribute("instid", String(params.instId));
277
- setPictureGeometry(pic, params.imageResource, params.displaySize);
278
- if (!firstElementByXPath("./hp:t", runElement)) {
279
- const emptyText = doc.createElementNS(XML_NS.hp, "hp:t");
280
- runElement.appendChild(emptyText);
281
- }
282
- return runElement;
283
- }
284
- function findNextImageIndex(contentDoc) {
285
- const itemNodes = listElementsByXPath("/opf:package/opf:manifest/opf:item", contentDoc);
286
- let max = 0;
287
- for (const item of itemNodes) {
288
- const id = item.getAttribute("id");
289
- if (!id) {
290
- continue;
291
- }
292
- const match = /^image(\d+)$/.exec(id);
293
- if (!match) {
294
- continue;
295
- }
296
- max = Math.max(max, Number.parseInt(match[1], 10));
297
- }
298
- return max + 1;
299
- }
300
- function ensureManifestItem(contentDoc, itemId, href, mediaType) {
301
- const manifest = firstElementByXPath("/opf:package/opf:manifest", contentDoc);
302
- if (!manifest) {
303
- throw new Error("Contents/content.hpf is missing opf:manifest.");
304
- }
305
- const existing = listElementsByXPath("./opf:item", manifest).find((item) => item.getAttribute("id") === itemId);
306
- if (existing) {
307
- existing.setAttribute("href", href);
308
- existing.setAttribute("media-type", mediaType);
309
- existing.setAttribute("isEmbeded", "1");
310
- return;
311
- }
312
- const item = contentDoc.createElementNS(XML_NS.opf, "opf:item");
313
- item.setAttribute("id", itemId);
314
- item.setAttribute("href", href);
315
- item.setAttribute("media-type", mediaType);
316
- item.setAttribute("isEmbeded", "1");
317
- manifest.appendChild(item);
318
- }
319
- function collectMaxNumericAttributes(sectionDocs) {
320
- let maxId = 1;
321
- let maxInstId = 1;
322
- for (const doc of sectionDocs) {
323
- const allElements = listElementsByXPath("//*", doc);
324
- for (const element of allElements) {
325
- const idAttr = element.getAttribute("id");
326
- if (idAttr && /^\d+$/.test(idAttr)) {
327
- maxId = Math.max(maxId, Number.parseInt(idAttr, 10));
328
- }
329
- const instAttr = element.getAttribute("instid");
330
- if (instAttr && /^\d+$/.test(instAttr)) {
331
- maxInstId = Math.max(maxInstId, Number.parseInt(instAttr, 10));
332
- }
333
- }
334
- }
335
- return { maxId, maxInstId };
336
- }
337
- async function loadTemplateRunXml(imageTemplatePath) {
338
- const templateBytes = await fs.readFile(imageTemplatePath);
339
- const templateZip = await JSZip.loadAsync(templateBytes);
340
- const sectionNames = Object.keys(templateZip.files).filter(isSectionEntry).sort();
341
- for (const sectionName of sectionNames) {
342
- const sectionFile = templateZip.file(sectionName);
343
- if (!sectionFile) {
344
- continue;
345
- }
346
- const xml = await sectionFile.async("string");
347
- const doc = parseXml(xml);
348
- const run = firstElementByXPath("//hp:run[hp:pic]", doc);
349
- if (run) {
350
- return new XMLSerializer().serializeToString(run);
351
- }
352
- }
353
- throw new Error(`No template hp:run with hp:pic was found in image template: ${imageTemplatePath}`);
354
- }
355
- function parsePlaceholderInTextNode(text) {
356
- const decoded = decodePlaceholderToken(text.trim());
357
- const matched = IMAGE_PLACEHOLDER_RE.exec(decoded);
358
- if (!matched) {
359
- return null;
360
- }
361
- return { key: matched[1] };
362
- }
363
- async function ensureImageResource(params) {
364
- const cached = params.cache.get(params.imageKey);
365
- if (cached) {
366
- return cached;
367
- }
368
- let fileBuffer;
369
- try {
370
- fileBuffer = await fs.readFile(params.imagePath);
371
- }
372
- catch (error) {
373
- throw new Error(`Image file not found for key "${params.imageKey}": ${params.imagePath}`, { cause: error });
374
- }
375
- const details = detectImageTypeAndSize(fileBuffer);
376
- const itemId = `image${params.nextImageIndexRef.value++}`;
377
- const fileName = `${itemId}.${details.extension}`;
378
- const zipEntryPath = `BinData/${fileName}`;
379
- params.zip.file(zipEntryPath, fileBuffer);
380
- ensureManifestItem(params.contentDoc, itemId, zipEntryPath, details.mediaType);
381
- const resource = {
382
- itemId,
383
- fileName,
384
- mediaType: details.mediaType,
385
- originalWidth: Math.max(1, Math.round(details.width * 75)),
386
- originalHeight: Math.max(1, Math.round(details.height * 75)),
387
- };
388
- params.cache.set(params.imageKey, resource);
389
- return resource;
390
- }
391
- export async function replaceHwpxPlaceholders(input) {
392
- const imageTemplatePath = input.imageTemplatePath ?? DEFAULT_TEMPLATE_PATH;
393
- const [inputBytes] = await Promise.all([
394
- fs.readFile(input.inputPath),
395
- fs.access(imageTemplatePath),
396
- ]);
397
- const zip = await JSZip.loadAsync(inputBytes);
398
- const unresolved = new Set();
399
- const imageKeys = new Set(Object.keys(input.replacements.images));
400
- let textReplacements = 0;
401
- const allEntries = Object.keys(zip.files);
402
- for (const entryName of allEntries) {
403
- const entry = zip.file(entryName);
404
- if (!entry || !isTextEntry(entryName)) {
405
- continue;
406
- }
407
- const sourceText = await entry.async("string");
408
- const replaced = replaceTextPlaceholders({
409
- input: sourceText,
410
- textValues: input.replacements.text,
411
- imageKeys,
412
- escapeOutput: entryName.endsWith(".xml") || entryName.endsWith(".hpf"),
413
- unresolved,
414
- });
415
- textReplacements += replaced.replaced;
416
- if (replaced.output !== sourceText) {
417
- zip.file(entryName, replaced.output);
418
- }
419
- }
420
- const templateRunXml = await loadTemplateRunXml(imageTemplatePath);
421
- const contentEntry = zip.file("Contents/content.hpf");
422
- if (!contentEntry) {
423
- throw new Error("Input HWPX is missing Contents/content.hpf.");
424
- }
425
- const contentText = await contentEntry.async("string");
426
- const contentDoc = parseXml(contentText);
427
- const nextImageIndexRef = { value: findNextImageIndex(contentDoc) };
428
- const imageResourceCache = new Map();
429
- const sectionNames = allEntries.filter(isSectionEntry).sort();
430
- const sectionDocs = new Map();
431
- for (const sectionName of sectionNames) {
432
- const sectionText = await zip.file(sectionName).async("string");
433
- sectionDocs.set(sectionName, { original: sectionText, doc: parseXml(sectionText) });
434
- }
435
- const initialMax = collectMaxNumericAttributes(Array.from(sectionDocs.values(), (item) => item.doc));
436
- let nextPicId = initialMax.maxId + 1;
437
- let nextInstId = initialMax.maxInstId + 1;
438
- let imageReplacements = 0;
439
- for (const [sectionName, section] of sectionDocs) {
440
- const runNodes = listElementsByXPath("//hp:run[hp:t]", section.doc);
441
- let changed = false;
442
- for (const runNode of runNodes) {
443
- const textNode = firstElementByXPath("./hp:t", runNode);
444
- if (!textNode || textNode.textContent === null) {
445
- continue;
446
- }
447
- const placeholder = parsePlaceholderInTextNode(textNode.textContent);
448
- if (!placeholder) {
449
- continue;
450
- }
451
- const imagePath = input.replacements.images[placeholder.key];
452
- if (!imagePath) {
453
- continue;
454
- }
455
- const imageResource = await ensureImageResource({
456
- zip,
457
- contentDoc,
458
- cache: imageResourceCache,
459
- imageKey: placeholder.key,
460
- imagePath,
461
- nextImageIndexRef,
462
- });
463
- const box = resolvePlaceholderBox(runNode);
464
- const displaySize = calculateDisplaySize({
465
- pixelWidth: Math.max(1, Math.round(imageResource.originalWidth / 75)),
466
- pixelHeight: Math.max(1, Math.round(imageResource.originalHeight / 75)),
467
- maxWidth: box.width,
468
- maxHeight: box.height,
469
- });
470
- const newRun = buildRunFromTemplate({
471
- templateRunXml,
472
- charPrIDRef: runNode.getAttribute("charPrIDRef"),
473
- imageResource,
474
- displaySize,
475
- picId: nextPicId++,
476
- instId: nextInstId++,
477
- });
478
- const parent = runNode.parentNode;
479
- if (!parent) {
480
- continue;
481
- }
482
- const importedNode = section.doc.importNode
483
- ? section.doc.importNode(newRun, true)
484
- : newRun.cloneNode(true);
485
- parent.replaceChild(importedNode, runNode);
486
- const paragraph = importedNode.nodeType === importedNode.ELEMENT_NODE
487
- ? getRunParagraph(importedNode)
488
- : null;
489
- const lineSeg = paragraph
490
- ? firstElementByXPath("./hp:linesegarray/hp:lineseg", paragraph)
491
- : null;
492
- if (lineSeg) {
493
- lineSeg.setAttribute("horzsize", "0");
494
- }
495
- imageReplacements += 1;
496
- changed = true;
497
- }
498
- if (changed) {
499
- const serialized = serializeXml(section.doc, section.original);
500
- zip.file(sectionName, serialized);
501
- }
502
- }
503
- const updatedContent = serializeXml(contentDoc, contentText);
504
- zip.file("Contents/content.hpf", updatedContent);
505
- const outputBytes = await zip.generateAsync({
506
- type: "nodebuffer",
507
- compression: "DEFLATE",
508
- compressionOptions: { level: 9 },
509
- });
510
- await fs.mkdir(path.dirname(input.outputPath), { recursive: true });
511
- await fs.writeFile(input.outputPath, outputBytes);
512
- return {
513
- textReplacements,
514
- imageReplacements,
515
- unresolvedPlaceholders: [...unresolved].sort(),
516
- };
517
- }
518
- //# sourceMappingURL=render-hwpx.js.map