ariel-rs 0.2.0

A faithful Rust port of Mermaid JS — headless SVG diagram rendering without a browser
Documentation
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
use super::constants::*;
use super::parser::{KanbanDiagram, KanbanSection, NodeShape};
use super::templates::{self, assignee_label, build_css, esc, priority_line, ticket_link};
/// Faithful Rust port of Mermaid's kanbanRenderer.ts.
///
/// Layout algorithm (column-based, NOT dagre):
///
/// The JS renderer (kanbanRenderer.ts) uses a custom column layout:
///   - Each section (column) gets a fixed width (sectionWidth, default 200).
///   - Columns are placed side by side with a 5 px gap.
///   - Items within a column are stacked vertically with fixed heights.
///   - The column header height = 25 px (LABEL_HEIGHT_DEFAULT).
///   - Each item height = 44 px, gap between items = 5 px.
///   - Column rect height = 79 + (n_items - 1) * 49 (for n_items >= 1), or 50 for 0 items.
///   - viewBox: "90 -310 W H" where W = 15 + n_cols*205, H = max_col_h + 20.
///
/// This port faithfully replicates the Mermaid coordinate system and CSS class scheme.
use crate::text::measure;
use crate::theme::Theme;

// ── Layout math ────────────────────────────────────────────────────────────────

/// Estimate card height for a given item, accounting for text wrapping and metadata row.
fn item_height_full(item: &crate::diagrams::kanban::parser::KanbanItem) -> f64 {
    let base = item_height(&item.label);
    // Add a metadata row if ticket or assigned is present
    if item.ticket.is_some() || item.assigned.is_some() {
        base + 12.0
    } else {
        base
    }
}

/// Estimate card height for a given label, accounting for text wrapping.
/// Mirrors Mermaid's getBBox()-based approach as a static approximation.
fn item_height(label: &str) -> f64 {
    // Mermaid renders kanban labels at 16px — use 16px for wrapping estimation
    // so items wrap at the same point the browser would wrap them.
    let (text_w, _) = measure(label, FONT_SIZE);
    let lines = ((text_w * TEXT_SCALE) / AVAILABLE_WIDTH).ceil().max(1.0);
    (lines * LINE_HEIGHT + V_PADDING).max(ITEM_HEIGHT)
}

/// Map a priority string to its stroke color.
fn priority_color(priority: &str) -> Option<&'static str> {
    match priority.to_lowercase().as_str() {
        "very high" => Some("red"),
        "high" => Some("orange"),
        "low" => Some("blue"),
        "very low" => Some("lightblue"),
        _ => None,
    }
}

/// foreignObject height for a given label (full card, no metadata).
fn fo_height(label: &str) -> f64 {
    item_height(label) - 4.0
}

/// Text-only height — just the wrapped lines, no padding.
/// Used when a metadata row is present so we can position label + metadata cleanly.
#[allow(dead_code)]
fn text_height(label: &str) -> f64 {
    let (text_w, _) = measure(label, FONT_SIZE);
    let lines = ((text_w * TEXT_SCALE) / AVAILABLE_WIDTH).ceil().max(1.0);
    lines * LINE_HEIGHT
}

/// Column left-edge x position (0-based index).
fn col_left_x(idx: usize) -> f64 {
    COL_LEFT_BASE + idx as f64 * (SECTION_WIDTH + SECTION_GAP)
}

/// Column center x position (0-based index).
fn col_center_x(idx: usize) -> f64 {
    col_left_x(idx) + SECTION_WIDTH / 2.0
}

/// Column height accounting for variable item heights (text wrapping + metadata).
fn col_height_dynamic(items: &[crate::diagrams::kanban::parser::KanbanItem]) -> f64 {
    if items.is_empty() {
        return 50.0;
    }
    let total: f64 = items
        .iter()
        .map(|it| item_height_full(it) + ITEM_GAP)
        .sum::<f64>()
        - ITEM_GAP;
    LABEL_HEIGHT + total + 10.0
}

/// Cumulative Y centers for each item in a section, accounting for variable heights.
fn item_center_ys(items: &[crate::diagrams::kanban::parser::KanbanItem]) -> Vec<f64> {
    let mut ys = Vec::with_capacity(items.len());
    let mut y = COL_TOP + LABEL_HEIGHT;
    for item in items {
        let h = item_height_full(item);
        ys.push(y + h / 2.0);
        y += h + ITEM_GAP;
    }
    ys
}

