inkhaven 1.6.10

Inkhaven — TUI literary work editor for Typst books
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
438
439
440
//! TDOC-4.3 — companion-book inclusion. When `docs.html.include.<book>` is on, that
//! system book is rendered as an appendix page on the site. Sources gets a formatted
//! bibliography; every other book renders its entries — prose through the normal
//! path, HJSON entries as readable field lists.

use crate::config::Config;
use crate::project::ProjectLayout;
use crate::store::hierarchy::Hierarchy;
use crate::store::node::{Node, NodeKind};

use super::markdown_html::{escape_html, slugify};
use super::render;

/// One appendix page rendered from a companion book.
pub struct CompanionPage {
    pub file: String,
    pub title: String,
    pub content: String,
}

/// Build an appendix page for each enabled companion book, in a stable order.
pub fn build(layout: &ProjectLayout, h: &Hierarchy, cfg: &Config) -> Vec<CompanionPage> {
    use crate::store::{
        SYSTEM_TAG_CHARACTERS, SYSTEM_TAG_GLOSSARY, SYSTEM_TAG_LANGUAGES, SYSTEM_TAG_MYTHOLOGY,
        SYSTEM_TAG_NOTES, SYSTEM_TAG_PLACES, SYSTEM_TAG_SOURCES, SYSTEM_TAG_WORLD,
    };
    let inc = &cfg.docs.html.include;
    let specs: &[(bool, &str, &str)] = &[
        (inc.sources, SYSTEM_TAG_SOURCES, "Sources"),
        (inc.glossary, SYSTEM_TAG_GLOSSARY, "Glossary"),
        (inc.characters, SYSTEM_TAG_CHARACTERS, "Characters"),
        (inc.places, SYSTEM_TAG_PLACES, "Places"),
        (inc.language, SYSTEM_TAG_LANGUAGES, "Language"),
        (inc.world, SYSTEM_TAG_WORLD, "World"),
        (inc.mythology, SYSTEM_TAG_MYTHOLOGY, "Mythology"),
        (inc.notes, SYSTEM_TAG_NOTES, "Notes"),
    ];

    let mut out = Vec::new();
    for (enabled, tag, title) in specs {
        if !enabled {
            continue;
        }
        let Some(book) = h.children_of(None).into_iter().find(|n| {
            n.kind == NodeKind::Book && n.system_tag.as_deref() == Some(*tag)
        }) else {
            continue;
        };
        let content = if *tag == SYSTEM_TAG_SOURCES {
            render_sources(layout, h, book.id)
        } else if *tag == SYSTEM_TAG_LANGUAGES {
            render_language(layout, h, book.id)
        } else if *tag == SYSTEM_TAG_WORLD {
            render_world(layout, h, book.id)
        } else {
            render_generic(layout, h, book.id, title)
        };
        // Skip a book with no real content (the render fns return "" when empty).
        if content.trim().is_empty() {
            continue;
        }
        out.push(CompanionPage {
            file: format!("appendix-{}.html", slugify(title)),
            title: title.to_string(),
            content,
        });
    }
    out
}

fn read_body(layout: &ProjectLayout, node: &Node) -> Option<String> {
    let rel = node.file.as_ref()?;
    std::fs::read_to_string(layout.root.join(rel)).ok()
}

/// Generic companion rendering: walk the book, emit a section per entry. Returns
/// `""` when the book has no renderable entries.
fn render_generic(layout: &ProjectLayout, h: &Hierarchy, book_id: uuid::Uuid, title: &str) -> String {
    let mut body = String::new();
    let mut has_entry = false;
    for id in h.collect_subtree(book_id) {
        if id == book_id {
            continue;
        }
        let Some(n) = h.get(id) else { continue };
        match n.kind {
            NodeKind::Chapter | NodeKind::Subchapter => {
                body.push_str(&format!(
                    "<h2 id=\"{}\">{}</h2>\n",
                    slugify(&n.title),
                    escape_html(&n.title)
                ));
            }
            NodeKind::Paragraph => {
                let Some(raw) = read_body(layout, n) else { continue };
                let entry = render_entry(&n.title, n.content_type.as_deref(), &raw);
                if !entry.trim().is_empty() {
                    has_entry = true;
                }
                body.push_str(&entry);
            }
            _ => {}
        }
    }
    if !has_entry {
        return String::new();
    }
    format!("<h1>{}</h1>\n{body}", escape_html(title))
}

