Skip to main content

weave_content/
html.rs

1//! Static HTML generator for case and entity pages.
2//!
3//! Produces semantic HTML fragments (no `<html>`/`<head>`/`<body>` wrapper)
4//! suitable for embedding in a Phoenix layout. Each fragment includes
5//! `data-og-*` attributes on the root element for meta tag extraction,
6//! Schema.org microdata, and a `<script type="application/ld+json">` block.
7
8#![allow(clippy::format_push_string)]
9
10use std::fmt::Write as _;
11
12use pulldown_cmark::{Event, Options, Parser, Tag, TagEnd};
13
14use crate::domain::Jurisdiction;
15use crate::output::{CaseOutput, NodeOutput, RelOutput};
16use crate::parser::SourceEntry;
17use sha2::{Digest, Sha256};
18
19/// Configuration for HTML generation.
20#[derive(Debug, Default, Clone)]
21pub struct HtmlConfig {
22    /// Base URL for rewriting thumbnail image sources.
23    ///
24    /// When set, original thumbnail URLs are rewritten to
25    /// `{base_url}/thumbnails/{sha256_hex[0..32]}.webp` using the same
26    /// deterministic key as `weave-image::thumbnail_key`.
27    ///
28    /// Example: `http://files.web.garage.localhost:3902`
29    pub thumbnail_base_url: Option<String>,
30}
31
32/// Length of the hex-encoded SHA-256 prefix used for thumbnail keys.
33const THUMB_KEY_HEX_LEN: usize = 32;
34
35/// Maximum size for a single HTML fragment file (500 KB).
36const MAX_FRAGMENT_BYTES: usize = 512_000;
37
38/// Generate a complete case page HTML fragment.
39///
40/// # Errors
41///
42/// Returns an error if the rendered HTML exceeds [`MAX_FRAGMENT_BYTES`].
43pub fn render_case(case: &CaseOutput, config: &HtmlConfig) -> Result<String, String> {
44    let mut html = String::with_capacity(8192);
45
46    let og_title = truncate(&case.title, 120);
47    let og_description = build_case_og_description(case);
48    let og_tagline = case
49        .tagline
50        .as_deref()
51        .map(|t| format!(" data-og-tagline=\"{}\"", escape_attr(t)))
52        .unwrap_or_default();
53
54    // Root element with OG data attributes
55    html.push_str(&format!(
56        "<article class=\"loom-case\" itemscope itemtype=\"https://schema.org/Article\" \
57         data-og-title=\"{}\" \
58         data-og-description=\"{}\" \
59         data-og-type=\"article\" \
60         data-og-url=\"/{}\"{}{}>\n",
61        escape_attr(&og_title),
62        escape_attr(&og_description),
63        escape_attr(case.slug.as_deref().unwrap_or(&case.case_id)),
64        og_image_attr(case_hero_image(case).as_deref(), config),
65        og_tagline,
66    ));
67
68    // Header
69    let country = case
70        .slug
71        .as_deref()
72        .and_then(extract_country_from_case_slug);
73    render_case_header(&mut html, case, country.as_deref());
74
75    // Financial details — prominent position right after header
76    render_financial_details(&mut html, &case.relationships, &case.nodes);
77
78    // Sources
79    render_sources(&mut html, &case.sources);
80
81    // People section
82    let people: Vec<&NodeOutput> = case.nodes.iter().filter(|n| n.label == "person").collect();
83    if !people.is_empty() {
84        render_entity_section(&mut html, "People", &people, config);
85    }
86
87    // Organizations section
88    let orgs: Vec<&NodeOutput> = case
89        .nodes
90        .iter()
91        .filter(|n| n.label == "organization")
92        .collect();
93    if !orgs.is_empty() {
94        render_entity_section(&mut html, "Organizations", &orgs, config);
95    }
96
97    // Timeline section (events sorted by occurred_at)
98    let mut events: Vec<&NodeOutput> = case.nodes.iter().filter(|n| n.label == "event").collect();
99    events.sort_by(|a, b| a.occurred_at.cmp(&b.occurred_at));
100    if !events.is_empty() {
101        render_timeline(&mut html, &events);
102    }
103
104    // Related Cases section
105    render_related_cases(&mut html, &case.relationships, &case.nodes);
106
107    // JSON-LD
108    render_case_json_ld(&mut html, case);
109
110    html.push_str("</article>\n");
111
112    if html.len() > MAX_FRAGMENT_BYTES {
113        return Err(format!(
114            "HTML fragment exceeds {MAX_FRAGMENT_BYTES} bytes ({} bytes)",
115            html.len()
116        ));
117    }
118
119    Ok(html)
120}
121
122/// Generate a person page HTML fragment.
123///
124/// # Errors
125///
126/// Returns an error if the rendered HTML exceeds [`MAX_FRAGMENT_BYTES`].
127pub fn render_person(
128    node: &NodeOutput,
129    cases: &[(String, String)], // (case_id, case_title)
130    config: &HtmlConfig,
131) -> Result<String, String> {
132    let mut html = String::with_capacity(4096);
133
134    let og_title = truncate(&node.name, 120);
135    let og_description = build_person_og_description(node);
136
137    html.push_str(&format!(
138        "<article class=\"loom-person\" itemscope itemtype=\"https://schema.org/Person\" \
139         data-og-title=\"{}\" \
140         data-og-description=\"{}\" \
141         data-og-type=\"profile\" \
142         data-og-url=\"/{}\"{}>\n",
143        escape_attr(&og_title),
144        escape_attr(&og_description),
145        escape_attr(node.slug.as_deref().unwrap_or(&node.id)),
146        og_image_attr(node.thumbnail.as_deref(), config),
147    ));
148
149    render_entity_detail(&mut html, node, config);
150    render_cases_list(&mut html, cases);
151    render_person_json_ld(&mut html, node);
152
153    html.push_str("</article>\n");
154
155    check_size(&html)
156}
157
158/// Generate an organization page HTML fragment.
159///
160/// # Errors
161///
162/// Returns an error if the rendered HTML exceeds [`MAX_FRAGMENT_BYTES`].
163pub fn render_organization(
164    node: &NodeOutput,
165    cases: &[(String, String)],
166    config: &HtmlConfig,
167) -> Result<String, String> {
168    let mut html = String::with_capacity(4096);
169
170    let og_title = truncate(&node.name, 120);
171    let og_description = build_org_og_description(node);
172
173    html.push_str(&format!(
174        "<article class=\"loom-organization\" itemscope itemtype=\"https://schema.org/Organization\" \
175         data-og-title=\"{}\" \
176         data-og-description=\"{}\" \
177         data-og-type=\"profile\" \
178         data-og-url=\"/{}\"{}>\n",
179        escape_attr(&og_title),
180        escape_attr(&og_description),
181        escape_attr(node.slug.as_deref().unwrap_or(&node.id)),
182        og_image_attr(node.thumbnail.as_deref(), config),
183    ));
184
185    render_entity_detail(&mut html, node, config);
186    render_cases_list(&mut html, cases);
187    render_org_json_ld(&mut html, node);
188
189    html.push_str("</article>\n");
190
191    check_size(&html)
192}
193
194// --- Case sections ---
195
196fn render_case_header(html: &mut String, case: &CaseOutput, country: Option<&str>) {
197    html.push_str(&format!(
198        "  <header class=\"loom-case-header\">\n    <h1 itemprop=\"headline\">{}</h1>\n",
199        escape(&case.title)
200    ));
201
202    if let Some(tagline) = &case.tagline {
203        html.push_str(&format!(
204            "    <blockquote class=\"loom-tagline\">{}</blockquote>\n",
205            escape(tagline)
206        ));
207    }
208
209    if !case.amounts.is_empty() {
210        html.push_str("    <div class=\"loom-case-amounts\">\n");
211        for entry in &case.amounts {
212            let approx_cls = if entry.approximate {
213                " loom-amount-approx"
214            } else {
215                ""
216            };
217            let label_cls = entry
218                .label
219                .as_deref()
220                .unwrap_or("unlabeled")
221                .replace('_', "-");
222            html.push_str(&format!(
223                "      <span class=\"loom-amount-badge loom-amount-{label_cls}{approx_cls}\">{}</span>\n",
224                escape(&entry.format_display())
225            ));
226        }
227        html.push_str("    </div>\n");
228    }
229
230    if !case.tags.is_empty() {
231        html.push_str("    <div class=\"loom-tags\">\n");
232        for tag in &case.tags {
233            let href = match country {
234                Some(cc) => format!("/tags/{}/{}", escape_attr(cc), escape_attr(tag)),
235                None => format!("/tags/{}", escape_attr(tag)),
236            };
237            html.push_str(&format!(
238                "      <a href=\"{}\" class=\"loom-tag\">{}</a>\n",
239                href,
240                escape(tag)
241            ));
242        }
243        html.push_str("    </div>\n");
244    }
245
246    if !case.summary.is_empty() {
247        html.push_str(&format!(
248            "    <p class=\"loom-summary\" itemprop=\"description\">{}</p>\n",
249            render_inline_markdown(&case.summary)
250        ));
251    }
252
253    // Wall link for the case node
254    html.push_str(&format!(
255        "    <a href=\"/walls/{}\" class=\"loom-wall-link\">View on the wall</a>\n",
256        escape_attr(&case.id)
257    ));
258
259    html.push_str("  </header>\n");
260}
261
262fn render_sources(html: &mut String, sources: &[SourceEntry]) {
263    if sources.is_empty() {
264        return;
265    }
266    html.push_str("  <section class=\"loom-sources\">\n    <h2>Sources</h2>\n    <ol>\n");
267    for source in sources {
268        match source {
269            SourceEntry::Url(url) => {
270                html.push_str(&format!(
271                    "      <li><a href=\"{}\" rel=\"noopener noreferrer\" target=\"_blank\">{}</a></li>\n",
272                    escape_attr(url),
273                    escape(url)
274                ));
275            }
276            SourceEntry::Structured { url, title, .. } => {
277                let display = title.as_deref().unwrap_or(url.as_str());
278                html.push_str(&format!(
279                    "      <li><a href=\"{}\" rel=\"noopener noreferrer\" target=\"_blank\">{}</a></li>\n",
280                    escape_attr(url),
281                    escape(display)
282                ));
283            }
284        }
285    }
286    html.push_str("    </ol>\n  </section>\n");
287}
288
289fn render_entity_section(
290    html: &mut String,
291    title: &str,
292    nodes: &[&NodeOutput],
293    config: &HtmlConfig,
294) {
295    html.push_str(&format!(
296        "  <section class=\"loom-entities loom-entities-{}\">\n    <h2>{title}</h2>\n    <div class=\"loom-entity-cards\">\n",
297        title.to_lowercase()
298    ));
299    for node in nodes {
300        render_entity_card(html, node, config);
301    }
302    html.push_str("    </div>\n  </section>\n");
303}
304
305fn render_entity_card(html: &mut String, node: &NodeOutput, config: &HtmlConfig) {
306    let schema_type = match node.label.as_str() {
307        "person" => "Person",
308        "organization" => "Organization",
309        _ => "Thing",
310    };
311    html.push_str(&format!(
312        "      <div class=\"loom-entity-card\" itemscope itemtype=\"https://schema.org/{schema_type}\">\n"
313    ));
314
315    if let Some(thumb) = &node.thumbnail {
316        let thumb_url = rewrite_thumbnail_url(thumb, config);
317        html.push_str(&format!(
318            "        <img src=\"{}\" alt=\"{}\" class=\"loom-thumbnail\" itemprop=\"image\" loading=\"lazy\" width=\"64\" height=\"64\" />\n",
319            escape_attr(&thumb_url),
320            escape_attr(&node.name)
321        ));
322    }
323
324    // Link to static view when slug is available, otherwise fall back to wall
325    let entity_href = if let Some(slug) = &node.slug {
326        format!("/{}", escape_attr(slug))
327    } else {
328        format!("/walls/{}", escape_attr(&node.id))
329    };
330
331    html.push_str(&format!(
332        "        <div class=\"loom-entity-info\">\n          \
333         <a href=\"{}\" class=\"loom-entity-name\" itemprop=\"name\">{}</a>\n",
334        entity_href,
335        escape(&node.name)
336    ));
337
338    if let Some(q) = &node.qualifier {
339        html.push_str(&format!(
340            "          <span class=\"loom-qualifier\">{}</span>\n",
341            escape(q)
342        ));
343    }
344
345    // Label-specific fields
346    match node.label.as_str() {
347        "person" => {
348            let roles: Vec<_> = node.role.iter().map(|r| format_enum(r)).collect();
349            render_dl_field(html, "Role", &roles.join(", "));
350            render_dl_opt_country(html, "Nationality", node.nationality.as_ref());
351        }
352        "organization" => {
353            render_dl_opt_formatted(html, "Type", node.org_type.as_ref());
354            if let Some(j) = &node.jurisdiction {
355                render_dl_field(html, "Jurisdiction", &format_jurisdiction(j));
356            }
357        }
358        "asset" => {
359            render_dl_opt_formatted(html, "Type", node.asset_type.as_ref());
360            if let Some(m) = &node.value {
361                render_dl_field(html, "Value", &m.display);
362            }
363            render_dl_opt_formatted(html, "Status", node.status.as_ref());
364        }
365        "document" => {
366            render_dl_opt_formatted(html, "Type", node.doc_type.as_ref());
367            render_dl_opt(html, "Issued", node.issued_at.as_ref());
368        }
369        "event" => {
370            render_dl_opt_formatted(html, "Type", node.event_type.as_ref());
371            render_dl_opt(html, "Date", node.occurred_at.as_ref());
372        }
373        _ => {}
374    }
375
376    html.push_str("        </div>\n      </div>\n");
377}
378
379fn render_timeline(html: &mut String, events: &[&NodeOutput]) {
380    html.push_str(
381        "  <section class=\"loom-timeline\">\n    <h2>Timeline</h2>\n    <ol class=\"loom-events\">\n",
382    );
383    for event in events {
384        html.push_str("      <li class=\"loom-event\">\n");
385        if let Some(date) = &event.occurred_at {
386            html.push_str(&format!(
387                "        <time datetime=\"{}\" class=\"loom-event-date\">{}</time>\n",
388                escape_attr(date),
389                escape(date)
390            ));
391        }
392        html.push_str("        <div class=\"loom-event-body\">\n");
393        html.push_str(&format!(
394            "          <span class=\"loom-event-name\">{}</span>\n",
395            escape(&event.name)
396        ));
397        if let Some(et) = &event.event_type {
398            html.push_str(&format!(
399                "          <span class=\"loom-event-type\">{}</span>\n",
400                escape(&format_enum(et))
401            ));
402        }
403        if let Some(desc) = &event.description {
404            html.push_str(&format!(
405                "          <p class=\"loom-event-description\">{}</p>\n",
406                escape(desc)
407            ));
408        }
409        html.push_str("        </div>\n");
410        html.push_str("      </li>\n");
411    }
412    html.push_str("    </ol>\n  </section>\n");
413}
414
415fn render_related_cases(html: &mut String, relationships: &[RelOutput], nodes: &[NodeOutput]) {
416    let related: Vec<&RelOutput> = relationships
417        .iter()
418        .filter(|r| r.rel_type == "related_to")
419        .collect();
420    if related.is_empty() {
421        return;
422    }
423    html.push_str(
424        "  <section class=\"loom-related-cases\">\n    <h2>Related Cases</h2>\n    <div class=\"loom-related-list\">\n",
425    );
426    for rel in &related {
427        if let Some(node) = nodes
428            .iter()
429            .find(|n| n.id == rel.target_id && n.label == "case")
430        {
431            let href = node
432                .slug
433                .as_deref()
434                .map_or_else(|| format!("/cases/{}", node.id), |s| format!("/{s}"));
435            let desc = rel.description.as_deref().unwrap_or("");
436            html.push_str(&format!(
437                "      <a href=\"{}\" class=\"loom-related-card\">\n        <span class=\"loom-related-title\">{}</span>\n",
438                escape_attr(&href),
439                escape(&node.name)
440            ));
441            if !desc.is_empty() {
442                html.push_str(&format!(
443                    "        <span class=\"loom-related-desc\">{}</span>\n",
444                    escape(desc)
445                ));
446            }
447            html.push_str("      </a>\n");
448        }
449    }
450    html.push_str("    </div>\n  </section>\n");
451}
452
453// --- Financial details ---
454
455fn render_financial_details(html: &mut String, relationships: &[RelOutput], nodes: &[NodeOutput]) {
456    let financial: Vec<&RelOutput> = relationships
457        .iter()
458        .filter(|r| !r.amounts.is_empty())
459        .collect();
460    if financial.is_empty() {
461        return;
462    }
463
464    let node_name = |id: &str| -> String {
465        nodes
466            .iter()
467            .find(|n| n.id == id)
468            .map_or_else(|| id.to_string(), |n| n.name.clone())
469    };
470
471    html.push_str(
472        "  <section class=\"loom-financial\">\n    <h2>Financial Details</h2>\n    <dl class=\"loom-financial-list\">\n",
473    );
474    for rel in &financial {
475        let source = node_name(&rel.source_id);
476        let target = node_name(&rel.target_id);
477        let rel_label = format_enum(&rel.rel_type);
478        html.push_str(&format!(
479            "      <div class=\"loom-financial-entry\">\n        <dt>{} &rarr; {} <span class=\"loom-rel-label\">{}</span></dt>\n",
480            escape(&source), escape(&target), escape(&rel_label)
481        ));
482        for entry in &rel.amounts {
483            let approx_cls = if entry.approximate {
484                " loom-amount-approx"
485            } else {
486                ""
487            };
488            html.push_str(&format!(
489                "        <dd><span class=\"loom-amount-badge{}\">{}</span></dd>\n",
490                approx_cls,
491                escape(&entry.format_display())
492            ));
493        }
494        html.push_str("      </div>\n");
495    }
496    html.push_str("    </dl>\n  </section>\n");
497}
498
499// --- Entity detail page ---
500
501fn render_entity_detail(html: &mut String, node: &NodeOutput, config: &HtmlConfig) {
502    html.push_str("  <header class=\"loom-entity-header\">\n");
503
504    if let Some(thumb) = &node.thumbnail {
505        let thumb_url = rewrite_thumbnail_url(thumb, config);
506        html.push_str(&format!(
507            "    <img src=\"{}\" alt=\"{}\" class=\"loom-thumbnail-large\" itemprop=\"image\" loading=\"lazy\" width=\"128\" height=\"128\" />\n",
508            escape_attr(&thumb_url),
509            escape_attr(&node.name)
510        ));
511    }
512
513    html.push_str(&format!(
514        "    <h1 itemprop=\"name\">{}</h1>\n",
515        escape(&node.name)
516    ));
517
518    if let Some(q) = &node.qualifier {
519        html.push_str(&format!(
520            "    <p class=\"loom-qualifier\">{}</p>\n",
521            escape(q)
522        ));
523    }
524
525    html.push_str(&format!(
526        "    <a href=\"/walls/{}\" class=\"loom-wall-link\">View on the wall</a>\n",
527        escape_attr(&node.id)
528    ));
529    html.push_str("  </header>\n");
530
531    // Description
532    if let Some(desc) = &node.description {
533        html.push_str(&format!(
534            "  <p class=\"loom-description\" itemprop=\"description\">{}</p>\n",
535            escape(desc)
536        ));
537    }
538
539    // Fields as definition list
540    html.push_str("  <dl class=\"loom-fields\">\n");
541
542    match node.label.as_str() {
543        "person" => {
544            let roles: Vec<_> = node.role.iter().map(|r| format_enum(r)).collect();
545            render_dl_item(html, "Role", &roles.join(", "));
546            render_dl_opt_country_item(html, "Nationality", node.nationality.as_ref());
547            render_dl_opt_item(html, "Date of Birth", node.date_of_birth.as_ref());
548            render_dl_opt_item(html, "Place of Birth", node.place_of_birth.as_ref());
549            render_dl_opt_formatted_item(html, "Status", node.status.as_ref());
550        }
551        "organization" => {
552            render_dl_opt_formatted_item(html, "Type", node.org_type.as_ref());
553            if let Some(j) = &node.jurisdiction {
554                render_dl_item(html, "Jurisdiction", &format_jurisdiction(j));
555            }
556            render_dl_opt_item(html, "Headquarters", node.headquarters.as_ref());
557            render_dl_opt_item(html, "Founded", node.founded_date.as_ref());
558            render_dl_opt_item(html, "Registration", node.registration_number.as_ref());
559            render_dl_opt_formatted_item(html, "Status", node.status.as_ref());
560        }
561        "asset" => {
562            render_dl_opt_formatted_item(html, "Type", node.asset_type.as_ref());
563            if let Some(m) = &node.value {
564                render_dl_item(html, "Value", &m.display);
565            }
566            render_dl_opt_formatted_item(html, "Status", node.status.as_ref());
567        }
568        "document" => {
569            render_dl_opt_formatted_item(html, "Type", node.doc_type.as_ref());
570            render_dl_opt_item(html, "Issued", node.issued_at.as_ref());
571            render_dl_opt_item(html, "Issuing Authority", node.issuing_authority.as_ref());
572            render_dl_opt_item(html, "Case Number", node.case_number.as_ref());
573        }
574        "event" => {
575            render_dl_opt_formatted_item(html, "Type", node.event_type.as_ref());
576            render_dl_opt_item(html, "Date", node.occurred_at.as_ref());
577            render_dl_opt_formatted_item(html, "Severity", node.severity.as_ref());
578            if let Some(j) = &node.jurisdiction {
579                render_dl_item(html, "Jurisdiction", &format_jurisdiction(j));
580            }
581        }
582        _ => {}
583    }
584
585    html.push_str("  </dl>\n");
586
587    render_entity_supplementary(html, node);
588}
589
590fn render_entity_supplementary(html: &mut String, node: &NodeOutput) {
591    if !node.aliases.is_empty() {
592        html.push_str("  <div class=\"loom-aliases\">\n    <h3>Also known as</h3>\n    <p>");
593        let escaped: Vec<String> = node.aliases.iter().map(|a| escape(a)).collect();
594        html.push_str(&escaped.join(", "));
595        html.push_str("</p>\n  </div>\n");
596    }
597
598    if !node.urls.is_empty() {
599        html.push_str("  <div class=\"loom-urls\">\n    <h3>Links</h3>\n    <p>");
600        let links: Vec<String> = node
601            .urls
602            .iter()
603            .map(|url| {
604                let label = url
605                    .strip_prefix("https://")
606                    .or_else(|| url.strip_prefix("http://"))
607                    .unwrap_or(url)
608                    .trim_end_matches('/');
609                format!(
610                    "<a href=\"{}\" rel=\"noopener noreferrer\" target=\"_blank\">{}</a>",
611                    escape_attr(url),
612                    escape(label)
613                )
614            })
615            .collect();
616        html.push_str(&links.join(" · "));
617        html.push_str("</p>\n  </div>\n");
618    }
619}
620
621fn render_cases_list(html: &mut String, cases: &[(String, String)]) {
622    if cases.is_empty() {
623        return;
624    }
625    html.push_str(
626        "  <section class=\"loom-cases\">\n    <h2>Cases</h2>\n    <ul class=\"loom-case-list\">\n",
627    );
628    for (case_slug, case_title) in cases {
629        html.push_str(&format!(
630            "      <li><a href=\"/{}\">{}</a></li>\n",
631            escape_attr(case_slug),
632            escape(case_title)
633        ));
634    }
635    html.push_str("    </ul>\n  </section>\n");
636}
637
638// --- JSON-LD ---
639
640fn render_case_json_ld(html: &mut String, case: &CaseOutput) {
641    let mut ld = serde_json::json!({
642        "@context": "https://schema.org",
643        "@type": "Article",
644        "headline": truncate(&case.title, 120),
645        "description": truncate(&case.summary, 200),
646        "url": format!("/{}", case.slug.as_deref().unwrap_or(&case.case_id)),
647    });
648
649    if !case.sources.is_empty() {
650        let urls: Vec<&str> = case
651            .sources
652            .iter()
653            .map(|s| match s {
654                SourceEntry::Url(u) => u.as_str(),
655                SourceEntry::Structured { url, .. } => url.as_str(),
656            })
657            .collect();
658        ld["citation"] = serde_json::json!(urls);
659    }
660
661    html.push_str(&format!(
662        "  <script type=\"application/ld+json\">{}</script>\n",
663        serde_json::to_string(&ld).unwrap_or_default()
664    ));
665}
666
667fn render_person_json_ld(html: &mut String, node: &NodeOutput) {
668    let mut ld = serde_json::json!({
669        "@context": "https://schema.org",
670        "@type": "Person",
671        "name": &node.name,
672        "url": format!("/{}", node.slug.as_deref().unwrap_or(&node.id)),
673    });
674
675    if let Some(nat) = &node.nationality {
676        ld["nationality"] = serde_json::json!(nat);
677    }
678    if let Some(desc) = &node.description {
679        ld["description"] = serde_json::json!(truncate(desc, 200));
680    }
681    if let Some(thumb) = &node.thumbnail {
682        ld["image"] = serde_json::json!(thumb);
683    }
684
685    html.push_str(&format!(
686        "  <script type=\"application/ld+json\">{}</script>\n",
687        serde_json::to_string(&ld).unwrap_or_default()
688    ));
689}
690
691fn render_org_json_ld(html: &mut String, node: &NodeOutput) {
692    let mut ld = serde_json::json!({
693        "@context": "https://schema.org",
694        "@type": "Organization",
695        "name": &node.name,
696        "url": format!("/{}", node.slug.as_deref().unwrap_or(&node.id)),
697    });
698
699    if let Some(desc) = &node.description {
700        ld["description"] = serde_json::json!(truncate(desc, 200));
701    }
702    if let Some(thumb) = &node.thumbnail {
703        ld["logo"] = serde_json::json!(thumb);
704    }
705
706    html.push_str(&format!(
707        "  <script type=\"application/ld+json\">{}</script>\n",
708        serde_json::to_string(&ld).unwrap_or_default()
709    ));
710}
711
712// --- Tag pages ---
713
714/// A case entry associated with a tag, used for tag page rendering.
715pub struct TagCaseEntry {
716    /// Display slug for the case link (e.g. `cases/id/corruption/2024/test-case`).
717    pub slug: String,
718    /// Case title.
719    pub title: String,
720    /// Structured amounts for display badge.
721    pub amounts: Vec<crate::domain::AmountEntry>,
722}
723
724/// Generate a tag page HTML fragment listing all cases with this tag.
725///
726/// # Errors
727///
728/// Returns an error if the rendered HTML exceeds [`MAX_FRAGMENT_BYTES`].
729pub fn render_tag_page(tag: &str, cases: &[TagCaseEntry]) -> Result<String, String> {
730    render_tag_page_with_path(tag, &format!("/tags/{}", escape_attr(tag)), cases)
731}
732
733pub fn render_tag_page_scoped(
734    tag: &str,
735    country: &str,
736    cases: &[TagCaseEntry],
737) -> Result<String, String> {
738    let display_tag = format!("{} ({})", tag.replace('-', " "), country.to_uppercase());
739    render_tag_page_with_path(
740        &display_tag,
741        &format!("/tags/{}/{}", escape_attr(country), escape_attr(tag)),
742        cases,
743    )
744}
745
746fn render_tag_page_with_path(
747    display: &str,
748    og_url: &str,
749    cases: &[TagCaseEntry],
750) -> Result<String, String> {
751    let mut html = String::with_capacity(2048);
752
753    let og_title = format!("Cases tagged \"{display}\"");
754
755    html.push_str(&format!(
756        "<article class=\"loom-tag-page\" \
757         data-og-title=\"{}\" \
758         data-og-description=\"{} cases tagged with {}\" \
759         data-og-type=\"website\" \
760         data-og-url=\"{}\">\n",
761        escape_attr(&og_title),
762        cases.len(),
763        escape_attr(display),
764        escape_attr(og_url),
765    ));
766
767    html.push_str(&format!(
768        "  <header class=\"loom-tag-header\">\n    \
769         <h1>{}</h1>\n    \
770         <p class=\"loom-tag-count\">{} cases</p>\n  \
771         </header>\n",
772        escape(display),
773        cases.len(),
774    ));
775
776    html.push_str("  <ul class=\"loom-case-list\">\n");
777    for entry in cases {
778        let amount_badges = if entry.amounts.is_empty() {
779            String::new()
780        } else {
781            let badges: Vec<String> = entry
782                .amounts
783                .iter()
784                .map(|a| {
785                    format!(
786                        " <span class=\"loom-amount-badge\">{}</span>",
787                        escape(&a.format_display())
788                    )
789                })
790                .collect();
791            badges.join("")
792        };
793        html.push_str(&format!(
794            "    <li><a href=\"/{}\">{}</a>{}</li>\n",
795            escape_attr(&entry.slug),
796            escape(&entry.title),
797            amount_badges,
798        ));
799    }
800    html.push_str("  </ul>\n");
801
802    html.push_str("</article>\n");
803
804    check_size(&html)
805}
806
807// --- Sitemap ---
808
809/// Generate a sitemap XML string.
810///
811/// All tuples are `(slug, display_name)` where slug is the full file-path slug
812/// (e.g. `cases/id/corruption/2024/hambalang-case`).
813pub fn render_sitemap(
814    cases: &[(String, String)],
815    people: &[(String, String)],
816    organizations: &[(String, String)],
817    base_url: &str,
818) -> String {
819    let mut xml = String::with_capacity(4096);
820    xml.push_str("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
821    xml.push_str("<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">\n");
822
823    for (slug, _) in cases {
824        xml.push_str(&format!(
825            "  <url><loc>{base_url}/{}</loc></url>\n",
826            escape(slug)
827        ));
828    }
829    for (slug, _) in people {
830        xml.push_str(&format!(
831            "  <url><loc>{base_url}/{}</loc></url>\n",
832            escape(slug)
833        ));
834    }
835    for (slug, _) in organizations {
836        xml.push_str(&format!(
837            "  <url><loc>{base_url}/{}</loc></url>\n",
838            escape(slug)
839        ));
840    }
841
842    xml.push_str("</urlset>\n");
843    xml
844}
845
846// --- Helpers ---
847
848fn build_case_og_description(case: &CaseOutput) -> String {
849    if let Some(tagline) = &case.tagline {
850        return truncate(tagline, 200);
851    }
852    if !case.summary.is_empty() {
853        return truncate(&case.summary, 200);
854    }
855    let people_count = case.nodes.iter().filter(|n| n.label == "person").count();
856    let org_count = case
857        .nodes
858        .iter()
859        .filter(|n| n.label == "organization")
860        .count();
861    truncate(
862        &format!(
863            "{} people, {} organizations, {} connections",
864            people_count,
865            org_count,
866            case.relationships.len()
867        ),
868        200,
869    )
870}
871
872fn build_person_og_description(node: &NodeOutput) -> String {
873    let mut parts = Vec::new();
874    if let Some(q) = &node.qualifier {
875        parts.push(q.clone());
876    }
877    if !node.role.is_empty() {
878        let roles: Vec<_> = node.role.iter().map(|r| format_enum(r)).collect();
879        parts.push(roles.join(", "));
880    }
881    if let Some(nat) = &node.nationality {
882        parts.push(country_name(nat));
883    }
884    if parts.is_empty() {
885        return truncate(&node.name, 200);
886    }
887    truncate(&format!("{} — {}", node.name, parts.join(" · ")), 200)
888}
889
890fn build_org_og_description(node: &NodeOutput) -> String {
891    let mut parts = Vec::new();
892    if let Some(q) = &node.qualifier {
893        parts.push(q.clone());
894    }
895    if let Some(ot) = &node.org_type {
896        parts.push(format_enum(ot));
897    }
898    if let Some(j) = &node.jurisdiction {
899        parts.push(format_jurisdiction(j));
900    }
901    if parts.is_empty() {
902        return truncate(&node.name, 200);
903    }
904    truncate(&format!("{} — {}", node.name, parts.join(" · ")), 200)
905}
906
907fn check_size(html: &str) -> Result<String, String> {
908    if html.len() > MAX_FRAGMENT_BYTES {
909        Err(format!(
910            "HTML fragment exceeds {MAX_FRAGMENT_BYTES} bytes ({} bytes)",
911            html.len()
912        ))
913    } else {
914        Ok(html.to_string())
915    }
916}
917
918fn truncate(s: &str, max: usize) -> String {
919    if s.len() <= max {
920        s.to_string()
921    } else {
922        let truncated: String = s.chars().take(max.saturating_sub(3)).collect();
923        format!("{truncated}...")
924    }
925}
926
927fn escape(s: &str) -> String {
928    s.replace('&', "&amp;")
929        .replace('<', "&lt;")
930        .replace('>', "&gt;")
931        .replace('"', "&quot;")
932}
933
934fn escape_attr(s: &str) -> String {
935    escape(s)
936}
937
938/// Render inline markdown to HTML, stripping block-level wrappers.
939///
940/// Supports bold, italic, links, and inline code. Block-level elements
941/// (paragraphs, headings, lists) are flattened to their inline content.
942fn render_inline_markdown(s: &str) -> String {
943    let parser = Parser::new_ext(s, Options::empty());
944    let mut out = String::with_capacity(s.len());
945
946    for event in parser {
947        match event {
948            Event::Text(text) => out.push_str(&escape(&text)),
949            Event::Code(code) => {
950                out.push_str("<code>");
951                out.push_str(&escape(&code));
952                out.push_str("</code>");
953            }
954            Event::Start(Tag::Strong) => out.push_str("<strong>"),
955            Event::End(TagEnd::Strong) => out.push_str("</strong>"),
956            Event::Start(Tag::Emphasis) => out.push_str("<em>"),
957            Event::End(TagEnd::Emphasis) => out.push_str("</em>"),
958            Event::Start(Tag::Link { dest_url, .. }) => {
959                out.push_str(&format!(
960                    "<a href=\"{}\" rel=\"noopener noreferrer\">",
961                    escape_attr(&dest_url)
962                ));
963            }
964            Event::End(TagEnd::Link) => out.push_str("</a>"),
965            Event::SoftBreak | Event::HardBreak => out.push(' '),
966            _ => {}
967        }
968    }
969
970    out
971}
972
973/// Rewrite a thumbnail source URL to a hosted URL.
974///
975/// When `config.thumbnail_base_url` is set, computes the deterministic
976/// key `thumbnails/{sha256_hex[0..32]}.webp` (matching `weave-image`)
977/// and returns `{base_url}/thumbnails/{hash}.webp`.
978///
979/// When not set, returns the original URL unchanged.
980fn rewrite_thumbnail_url(source_url: &str, config: &HtmlConfig) -> String {
981    match &config.thumbnail_base_url {
982        Some(base) => {
983            // If already rewritten (e.g. by loom-seed thumbnail processor), return as-is
984            if source_url.starts_with(base.as_str()) {
985                return source_url.to_string();
986            }
987            let key = thumbnail_key(source_url);
988            format!("{base}/{key}")
989        }
990        None => source_url.to_string(),
991    }
992}
993
994/// Compute the thumbnail object key from a source URL.
995///
996/// Returns `thumbnails/{sha256_hex[0..32]}.webp`, matching the algorithm
997/// in `weave-image::thumbnail_key`.
998fn thumbnail_key(source_url: &str) -> String {
999    let mut hasher = Sha256::new();
1000    hasher.update(source_url.as_bytes());
1001    let hash = hasher.finalize();
1002    let hex = hex_encode(&hash);
1003    format!("thumbnails/{}.webp", &hex[..THUMB_KEY_HEX_LEN])
1004}
1005
1006/// Encode bytes as lowercase hex string.
1007fn hex_encode(bytes: &[u8]) -> String {
1008    let mut s = String::with_capacity(bytes.len() * 2);
1009    for b in bytes {
1010        let _ = write!(s, "{b:02x}");
1011    }
1012    s
1013}
1014
1015/// Build `data-og-image` attribute string if a URL is available.
1016fn og_image_attr(url: Option<&str>, config: &HtmlConfig) -> String {
1017    match url {
1018        Some(u) if !u.is_empty() => {
1019            let rewritten = rewrite_thumbnail_url(u, config);
1020            format!(" data-og-image=\"{}\"", escape_attr(&rewritten))
1021        }
1022        _ => String::new(),
1023    }
1024}
1025
1026/// Find the first person thumbnail in a case to use as hero image.
1027fn case_hero_image(case: &CaseOutput) -> Option<String> {
1028    case.nodes
1029        .iter()
1030        .filter(|n| n.label == "person")
1031        .find_map(|n| n.thumbnail.clone())
1032}
1033
1034fn format_jurisdiction(j: &Jurisdiction) -> String {
1035    let country = country_name(&j.country);
1036    match &j.subdivision {
1037        Some(sub) => format!("{country}, {sub}"),
1038        None => country,
1039    }
1040}
1041
1042/// Map ISO 3166-1 alpha-2 codes to country names.
1043/// Returns the code itself if not found (graceful fallback).
1044fn country_name(code: &str) -> String {
1045    match code.to_uppercase().as_str() {
1046        "AF" => "Afghanistan",
1047        "AL" => "Albania",
1048        "DZ" => "Algeria",
1049        "AR" => "Argentina",
1050        "AU" => "Australia",
1051        "AT" => "Austria",
1052        "BD" => "Bangladesh",
1053        "BE" => "Belgium",
1054        "BR" => "Brazil",
1055        "BN" => "Brunei",
1056        "KH" => "Cambodia",
1057        "CA" => "Canada",
1058        "CN" => "China",
1059        "CO" => "Colombia",
1060        "HR" => "Croatia",
1061        "CZ" => "Czech Republic",
1062        "DK" => "Denmark",
1063        "EG" => "Egypt",
1064        "FI" => "Finland",
1065        "FR" => "France",
1066        "DE" => "Germany",
1067        "GH" => "Ghana",
1068        "GR" => "Greece",
1069        "HK" => "Hong Kong",
1070        "HU" => "Hungary",
1071        "IN" => "India",
1072        "ID" => "Indonesia",
1073        "IR" => "Iran",
1074        "IQ" => "Iraq",
1075        "IE" => "Ireland",
1076        "IL" => "Israel",
1077        "IT" => "Italy",
1078        "JP" => "Japan",
1079        "KE" => "Kenya",
1080        "KR" => "South Korea",
1081        "KW" => "Kuwait",
1082        "LA" => "Laos",
1083        "LB" => "Lebanon",
1084        "MY" => "Malaysia",
1085        "MX" => "Mexico",
1086        "MM" => "Myanmar",
1087        "NL" => "Netherlands",
1088        "NZ" => "New Zealand",
1089        "NG" => "Nigeria",
1090        "NO" => "Norway",
1091        "PK" => "Pakistan",
1092        "PH" => "Philippines",
1093        "PL" => "Poland",
1094        "PT" => "Portugal",
1095        "QA" => "Qatar",
1096        "RO" => "Romania",
1097        "RU" => "Russia",
1098        "SA" => "Saudi Arabia",
1099        "SG" => "Singapore",
1100        "ZA" => "South Africa",
1101        "ES" => "Spain",
1102        "LK" => "Sri Lanka",
1103        "SE" => "Sweden",
1104        "CH" => "Switzerland",
1105        "TW" => "Taiwan",
1106        "TH" => "Thailand",
1107        "TL" => "Timor-Leste",
1108        "TR" => "Turkey",
1109        "AE" => "United Arab Emirates",
1110        "GB" => "United Kingdom",
1111        "US" => "United States",
1112        "VN" => "Vietnam",
1113        _ => return code.to_uppercase(),
1114    }
1115    .to_string()
1116}
1117
1118/// Extract a 2-letter country code from a case slug like `cases/id/corruption/2024/...`.
1119fn extract_country_from_case_slug(slug: &str) -> Option<String> {
1120    let parts: Vec<&str> = slug.split('/').collect();
1121    if parts.len() >= 2 {
1122        let candidate = parts[1];
1123        if candidate.len() == 2 && candidate.chars().all(|c| c.is_ascii_lowercase()) {
1124            return Some(candidate.to_string());
1125        }
1126    }
1127    None
1128}
1129
1130fn format_enum(s: &str) -> String {
1131    if let Some(custom) = s.strip_prefix("custom:") {
1132        return custom.to_string();
1133    }
1134    s.split('_')
1135        .map(|word| {
1136            let mut chars = word.chars();
1137            match chars.next() {
1138                None => String::new(),
1139                Some(c) => {
1140                    let upper: String = c.to_uppercase().collect();
1141                    upper + chars.as_str()
1142                }
1143            }
1144        })
1145        .collect::<Vec<_>>()
1146        .join(" ")
1147}
1148
1149fn render_dl_field(html: &mut String, label: &str, value: &str) {
1150    if !value.is_empty() {
1151        html.push_str(&format!(
1152            "          <span class=\"loom-field\"><strong>{label}:</strong> {}</span>\n",
1153            escape(value)
1154        ));
1155    }
1156}
1157
1158fn render_dl_opt(html: &mut String, label: &str, value: Option<&String>) {
1159    if let Some(v) = value {
1160        render_dl_field(html, label, v);
1161    }
1162}
1163
1164fn render_dl_opt_formatted(html: &mut String, label: &str, value: Option<&String>) {
1165    if let Some(v) = value {
1166        render_dl_field(html, label, &format_enum(v));
1167    }
1168}
1169
1170fn render_dl_item(html: &mut String, label: &str, value: &str) {
1171    if !value.is_empty() {
1172        html.push_str(&format!(
1173            "    <dt>{label}</dt>\n    <dd>{}</dd>\n",
1174            escape(value)
1175        ));
1176    }
1177}
1178
1179fn render_dl_opt_item(html: &mut String, label: &str, value: Option<&String>) {
1180    if let Some(v) = value {
1181        render_dl_item(html, label, v);
1182    }
1183}
1184
1185fn render_dl_opt_country(html: &mut String, label: &str, value: Option<&String>) {
1186    if let Some(v) = value {
1187        render_dl_field(html, label, &country_name(v));
1188    }
1189}
1190
1191fn render_dl_opt_country_item(html: &mut String, label: &str, value: Option<&String>) {
1192    if let Some(v) = value {
1193        render_dl_item(html, label, &country_name(v));
1194    }
1195}
1196
1197fn render_dl_opt_formatted_item(html: &mut String, label: &str, value: Option<&String>) {
1198    if let Some(v) = value {
1199        render_dl_item(html, label, &format_enum(v));
1200    }
1201}
1202
1203#[cfg(test)]
1204mod tests {
1205    use super::*;
1206    use crate::output::{CaseOutput, NodeOutput, RelOutput};
1207    use crate::parser::SourceEntry;
1208
1209    fn make_case() -> CaseOutput {
1210        CaseOutput {
1211            id: "01TESTCASE0000000000000000".into(),
1212            case_id: "test-case".into(),
1213            title: "Test Corruption Case".into(),
1214            summary: "A politician was caught accepting bribes.".into(),
1215            tags: vec!["bribery".into(), "government".into()],
1216            slug: None,
1217            case_type: None,
1218            amounts: vec![],
1219            status: None,
1220            tagline: None,
1221            nodes: vec![
1222                NodeOutput {
1223                    id: "01AAA".into(),
1224                    label: "person".into(),
1225                    name: "John Doe".into(),
1226                    slug: Some("people/id/john-doe--governor-of-test-province".into()),
1227                    qualifier: Some("Governor of Test Province".into()),
1228                    description: None,
1229                    thumbnail: Some("https://files.example.com/thumb.webp".into()),
1230                    aliases: vec![],
1231                    urls: vec![],
1232                    role: vec!["politician".into()],
1233                    nationality: Some("ID".into()),
1234                    date_of_birth: None,
1235                    place_of_birth: None,
1236                    status: Some("convicted".into()),
1237                    org_type: None,
1238                    jurisdiction: None,
1239                    headquarters: None,
1240                    founded_date: None,
1241                    registration_number: None,
1242                    event_type: None,
1243                    occurred_at: None,
1244                    severity: None,
1245                    doc_type: None,
1246                    issued_at: None,
1247                    issuing_authority: None,
1248                    case_number: None,
1249                    case_type: None,
1250                    tagline: None,
1251                    amounts: vec![],
1252                    asset_type: None,
1253                    value: None,
1254                    tags: vec![],
1255                },
1256                NodeOutput {
1257                    id: "01BBB".into(),
1258                    label: "organization".into(),
1259                    name: "KPK".into(),
1260                    slug: Some("organizations/id/kpk--anti-corruption-commission".into()),
1261                    qualifier: Some("Anti-Corruption Commission".into()),
1262                    description: None,
1263                    thumbnail: None,
1264                    aliases: vec![],
1265                    urls: vec![],
1266                    role: vec![],
1267                    nationality: None,
1268                    date_of_birth: None,
1269                    place_of_birth: None,
1270                    status: None,
1271                    org_type: Some("government_agency".into()),
1272                    jurisdiction: Some(Jurisdiction {
1273                        country: "ID".into(),
1274                        subdivision: None,
1275                    }),
1276                    headquarters: None,
1277                    founded_date: None,
1278                    registration_number: None,
1279                    event_type: None,
1280                    occurred_at: None,
1281                    severity: None,
1282                    doc_type: None,
1283                    issued_at: None,
1284                    issuing_authority: None,
1285                    case_number: None,
1286                    case_type: None,
1287                    tagline: None,
1288                    amounts: vec![],
1289                    asset_type: None,
1290                    value: None,
1291                    tags: vec![],
1292                },
1293                NodeOutput {
1294                    id: "01CCC".into(),
1295                    label: "event".into(),
1296                    name: "Arrest".into(),
1297                    slug: None,
1298                    qualifier: None,
1299                    description: Some("John Doe arrested by KPK.".into()),
1300                    thumbnail: None,
1301                    aliases: vec![],
1302                    urls: vec![],
1303                    role: vec![],
1304                    nationality: None,
1305                    date_of_birth: None,
1306                    place_of_birth: None,
1307                    status: None,
1308                    org_type: None,
1309                    jurisdiction: None,
1310                    headquarters: None,
1311                    founded_date: None,
1312                    registration_number: None,
1313                    event_type: Some("arrest".into()),
1314                    occurred_at: Some("2024-03-15".into()),
1315                    severity: None,
1316                    doc_type: None,
1317                    issued_at: None,
1318                    issuing_authority: None,
1319                    case_number: None,
1320                    case_type: None,
1321                    tagline: None,
1322                    amounts: vec![],
1323                    asset_type: None,
1324                    value: None,
1325                    tags: vec![],
1326                },
1327            ],
1328            relationships: vec![RelOutput {
1329                id: "01DDD".into(),
1330                rel_type: "investigated_by".into(),
1331                source_id: "01BBB".into(),
1332                target_id: "01CCC".into(),
1333                source_urls: vec![],
1334                description: None,
1335                amounts: vec![],
1336                valid_from: None,
1337                valid_until: None,
1338            }],
1339            sources: vec![SourceEntry::Url("https://example.com/article".into())],
1340        }
1341    }
1342
1343    #[test]
1344    fn render_case_produces_valid_html() {
1345        let case = make_case();
1346        let config = HtmlConfig::default();
1347        let html = render_case(&case, &config).unwrap();
1348
1349        assert!(html.starts_with("<article"));
1350        assert!(html.ends_with("</article>\n"));
1351        assert!(html.contains("data-og-title=\"Test Corruption Case\""));
1352        assert!(html.contains("data-og-description="));
1353        assert!(html.contains("<h1 itemprop=\"headline\">Test Corruption Case</h1>"));
1354        assert!(html.contains("loom-tag"));
1355        assert!(html.contains("bribery"));
1356        assert!(html.contains("John Doe"));
1357        assert!(html.contains("KPK"));
1358        assert!(html.contains("Arrest"));
1359        assert!(html.contains("2024-03-15"));
1360        assert!(html.contains("application/ld+json"));
1361        // Case header has wall link
1362        assert!(html.contains("View on the wall"));
1363        assert!(html.contains("/walls/01TESTCASE0000000000000000"));
1364    }
1365
1366    #[test]
1367    fn render_case_summary_renders_inline_markdown() {
1368        let mut case = make_case();
1369        case.summary = "**Amount not publicly documented.**".into();
1370        let config = HtmlConfig::default();
1371        let html = render_case(&case, &config).unwrap();
1372        assert!(html.contains(
1373            "<p class=\"loom-summary\" itemprop=\"description\"><strong>Amount not publicly documented.</strong></p>"
1374        ));
1375    }
1376
1377    #[test]
1378    fn render_case_has_sources() {
1379        let case = make_case();
1380        let config = HtmlConfig::default();
1381        let html = render_case(&case, &config).unwrap();
1382        assert!(html.contains("Sources"));
1383        assert!(html.contains("https://example.com/article"));
1384    }
1385
1386    #[test]
1387    fn render_case_entity_cards_link_to_static_views() {
1388        let case = make_case();
1389        let config = HtmlConfig::default();
1390        let html = render_case(&case, &config).unwrap();
1391
1392        // Entity cards should link to static views, not wall
1393        assert!(html.contains("href=\"/people/id/john-doe--governor-of-test-province\""));
1394        assert!(html.contains("href=\"/organizations/id/kpk--anti-corruption-commission\""));
1395        // Should NOT link to /walls/ for entities with slugs
1396        assert!(!html.contains("href=\"/walls/01AAA\""));
1397        assert!(!html.contains("href=\"/walls/01BBB\""));
1398    }
1399
1400    #[test]
1401    fn render_case_entity_cards_fallback_to_walls() {
1402        let mut case = make_case();
1403        let config = HtmlConfig::default();
1404        // Remove slugs from entities
1405        for node in &mut case.nodes {
1406            node.slug = None;
1407        }
1408        let html = render_case(&case, &config).unwrap();
1409
1410        // Without slugs, entity cards fall back to wall links
1411        assert!(html.contains("href=\"/walls/01AAA\""));
1412        assert!(html.contains("href=\"/walls/01BBB\""));
1413    }
1414
1415    #[test]
1416    fn render_case_omits_connections_table() {
1417        let case = make_case();
1418        let config = HtmlConfig::default();
1419        let html = render_case(&case, &config).unwrap();
1420        // Connections table is intentionally omitted — relationships are
1421        // already expressed in People/Organizations cards and Timeline
1422        assert!(!html.contains("Connections"));
1423        assert!(!html.contains("loom-rel-table"));
1424    }
1425
1426    #[test]
1427    fn render_person_page() {
1428        let case = make_case();
1429        let config = HtmlConfig::default();
1430        let person = &case.nodes[0];
1431        let cases_list = vec![("test-case".into(), "Test Corruption Case".into())];
1432        let html = render_person(person, &cases_list, &config).unwrap();
1433
1434        assert!(html.contains("itemtype=\"https://schema.org/Person\""));
1435        assert!(html.contains("John Doe"));
1436        assert!(html.contains("Governor of Test Province"));
1437        assert!(html.contains("/walls/01AAA"));
1438        assert!(html.contains("Test Corruption Case"));
1439        assert!(html.contains("application/ld+json"));
1440    }
1441
1442    #[test]
1443    fn render_organization_page() {
1444        let case = make_case();
1445        let config = HtmlConfig::default();
1446        let org = &case.nodes[1];
1447        let cases_list = vec![("test-case".into(), "Test Corruption Case".into())];
1448        let html = render_organization(org, &cases_list, &config).unwrap();
1449
1450        assert!(html.contains("itemtype=\"https://schema.org/Organization\""));
1451        assert!(html.contains("KPK"));
1452        assert!(html.contains("Indonesia")); // jurisdiction (resolved from ID)
1453    }
1454
1455    #[test]
1456    fn render_sitemap_includes_all_urls() {
1457        let cases = vec![("cases/id/corruption/2024/test-case".into(), "Case 1".into())];
1458        let people = vec![("people/id/john-doe".into(), "John".into())];
1459        let orgs = vec![("organizations/id/test-corp".into(), "Corp".into())];
1460        let xml = render_sitemap(&cases, &people, &orgs, "https://redberrythread.org");
1461
1462        assert!(xml.contains("<?xml"));
1463        assert!(xml.contains("/cases/id/corruption/2024/test-case"));
1464        assert!(xml.contains("/people/id/john-doe"));
1465        assert!(xml.contains("/organizations/id/test-corp"));
1466    }
1467
1468    #[test]
1469    fn escape_html_special_chars() {
1470        assert_eq!(escape("<script>"), "&lt;script&gt;");
1471        assert_eq!(escape("AT&T"), "AT&amp;T");
1472        assert_eq!(escape("\"quoted\""), "&quot;quoted&quot;");
1473    }
1474
1475    #[test]
1476    fn render_inline_markdown_bold_and_italic() {
1477        assert_eq!(
1478            render_inline_markdown("**Amount not publicly documented.**"),
1479            "<strong>Amount not publicly documented.</strong>"
1480        );
1481        assert_eq!(
1482            render_inline_markdown("Some *italic* text"),
1483            "Some <em>italic</em> text"
1484        );
1485    }
1486
1487    #[test]
1488    fn render_inline_markdown_escapes_plain_text() {
1489        // pulldown-cmark decodes HTML entities, then we re-escape
1490        assert_eq!(render_inline_markdown("A & B"), "A &amp; B");
1491        assert_eq!(
1492            render_inline_markdown("use `x < y`"),
1493            "use <code>x &lt; y</code>"
1494        );
1495    }
1496
1497    #[test]
1498    fn render_inline_markdown_links() {
1499        assert_eq!(
1500            render_inline_markdown("[KPK](https://kpk.go.id) arrested him"),
1501            "<a href=\"https://kpk.go.id\" rel=\"noopener noreferrer\">KPK</a> arrested him"
1502        );
1503    }
1504
1505    #[test]
1506    fn truncate_short_string() {
1507        assert_eq!(truncate("hello", 10), "hello");
1508    }
1509
1510    #[test]
1511    fn truncate_long_string() {
1512        let long = "a".repeat(200);
1513        let result = truncate(&long, 120);
1514        assert!(result.len() <= 120);
1515        assert!(result.ends_with("..."));
1516    }
1517
1518    #[test]
1519    fn format_enum_underscore() {
1520        assert_eq!(format_enum("investigated_by"), "Investigated By");
1521        assert_eq!(format_enum("custom:Special Type"), "Special Type");
1522    }
1523
1524    #[test]
1525    fn thumbnail_key_deterministic() {
1526        let k1 = thumbnail_key("https://example.com/photo.jpg");
1527        let k2 = thumbnail_key("https://example.com/photo.jpg");
1528        assert_eq!(k1, k2);
1529        assert!(k1.starts_with("thumbnails/"));
1530        assert!(k1.ends_with(".webp"));
1531        // Key hex part is 32 chars
1532        let hex_part = k1
1533            .strip_prefix("thumbnails/")
1534            .and_then(|s| s.strip_suffix(".webp"))
1535            .unwrap_or("");
1536        assert_eq!(hex_part.len(), THUMB_KEY_HEX_LEN);
1537    }
1538
1539    #[test]
1540    fn thumbnail_key_different_urls_differ() {
1541        let k1 = thumbnail_key("https://example.com/a.jpg");
1542        let k2 = thumbnail_key("https://example.com/b.jpg");
1543        assert_ne!(k1, k2);
1544    }
1545
1546    #[test]
1547    fn rewrite_thumbnail_url_no_config() {
1548        let config = HtmlConfig::default();
1549        let result = rewrite_thumbnail_url("https://example.com/photo.jpg", &config);
1550        assert_eq!(result, "https://example.com/photo.jpg");
1551    }
1552
1553    #[test]
1554    fn rewrite_thumbnail_url_with_base() {
1555        let config = HtmlConfig {
1556            thumbnail_base_url: Some("http://files.garage.local:3902/files".into()),
1557        };
1558        let result = rewrite_thumbnail_url("https://example.com/photo.jpg", &config);
1559        assert!(result.starts_with("http://files.garage.local:3902/files/thumbnails/"));
1560        assert!(result.ends_with(".webp"));
1561        assert!(!result.contains("example.com"));
1562    }
1563
1564    #[test]
1565    fn rewrite_thumbnail_url_already_rewritten() {
1566        let config = HtmlConfig {
1567            thumbnail_base_url: Some("https://files.redberrythread.org".into()),
1568        };
1569        let already =
1570            "https://files.redberrythread.org/thumbnails/6fc3a49567393053be6138aa346fa97a.webp";
1571        let result = rewrite_thumbnail_url(already, &config);
1572        assert_eq!(
1573            result, already,
1574            "should not double-hash already-rewritten URLs"
1575        );
1576    }
1577
1578    #[test]
1579    fn render_case_rewrites_thumbnails() {
1580        let case = make_case();
1581        let config = HtmlConfig {
1582            thumbnail_base_url: Some("http://garage.local/files".into()),
1583        };
1584        let html = render_case(&case, &config).unwrap();
1585
1586        // Original URL should not appear in img src
1587        assert!(!html.contains("src=\"https://files.example.com/thumb.webp\""));
1588        // Rewritten URL should appear
1589        assert!(html.contains("src=\"http://garage.local/files/thumbnails/"));
1590        // OG image should also be rewritten
1591        assert!(html.contains("data-og-image=\"http://garage.local/files/thumbnails/"));
1592    }
1593
1594    #[test]
1595    fn render_person_rewrites_thumbnails() {
1596        let case = make_case();
1597        let person = &case.nodes[0];
1598        let config = HtmlConfig {
1599            thumbnail_base_url: Some("http://garage.local/files".into()),
1600        };
1601        let html = render_person(person, &[], &config).unwrap();
1602
1603        assert!(!html.contains("src=\"https://files.example.com/thumb.webp\""));
1604        assert!(html.contains("src=\"http://garage.local/files/thumbnails/"));
1605    }
1606
1607    #[test]
1608    fn render_case_with_related_cases() {
1609        let mut case = make_case();
1610        // Add a related_to relationship and target case node
1611        case.relationships.push(RelOutput {
1612            id: "01RELID".into(),
1613            rel_type: "related_to".into(),
1614            source_id: "01TESTCASE0000000000000000".into(),
1615            target_id: "01TARGETCASE000000000000000".into(),
1616            source_urls: vec![],
1617            description: Some("Connected bribery scandal".into()),
1618            amounts: vec![],
1619            valid_from: None,
1620            valid_until: None,
1621        });
1622        case.nodes.push(NodeOutput {
1623            id: "01TARGETCASE000000000000000".into(),
1624            label: "case".into(),
1625            name: "Target Scandal Case".into(),
1626            slug: Some("cases/id/corruption/2002/target-scandal".into()),
1627            qualifier: None,
1628            description: None,
1629            thumbnail: None,
1630            aliases: vec![],
1631            urls: vec![],
1632            role: vec![],
1633            nationality: None,
1634            date_of_birth: None,
1635            place_of_birth: None,
1636            status: None,
1637            org_type: None,
1638            jurisdiction: None,
1639            headquarters: None,
1640            founded_date: None,
1641            registration_number: None,
1642            event_type: None,
1643            occurred_at: None,
1644            severity: None,
1645            doc_type: None,
1646            issued_at: None,
1647            issuing_authority: None,
1648            case_number: None,
1649            case_type: None,
1650            amounts: vec![],
1651            asset_type: None,
1652            value: None,
1653            tags: vec![],
1654        });
1655
1656        let config = HtmlConfig::default();
1657        let html = render_case(&case, &config).unwrap();
1658
1659        assert!(html.contains("loom-related-cases"));
1660        assert!(html.contains("Related Cases"));
1661        assert!(html.contains("Target Scandal Case"));
1662        assert!(html.contains("loom-related-card"));
1663        assert!(html.contains("Connected bribery scandal"));
1664    }
1665
1666    #[test]
1667    fn render_case_without_related_cases() {
1668        let case = make_case();
1669        let config = HtmlConfig::default();
1670        let html = render_case(&case, &config).unwrap();
1671
1672        assert!(!html.contains("loom-related-cases"));
1673    }
1674}