// ── HSL section color palette (mirrors Mermaid's kanban CSS generation) ────────
//
// Mermaid generates per-section CSS rules with HSL colors.
// Hue sequence for section-1 onward: 80, 270, 300, 330, 0, 30, 90, 150, 180, 210.
// Lightness 86.27% (= 88/102 * 100% approximately, exact = 86.2745098039%).
// Section-1 text: black; section-2 text: #ffffff; section-3+ text: black (varies).
//
// We embed the same CSS classes as Mermaid so the PNG renderer applies them.

// ── Section rendering ──────────────────────────────────────────────────────────

/// Render a single column section.
/// Returns: (section_svg, items_svg) — sections go in <g class="sections">,
/// items go in <g class="items">.
fn render_section_and_items(
    section: &KanbanSection,
    sec_idx: usize, // 1-based
    col_idx: usize, // 0-based
    svg_id: &str,
    ticket_base_url: Option<&str>,
) -> (String, String) {
    let cx = col_center_x(col_idx);
    let lx = col_left_x(col_idx);
    let col_top = COL_TOP;
    let col_h = col_height_dynamic(&section.items);
    let item_centers = item_center_ys(&section.items);

    // ── Section column rect ──────────────────────────────────────────────────
    let mut sec_svg = templates::section_group_open(sec_idx, svg_id, &esc(&section.id));

    sec_svg.push_str(&templates::section_rect(lx, col_top, SECTION_WIDTH, col_h));

    // Column header label — full-width foreignObject centered in the column.
    sec_svg.push_str(&templates::section_label_fo(
        lx + 20.0,
        col_top,
        &esc(&section.label),
    ));

    sec_svg.push_str("</g>");

    // ── Items ────────────────────────────────────────────────────────────────
    let mut items_svg = String::new();
    let item_w_half = ITEM_WIDTH / 2.0;

    for (item_idx, item) in section.items.iter().enumerate() {
        let icy = item_centers[item_idx];
        let dyn_h = item_height_full(item);
        let item_h_half = dyn_h / 2.0;
        let has_meta = item.ticket.is_some() || item.assigned.is_some();

        items_svg.push_str(&templates::item_group_open(svg_id, &esc(&item.id), cx, icy));

        // Card rect (shape determines style)
        let (rx_val, _shape_extra) = match item.shape {
            NodeShape::Circle => {
                let r = item_h_half.min(item_w_half);
                items_svg.push_str(&templates::item_circle(r));
                items_svg.push_str(&templates::item_label_fo_fixed(
                    -item_w_half + 10.0,
                    -item_h_half + 4.0,
                    ITEM_WIDTH - 10.0,
                    ITEM_WIDTH - 10.0,
                    fo_height(&item.label),
                    &esc(&item.label),
                ));
                items_svg.push_str("</g>");
                continue;
            }
            NodeShape::RoundedRect => (item_h_half / 2.0, None::<String>),
            NodeShape::Hexagon => {
                let dx = item_w_half / 2.0;
                let pts = format!(
                    "{:.2},{:.2} {:.2},{:.2} {:.2},{:.2} {:.2},{:.2} {:.2},{:.2} {:.2},{:.2}",
                    -item_w_half,
                    0.0,
                    -item_w_half + dx,
                    -item_h_half,
                    item_w_half - dx,
                    -item_h_half,
                    item_w_half,
                    0.0,
                    item_w_half - dx,
                    item_h_half,
                    -item_w_half + dx,
                    item_h_half,
                );
                items_svg.push_str(&templates::item_hexagon(&pts));
                items_svg.push_str(&templates::item_label_fo_fixed(
                    -item_w_half + 10.0,
                    -item_h_half / 2.0,
                    ITEM_WIDTH - 10.0,
                    ITEM_WIDTH - 10.0,
                    fo_height(&item.label),
                    &esc(&item.label),
                ));
                items_svg.push_str("</g>");
                continue;
            }
            NodeShape::Cloud => (item_h_half, None),
            NodeShape::Bang => (4.0, None),
            NodeShape::Default => {
                items_svg.push_str(&templates::item_default_rect(
                    -item_w_half,
                    -item_h_half,
                    ITEM_WIDTH,
                    dyn_h,
                ));
                items_svg.push_str(&templates::item_label_fo_fixed(
                    -item_w_half + 10.0,
                    -item_h_half / 2.0 - 6.0,
                    ITEM_WIDTH - 10.0,
                    ITEM_WIDTH - 10.0,
                    fo_height(&item.label),
                    &esc(&item.label),
                ));
                items_svg.push_str("</g>");
                continue;
            }
            NodeShape::Rect => (5.0, None),
        };

        // Default rect-based rendering
        items_svg.push_str(&templates::item_rect(
            rx_val,
            -item_w_half,
            -item_h_half,
            ITEM_WIDTH,
            dyn_h,
        ));

        // Primary label — position depends on whether metadata row is present.
        let (label_ty, label_fo_h) = if has_meta {
            // Label occupies top portion, metadata row follows immediately after.
            let ty = -(item_h_half - 4.0);
            let fh = text_height(&item.label);
            (ty, fh)
        } else {
            (-item_h_half + 10.0, fo_height(&item.label))
        };
        items_svg.push_str(&templates::item_label_fo(
            -item_w_half + 10.0,
            label_ty,
            ITEM_WIDTH - 10.0,
            ITEM_WIDTH - 10.0,
            label_fo_h,
            &esc(&item.label),
        ));

        // Ticket + assignee metadata row — positioned immediately below the label.
        if has_meta {
            let meta_y = label_ty + label_fo_h;
            // Ticket number as clickable link (left side)
            if let Some(ref ticket) = item.ticket {
                let ticket_url = ticket_base_url
                    .map(|u| u.replace("#TICKET#", ticket))
                    .unwrap_or_default();
                if ticket_url.is_empty() {
                    items_svg.push_str(&templates::item_label_fo_fixed(
                        -item_w_half + 10.0,
                        meta_y,
                        60.0,
                        60.0,
                        24.0,
                        &esc(ticket),
                    ));
                } else {
                    items_svg.push_str(&ticket_link(
                        &ticket_url,
                        -item_w_half + 10.0,
                        meta_y,
                        &esc(ticket),
                    ));
                }
            }
            // Assignee (right side) — nowrap so names don't get cut off.
            if let Some(ref assigned) = item.assigned {
                items_svg.push_str(&assignee_label(
                    -5.0,
                    meta_y,
                    item_w_half + 5.0,
                    &esc(assigned),
                ));
            }
        } else {
            // Empty secondary label slots — mirrors Mermaid structure
            items_svg.push_str(&templates::item_label_empty(
                -item_w_half + 10.0,
                item_h_half - 10.0,
                ITEM_WIDTH - 10.0,
            ));
            items_svg.push_str(&templates::item_label_empty(
                item_w_half - 10.0,
                item_h_half - 10.0,
                ITEM_WIDTH - 10.0,
            ));
        }

        // Priority indicator — colored vertical line on left edge of card.
        if let Some(ref p) = item.priority {
            if let Some(color) = priority_color(p) {
                items_svg.push_str(&priority_line(
                    -item_w_half + 2.0,
                    -item_h_half + 2.0,
                    item_h_half - 2.0,
                    color,
                ));
            }
        }

        items_svg.push_str("</g>");
    }

    (sec_svg, items_svg)
}