/// TDOC-4.4 — the Language book: a sortable/filterable lexicon table per invented
/// language, plus any grammar / sample prose. Returns `""` when nothing is defined.
fn render_language(layout: &ProjectLayout, h: &Hierarchy, lang_root_id: uuid::Uuid) -> String {
    let mut body = String::new();
    let mut any_table = false;
    for lang in h
        .children_of(Some(lang_root_id))
        .into_iter()
        .filter(|n| n.kind == NodeKind::Book)
    {
        let entries = load_dictionary_from_disk(layout, h, lang.id);
        if !entries.is_empty() {
            body.push_str(&lexicon_table(&lang.title, &entries));
            any_table = true;
        }

        // Grammar / Phonology / Sample-text chapters — rendered as prose/HJSON.
        for ch in h
            .children_of(Some(lang.id))
            .into_iter()
            .filter(|n| n.kind == NodeKind::Chapter)
        {
            let t = ch.title.to_ascii_lowercase();
            if t == "dictionary" || t == "meta" {
                continue;
            }
            let mut sec = String::new();
            for id in h.collect_subtree(ch.id) {
                let Some(n) = h.get(id) else { continue };
                if n.kind != NodeKind::Paragraph {
                    continue;
                }
                let Some(raw) = read_body(layout, n) else { continue };
                sec.push_str(&render_entry(&n.title, n.content_type.as_deref(), &raw));
            }
            if !sec.trim().is_empty() {
                body.push_str(&format!(
                    "<h2 id=\"{}\">{}{}</h2>\n{sec}",
                    slugify(&format!("{}-{}", lang.title, ch.title)),
                    escape_html(&lang.title),
                    escape_html(&ch.title)
                ));
            }
        }
    }
    if body.trim().is_empty() {
        return String::new();
    }
    if any_table {
        body.push_str(LEXICON_SCRIPT);
    }
    format!("<h1>Language</h1>\n{body}")
}

/// A sortable, filterable HTML table for one language's dictionary.
fn lexicon_table(lang: &str, entries: &[crate::language_entry::DictionaryEntry]) -> String {
    let mut s = format!(
        "<h2 id=\"{}\">{} — Dictionary</h2>\n<p class=\"lexicon-count\">{} entries · click a column to sort, type to filter</p>\n",
        slugify(&format!("{lang}-dictionary")),
        escape_html(lang),
        entries.len()
    );
    s.push_str("<input type=\"search\" class=\"lexicon-filter\" placeholder=\"Filter the dictionary…\" aria-label=\"Filter dictionary\">\n");
    s.push_str("<table class=\"lexicon\"><thead><tr>");
    for head in ["Word", "Part of speech", "Meaning", "Registers", "Domain", "Era", "Etymology"] {
        s.push_str(&format!("<th>{head}</th>"));
    }
    s.push_str("</tr></thead><tbody>\n");
    for e in entries {
        s.push_str("<tr>");
        s.push_str(&td(&e.word));
        s.push_str(&td(&e.pos));
        s.push_str(&td(&e.translation));
        s.push_str(&td(&e.registers.join(", ")));
        s.push_str(&td(&e.domain.join(", ")));
        s.push_str(&td(e.era.as_deref().unwrap_or("")));
        s.push_str(&td(e.etymology.as_deref().unwrap_or("")));
        s.push_str("</tr>\n");
    }
    s.push_str("</tbody></table>\n");
    s
}

fn td(text: &str) -> String {
    format!("<td>{}</td>", escape_html(text))
}

/// Self-contained (inline) vanilla-JS: click a header to sort, type in the filter
/// box to narrow. No external dependency — the page stays self-contained.
const LEXICON_SCRIPT: &str = r#"<script>
(function(){
  function cell(row,i){return (row.cells[i].textContent||'').trim().toLowerCase();}
  document.querySelectorAll('table.lexicon').forEach(function(table){
    var tbody=table.tBodies[0];
    var input=table.previousElementSibling;
    if(input&&input.classList&&input.classList.contains('lexicon-filter')){
      input.addEventListener('input',function(){
        var q=input.value.toLowerCase();
        Array.prototype.forEach.call(tbody.rows,function(row){
          row.style.display=row.textContent.toLowerCase().indexOf(q)>=0?'':'none';
        });
      });
    }
    Array.prototype.forEach.call(table.tHead.rows[0].cells,function(th,i){
      var dir=0;
      th.addEventListener('click',function(){
        dir=dir<=0?1:-1;
        var rows=Array.prototype.slice.call(tbody.rows);
        rows.sort(function(a,b){return cell(a,i).localeCompare(cell(b,i))*dir;});
        rows.forEach(function(r){tbody.appendChild(r);});
        Array.prototype.forEach.call(table.tHead.rows[0].cells,function(c){c.removeAttribute('aria-sort');});
        th.setAttribute('aria-sort',dir>0?'ascending':'descending');
      });
    });
  });
})();
</script>
"#;

