import { Tree, TreeCursor } from "@lezer/common"; import { parser } from "../parser/leiden+/parser.js"; const blockElements = ['Recto', 'Verso', 'Fragment', 'Part', 'Div', 'Column']; function text(input: string, node:TreeCursor) { return input.substring(node.from, node.to); } export function debugLeidenTree(input: string, root: Tree = parser.parse(input)) { root.iterate({ enter: (node: TreeCursor) => { console.log(`> ${node.name}`); if (node.name === 'Text') { console.log(text(input, node)); } }, leave: (node) => { console.log(`< ${node.name}`); } }) } export function leidenPlus2epiDoc(input: string, root: Tree = parser.parse(input)) { const stack:string[] = []; const xml:string[] = []; let needsWrap = false; let wrapper = 'ab'; let value; root.iterate({ enter: (node:TreeCursor) => { if (node.type.isError) { xml.push(``); return; } const name = node.name; switch (name) { case 'Document': let foundInline = false; if (node.firstChild()) { do { if (!blockElements.includes(node.type.name)) { foundInline = !/^[\s\n]+$/.test(text(input, node)); } } while (node.nextSibling()); } needsWrap = foundInline; if (needsWrap) { xml.push(`<${wrapper}>\n`); } node.parent(); break; case 'Text': case 'Number': xml.push(text(input, node)); break; case 'LineBreak': node.firstChild(); value = /^([0-9]+)\..*$/.exec(text(input, node)); xml.push(``); break; case 'LineBreakWrapped': node.firstChild(); value = /^([0-9]+)\..*$/.exec(text(input, node)); xml.push(``); break; case 'Div': xml.push("") break; case 'Recto': xml.push('
'); break; case 'Verso': xml.push('
'); break; case 'Fragment': node.firstChild(); value = /^([0-9]+)\..*$/.exec(text(input, node)); xml.push(`
`); break; case 'Part': node.firstChild(); value = /^([a-zA-Z0-9]+)\..*$/.exec(text(input, node)); xml.push(`
`) break; case 'Column': node.firstChild(); value = /^([a-zA-Z0-9]+)\..*$/.exec(text(input, node)); xml.push(`
`) break; case 'Foreign': node.lastChild(); value = /^\|\~(.*)$/.exec(text(input, node)); xml.push(``); node.parent(); break; case 'Unclear': const content = text(input, node); let stripped = ''; for (let i = 0; i < content.length; i++) { const codepoint = content.codePointAt(i); if (codepoint && codepoint !== 0x0323) { stripped += String.fromCodePoint(codepoint); } } xml.push(`${stripped}`); return false; case 'Gap': node.next(true); xml.push(``); return false; case 'GapUnknown': xml.push(``); return false; case 'Illegible': value = /^\.([0-9?]+)(lin)?$/.exec(text(input, node)); if (value) { if (value[2] === 'lin') { if (value[1] === '?') { xml.push(``); } else { xml.push(``); } } else if (value[1] === '?') { xml.push(``); } else { xml.push(``); } } return false; case 'Erasure': xml.push(''); break; case 'LostLines': value = /^lost\.([0-9?]+)lin$/.exec(text(input, node)); if (value) { if (value[1] === '?') { xml.push(``); } else { xml.push(``); } } return false; case 'AbbrevUnresolved': xml.push(''); break; case 'Abbrev': if (stack.length > 0) { node.lastChild(); value = text(input, node); if (value.length > 0 && value.charAt(value.length - 1) === '?') { xml.push(``); } else { xml.push(``); } node.parent(); } else { xml.push(''); } stack.push('expan'); break; case 'Supplied': node.lastChild(); if (node.name === 'CertLow') { xml.push(''); } else { xml.push(''); } node.parent(); break; case 'SuppliedLost': node.lastChild(); if (node.name === 'CertLow') { xml.push(''); } else { xml.push(''); } node.parent(); break; case 'ForeignEnd': case 'CertLow': return false; case 'QuestionMark': if (stack.length < 2) { xml.push('?'); } return false; case 'Inline': break; default: xml.push(`<${name}>`); break; } }, leave: (node) => { switch (node.name) { case 'Document': if (needsWrap) { xml.push(`\n`); } break; case 'AbbrevUnresolved': xml.push(''); break; case 'Abbrev': if (stack.length > 1) { const last = xml[xml.length - 1]; if (last.endsWith('?')) { xml[xml.length - 1] = last.substring(0, last.length - 2); } xml.push(''); } else { xml.push(''); } stack.pop(); break; case 'Div': xml.push(''); break; case 'Recto': case 'Verso': case 'Fragment': case 'Part': case 'Column': xml.push('
\n'); break; case 'Supplied': case 'SuppliedLost': xml.push(''); break; case 'Erasure': xml.push(''); break; case 'Foreign': xml.push(''); break; } } }); if (root.type.name === 'Inline') { xml.push(''); } return xml.join(''); }