snapper-fmt 0.10.0

Semantic line break formatter for Org, LaTeX, Markdown, RST, and plaintext
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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
//! Apply snapper to a document **after** pandoc has parsed it.
//!
//! Pipeline for the pandoc path:
//! 1. Pandoc reads the source (CLI `pandoc -t json` or in-process FFI) → AST.
//! 2. This module walks **pandoc block/inline node kinds** and marks which
//!    pieces snapper may reflow vs must leave alone.
//! 3. Snapper’s reflow runs only on prose regions.
//!
//! Math and code are never reflowed as prose:
//! - `CodeBlock` → [`Region::Code`]
//! - `Para`/`Plain` that are **only** display/inline math → structure
//! - Mixed paragraphs: split into prose runs and structure islands for
//!   `Math` / inline `Code` so periods inside math cannot end a sentence.
//!
//! Structure truth is the node kind, not source regex after a successful parse.

use pandoc_ast::{Block, Cell, Inline, MathType, Pandoc, Row, TableBody, TableFoot, TableHead};

use crate::parser::Region;

/// Map a pandoc document to snapper regions for reflow.
///
/// | Pandoc node | Snapper treatment |
/// |-------------|-------------------|
/// | `Para` / `Plain` of ordinary text | [`Region::Prose`] (reflow) |
/// | `Para` / `Plain` with `Math` / inline `Code` | split: prose runs + structure islands |
/// | display-math-only `Para` | [`Region::Structure`] |
/// | `Header` | [`Region::Structure`] |
/// | `CodeBlock` | [`Region::Code`] (fenced from Attr) |
/// | `Table` | [`Region::Structure`] (pipe table from cells) |
/// | `BulletList` / `OrderedList` | marker structure + item blocks |
/// | `BlockQuote` | `>` markers + nested blocks |
pub fn regions_from_pandoc(doc: &Pandoc) -> Vec<Region> {
    let mut regions = Vec::new();
    for (i, block) in doc.blocks.iter().enumerate() {
        if i > 0 {
            push_block_separator(&mut regions);
        }
        extract_block(block, &mut regions);
    }
    regions
}

/// Blank line between top-level blocks (pandoc does not emit blank nodes).
fn push_block_separator(regions: &mut Vec<Region>) {
    if regions.is_empty() {
        return;
    }
    if matches!(regions.last(), Some(Region::BlankLines(_))) {
        return;
    }
    regions.push(Region::BlankLines("\n".to_string()));
}

/// Deserialize pandoc JSON AST, then [`regions_from_pandoc`].
pub fn regions_from_pandoc_json(json: &str) -> Result<Vec<Region>, String> {
    let doc: Pandoc = serde_json::from_str(json)
        .map_err(|e| format!("failed to deserialize pandoc AST JSON: {e}"))?;
    Ok(regions_from_pandoc(&doc))
}

