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 [``, ``]; }