From 4e4f2f8c2b2c043077390cb4ccf7ce97ffe4ac6b Mon Sep 17 00:00:00 2001 From: Ralph Amissah Date: Mon, 12 Oct 2026 20:24:11 -0400 Subject: format 2.1: ocda-read, the readers folded in ocda-read, one reader where there were two. The .ssp reader and the .ocda.db reader each had their own parser; this one is built on the library, so every mode reads a .ssp, a .ocda.db and a pod, and says the same thing of each. Its output modules are the .ssp reader's, copied (the reader repositories are kept, frozen) and moved onto the library's model; --languages and --files come from the database reader. ocda-read [--tree|--html|--llm-text|--llm-jsonl|--images| --verify --image-dir=|--languages|--files| --report-unknown|--validate] [lang] In D every mode of both readers; in Gleam the modes the Gleam readers had (the summary, --tree, --languages, --files, --report-unknown), whose output is the D reader's, byte for byte. In each language it lives in src/read/ and is not part of the library: dub configurations keep it out of a dependant's build in D, and Gleam compiles all of src/ but a dependant need not use it. Checked over the markup document samples: against the .ssp reader on a .ssp, byte for byte after the first line in every D mode but the summary, whose closing hint differs; against the database reader on a .ocda.db, --files and --languages (its id column left out); the same output from the .ssp, the .ocda.db and the .sisupod; and D == Gleam. The database reader's --outline is not reproduced: it ordered headings within their own section, so that other sections' headings fell among the body's. --outline here is --tree. What writing it found in the library: the database loader could not say what it had not understood. It asked for columns by name and so never saw the rest, and reported that it had understood everything. It now counts them on the conditions under which the .ssp writes the same property, and --report-unknown is the same from every form. ocda.inventory answers two questions about the file rather than the document: its languages, in the manifest's order, each with the markup it was made from and the sha256 of its .ssp, for every form alike; and what a .ocda.db carries besides the abstraction. In Gleam the sha256 comes from OTP's crypto through a four line Erlang FFI. The flake's default is all three D programs in one result: built separately into the same link, each replaced the one before. (assisted by Claude-Code) --- dlang/src/read/html.d | 388 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 388 insertions(+) create mode 100644 dlang/src/read/html.d (limited to 'dlang/src/read/html.d') diff --git a/dlang/src/read/html.d b/dlang/src/read/html.d new file mode 100644 index 0000000..cd679e4 --- /dev/null +++ b/dlang/src/read/html.d @@ -0,0 +1,388 @@ +module read.html; + +import std.array : replace, split, join, appender; +import std.conv : to; +import std.stdio : writeln, File; +import std.string : strip, indexOf, startsWith; +import ocda.model; + +void outputHtmlScroll(OcdaDocument doc, string output_file) { + auto buf = appender!(string[]); + + buf ~= htmlHead(doc); + + /+ ↓ title +/ + if (doc.head.length > 0) { + auto title_obj = doc.head[0]; + buf ~= `
`; + buf ~= `

` ~ htmlEscape(doc.meta("title.main")) ~ `

`; + if (doc.meta("title.subtitle").length > 0) + buf ~= `

` ~ htmlEscape(doc.meta("title.subtitle")) ~ `

`; + buf ~= `

` ~ htmlEscape(doc.meta("creator.author")) ~ `

`; + buf ~= `
`; + } + + /+ ↓ table of contents +/ + if (doc.toc.length > 0) { + buf ~= `
`; + buf ~= `

Table of Contents

`; + foreach (obj; doc.toc) { + if (obj.is_a == "toc") { + string text = convertInlineMarkup(obj.text); + int indent = obj.indent_base; + string cls = (indent > 4) ? "toc-sub" : "toc-entry"; + buf ~= `

` ~ text ~ `

`; + } + } + buf ~= `
`; + buf ~= `
`; + } + + /+ ↓ body +/ + buf ~= `
`; + foreach (obj; doc.body) { + buf ~= renderObject(obj); + } + buf ~= `
`; + + /+ ↓ endnotes +/ + bool has_endnotes = false; + foreach (obj; doc.endnotes) { + if (obj.is_a == "endnote") { has_endnotes = true; break; } + } + if (has_endnotes) { + buf ~= `
`; + buf ~= `
`; + buf ~= `

Endnotes

`; + foreach (obj; doc.endnotes) { + if (obj.is_a == "endnote") { + string text = convertInlineMarkup(obj.text); + string parent_ref = (obj.parent_ocn > 0) + ? ` [` ~ obj.parent_ocn.to!string ~ `]` + : ""; + buf ~= `

` ~ text ~ parent_ref ~ `

`; + } + } + buf ~= `
`; + } + + /+ ↓ glossary +/ + if (doc.glossary.length > 0) { + buf ~= `
`; + buf ~= `
`; + buf ~= `

Glossary

`; + foreach (obj; doc.glossary) { + string text = convertInlineMarkup(obj.text); + buf ~= `

` ~ text ~ `

`; + } + buf ~= `
`; + } + + /+ ↓ bookindex +/ + bool has_bookindex = false; + foreach (obj; doc.bookindex) { + if (obj.is_a == "bookindex") { has_bookindex = true; break; } + } + if (has_bookindex) { + buf ~= `
`; + buf ~= `
`; + buf ~= `

Book Index

`; + foreach (obj; doc.bookindex) { + if (obj.is_a == "bookindex") { + string text = convertInlineMarkup(obj.text); + buf ~= `

` ~ text ~ `

`; + } + } + buf ~= `
`; + } + + buf ~= htmlTail(); + + /+ ↓ write output +/ + string[] output = buf.data; + if (output_file.length > 0) { + auto f = File(output_file, "w"); + foreach (line; output) f.writeln(line); + } else { + foreach (line; output) writeln(line); + } +} + +private string[] renderObject(OcdaObject obj) { + /+ ↓ markup commentary is carried by the abstraction and is not output. + Rendering it would put the author notes into the document, which + spine itself does not do + +/ + if (!obj.renders) { return []; } + string[] lines; + string text = convertInlineMarkup(obj.text); + string ocn_anchor = (obj.ocn > 0 && !obj.ocn_off) + ? ` id="o` ~ obj.ocn.to!string ~ `"` + : ""; + string ocn_display = (obj.ocn > 0 && !obj.ocn_off) + ? `` ~ obj.ocn.to!string ~ `` + : ""; + + switch (obj.is_a) { + case "heading": + string tag = headingTag(obj.heading_level); + string seg_anchor = (obj.segment.length > 0) + ? ` id="` ~ htmlEscape(obj.segment) ~ `"` + : ocn_anchor; + lines ~= `<` ~ tag ~ seg_anchor ~ `>` ~ text ~ ``; + break; + case "para": + string indent_cls = ""; + if (obj.indent_base > 0) indent_cls = ` class="indent-` ~ obj.indent_base.to!string ~ `"`; + else if (obj.bullet) indent_cls = ` class="bullet"`; + lines ~= `` ~ text ~ ocn_display ~ `

`; + break; + case "code": + lines ~= `` ~ htmlEscape(obj.text) ~ ``; + break; + case "table": + lines ~= renderTable(obj, ocn_anchor); + break; + case "group": + lines ~= `` ~ text ~ ``; + break; + case "block": + lines ~= `` ~ htmlEscape(obj.text) ~ ``; + break; + case "verse": + lines ~= `` ~ text.replace("\n", "
\n") ~ ocn_display ~ `

`; + break; + case "quote": + lines ~= `` ~ text ~ ocn_display ~ ``; + break; + default: + if (text.length > 0) + lines ~= `` ~ text ~ ocn_display ~ `

`; + break; + } + return lines; +} + +private string headingTag(string level) { + switch (level) { + case "A": return "h1"; + case "B": return "h2"; + case "C": return "h3"; + case "D": return "h4"; + case "1": return "h3"; + case "2": return "h4"; + case "3": return "h5"; + case "4": return "h6"; + default: return "h4"; + } +} + +private string renderTable(OcdaObject obj, string ocn_anchor) { + string[] lines; + lines ~= ``; + /+ ↓ table content uses ┊ as cell separator, rows separated by newlines +/ + auto rows = obj.text.split("\n"); + bool first_row = obj.table_header; + foreach (row; rows) { + if (row.strip.length == 0) continue; + string tag = first_row ? "th" : "td"; + lines ~= ``; + foreach (cell; row.split("┊")) { + lines ~= `<` ~ tag ~ `>` ~ convertInlineMarkup(cell.strip) ~ ``; + } + lines ~= ``; + first_row = false; + } + lines ~= ``; + return lines.join("\n"); +} + +/+ ↓ convert spine internal Unicode markup to HTML +/ +string convertInlineMarkup(string text) { + if (text.length == 0) return ""; + string result = htmlEscape(text); + + /+ ↓ font face markers: ⑆*bold⑆*, ⑆/italic⑆/, etc. + Pattern: ⑆X┨content┣X or ⑆Xcontent⑆X + The ┨┣ form is used within heading content +/ + + /+ ↓ bold +/ + result = replaceInlineMarker(result, "⑆!", "!", "", ""); + result = replaceInlineMarkerBracketed(result, "⑆!", "┨", "┣!", "", ""); + + /+ ↓ emphasis +/ + result = replaceInlineMarker(result, "⑆*", "*", "", ""); + result = replaceInlineMarkerBracketed(result, "⑆*", "┨", "┣*", "", ""); + + /+ ↓ italic +/ + result = replaceInlineMarker(result, "⑆/", "/", "", ""); + result = replaceInlineMarkerBracketed(result, "⑆/", "┨", "┣/", "", ""); + + /+ ↓ underscore +/ + result = replaceInlineMarker(result, "⑆_", "_", "", ""); + result = replaceInlineMarkerBracketed(result, "⑆_", "┨", "┣_", "", ""); + + /+ ↓ superscript +/ + result = replaceInlineMarker(result, "⑆^", "^", "", ""); + result = replaceInlineMarkerBracketed(result, "⑆^", "┨", "┣^", "", ""); + + /+ ↓ subscript +/ + result = replaceInlineMarker(result, "⑆,", ",", "", ""); + result = replaceInlineMarkerBracketed(result, "⑆,", "┨", "┣,", "", ""); + + /+ ↓ monospace +/ + result = replaceInlineMarker(result, "⑆■", "■", "", ""); + + /+ ↓ links: ┥link text┝┤url├ +/ + result = replaceLinks(result); + + /+ ↓ footnote markers: 【N text】 +/ + result = replaceFootnotes(result); + + /+ ↓ line breaks +/ + result = result.replace("┘", "
"); + result = result.replace("┙", "
"); + + /+ ↓ non-breaking spaces +/ + result = result.replace("░", " "); + + /+ ↓ bullet markers +/ + result = result.replace("● ", "• "); + + /+ ↓ page break markers +/ + result = result.replace("┼", "").replace("┿", "").replace("╂", ""); + + return result; +} + +private string replaceInlineMarker(string text, string open_mark, string close_suffix, string html_open, string html_close) { + /+ ↓ replace ⑆Xcontent⑆X pattern (no bracket form) +/ + string result = text; + while (true) { + auto open_pos = result.indexOf(open_mark); + if (open_pos < 0) break; + auto after_open = open_pos + open_mark.length; + /+ ↓ look for closing ⑆X +/ + string close_mark = "⑆" ~ close_suffix; + auto close_pos = indexOf(result[after_open .. $], close_mark); + if (close_pos < 0) break; + close_pos += after_open; + string content = result[after_open .. close_pos]; + result = result[0 .. open_pos] ~ html_open ~ content ~ html_close + ~ result[close_pos + close_mark.length .. $]; + } + return result; +} + +private string replaceInlineMarkerBracketed(string text, string open_mark, string bracket_open, string bracket_close, string html_open, string html_close) { + /+ ↓ replace ⑆X┨content┣X pattern (bracketed form) +/ + string result = text; + string full_open = open_mark ~ bracket_open; + while (true) { + auto open_pos = result.indexOf(full_open); + if (open_pos < 0) break; + auto after_open = open_pos + full_open.length; + auto close_pos = indexOf(result[after_open .. $], bracket_close); + if (close_pos < 0) break; + close_pos += after_open; + string content = result[after_open .. close_pos]; + result = result[0 .. open_pos] ~ html_open ~ content ~ html_close + ~ result[close_pos + bracket_close.length .. $]; + } + return result; +} + +private string replaceLinks(string text) { + /+ ↓ replace ┥link text┝┤url├ with link text +/ + string result = text; + while (true) { + auto link_open = result.indexOf("┥"); + if (link_open < 0) break; + auto link_mid = indexOf(result[link_open .. $], "┝┤"); + if (link_mid < 0) break; + link_mid += link_open; + auto link_close = indexOf(result[link_mid .. $], "├"); + if (link_close < 0) break; + link_close += link_mid; + string link_text = result[link_open + "┥".length .. link_mid]; + string url = result[link_mid + "┝┤".length .. link_close]; + /+ ↓ handle internal links (¤ prefix) +/ + if (url.length > 0 && url[0] == '¤') { + url = url[("¤").length .. $]; + } + /+ ↓ handle OCN-only links (#N) +/ + if (url.length > 0 && url[0] == '#') { + url = "#o" ~ url[1 .. $]; + } + result = result[0 .. link_open] + ~ `` ~ link_text ~ `` + ~ result[link_close + "├".length .. $]; + } + return result; +} + +private string replaceFootnotes(string text) { + /+ ↓ replace 【N content】 with superscript note number +/ + string result = text; + while (true) { + auto fn_open = result.indexOf("【"); + if (fn_open < 0) break; + auto fn_close = indexOf(result[fn_open .. $], "】"); + if (fn_close < 0) break; + fn_close += fn_open; + string fn_content = result[fn_open + "【".length .. fn_close]; + /+ ↓ extract note number (first token before space) +/ + auto space_pos = fn_content.indexOf(' '); + string note_num = (space_pos > 0) ? fn_content[0 .. space_pos] : fn_content; + result = result[0 .. fn_open] + ~ `` ~ note_num ~ `` + ~ result[fn_close + "】".length .. $]; + } + return result; +} + +string htmlEscape(string text) { + return text.replace("&", "&").replace("<", "<").replace(">", ">") + .replace("\"", """); +} + +private string[] htmlHead(OcdaDocument doc) { + string[] lines; + lines ~= ``; + lines ~= ``; + lines ~= ``; + lines ~= ``; + lines ~= `` ~ htmlEscape(doc.meta("title.main")) ~ ``; + lines ~= ``; + lines ~= ``; + lines ~= ``; + return lines; +} + +private string[] htmlTail() { + return [``, ``]; +} -- cgit v1.2.3