fn extract_block(block: &Block, regions: &mut Vec<Region>) {
    match block {
        Block::Para(inlines) | Block::Plain(inlines) => {
            extract_para_inlines(inlines, regions);
        }
        Block::Header(_level, _attr, inlines) => {
            // Header is non-prose because pandoc classified it as Header — not
            // because we rebuilt an ATX source line. Title text (incl. "1. …")
            // must not enter Prose or snapper would sentence-split it.
            let text = extract_inlines(inlines);
            let title = text.trim();
            if !title.is_empty() {
                regions.push(Region::Structure(format!("{title}\n")));
            }
        }
        Block::CodeBlock(attr, code) => {
            // Pandoc's CodeBlock is authoritative (fenced, minted, lstlisting, …).
            // Emit a coherent Region::Code: lang from Attr; minimal fence framing
            // for reflow output (not source-glyph fidelity, not a native re-parse).
            let lang = code_lang_from_attr(attr);
            let body = if code.ends_with('\n') {
                code.clone()
            } else if code.is_empty() {
                String::new()
            } else {
                format!("{code}\n")
            };
            let (header, footer) = code_fence_frame(lang.as_deref());
            regions.push(Region::Code {
                lang,
                header,
                body,
                footer,
            });
        }
        Block::RawBlock(_, raw) => {
            let s = if raw.ends_with('\n') {
                raw.clone()
            } else {
                format!("{raw}\n")
            };
            regions.push(Region::Structure(s));
        }
        Block::BlockQuote(blocks) => {
            for (i, b) in blocks.iter().enumerate() {
                if i > 0 {
                    push_block_separator(regions);
                }
                extract_quoted_block(b, regions);
            }
        }
        Block::BulletList(items) => {
            for (i, item) in items.iter().enumerate() {
                if i > 0 {
                    // single newline between items (not a full blank)
                    ensure_trailing_newline(regions);
                }
                extract_list_item("- ", item, regions);
            }
        }
        Block::OrderedList(_, items) => {
            for (i, item) in items.iter().enumerate() {
                if i > 0 {
                    ensure_trailing_newline(regions);
                }
                let marker = format!("{}. ", i + 1);
                extract_list_item(&marker, item, regions);
            }
        }
        Block::DefinitionList(defs) => {
            for (term, definitions) in defs {
                let term_text = extract_inlines(term);
                if !term_text.trim().is_empty() {
                    regions.push(Region::Structure(format!("{}\n", term_text.trim())));
                }
                for def in definitions {
                    for b in def {
                        extract_block(b, regions);
                    }
                }
            }
        }
        Block::Table(_attr, _caption, _cols, head, bodies, foot) => {
            let rendered = render_table_structure(head, bodies, foot);
            if !rendered.is_empty() {
                regions.push(Region::Structure(rendered));
            }
        }
        Block::HorizontalRule => {
            regions.push(Region::Structure("---\n".to_string()));
        }
        Block::Div(_, blocks) => {
            for b in blocks {
                extract_block(b, regions);
            }
        }
        Block::Figure(_, _, blocks) => {
            for b in blocks {
                extract_block(b, regions);
            }
        }
        Block::Null => {}
        Block::LineBlock(lines) => {
            for line in lines {
                let text = extract_inlines(line);
                regions.push(Region::Structure(format!("{text}\n")));
            }
        }
    }
}

/// Pandoc `Attr` is `(id, classes, keyvals)`; first class is usually the language.
fn code_lang_from_attr(attr: &pandoc_ast::Attr) -> Option<String> {
    let (_id, classes, _kvs) = attr;
    // Prefer first non-empty class; pandoc also stores language= on keyvals for lstlisting.
    if let Some(c) = classes.first().cloned().filter(|c| !c.is_empty()) {
        return Some(c);
    }
    for (k, v) in _kvs {
        if k.eq_ignore_ascii_case("language") && !v.is_empty() {
            return Some(v.clone());
        }
    }
    None
}

/// Minimal fence framing so reflow emits a coherent code unit (header/body/footer).
fn code_fence_frame(lang: Option<&str>) -> (String, String) {
    let header = match lang {
        Some(l) if !l.is_empty() => format!("```{l}\n"),
        _ => "```\n".to_string(),
    };
    (header, "```\n".to_string())
}

fn ensure_trailing_newline(regions: &mut Vec<Region>) {
    match regions.last() {
        Some(Region::Structure(s) | Region::Prose(s) | Region::BlankLines(s))
            if s.ends_with('\n') => {}
        Some(Region::Code { footer, .. }) if footer.ends_with('\n') => {}
        Some(_) => regions.push(Region::Structure("\n".to_string())),
        None => {}
    }
}

fn extract_quoted_block(block: &Block, regions: &mut Vec<Region>) {
    match block {
        Block::Para(inlines) | Block::Plain(inlines) => {
            regions.push(Region::Structure("> ".to_string()));
            extract_para_inlines(inlines, regions);
            ensure_trailing_newline(regions);
        }
        other => {
            regions.push(Region::Structure("> ".to_string()));
            extract_block(other, regions);
        }
    }
}

fn extract_list_item(marker: &str, item: &[Block], regions: &mut Vec<Region>) {
    regions.push(Region::Structure(marker.to_string()));
    for (i, block) in item.iter().enumerate() {
        if i == 0 {
            match block {
                Block::Para(inlines) | Block::Plain(inlines) => {
                    extract_para_inlines(inlines, regions);
                    ensure_trailing_newline(regions);
                }
                other => extract_block(other, regions),
            }
        } else {
            push_block_separator(regions);
            extract_block(block, regions);
        }
    }
}

