Files
PM_test/scratch_parse_hwpx.js
2026-06-19 17:58:47 +09:00

58 lines
2.4 KiB
JavaScript

const fs = require('fs');
const jszip = require('jszip');
async function run() {
try {
const fileData = fs.readFileSync('downloaded_test.hwpx');
const zip = await jszip.loadAsync(fileData);
// List files in the HWPX zip
console.log('Zip file entries:');
zip.forEach((relativePath, file) => {
console.log(' -', relativePath);
});
// Find section0.xml or similar XML files containing text content
const sectionFile = Object.keys(zip.files).find(name => name.includes('section') && name.endsWith('.xml'));
if (sectionFile) {
console.log(`\nReading ${sectionFile}...`);
const content = await zip.file(sectionFile).async('text');
// Search for shapes
// In HWPX, shapes are under <hp:rect>, <hp:ellipse>, <hp:line> etc.
// Let's write them to a local file for inspection
fs.writeFileSync('section0.xml', content);
console.log('Main section XML saved as section0.xml');
// Regex to find shape properties
// HWPX coordinates: vertRelTo, horzRelTo, vertAlign, horzAlign, vertOffset, horzOffset
console.log('\nSearching for shape properties in section XML:');
const shapeObjectRegex = /<hp:shapeObject[^>]*>([\s\S]*?)<\/hp:shapeObject>/g;
let match;
let count = 0;
while ((match = shapeObjectRegex.exec(content)) !== null) {
count++;
console.log(`\n--- Shape Object #${count} ---`);
const shapeObjectHeader = match[0].substring(0, match[0].indexOf('>') + 1);
console.log('Header:', shapeObjectHeader);
// Find vertRelTo, horzRelTo, etc.
const attrs = ['vertRelTo', 'horzRelTo', 'vertAlign', 'horzAlign', 'vertOffset', 'horzOffset'];
attrs.forEach(attr => {
const reg = new RegExp(`${attr}="([^"]*)"`);
const m = reg.exec(shapeObjectHeader);
if (m) {
console.log(` ${attr}: ${m[1]}`);
}
});
}
} else {
console.log('No section XML file found.');
}
} catch (err) {
console.error('Error parsing HWPX:', err);
}
}
run();