summaryrefslogtreecommitdiffhomepage
path: root/dlang/src/read/html.d
blob: e3bcd6feae28589d3e7a1ce41b869d492efd7acb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
/+
- Name: SisuDoc Spine, Doc Reform [a part of]
  - Description: documents, structuring, processing, publishing, search
    - static content generator

  - Author: Ralph Amissah
    [ralph.amissah@gmail.com]

  - Copyright: (C) 2015 (continuously updated, current 2026) Ralph Amissah, All Rights Reserved.

  - License: AGPL 3 or later:

    Spine (SiSU), a framework for document structuring, publishing and
    search

    Copyright (C) Ralph Amissah

    This program is free software: you can redistribute it and/or modify it
    under the terms of the GNU AFERO General Public License as published by the
    Free Software Foundation, either version 3 of the License, or (at your
    option) any later version.

    This program is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
    more details.

    You should have received a copy of the GNU General Public License along with
    this program. If not, see [https://www.gnu.org/licenses/].

    If you have Internet connection, the latest version of the AGPL should be
    available at these locations:
    [https://www.fsf.org/licensing/licenses/agpl.html]
    [https://www.gnu.org/licenses/agpl.html]

  - Spine (by Doc Reform, related to SiSU) uses standard:
    - docReform markup syntax
      - standard SiSU markup syntax with modified headers and minor modifications
    - docReform object numbering
      - standard SiSU object citation numbering & system

  - Homepages:
    [https://www.sisudoc.org]
    [https://www.doc-reform.org]

  - Git
    [https://git.sisudoc.org/]

+/
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 ~= `<div class="title">`;
    buf ~= `<h1>` ~ htmlEscape(doc.meta("title.main")) ~ `</h1>`;
    if (doc.meta("title.subtitle").length > 0)
      buf ~= `<h2 class="subtitle">` ~ htmlEscape(doc.meta("title.subtitle")) ~ `</h2>`;
    buf ~= `<p class="author">` ~ htmlEscape(doc.meta("creator.author")) ~ `</p>`;
    buf ~= `</div>`;
  }

  /+ ↓ table of contents +/
  if (doc.toc.length > 0) {
    buf ~= `<div class="toc">`;
    buf ~= `<h2>Table of Contents</h2>`;
    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 ~= `<p class="` ~ cls ~ `">` ~ text ~ `</p>`;
      }
    }
    buf ~= `</div>`;
    buf ~= `<hr/>`;
  }

  /+ ↓ body +/
  buf ~= `<div class="body">`;
  foreach (obj; doc.body) {
    buf ~= renderObject(obj);
  }
  buf ~= `</div>`;

  /+ ↓ endnotes +/
  bool has_endnotes = false;
  foreach (obj; doc.endnotes) {
    if (obj.is_a == "endnote") { has_endnotes = true; break; }
  }
  if (has_endnotes) {
    buf ~= `<div class="endnotes">`;
    buf ~= `<hr/>`;
    buf ~= `<h2>Endnotes</h2>`;
    foreach (obj; doc.endnotes) {
      if (obj.is_a == "endnote") {
        string text = convertInlineMarkup(obj.text);
        string parent_ref = (obj.parent_ocn > 0)
          ? ` <a href="#o` ~ obj.parent_ocn.to!string ~ `">[` ~ obj.parent_ocn.to!string ~ `]</a>`
          : "";
        buf ~= `<p class="endnote">` ~ text ~ parent_ref ~ `</p>`;
      }
    }
    buf ~= `</div>`;
  }

  /+ ↓ glossary +/
  if (doc.glossary.length > 0) {
    buf ~= `<div class="glossary">`;
    buf ~= `<hr/>`;
    buf ~= `<h2>Glossary</h2>`;
    foreach (obj; doc.glossary) {
      string text = convertInlineMarkup(obj.text);
      buf ~= `<p class="glossary-entry">` ~ text ~ `</p>`;
    }
    buf ~= `</div>`;
  }

  /+ ↓ bookindex +/
  bool has_bookindex = false;
  foreach (obj; doc.bookindex) {
    if (obj.is_a == "bookindex") { has_bookindex = true; break; }
  }
  if (has_bookindex) {
    buf ~= `<div class="bookindex">`;
    buf ~= `<hr/>`;
    buf ~= `<h2>Book Index</h2>`;
    foreach (obj; doc.bookindex) {
      if (obj.is_a == "bookindex") {
        string text = convertInlineMarkup(obj.text);
        buf ~= `<p class="index-entry">` ~ text ~ `</p>`;
      }
    }
    buf ~= `</div>`;
  }

  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)
    ? `<span class="ocn">` ~ obj.ocn.to!string ~ `</span>`
    : "";

  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 ~ `</` ~ tag ~ `>`;
      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 ~= `<p` ~ ocn_anchor ~ indent_cls ~ `>` ~ text ~ ocn_display ~ `</p>`;
      break;
    case "code":
      lines ~= `<pre` ~ ocn_anchor ~ `><code>` ~ htmlEscape(obj.text) ~ `</code></pre>`;
      break;
    case "table":
      lines ~= renderTable(obj, ocn_anchor);
      break;
    case "group":
      lines ~= `<div` ~ ocn_anchor ~ ` class="group">` ~ text ~ `</div>`;
      break;
    case "block":
      lines ~= `<pre` ~ ocn_anchor ~ ` class="block">` ~ htmlEscape(obj.text) ~ `</pre>`;
      break;
    case "verse":
      lines ~= `<p` ~ ocn_anchor ~ ` class="verse">` ~ text.replace("\n", "<br/>\n") ~ ocn_display ~ `</p>`;
      break;
    case "quote":
      lines ~= `<blockquote` ~ ocn_anchor ~ `>` ~ text ~ ocn_display ~ `</blockquote>`;
      break;
    default:
      if (text.length > 0)
        lines ~= `<p` ~ ocn_anchor ~ `>` ~ text ~ ocn_display ~ `</p>`;
      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` ~ ocn_anchor ~ `>`;
  /+ ↓ 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 ~= `<tr>`;
    foreach (cell; row.split("┊")) {
      lines ~= `<` ~ tag ~ `>` ~ convertInlineMarkup(cell.strip) ~ `</` ~ tag ~ `>`;
    }
    lines ~= `</tr>`;
    first_row = false;
  }
  lines ~= `</table>`;
  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, "⑆!", "!", "<b>", "</b>");
  result = replaceInlineMarkerBracketed(result, "⑆!", "┨", "┣!", "<b>", "</b>");

  /+ ↓ emphasis +/
  result = replaceInlineMarker(result, "⑆*", "*", "<em>", "</em>");
  result = replaceInlineMarkerBracketed(result, "⑆*", "┨", "┣*", "<em>", "</em>");

  /+ ↓ italic +/
  result = replaceInlineMarker(result, "⑆/", "/", "<i>", "</i>");
  result = replaceInlineMarkerBracketed(result, "⑆/", "┨", "┣/", "<i>", "</i>");

  /+ ↓ underscore +/
  result = replaceInlineMarker(result, "⑆_", "_", "<u>", "</u>");
  result = replaceInlineMarkerBracketed(result, "⑆_", "┨", "┣_", "<u>", "</u>");

  /+ ↓ superscript +/
  result = replaceInlineMarker(result, "⑆^", "^", "<sup>", "</sup>");
  result = replaceInlineMarkerBracketed(result, "⑆^", "┨", "┣^", "<sup>", "</sup>");

  /+ ↓ subscript +/
  result = replaceInlineMarker(result, "⑆,", ",", "<sub>", "</sub>");
  result = replaceInlineMarkerBracketed(result, "⑆,", "┨", "┣,", "<sub>", "</sub>");

  /+ ↓ monospace +/
  result = replaceInlineMarker(result, "⑆■", "■", "<code>", "</code>");

  /+ ↓ links: ┥link text┝┤url├ +/
  result = replaceLinks(result);

  /+ ↓ footnote markers: 【N text】 +/
  result = replaceFootnotes(result);

  /+ ↓ line breaks +/
  result = result.replace("┘", "<br/>");
  result = result.replace("┙", "<br/>");

  /+ ↓ non-breaking spaces +/
  result = result.replace("░", "&nbsp;");

  /+ ↓ bullet markers +/
  result = result.replace("● ", "&#8226; ");

  /+ ↓ 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 <a href="url">link text</a> +/
  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]
      ~ `<a href="` ~ url ~ `">` ~ link_text ~ `</a>`
      ~ 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]
      ~ `<sup class="fn">` ~ note_num ~ `</sup>`
      ~ result[fn_close + "】".length .. $];
  }
  return result;
}

string htmlEscape(string text) {
  return text.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;")
    .replace("\"", "&quot;");
}

private string[] htmlHead(OcdaDocument doc) {
  string[] lines;
  lines ~= `<!DOCTYPE html>`;
  lines ~= `<html>`;
  lines ~= `<head>`;
  lines ~= `<meta charset="utf-8"/>`;
  lines ~= `<title>` ~ htmlEscape(doc.meta("title.main")) ~ `</title>`;
  lines ~= `<style>`;
  lines ~= `  body { max-width: 48em; margin: 2em auto; padding: 0 1em; font-family: serif; line-height: 1.5; }`;
  lines ~= `  .title { text-align: center; margin-bottom: 2em; }`;
  lines ~= `  .subtitle { font-weight: normal; font-style: italic; }`;
  lines ~= `  .ocn { float: right; font-size: 0.7em; color: #999; }`;
  lines ~= `  .toc { margin-bottom: 2em; }`;
  lines ~= `  .toc-entry { margin: 0.2em 0; }`;
  lines ~= `  .toc-sub { margin: 0.2em 0 0.2em 2em; }`;
  lines ~= `  .indent-1 { margin-left: 2em; }`;
  lines ~= `  .indent-2 { margin-left: 4em; }`;
  lines ~= `  .indent-3 { margin-left: 6em; }`;
  lines ~= `  .bullet { margin-left: 2em; }`;
  lines ~= `  .bullet:before { content: "\2022 "; }`;
  lines ~= `  .verse { white-space: pre-wrap; font-style: italic; }`;
  lines ~= `  .group { margin: 1em 2em; }`;
  lines ~= `  .block { margin: 1em 2em; background: #f8f8f8; padding: 1em; }`;
  lines ~= `  pre code { display: block; background: #f4f4f4; padding: 1em; overflow-x: auto; }`;
  lines ~= `  table { border-collapse: collapse; margin: 1em 0; }`;
  lines ~= `  th, td { border: 1px solid #ccc; padding: 0.4em 0.8em; }`;
  lines ~= `  th { background: #f0f0f0; }`;
  lines ~= `  .endnote { font-size: 0.9em; }`;
  lines ~= `  .fn { color: #666; }`;
  lines ~= `  .index-entry { font-size: 0.9em; }`;
  lines ~= `  blockquote { margin: 1em 2em; padding-left: 1em; border-left: 3px solid #ccc; }`;
  lines ~= `</style>`;
  lines ~= `</head>`;
  lines ~= `<body>`;
  return lines;
}

private string[] htmlTail() {
  return [`</body>`, `</html>`];
}