/// Pipe-table structure from pandoc table AST (cells only; non-prose).
fn render_table_structure(head: &TableHead, bodies: &[TableBody], foot: &TableFoot) -> String {
    let mut rows: Vec<Vec<String>> = Vec::new();
    let (_hattr, head_rows) = head;
    for row in head_rows {
        rows.push(row_cells(row));
    }
    let head_count = head_rows.len();
    for body in bodies {
        let (_battr, _rhc, intermediate, body_rows) = body;
        for row in intermediate {
            rows.push(row_cells(row));
        }
        for row in body_rows {
            rows.push(row_cells(row));
        }
    }
    let (_fattr, foot_rows) = foot;
    for row in foot_rows {
        rows.push(row_cells(row));
    }
    if rows.is_empty() {
        return String::new();
    }
    let ncols = rows.iter().map(|r| r.len()).max().unwrap_or(0);
    if ncols == 0 {
        return String::new();
    }
    for r in &mut rows {
        while r.len() < ncols {
            r.push(String::new());
        }
    }
    // Separator after header rows (or after first row if no header).
    let sep_after = if head_count > 0 { head_count - 1 } else { 0 };
    let mut out = String::new();
    for (i, row) in rows.iter().enumerate() {
        out.push_str("| ");
        out.push_str(
            &row.iter()
                .map(|c| c.replace('|', "\\|"))
                .collect::<Vec<_>>()
                .join(" | "),
        );
        out.push_str(" |\n");
        if i == sep_after {
            out.push('|');
            for _ in 0..ncols {
                out.push_str(" --- |");
            }
            out.push('\n');
        }
    }
    out
}

fn row_cells(row: &Row) -> Vec<String> {
    let (_attr, cells) = row;
    cells.iter().map(cell_text).collect()
}

fn cell_text(cell: &Cell) -> String {
    let (_attr, _align, _rs, _cs, blocks) = cell;
    let mut parts = Vec::new();
    for b in blocks {
        match b {
            Block::Plain(inlines) | Block::Para(inlines) => {
                parts.push(extract_inlines(inlines));
            }
            Block::CodeBlock(_, code) => parts.push(code.clone()),
            Block::RawBlock(_, raw) => parts.push(raw.clone()),
            _ => {}
        }
    }
    parts.join(" ").trim().to_string()
}

/// True when the para is only math (and ignorable space/breaks) — display or
/// bare math blocks that must not go through the sentence reflower.
fn is_math_only_para(inlines: &[Inline]) -> bool {
    let mut saw_math = false;
    for inline in inlines {
        match inline {
            Inline::Math(..) => saw_math = true,
            Inline::Space | Inline::SoftBreak | Inline::LineBreak => {}
            _ => return false,
        }
    }
    saw_math
}

fn format_math(ty: &MathType, body: &str) -> String {
    match ty {
        MathType::DisplayMath => {
            // Keep payload as structure; delimiters are not source-faithful, only non-prose.
            let b = body.trim();
            if b.is_empty() {
                "$$\n".to_string()
            } else if b.contains('\n') {
                format!("$$\n{b}\n$$\n")
            } else {
                format!("$${b}$$\n")
            }
        }
        MathType::InlineMath => format!("${body}$"),
    }
}

fn flush_prose_buf(prose: &mut String, regions: &mut Vec<Region>, trim_trailing: bool) {
    // Drop all-whitespace buffers. When flushing mid-paragraph before a math
    // island, keep a trailing space so "word $math$" does not become "word$math$".
    if prose.chars().all(|c| c.is_whitespace()) {
        prose.clear();
        return;
    }
    let s = if trim_trailing {
        prose.trim_end()
    } else {
        prose.as_str()
    };
    if !s.is_empty() {
        regions.push(Region::Prose(s.to_string()));
    }
    prose.clear();
}