/// TDOC-4.5 — the World book: a narrative guide compiled from `world.hjson`, falling
/// back to the materialised paragraphs if no world definition is present.
fn render_world(layout: &ProjectLayout, h: &Hierarchy, book_id: uuid::Uuid) -> String {
    let world_path = layout.root.join("world.hjson");
    if let Ok(raw) = std::fs::read_to_string(&world_path) {
        if let Ok(def) = crate::world::types::WorldDefinition::from_hjson(&raw) {
            let narrative = super::world_html::render(&def);
            if !narrative.trim().is_empty() {
                return narrative;
            }
        }
    }
    render_generic(layout, h, book_id, "World")
}

/// Load every parseable `DictionaryEntry` under a language's `Dictionary` chapter,
/// reading bodies from disk (the exporter never touches the store).
fn load_dictionary_from_disk(
    layout: &ProjectLayout,
    h: &Hierarchy,
    lang_book_id: uuid::Uuid,
) -> Vec<crate::language_entry::DictionaryEntry> {
    let Some(chapter) = h
        .children_of(Some(lang_book_id))
        .into_iter()
        .find(|n| n.kind == NodeKind::Chapter && n.title.eq_ignore_ascii_case("Dictionary"))
    else {
        return Vec::new();
    };
    let mut out = Vec::new();
    for id in h.collect_subtree(chapter.id) {
        let Some(n) = h.get(id) else { continue };
        if n.kind != NodeKind::Paragraph {
            continue;
        }
        let Some(body) = read_body(layout, n) else { continue };
        if let Ok(Some(entry)) = crate::language_entry::parse(&body) {
            out.push(entry);
        }
    }
    out
}

/// One entry: an HJSON body becomes a titled field list; prose goes through the
/// normal renderer.
fn render_entry(title: &str, content_type: Option<&str>, body: &str) -> String {
    if content_type == Some("hjson") {
        match serde_hjson::from_str::<serde_json::Value>(body) {
            Ok(value) => format!(
                "<section class=\"entry\"><h3 id=\"{}\">{}</h3>\n{}</section>\n",
                slugify(title),
                escape_html(title),
                value_to_html(&value)
            ),
            Err(_) => String::new(),
        }
    } else {
        let html = render::render_body(&[], body, &std::collections::BTreeMap::new());
        format!("<section class=\"entry\">{html}</section>\n")
    }
}

/// Render a parsed HJSON/JSON value as readable HTML. Objects become field lists;
/// a list of objects becomes a series of named cards (so a world's settlements or a
/// gazetteer's places read as entries, not raw nesting); a list of scalars becomes
/// an inline comma list.
fn value_to_html(v: &serde_json::Value) -> String {
    match v {
        serde_json::Value::Object(map) => {
            let mut s = String::from("<dl class=\"fields\">");
            for (k, val) in map {
                s.push_str(&format!("<dt>{}</dt><dd>{}</dd>", escape_html(k), value_to_html(val)));
            }
            s.push_str("</dl>");
            s
        }
        serde_json::Value::Array(arr) if !arr.is_empty() && arr.iter().all(|x| x.is_object()) => {
            let mut s = String::new();
            for item in arr {
                let name = item
                    .get("name")
                    .or_else(|| item.get("title"))
                    .and_then(|x| x.as_str());
                let head = name
                    .map(|n| format!("<h4>{}</h4>", escape_html(n)))
                    .unwrap_or_default();
                s.push_str(&format!("<div class=\"card\">{head}{}</div>", value_to_html(item)));
            }
            s
        }
        serde_json::Value::Array(arr)
            if arr.iter().all(|x| x.is_string() || x.is_number() || x.is_boolean()) =>
        {
            let items: Vec<String> = arr.iter().map(scalar_text).collect();
            format!("<p>{}</p>", items.join(", "))
        }
        serde_json::Value::Array(arr) => {
            let mut s = String::from("<ul>");
            for item in arr {
                s.push_str(&format!("<li>{}</li>", value_to_html(item)));
            }
            s.push_str("</ul>");
            s
        }
        serde_json::Value::String(s) => escape_html(s),
        serde_json::Value::Null => String::new(),
        other => escape_html(&other.to_string()),
    }
}

