Files
Aislo/tools/est-test/wpt-parse.cjs

25 lines
1007 B
JavaScript

const quantity = require("./native-quantity.cjs");
function rowParser(text) {
const superscripts = new Set(
[...text.matchAll(/<cs\b[^>]*\bnr=(\d+)[^>]*>/gi)]
.filter((match) => (Number(match[0].match(/CharStyleON:(\d+)/)?.[1] || 0) & 16) !== 0)
.map((match) => match[1]),
);
const tags = new Map();
for (const match of text.matchAll(/<begin\b[^>]*\bTag=(\d+)[^>]*>/gi)) {
if (quantity.entities(match[0]).match(/Name:["']([^"']+)["']/)) tags.set(match[1], match[0]);
}
return (div) => {
const ids = new Set([...div.matchAll(/\bTag=(\d+)/gi)].map((match) => match[1]));
const declarations = [...ids].map((id) => tags.get(id) || "").join("");
const styles = [...new Set([...div.matchAll(/<c\s+nr=(\d+)\s*\/>/gi)].map((m) => m[1]))]
.filter((id) => superscripts.has(id))
.map((id) => `<cs nr=${id} CharStyleON:16/>`)
.join("");
return quantity.fromWpt("", "", styles + declarations + div, [], {}).rows;
};
}
module.exports = { rowParser };