/// Split a pandoc paragraph into reflowable prose runs and non-prose islands
/// (`Math`, inline `Code`) so periods inside math/code never end a sentence.
fn extract_para_inlines(inlines: &[Inline], regions: &mut Vec<Region>) {
    if inlines.is_empty() {
        return;
    }

    if is_math_only_para(inlines) {
        let mut out = String::new();
        for inline in inlines {
            if let Inline::Math(ty, body) = inline {
                out.push_str(&format_math(ty, body));
            }
        }
        if !out.is_empty() {
            if !out.ends_with('\n') {
                out.push('\n');
            }
            regions.push(Region::Structure(out));
        }
        return;
    }

    let mut prose = String::new();
    walk_para_inlines(inlines, regions, &mut prose);
    flush_prose_buf(&mut prose, regions, true);
}

/// Recursively walk inlines: `Math` / `Code` → structure islands; text → prose.
fn walk_para_inlines(inlines: &[Inline], regions: &mut Vec<Region>, prose: &mut String) {
    for inline in inlines {
        match inline {
            Inline::Math(ty, body) => {
                // trim_trailing: sentence splitter also trims; put spaces on the
                // structure island so "word $math$ word" survives reflow.
                let need_lead = !prose.is_empty() && !prose.ends_with('\n');
                flush_prose_buf(prose, regions, true);
                let mut s = String::new();
                if matches!(ty, MathType::DisplayMath) {
                    if need_lead {
                        // display math on its own lines
                    }
                    s.push_str(&format_math(ty, body));
                    if !s.ends_with('\n') {
                        s.push('\n');
                    }
                } else {
                    if need_lead {
                        s.push(' ');
                    }
                    s.push_str(&format_math(ty, body));
                    s.push(' ');
                }
                regions.push(Region::Structure(s));
            }
            Inline::Code(_, code) => {
                let need_lead = !prose.is_empty() && !prose.ends_with('\n');
                flush_prose_buf(prose, regions, true);
                let mut s = String::new();
                if need_lead {
                    s.push(' ');
                }
                s.push('`');
                s.push_str(code);
                s.push_str("` ");
                regions.push(Region::Structure(s));
            }
            Inline::Str(s) => prose.push_str(s),
            Inline::Space => prose.push(' '),
            Inline::SoftBreak => prose.push(' '),
            Inline::LineBreak => prose.push('\n'),
            Inline::Emph(children)
            | Inline::Strong(children)
            | Inline::Underline(children)
            | Inline::Strikeout(children)
            | Inline::Superscript(children)
            | Inline::Subscript(children)
            | Inline::SmallCaps(children)
            | Inline::Quoted(_, children)
            | Inline::Span(_, children)
            | Inline::Cite(_, children)
            | Inline::Link(_, children, _)
            | Inline::Image(_, children, _) => {
                walk_para_inlines(children, regions, prose);
            }
            Inline::RawInline(_, raw) => prose.push_str(raw),
            Inline::Note(_) => {}
        }
    }
}

/// Flatten inlines for non-prose contexts (headers, list terms) — not reflowed.
fn extract_inlines(inlines: &[Inline]) -> String {
    let mut result = String::new();
    flatten_inlines(inlines, &mut result);
    result
}

