JGOS-Origin / pkg /extract_hwp.mjs
openfree's picture
feat: HWP/HWPX ์‹ค์ œ ์ถ”์ถœ (rhwp WASM + Node 22 Docker) โ€” getSectionCount/getParagraphCount fix
1ae2310 verified
Raw
History Blame Contribute Delete
2.26 kB
// extract_hwp.mjs โ€” HWP/HWPX ํŒŒ์ผ์„ ํ…์ŠคํŠธ๋กœ ์ถ”์ถœ (rhwp WASM ์‚ฌ์šฉ)
// ์‚ฌ์šฉ: node extract_hwp.mjs <input_hwp_path>
// ์ถœ๋ ฅ: stdout JSON {ok, text, paragraphs, sections, error?}
import init, { HwpDocument } from "./rhwp.js";
import { readFile } from "node:fs/promises";
async function main() {
const inputPath = process.argv[2];
if (!inputPath) {
console.error(JSON.stringify({ ok: false, error: "missing arg: input path" }));
process.exit(1);
}
try {
// WASM ์ดˆ๊ธฐํ™” โ€” Node.js fs๋กœ ์ง์ ‘ ๋กœ๋“œ
const { readFileSync } = await import("node:fs");
const wasmPath = new URL("./rhwp_bg.wasm", import.meta.url);
const wasmBytes = readFileSync(wasmPath);
await init({ module_or_path: wasmBytes });
// HWP ํŒŒ์ผ ๋กœ๋“œ
const fileBytes = await readFile(inputPath);
const doc = new HwpDocument(new Uint8Array(fileBytes));
// Method 1: getTextRange๋กœ ๋ชจ๋“  section/paragraph ์ˆœํšŒ
const allParas = [];
let sectionCount = 0;
try {
sectionCount = doc.getSectionCount();
} catch (e) {
sectionCount = 1;
}
for (let s = 0; s < sectionCount; s++) {
let paraCount = 0;
try {
paraCount = doc.getParagraphCount(s);
} catch (e) { continue; }
for (let p = 0; p < paraCount; p++) {
let paraLen = 0;
try {
paraLen = doc.getParagraphLength(s, p);
} catch (e) { continue; }
if (paraLen === 0) { allParas.push(""); continue; }
try {
const text = doc.getTextRange(s, p, 0, paraLen);
allParas.push(text || "");
} catch (e) {
allParas.push(`[para ${s}.${p} ์ถ”์ถœ ์‹คํŒจ]`);
}
}
}
const finalText = allParas.filter(p => p && p.trim()).join("\n");
// free
try { doc.free(); } catch (e) {}
console.log(JSON.stringify({
ok: true,
text: finalText,
paragraphs: allParas.length,
sections: sectionCount,
char_count: finalText.length,
}, null, 0));
} catch (e) {
console.error(JSON.stringify({
ok: false,
error: `${e.name || "Error"}: ${e.message || e}`,
stack: (e.stack || "").split("\n").slice(0, 5).join(" | "),
}));
process.exit(2);
}
}
main();