fn scalar_text(v: &serde_json::Value) -> String {
    match v {
        serde_json::Value::String(s) => escape_html(s),
        other => escape_html(&other.to_string()),
    }
}

/// Sources → a formatted, author-sorted bibliography.
fn render_sources(layout: &ProjectLayout, h: &Hierarchy, book_id: uuid::Uuid) -> String {
    let mut entries: Vec<crate::sources::BibEntry> = Vec::new();
    for id in h.collect_subtree(book_id) {
        let Some(n) = h.get(id) else { continue };
        if n.kind != NodeKind::Paragraph {
            continue;
        }
        let Some(body) = read_body(layout, n) else { continue };
        if let Some(e) = crate::sources::BibEntry::from_hjson(&body) {
            entries.push(e);
        }
    }
    if entries.is_empty() {
        return String::new();
    }
    entries.sort_by(|a, b| a.author.cmp(&b.author).then(a.year.cmp(&b.year)));

    let mut out = String::from("<h1>Sources</h1>\n<div class=\"bibliography\">\n");
    for e in &entries {
        out.push_str(&format_reference(e));
    }
    out.push_str("</div>\n");
    out
}

fn format_reference(e: &crate::sources::BibEntry) -> String {
    let mut s = format!("<p class=\"ref\" id=\"{}\">", escape_html(&e.key));
    if !e.author.is_empty() {
        s.push_str(&format!("<span class=\"ref-author\">{}</span> ", escape_html(&e.author)));
    }
    if !e.year.is_empty() {
        s.push_str(&format!("({}). ", escape_html(&e.year)));
    }
    if !e.title.is_empty() {
        s.push_str(&format!("<span class=\"ref-title\">{}</span>. ", escape_html(&e.title)));
    }
    if let Some(j) = e.journal.as_deref().filter(|s| !s.is_empty()) {
        s.push_str(&format!("<em>{}</em>. ", escape_html(j)));
    } else if let Some(p) = e.publisher.as_deref().filter(|s| !s.is_empty()) {
        s.push_str(&format!("{}. ", escape_html(p)));
    }
    if let Some(url) = e.url.as_deref().filter(|s| !s.is_empty()) {
        s.push_str(&format!("<a href=\"{}\">{}</a>", escape_html(url), escape_html(url)));
    }
    s.push_str("</p>\n");
    s
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn hjson_value_renders_field_list() {
        let v = serde_json::json!({"kind": "marsh", "notable": "the drowned tower"});
        let html = value_to_html(&v);
        assert!(html.contains("<dt>kind</dt><dd>marsh</dd>"), "got: {html}");
        assert!(html.contains("<dt>notable</dt><dd>the drowned tower</dd>"));
    }

    #[test]
    fn array_of_objects_renders_cards() {
        let v = serde_json::json!({
            "settlements": [
                {"name": "Fenhold", "population": 1200},
                {"name": "Tidemarsh", "population": 800}
            ],
            "climate": ["cold", "wet"]
        });
        let html = value_to_html(&v);
        assert!(html.contains("<div class=\"card\"><h4>Fenhold</h4>"), "got: {html}");
        assert!(html.contains("<h4>Tidemarsh</h4>"));
        // A list of scalars becomes an inline comma list, not nested bullets.
        assert!(html.contains("<p>cold, wet</p>"));
    }

    #[test]
    fn bibentry_formats_as_reference() {
        let e = crate::sources::BibEntry {
            key: "smith2020".into(),
            author: "Smith, J.".into(),
            title: "A Study".into(),
            year: "2020".into(),
            url: Some("https://x.example".into()),
            ..Default::default()
        };
        let s = format_reference(&e);
        assert!(s.contains("id=\"smith2020\""));
        assert!(s.contains("<span class=\"ref-author\">Smith, J.</span>"));
        assert!(s.contains("(2020)."));
        assert!(s.contains("<a href=\"https://x.example\">"));
    }
}