fn flatten_inlines(inlines: &[Inline], out: &mut String) {
    for inline in inlines {
        match inline {
            Inline::Str(s) => out.push_str(s),
            Inline::Space => out.push(' '),
            Inline::SoftBreak => out.push(' '),
            Inline::LineBreak => out.push('\n'),
            Inline::Code(_, code) => {
                out.push('`');
                out.push_str(code);
                out.push('`');
            }
            Inline::Math(ty, body) => out.push_str(&format_math(ty, body)),
            Inline::Emph(c)
            | Inline::Strong(c)
            | Inline::Underline(c)
            | Inline::Strikeout(c)
            | Inline::Superscript(c)
            | Inline::Subscript(c)
            | Inline::SmallCaps(c)
            | Inline::Quoted(_, c)
            | Inline::Span(_, c)
            | Inline::Cite(_, c)
            | Inline::Link(_, c, _)
            | Inline::Image(_, c, _) => flatten_inlines(c, out),
            Inline::RawInline(_, raw) => out.push_str(raw),
            Inline::Note(_) => {}
        }
    }
}

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

    /// Real pandoc-generated JSON (Header, Para, CodeBlock, Table, Para).
    fn mixed_doc_json() -> &'static str {
        include_str!("../../../tests/fixtures/pandoc_ast/mixed_minimal.json")
    }

    #[test]
    fn ast_classifies_prose_heading_code_table_from_node_kinds() {
        let regions = regions_from_pandoc_json(mixed_doc_json()).expect("valid fixture JSON");

        let prose: Vec<_> = regions
            .iter()
            .filter(|r| matches!(r, Region::Prose(_)))
            .collect();
        assert!(
            prose.len() >= 1,
            "expected ≥1 prose region from Para nodes, got {regions:?}"
        );
        assert!(
            prose
                .iter()
                .any(|r| matches!(r, Region::Prose(s) if s.contains("Hello"))),
            "expected paragraph prose, got {prose:?}"
        );

        let has_heading_structure = regions
            .iter()
            .any(|r| matches!(r, Region::Structure(s) if s.contains("Title")));
        assert!(
            has_heading_structure,
            "Header node must be Structure (not Prose): {regions:?}"
        );
        assert!(
            !regions
                .iter()
                .any(|r| matches!(r, Region::Prose(s) if s.contains("Title"))),
            "Header title must not be Prose: {regions:?}"
        );

        let code = regions.iter().find_map(|r| match r {
            Region::Code {
                lang,
                header,
                body,
                footer,
            } => Some((lang.clone(), header.clone(), body.clone(), footer.clone())),
            _ => None,
        });
        let (lang, header, body, footer) = code.expect("CodeBlock must become Region::Code");
        assert_eq!(lang.as_deref(), Some("python"));
        assert!(
            header.starts_with("```") && header.contains("python"),
            "fence header from Attr lang: {header:?}"
        );
        assert!(footer.starts_with("```"), "fence footer: {footer:?}");
        assert!(body.contains("print(1)"), "code body: {body}");

        let has_table = regions.iter().any(|r| {
            matches!(r, Region::Structure(s) if s.contains('|') && (s.contains('a') || s.contains("---")))
        });
        assert!(
            has_table,
            "Table must be pipe Structure from cells: {regions:?}"
        );

        for r in &regions {
            if let Region::Prose(s) = r {
                assert!(
                    !s.contains("print(1)") && !s.lines().any(|l| l.trim().starts_with('|')),
                    "structure leaked into prose: {s}"
                );
            }
        }
    }

    #[test]
    fn ast_table_list_quote_from_structure_fixture() {
        let json = include_str!("../../../tests/fixtures/pandoc_ast/structure_blocks.json");
        let regions = regions_from_pandoc_json(json).expect("structure_blocks.json");

        let table = regions.iter().find_map(|r| match r {
            Region::Structure(s) if s.contains('|') && s.contains("---") => Some(s.as_str()),
            _ => None,
        });
        let table = table.expect(&format!("pipe table structure: {regions:?}"));
        assert!(table.contains('a') && table.contains('b') && table.contains('1'));
        assert!(
            !regions
                .iter()
                .any(|r| matches!(r, Region::Prose(p) if p.contains('|') && p.contains("---"))),
            "table not Prose: {regions:?}"
        );

        assert!(
            regions.iter().any(
                |r| matches!(r, Region::Structure(s) if s.trim() == "-" || s.starts_with("- "))
            ),
            "bullet marker: {regions:?}"
        );
        assert!(
            regions
                .iter()
                .any(|r| matches!(r, Region::Structure(s) if s.starts_with("> "))),
            "blockquote marker: {regions:?}"
        );
        assert!(
            regions
                .iter()
                .any(|r| matches!(r, Region::Prose(p) if p.contains("Intro") || p.contains("End"))),
            "body prose: {regions:?}"
        );
    }

    #[test]
    fn ast_rejects_invalid_json_explicitly() {
        let err = regions_from_pandoc_json("not-json").unwrap_err();
        assert!(
            err.contains("deserialize") || err.contains("failed"),
            "unexpected error: {err}"
        );
    }

    /// After pandoc parse, a Header whose title looks like "1. code …" must
    /// not be Prose — snapper would reflow it. We do not invent ATX markup.
    #[test]
    fn ast_numbered_header_is_structure_not_prose() {
        let json = include_str!("../../../tests/fixtures/pandoc_ast/numbered_heading.json");
        let regions = regions_from_pandoc_json(json).expect("numbered_heading.json");

        let heading = regions.iter().find_map(|r| match r {
            Region::Structure(s) if s.contains("cargo binstall") => Some(s.as_str()),
            _ => None,
        });
        let heading = heading.expect(&format!(
            "expected Structure from Header node, got {regions:?}"
        ));
        assert!(
            heading.contains("1.") && heading.contains("cargo binstall"),
            "Header title text preserved as structure payload: {heading:?}"
        );
        assert!(
            !regions
                .iter()
                .any(|r| matches!(r, Region::Prose(p) if p.contains("cargo binstall"))),
            "Header title must not be Prose (would be sentence-reflowed): {regions:?}"
        );
        assert!(
            regions
                .iter()
                .any(|r| matches!(r, Region::Prose(p) if p.contains("Hello"))),
            "Para nodes remain Prose: {regions:?}"
        );
    }

    #[test]
    fn ast_codeblock_and_display_math_not_prose() {
        let json = include_str!("../../../tests/fixtures/pandoc_ast/math_code_md.json");
        let regions = regions_from_pandoc_json(json).expect("math_code_md.json");

        let code = regions
            .iter()
            .find(|r| matches!(r, Region::Code { body, .. } if body.contains("print")));
        match code {
            Some(Region::Code {
                lang,
                header,
                body,
                footer,
            }) => {
                assert_eq!(lang.as_deref(), Some("python"));
                assert!(
                    header.contains("```") && header.contains("python"),
                    "{header}"
                );
                assert!(footer.contains("```"), "{footer}");
                assert!(body.contains("print(1.0)") && body.contains("x = 2."));
            }
            other => panic!("CodeBlock → Region::Code with fence: {other:?} / {regions:?}"),
        }
        // Display-math-only Para → Structure containing math body, not Prose.
        assert!(
            regions.iter().any(|r| {
                matches!(r, Region::Structure(s) if s.contains("1.5") || s.contains("x = 1"))
            }),
            "display math must be Structure: {regions:?}"
        );
        assert!(
            !regions.iter().any(|r| {
                matches!(r, Region::Prose(p) if p.contains("1.5") && p.contains("y = 2"))
            }),
            "display math body must not be Prose: {regions:?}"
        );
        // Inline math island: mc^2. period not in Prose.
        assert!(
            regions.iter().any(|r| {
                matches!(r, Region::Structure(s) if s.contains("mc^2") || s.contains("E = mc"))
            }),
            "inline math → Structure island: {regions:?}"
        );
        assert!(
            !regions
                .iter()
                .any(|r| matches!(r, Region::Prose(p) if p.contains("mc^2"))),
            "inline math payload must not sit in Prose: {regions:?}"
        );
        // Ordinary multi-sentence prose still Prose.
        assert!(
            regions
                .iter()
                .any(|r| matches!(r, Region::Prose(p) if p.contains("First sentence"))),
            "plain prose Para: {regions:?}"
        );
    }

    #[test]
    fn ast_latex_equation_and_minted_not_prose() {
        let json = include_str!("../../../tests/fixtures/pandoc_ast/math_code_tex.json");
        let regions = regions_from_pandoc_json(json).expect("math_code_tex.json");

        assert!(
            regions.iter().any(
                |r| matches!(r, Region::Structure(s) if s.contains("mc^2") || s.contains("E = mc"))
            ),
            "equation DisplayMath → Structure: {regions:?}"
        );
        assert!(
            !regions
                .iter()
                .any(|r| matches!(r, Region::Prose(p) if p.contains("mc^2"))),
            "equation not Prose: {regions:?}"
        );
        assert!(
            regions
                .iter()
                .filter(|r| matches!(r, Region::Code { .. }))
                .count()
                >= 1,
            "minted/lstlisting/verbatim as CodeBlock: {regions:?}"
        );
        for r in &regions {
            if let Region::Code { body, .. } = r {
                // Code bodies must not be reflowed as prose regions.
                assert!(!matches!(r, Region::Prose(_)), "code body present: {body}");
            }
        }
        assert!(
            regions
                .iter()
                .any(|r| matches!(r, Region::Prose(p) if p.contains("Hello") || p.contains("End"))),
            "body prose remains: {regions:?}"
        );
    }
}