/// Rough text width estimation (for foreignObject sizing only).
/// Uses 0.6 * font_size * char_count as approximation.
#[allow(dead_code)]
fn estimate_text_width(text: &str, font_size: f64) -> f64 {
    text.len() as f64 * font_size * 0.6
}

// ── Main render ────────────────────────────────────────────────────────────────

pub fn render(diag: &KanbanDiagram, theme: Theme) -> String {
    let vars = theme.resolve();
    let ff = vars.font_family;
    if diag.sections.is_empty() {
        return templates::empty_svg().to_string();
    }

    let svg_id = "mermaid-svg-65"; // match Mermaid's deterministic ID pattern

    let n_cols = diag.sections.len();

    // Compute max column height
    let max_col_h = diag
        .sections
        .iter()
        .map(|s| col_height_dynamic(&s.items))
        .fold(0.0_f64, f64::max);

    // viewBox dimensions
    let vb_w = 15.0 + n_cols as f64 * (SECTION_WIDTH + SECTION_GAP);
    let vb_h = max_col_h + MARGIN * 2.0;

    let css = build_css(svg_id, ff);

    let mut out = String::new();

    out.push_str(&templates::svg_root(
        svg_id,
        vb_w,
        VIEWBOX_X as i64,
        VIEWBOX_Y as i64,
        vb_w as u64,
        vb_h as u64,
    ));

    out.push_str("<style>");
    out.push_str(&css);
    out.push_str("</style>");

    // Empty g (matches Mermaid structure)
    out.push_str("<g></g>");

    // Sections group
    let mut sections_svg = String::new();
    let mut items_svg_parts: Vec<String> = Vec::new();

    for (i, section) in diag.sections.iter().enumerate() {
        let sec_idx = i + 1; // 1-based
        let (sec_svg, items_svg) = render_section_and_items(
            section,
            sec_idx,
            i,
            svg_id,
            diag.config.ticket_base_url.as_deref(),
        );
        sections_svg.push_str(&sec_svg);
        items_svg_parts.push(items_svg);
    }

    out.push_str(r#"<g class="sections">"#);
    out.push_str(&sections_svg);
    out.push_str("</g>");

    out.push_str(r#"<g class="items">"#);
    for items_svg in &items_svg_parts {
        out.push_str(items_svg);
    }
    out.push_str("</g>");

    out.push_str("</svg>");

    out
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::diagrams::kanban::parser;

    #[test]
    fn basic_render_produces_svg() {
        let input = "kanban\n  todo\n    id1[Task 1]\n    id2[Task 2]\n  inProgress\n    id3[Task 3]\n  done\n    id4[Task 4]";
        let diag = parser::parse(input).diagram;
        let svg = render(&diag, Theme::Default);
        assert!(svg.contains("<svg"), "missing <svg");
        assert!(svg.contains("Task 1"));
        assert!(svg.contains("Task 2"));
        assert!(svg.contains("Task 3"));
        assert!(svg.contains("Task 4"));
        assert!(svg.contains("todo"));
        assert!(svg.contains("done"));
    }

    #[test]
    fn empty_kanban_produces_svg() {
        let diag = KanbanDiagram {
            sections: vec![],
            config: crate::diagrams::kanban::parser::KanbanConfig {
                ticket_base_url: None,
            },
        };
        let svg = render(&diag, Theme::Default);
        assert!(svg.contains("<svg"));
    }

    #[test]
    fn section_with_label_renders() {
        let input = "kanban\n  col1[\"To Do\"]\n    item1[\"My Task\"]\n";
        let diag = parser::parse(input).diagram;
        let svg = render(&diag, Theme::Default);
        assert!(svg.contains("To Do"));
        assert!(svg.contains("My Task"));
    }

    #[test]
    fn multiple_columns_have_different_x() {
        let input = "kanban\n  col1\n    a[A]\n  col2\n    b[B]\n";
        let diag = parser::parse(input).diagram;
        let svg = render(&diag, Theme::Default);
        assert!(svg.contains("col1"));
        assert!(svg.contains("col2"));
    }

    #[test]
    fn viewbox_matches_mermaid() {
        // 3 columns: vb_w = 15 + 3*205 = 630
        let input = "kanban\n  Todo\n    id1[Write blog post]\n    id2[Plan vacation]\n  In Progress\n    id3[Write code]\n  Done\n    id4[Create diagrams]";
        let diag = parser::parse(input).diagram;
        let svg = render(&diag, Theme::Default);
        assert!(
            svg.contains(r#"viewBox="90 -310 630 "#),
            "viewBox width mismatch: {}",
            &svg[..200]
        );
    }

    #[test]
    fn snapshot_default_theme() {
        let input = "kanban\n  Todo\n    id1[Write blog post]\n    id2[Plan vacation]\n  In Progress\n    id3[Write code]\n  Done\n    id4[Create diagrams]";
        let diag = parser::parse(input).diagram;
        let svg = render(&diag, crate::theme::Theme::Default);
        insta::assert_snapshot!(crate::svg::normalize_floats(&svg));
    }
}