aozora 0.5.0

Aozora Bunko notation parser with incremental document snapshots
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
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
//! Block-level HTML render state + text escaper.
//!
//! The shared, AST-free machinery the owned HTML renderer
//! ([`crate::render::html`]) drives: the two-state paragraph machine
//! ([`RenderState`]) that emits `<p>` / `<br />` / container brackets from the
//! walk's sentinel + newline events, and the bulk-copy [`escape_text_chunk`]
//! pass over plain runs. Container open/close tags route through the
//! lifetime-free [`render_container`], so the byte spelling stays
//! single-source.

use core::fmt::{self, Write};

use crate::spec::roman_slug;
use crate::syntax::{
    BlockStyles, BoutenPosition, Container, HeadingKind, HeadingStyle, IndentBlock, IndentLayout,
    LineFormat, RegionFormat,
};
use memchr::{memchr_iter, memchr3_iter};

use crate::render::classes;

/// Block-level walker state. Tracks paragraph and block-separator boundaries so
/// consecutive inline runs collapse into one paragraph and adjacent block-leaf
/// nodes get the right inter-block whitespace.
#[derive(Debug, Default)]
pub(crate) struct RenderState {
    pub(crate) in_paragraph: bool,
    pending_block_separator: bool,
    /// Inside a phrasing-content container (a heading): its `<hN>` is the inline
    /// context, so [`Self::ensure_in_paragraph`] suppresses `<p>`.
    in_heading: bool,
    /// In-flight container opens. The close marker reads the matched open
    /// [`RegionFormat`] (open-authoritative).
    open_stack: Vec<RegionFormat>,
    /// Inline containers (`kind.is_inline()`) closed at a paragraph boundary,
    /// awaiting reopen in the next paragraph. An inline container is phrasing
    /// content, so a still-open one must not straddle `</p>`; but popping it off
    /// `open_stack` would desync a later `[#…終わり]` close marker. So
    /// [`Self::close_paragraph`] closes it top-down and records it here, and
    /// [`Self::ensure_in_paragraph`] reopens it (re-pushing onto `open_stack`) in
    /// the next paragraph — keeping the stack consistent so the eventual close
    /// still pairs. A never-closed inline container renders balanced in each
    /// paragraph to EOF (#420).
    reopen_after_para: Vec<RegionFormat>,
    /// Count of open inline-warichu spans (`[#割り注]`) awaiting their close
    /// (`[#割り注終わり]`). A warichu span is phrasing content, so it must
    /// never straddle a `</p>` or `</div>`: [`Self::close_paragraph`] drains any
    /// still-open span before closing the paragraph, and [`Self::close_warichu`]
    /// absorbs a stray close with no matching open. Sources that mismatch the
    /// block- and inline-warichu forms (9 corpus works, #415) rely on this to
    /// stay balanced.
    warichu_depth: u32,
}

impl RenderState {
    fn flush_pending_separator<W: Write>(&mut self, out: &mut W) -> fmt::Result {
        if self.pending_block_separator {
            out.write_char('\n')?;
            self.pending_block_separator = false;
        }
        Ok(())
    }

    pub(crate) fn ensure_in_paragraph<W: Write>(&mut self, out: &mut W) -> fmt::Result {
        if self.in_heading {
            return Ok(());
        }
        if !self.in_paragraph {
            self.flush_pending_separator(out)?;
            out.write_str("<p>")?;
            self.in_paragraph = true;
            // Reopen any inline containers `close_paragraph` closed at the last
            // paragraph boundary (#420). `pop()` yields them in reverse of the
            // top-down push order, i.e. outermost-first, restoring the original
            // nesting; re-pushing onto `open_stack` keeps the eventual close
            // paired. This runs only for real paragraphs — the `in_heading`
            // early return above skips it.
            while let Some(kind) = self.reopen_after_para.pop() {
                render_container(Container { kind }, true, out)?;
                self.open_stack.push(kind);
            }
        }
        Ok(())
    }

    pub(crate) fn close_paragraph<W: Write>(&mut self, out: &mut W) -> fmt::Result {
        // A warichu span is phrasing content that must close before the
        // enclosing paragraph (#415, Case 2): drain any still-open span here,
        // the single choke-point every block-leaf and container path uses.
        self.drain_open_warichu(out)?;
        if self.in_paragraph {
            // An inline container is phrasing content and sits at the TOP of
            // `open_stack` (any block container is below it), so it must not
            // straddle `</p>` either (#420). Close each open inline container
            // top-down here and remember it, so `ensure_in_paragraph` can reopen
            // it in the next paragraph — re-pushing onto `open_stack` keeps a
            // later close marker paired.
            while let Some(&kind) = self.open_stack.last() {
                if !kind.is_inline() {
                    break;
                }
                self.open_stack.pop();
                render_container(Container { kind }, false, out)?;
                self.reopen_after_para.push(kind);
            }
            out.write_str("</p>\n")?;
            self.in_paragraph = false;
            self.pending_block_separator = false;
        }
        Ok(())
    }

    pub(crate) fn before_block_emit<W: Write>(&mut self, out: &mut W) -> fmt::Result {
        self.close_paragraph(out)?;
        self.flush_pending_separator(out)
    }

    pub(crate) fn after_block_emit(&mut self) {
        self.pending_block_separator = true;
    }

    /// Emit a container's opening tag, honouring its content model. An inline
    /// container stays in the current paragraph; a phrasing-content container (a
    /// heading) flushes the paragraph and holds its content inline under the
    /// `<hN>` (`in_heading`); every other block container flushes and brackets
    /// its content as block paragraphs.
    pub(crate) fn open_container<W: Write>(
        &mut self,
        kind: RegionFormat,
        out: &mut W,
    ) -> fmt::Result {
        self.open_stack.push(kind);
        let container = Container { kind };
        if kind.is_inline() {
            self.ensure_in_paragraph(out)?;
            return render_container(container, true, out);
        }
        self.before_block_emit(out)?;
        render_container(container, true, out)?;
        if kind.content_is_phrasing() {
            self.in_heading = true;
        } else {
            self.after_block_emit();
        }
        Ok(())
    }

    /// Emit a container's closing tag — the mirror of [`Self::open_container`],
    /// reconstructed from the matched open [`RegionFormat`] popped off the stack
    /// (open-authoritative). A degraded empty stack best-effort skips.
    pub(crate) fn close_container<W: Write>(
        &mut self,
        closing_inline: bool,
        out: &mut W,
    ) -> fmt::Result {
        // An inline close marker (`[#太字終わり]` etc.) that lands in the gap
        // between a paragraph boundary and the next text cancels a pending
        // reopen instead of no-oping (#420). `close_paragraph` already drained
        // the paragraph's inline containers into `reopen_after_para`, so the
        // stack is empty (or holds only the enclosing block) and a plain
        // `open_stack.pop()` would silently lose the close — and the next
        // paragraph would then wrongly re-apply the emphasis. The front entry
        // is the innermost (closed first), matching the inner-first close
        // order; a *block* close (`closing_inline == false`) still pops its
        // region off the stack as usual, so block markup is unaffected.
        if closing_inline && !self.reopen_after_para.is_empty() {
            self.reopen_after_para.remove(0);
            return Ok(());
        }
        let Some(kind) = self.open_stack.pop() else {
            return Ok(());
        };
        let container = Container { kind };
        if kind.is_inline() {
            self.ensure_in_paragraph(out)?;
            return render_container(container, false, out);
        }
        if kind.content_is_phrasing() {
            self.in_heading = false;
        } else {
            self.before_block_emit(out)?;
        }
        render_container(container, false, out)?;
        self.after_block_emit();
        Ok(())
    }

    /// Open an inline-warichu span (`[#割り注]`), emitting its
    /// `<span class="aozora-warichu">` and recording the open so its close is
    /// balanced. The byte spelling matches the per-node fallback in
    /// [`crate::render::render_node`], so well-formed warichu output is unchanged.
    pub(crate) fn open_warichu<W: Write>(&mut self, out: &mut W) -> fmt::Result {
        out.write_str(r#"<span class="aozora-warichu">"#)?;
        self.warichu_depth += 1;
        Ok(())
    }

    /// Close one inline-warichu span (`[#割り注終わり]`). A close with no
    /// matching open — a source that mismatches the block- and inline-warichu
    /// forms (#415, Case 1) — is absorbed as a no-op rather than emitting a stray
    /// `</span>`.
    pub(crate) fn close_warichu<W: Write>(&mut self, out: &mut W) -> fmt::Result {
        if let Some(depth) = self.warichu_depth.checked_sub(1) {
            out.write_str("</span>")?;
            self.warichu_depth = depth;
        }
        Ok(())
    }

    /// Close every warichu span left open when the paragraph / document ends —
    /// an inline `[#割り注]` with no matching inline close (#415, Case 2). The
    /// span renders as extending to the paragraph boundary.
    pub(crate) fn drain_open_warichu<W: Write>(&mut self, out: &mut W) -> fmt::Result {
        while let Some(depth) = self.warichu_depth.checked_sub(1) {
            out.write_str("</span>")?;
            self.warichu_depth = depth;
        }
        Ok(())
    }

    pub(crate) fn finish<W: Write>(&mut self, out: &mut W) -> fmt::Result {
        self.close_paragraph(out)?;
        self.reopen_after_para.clear();
        let mut closed = false;
        while let Some(kind) = self.open_stack.pop() {
            render_container(Container { kind }, false, out)?;
            closed = true;
        }
        if closed {
            out.write_char('\n')?;
        }
        self.in_heading = false;
        self.pending_block_separator = false;
        Ok(())
    }
}

/// HTML-escape a plain-text chunk (the bytes between two structural matches in
/// the streaming walk).
///
/// The five HTML-unsafe ASCII characters (`< > & " '`) are rare in
/// Japanese-text-heavy corpora — most chunks contain none. Two `memchr` passes
/// (`memchr3` for `< > &` then `memchr` for `"`) fast-skip those clean chunks at
/// memory-bandwidth speed; only when at least one needle hits do we fall through
/// to a byte loop that merges the candidate positions and emits the escapes in
/// document order.
pub(crate) fn escape_text_chunk<W: Write>(chunk: &str, out: &mut W) -> fmt::Result {
    let bytes = chunk.as_bytes();

    let mut iter_lt_gt_amp = memchr3_iter(b'<', b'>', b'&', bytes);
    let first_lt_gt_amp = iter_lt_gt_amp.next();
    let mut iter_quote = memchr_iter(b'"', bytes);
    let first_quote = iter_quote.next();
    let mut iter_apos = memchr_iter(b'\'', bytes);
    let first_apos = iter_apos.next();

    if first_lt_gt_amp.is_none() && first_quote.is_none() && first_apos.is_none() {
        return out.write_str(chunk);
    }

    let mut cursor = 0usize;
    let mut next_lt_gt_amp = first_lt_gt_amp;
    let mut next_quote = first_quote;
    let mut next_apos = first_apos;

    loop {
        let pos = [next_lt_gt_amp, next_quote, next_apos]
            .into_iter()
            .flatten()
            .min();
        let Some(pos) = pos else { break };

        out.write_str(&chunk[cursor..pos])?;
        let entity = match bytes[pos] {
            b'<' => {
                next_lt_gt_amp = iter_lt_gt_amp.next();
                "&lt;"
            }
            b'>' => {
                next_lt_gt_amp = iter_lt_gt_amp.next();
                "&gt;"
            }
            b'&' => {
                next_lt_gt_amp = iter_lt_gt_amp.next();
                "&amp;"
            }
            b'"' => {
                next_quote = iter_quote.next();
                "&quot;"
            }
            // Hex form `&#x27;` matches `escape_text` so the
            // streaming and per-node renderers produce byte-identical output.
            b'\'' => {
                next_apos = iter_apos.next();
                "&#x27;"
            }
            // INVARIANT(escape): `pos` only ever indexes one of the five needle
            // bytes — established by escape_text_chunk's memchr3/memchr scans,
            // which yield positions of exactly `< > & " '`; exercised by the
            // `render_html` fuzz target.
            _ => unreachable!("escape iterator yielded non-needle byte"),
        };
        out.write_str(entity)?;
        cursor = pos.checked_add(1).expect("escape offset fits usize");
    }
    out.write_str(&chunk[cursor..])
}

// ── HTML container / heading / line tag byte-spelling ──

pub(crate) fn render_container<W: Write>(
    c: Container,
    entering: bool,
    writer: &mut W,
) -> fmt::Result {
    if entering {
        render_container_open(c.kind, writer)
    } else {
        render_container_close(c.kind, writer)
    }
}

/// Emit a container's opening tag. Block containers render a
/// `<div class="aozora-container …">`; the inline range forms (bouten,
/// bare 太字 / 斜体, 小書き) render their inline element directly.
#[expect(
    clippy::too_many_lines,
    reason = "one match arm per RegionFormat — splitting would scatter the \
              1:1 kind→markup mapping that mirrors emit_container_open"
)]
fn render_container_open<W: Write>(kind: RegionFormat, writer: &mut W) -> fmt::Result {
    match kind {
        RegionFormat::Indent(IndentBlock {
            amount,
            wrap,
            center,
            layout,
            styles,
        }) => {
            // Exhaustive destructure (no `..`) so a new decoration is
            // compiler-flagged here rather than silently dropped from the markup.
            let BlockStyles {
                gothic,
                horizontal,
                framed,
                font,
            } = styles;
            write!(
                writer,
                r#"<div class="aozora-container aozora-container-indent aozora-container-indent-{amount}"#,
            )?;
            if wrap.is_some() {
                writer.write_str(" aozora-container-wrap-indent")?;
            }
            if center {
                writer.write_str(" aozora-container-center")?;
            }
            // #78 secondary line-layout: 字組み grid gets its own class,
            // 字詰め reuses the standalone line-width class (same semantics).
            match layout {
                IndentLayout::Kumi(_) => {
                    writer.write_str(" aozora-container-line-kumi")?;
                }
                IndentLayout::LineWidth(_) => {
                    writer.write_str(" aozora-container-line-width")?;
                }
                IndentLayout::None => {}
            }
            // #78 co-applied decorative styles — flat classes on the same
            // `<div>` (close stays a single `</div>`), reusing each
            // attribute's standalone-container class so one stylesheet rule
            // serves both forms. Canonical order = gothic, horizontal, framed,
            // font (matches `BlockStyles::iter_formats` / the serializer).
            if gothic {
                writer.write_str(" aozora-container-goshikku")?;
            }
            if horizontal {
                writer.write_str(" aozora-container-yokogumi")?;
            }
            if framed {
                writer.write_str(" aozora-container-keigakomi")?;
            }
            if let Some(shift) = font {
                writer.write_str(if shift.larger() {
                    " aozora-container-font-larger"
                } else {
                    " aozora-container-font-smaller"
                })?;
            }
            write!(writer, r#"" data-amount="{amount}""#)?;
            if let Some(w) = wrap {
                write!(writer, r#" data-wrap="{w}""#)?;
            }
            match layout {
                IndentLayout::Kumi(kumi) => {
                    write!(
                        writer,
                        r#" data-kumi-lines="{}" data-kumi-width="{}""#,
                        kumi.lines, kumi.width
                    )?;
                }
                IndentLayout::LineWidth(width) => {
                    write!(writer, r#" data-width="{}""#, width.0)?;
                }
                IndentLayout::None => {}
            }
            if let Some(shift) = font {
                write!(writer, r#" data-steps="{}""#, shift.magnitude())?;
            }
            writer.write_str(">")
        }
        RegionFormat::AlignEnd { offset } => {
            write!(
                writer,
                r#"<div class="aozora-container aozora-container-align-end" data-offset="{offset}">"#,
            )
        }
        RegionFormat::LineWidth(width) => {
            write!(
                writer,
                r#"<div class="aozora-container aozora-container-line-width" data-width="{}">"#,
                width.0,
            )
        }
        RegionFormat::Framed(_) => {
            writer.write_str(r#"<div class="aozora-container aozora-container-keigakomi">"#)
        }
        RegionFormat::Warichu => {
            writer.write_str(r#"<div class="aozora-container aozora-container-warichu">"#)
        }
        RegionFormat::Bouten { kind, position } => {
            // Range-form 傍点 / 傍線: an inline `<em>` matching the
            // forward-reference bouten markup so a stylesheet picks the
            // same per-variant treatment.
            write!(
                writer,
                r#"<em class="aozora-bouten aozora-bouten-{kind} aozora-bouten-{pos}">"#,
                kind = classes::bouten_kind_slug(kind),
                pos = classes::bouten_position_slug(position),
            )
        }
        // 太字 / 斜体. The bare inline range (`block: false`) uses the
        // same presentational `<b>` / `<i>` element as the
        // forward-reference [`render_emphasis`] leaf. The ここから-block
        // form (`block: true`) wraps whole paragraphs, so it takes a
        // block `<div>` (an inline `<b>` around `<p>` would be invalid),
        // following the indent / keigakomi container convention; the
        // `aozora-container-futoji` / `-shatai` class carries the styling.
        RegionFormat::Bold { padded: false } => writer.write_str(r#"<b class="aozora-futoji">"#),
        RegionFormat::Gothic { padded: false } => {
            writer.write_str(r#"<b class="aozora-goshikku">"#)
        }
        RegionFormat::Italic { padded: false } => writer.write_str(r#"<i class="aozora-shatai">"#),
        RegionFormat::Bold { padded: true } => {
            writer.write_str(r#"<div class="aozora-container aozora-container-futoji">"#)
        }
        RegionFormat::Gothic { padded: true } => {
            writer.write_str(r#"<div class="aozora-container aozora-container-goshikku">"#)
        }
        RegionFormat::Italic { padded: true } => {
            writer.write_str(r#"<div class="aozora-container aozora-container-shatai">"#)
        }
        RegionFormat::Columns(count) => write!(
            writer,
            r#"<div class="aozora-container aozora-container-columns" data-columns="{}">"#,
            count.0,
        ),
        RegionFormat::Table => {
            writer.write_str(r#"<div class="aozora-container aozora-container-table">"#)
        }
        RegionFormat::Horizontal => {
            writer.write_str(r#"<div class="aozora-container aozora-container-yokogumi">"#)
        }
        RegionFormat::FontSize(shift) => {
            let class = if shift.larger() {
                "aozora-container-font-larger"
            } else {
                "aozora-container-font-smaller"
            };
            write!(
                writer,
                r#"<div class="aozora-container {class}" data-steps="{}">"#,
                shift.magnitude(),
            )
        }
        // Paired / block heading — same element as the forward-reference
        // leaf, but wrapping the delimited content (phrasing).
        RegionFormat::Heading { level, style, .. } => write_heading_open(level, style, writer),
        // 小書き range — inline `<span>`, matching the forward-reference
        // small-script leaf classes.
        RegionFormat::SmallScript(BoutenPosition::Left) => {
            writer.write_str(r#"<span class="aozora-kogaki-left">"#)
        }
        RegionFormat::SmallScript(_) => writer.write_str(r#"<span class="aozora-kogaki-right">"#),
        // Caption: inline `<span>` for the bare range, block `<div>` for ここから.
        RegionFormat::Caption { padded: false } => {
            writer.write_str(r#"<span class="aozora-caption">"#)
        }
        RegionFormat::Caption { padded: true } => {
            writer.write_str(r#"<div class="aozora-container aozora-caption">"#)
        }
    }
}

/// Emit a container's closing tag — `</em>` / `</b>` / `</i>` for the inline
/// range forms, the heading element for a block heading, `</div>` otherwise.
fn render_container_close<W: Write>(kind: RegionFormat, writer: &mut W) -> fmt::Result {
    match kind {
        RegionFormat::Heading { level, style, .. } => write_heading_close(level, style, writer),
        _ => writer.write_str(match kind {
            RegionFormat::Bouten { .. } => "</em>",
            RegionFormat::Bold { padded: false } | RegionFormat::Gothic { padded: false } => "</b>",
            RegionFormat::Italic { padded: false } => "</i>",
            RegionFormat::SmallScript(_) | RegionFormat::Caption { padded: false } => "</span>",
            _ => "</div>",
        }),
    }
}

/// Render a `[#挿絵(file)入る]` illustration as a semantic
/// Parse the bundled `横W×縦H` pixel-size note into `(width, height)` —
/// both runs of ASCII digits. Returns `None` for any other shape (the
/// dimensions then carry no HTML width/height hint).
pub(crate) fn parse_sashie_dimensions(dims: &str) -> Option<(&str, &str)> {
    let (w, h) = dims.split_once('×')?;
    let w = w.strip_prefix('')?;
    let h = h.strip_prefix('')?;
    let digits = |s: &str| !s.is_empty() && s.bytes().all(|b| b.is_ascii_digit());
    (digits(w) && digits(h)).then_some((w, h))
}

/// The HTML tag for a heading. The 窓 (window) style is an inset block, not an
/// outline level, so it takes a `<div>`; otherwise the 大 / 中 / 小 level maps
/// to the semantic `<h1>`–`<h3>` outline tag.
fn heading_tag(kind: HeadingKind, style: HeadingStyle) -> &'static str {
    if matches!(style, HeadingStyle::Window) {
        "div"
    } else {
        match kind {
            HeadingKind::Medium => "h2",
            HeadingKind::Small => "h3",
            _ => "h1",
        }
    }
}

/// Write a heading's opening tag — `<hN>` / `<div>` with an
/// `aozora-heading-<large|medium|small>` class plus an
/// `aozora-heading-<same-line|window>` modifier for a non-standard style.
/// Shared by the forward-reference leaf `render_aozora_heading` and the
/// paired / block [`RegionFormat::Heading`] container so both render
/// identically.
pub(crate) fn write_heading_open<W: Write>(
    kind: HeadingKind,
    style: HeadingStyle,
    writer: &mut W,
) -> fmt::Result {
    write!(
        writer,
        r#"<{tag} class="aozora-heading aozora-heading-{level_slug}"#,
        tag = heading_tag(kind, style),
        level_slug = classes::heading_level_slug(kind),
    )?;
    if let Some(modifier) = classes::heading_style_slug(style) {
        write!(writer, " aozora-heading-{modifier}")?;
    }
    writer.write_str(r#"">"#)
}

/// Write a heading's closing tag (matching [`write_heading_open`]).
pub(crate) fn write_heading_close<W: Write>(
    kind: HeadingKind,
    style: HeadingStyle,
    writer: &mut W,
) -> fmt::Result {
    write!(writer, "</{}>", heading_tag(kind, style))
}

/// Render a single-line layout directive (字下げ / 地付き / 中央 / 罫囲み) as
/// a zero-width hook span; the actual layout is left to a stylesheet.
pub(crate) fn render_line<W: Write>(lf: LineFormat, writer: &mut W) -> fmt::Result {
    match lf {
        LineFormat::Indent {
            amount,
            end_offset: None,
        } => write!(
            writer,
            r#"<span class="aozora-indent aozora-indent-{amount}" data-amount="{amount}"></span>"#,
        ),
        // Both-margin compound: the head-indent classes plus the existing
        // align-end classes for the foot-edge lift (reused, not new tokens).
        LineFormat::Indent {
            amount,
            end_offset: Some(offset),
        } => write!(
            writer,
            r#"<span class="aozora-indent aozora-indent-{amount} aozora-align-end aozora-align-end-{offset}" data-amount="{amount}" data-offset="{offset}"></span>"#,
        ),
        LineFormat::AlignEnd { offset: 0 } => {
            writer.write_str(r#"<span class="aozora-align-end" data-offset="0"></span>"#)
        }
        LineFormat::AlignEnd { offset } => write!(
            writer,
            r#"<span class="aozora-align-end aozora-align-end-{offset}" data-offset="{offset}"></span>"#,
        ),
        LineFormat::Center { .. } => writer.write_str(r#"<span class="aozora-center"></span>"#),
        LineFormat::Gothic => writer.write_str(r#"<span class="aozora-line-goshikku"></span>"#),
        // Absolute font-size line marker; `、太字` adds the line-bold class too.
        LineFormat::FontSizeAbsolute { size, bold } => {
            let slug = roman_slug(size.keyword()).unwrap_or("font-small");
            if bold {
                write!(
                    writer,
                    r#"<span class="aozora-line-{slug} aozora-line-futoji"></span>"#,
                )
            } else {
                write!(writer, r#"<span class="aozora-line-{slug}"></span>"#)
            }
        }
    }
}

/// Minimal HTML5 text escape — five structural ASCII characters.
/// Apostrophe uses the hex form `&#x27;`; the contract is pinned by
/// the integration tests in this crate.
pub(crate) fn escape_text<W: Write>(text: &str, writer: &mut W) -> fmt::Result {
    let mut cursor = 0;
    for (pos, m) in text.match_indices(HTML_UNSAFE_CHARS) {
        writer.write_str(&text[cursor..pos])?;
        let ch = m.as_bytes()[0] as char;
        writer.write_str(html_entity(ch))?;
        cursor = pos + m.len();
    }
    writer.write_str(&text[cursor..])
}

const HTML_UNSAFE_CHARS: &[char] = &['<', '>', '&', '"', '\''];

#[inline]
const fn html_entity(c: char) -> &'static str {
    match c {
        '<' => "&lt;",
        '>' => "&gt;",
        '&' => "&amp;",
        '"' => "&quot;",
        '\'' => "&#x27;",
        _ => "",
    }
}

#[cfg(test)]
mod tests {
    use crate::render::render_html;
    use pretty_assertions::assert_eq;

    use super::{
        RenderState, escape_text, heading_tag, html_entity, parse_sashie_dimensions,
        render_container_close, render_container_open, render_line,
    };
    use crate::pipeline::lex;
    use crate::syntax::{
        BoutenKind, BoutenPosition, EnclosureKind, HeadingKind, HeadingStyle, LineFormat,
        LineWidth, RegionFormat,
    };
    use core::num::NonZeroU8;

    fn render(src: &str) -> String {
        render_html(&lex(src))
    }

    fn open_tag(kind: RegionFormat) -> String {
        let mut s = String::new();
        render_container_open(kind, &mut s).expect("render into String is infallible");
        s
    }

    fn close_tag(kind: RegionFormat) -> String {
        let mut s = String::new();
        render_container_close(kind, &mut s).expect("render into String is infallible");
        s
    }

    fn line_tag(lf: LineFormat) -> String {
        let mut s = String::new();
        render_line(lf, &mut s).expect("render into String is infallible");
        s
    }

    #[test]
    fn plain_paragraph_wraps_in_p() {
        assert_eq!(render("Hello."), "<p>Hello.</p>\n");
    }

    #[test]
    fn pending_block_separator_is_emitted_before_paragraph() {
        let mut state = RenderState::default();
        let mut out = String::new();
        state.after_block_emit();
        state
            .ensure_in_paragraph(&mut out)
            .expect("render into String is infallible");
        assert_eq!(out, "\n<p>");
    }

    /// A well-formed inline warichu pair emits the same balanced span it always
    /// did — a byte-identity guard that the `RenderState`-owned depth machinery
    /// (#415) does not perturb the correct case.
    #[test]
    fn warichu_wellformed_inline_pair_is_byte_identical() {
        let html = render("前[#割り注]上等/下等[#割り注終わり]後");
        assert_eq!(
            html,
            "<p>前<span class=\"aozora-warichu\">上等/下等</span>後</p>\n",
        );
        assert_eq!(
            html.matches("<span").count(),
            html.matches("</span>").count()
        );
    }

    /// #415 Case 1: a block-form warichu open (`[#ここから割り注]`) paired with an
    /// inline-form close (`[#割り注終わり]`) must not leak a stray `</span>` — the
    /// unmatched inline close is absorbed as a no-op.
    #[test]
    fn warichu_block_open_inline_close_absorbs_stray_close() {
        let html = render("[#ここから割り注]\n上等\n[#割り注終わり]");
        assert_eq!(
            html.matches("<span").count(),
            html.matches("</span>").count(),
            "span tags must balance (no stray </span>): {html}",
        );
        assert!(
            !html.contains("</span>"),
            "no warichu span was opened, so no </span> should appear: {html}",
        );
        assert_eq!(html.matches("<div").count(), 1);
        assert_eq!(html.matches("</div>").count(), 1);
    }

    /// #415 Case 2: an inline-form warichu open (`[#割り注]`) paired with a
    /// block-form close (`[#ここで割り注終わり]`) must have its span drained before
    /// the paragraph closes — the open `<span>` never straddles `</p>`.
    #[test]
    fn warichu_inline_open_block_close_drains_span() {
        let html = render("前[#割り注]上等[#ここで割り注終わり]後");
        assert_eq!(
            html.matches("<span").count(),
            html.matches("</span>").count(),
            "span tags must balance (open span must be drained): {html}",
        );
        assert!(
            html.contains(r#"<span class="aozora-warichu">"#),
            "the inline warichu span must still open: {html}",
        );
        // The drained close lands before the paragraph boundary, not after it.
        assert!(
            html.contains("</span></p>"),
            "the span must close before the </p>, not straddle it: {html}",
        );
    }

    #[test]
    fn ruby_emits_semantic_form() {
        let html = render("|青梅《おうめ》");
        assert!(html.contains("<ruby>青梅"), "missing ruby tag: {html}");
        assert!(html.contains("<rt>おうめ"), "missing rt tag: {html}");
    }

    #[test]
    fn page_break_inside_text_emits_div() {
        let html = render("\n\n[#改ページ]\n\n");
        assert!(html.contains(r#"<div class="aozora-page-break"></div>"#));
        assert!(!html.contains("[#"), "[# leaked: {html}");
    }

    #[test]
    fn paired_container_open_close_renders_div_pair() {
        let html = render("[#ここから2字下げ]\n本文\n[#ここで字下げ終わり]");
        assert!(html.contains("aozora-container-indent aozora-container-indent-2"));
        assert!(html.contains("</div>"));
    }

    #[test]
    fn unclosed_block_container_closes_at_eof() {
        let html = render("[#ここから2字下げ]\n本文");
        assert_eq!(html.matches("<div").count(), html.matches("</div>").count());
    }

    #[test]
    fn newline_inside_paragraph_emits_br() {
        let html = render("a\nb");
        assert!(html.contains("a<br />\nb"));
    }

    #[test]
    fn double_newline_closes_paragraph() {
        let html = render("a\n\nb");
        assert!(html.contains("<p>a</p>\n"));
        assert!(html.contains("<p>b</p>\n"));
    }

    #[test]
    fn html_unsafe_chars_in_plain_text_are_escaped() {
        let html = render("a<b>&\"'");
        assert!(
            html.contains("a&lt;b&gt;&amp;&quot;&#x27;"),
            "expected byte-identical entities (incl. `&#x27;` for apostrophe), got: {html}",
        );
    }

    #[test]
    fn empty_input_emits_empty_string() {
        assert_eq!(render(""), "");
    }

    #[test]
    fn inline_container_stays_inside_paragraph() {
        // Byte-identity regression guard for #420: a well-formed inline container
        // opens AND closes within its paragraph, so the paragraph-boundary
        // reopen machinery is a no-op — the top of `open_stack` is never inline
        // at `close_paragraph`. Output must be exactly as before the fix.
        let html = render("前[#太字]中[#太字終わり]後");
        assert_eq!(
            html, "<p>前<b class=\"aozora-futoji\">中</b>後</p>\n",
            "inline container must stay within the paragraph",
        );
    }

    /// True iff `<b>` is balanced at every `</p>` boundary — i.e. no open bold
    /// ever straddles a paragraph close (#420). A `</b>` (`<`,`/`,`b`,`>`) never
    /// contains the substring `<b`, so `matches("<b")` counts only opening tags.
    fn bold_never_straddles_p_close(html: &str) -> bool {
        let mut cursor = 0;
        while let Some(rel) = html[cursor..].find("</p>") {
            let end = cursor + rel; // start of this `</p>`
            let prefix = &html[..end];
            if prefix.matches("<b").count() != prefix.matches("</b>").count() {
                return false;
            }
            cursor = end + "</p>".len();
        }
        true
    }

    /// #420: an inline 太字 container the source never closes must not leave an
    /// open `<b>` straddling `</p>` across a paragraph break. The container is
    /// closed before `</p>` and reopened in the next paragraph, so bold is
    /// globally balanced and balanced at the `</p>` boundary.
    #[test]
    fn unclosed_bold_across_paragraph_break_never_straddles_p() {
        let html = render("前[#太字]中\n\n");
        assert_eq!(
            html.matches("<b").count(),
            html.matches("</b>").count(),
            "bold must be globally balanced: {html}",
        );
        assert!(
            bold_never_straddles_p_close(&html),
            "no open <b> may straddle </p>: {html}",
        );
        assert!(
            !html.contains("<b class=\"aozora-futoji\">中</p>"),
            "the </b> must precede </p>, not straddle it: {html}",
        );
        assert!(
            html.contains("</b></p>"),
            "bold closes before the paragraph boundary: {html}",
        );
    }

    /// #420: an unclosed inline 太字 that reaches EOF renders balanced, with each
    /// trailing paragraph bold and no bold straddling any `</p>`.
    #[test]
    fn unclosed_bold_reaching_eof_is_balanced_each_paragraph() {
        let html = render("前[#太字]中\n\nもっと\n\n最後");
        assert_eq!(
            html.matches("<b").count(),
            html.matches("</b>").count(),
            "bold must be globally balanced to EOF: {html}",
        );
        assert_eq!(
            html.matches("<b").count(),
            3,
            "the never-closed bold reopens in each of the 3 paragraphs: {html}",
        );
        assert!(
            bold_never_straddles_p_close(&html),
            "no open <b> may straddle </p> anywhere: {html}",
        );
        assert!(
            html.contains("<p><b class=\"aozora-futoji\">もっと</b></p>"),
            "a trailing paragraph is fully bold: {html}",
        );
    }

    /// #420: a 太字 opened before a paragraph break and closed with
    /// `[#太字終わり]` in a later paragraph must still pair — the reopened
    /// container stays on `open_stack`, so the close marker finds its match and
    /// text after the close is no longer bold.
    #[test]
    fn bold_close_marker_after_paragraph_break_still_pairs() {
        let html = render("前[#太字]中\n\n後[#太字終わり]尾");
        assert_eq!(
            html.matches("<b").count(),
            html.matches("</b>").count(),
            "bold must be balanced (close marker pairs): {html}",
        );
        assert!(
            bold_never_straddles_p_close(&html),
            "no open <b> may straddle </p>: {html}",
        );
        assert!(
            html.contains("<p>前<b class=\"aozora-futoji\">中</b></p>"),
            "first paragraph is bold and closes before </p>: {html}",
        );
        assert!(
            html.contains("<p><b class=\"aozora-futoji\">後</b>尾</p>"),
            "second paragraph reopens bold, the close marker ends it, 尾 is plain: {html}",
        );
    }

    /// #420: an inline close marker landing in the *gap* between a paragraph
    /// break and the next text (no intervening text) ends the emphasis — it
    /// must cancel the pending reopen, not silently no-op and then wrongly
    /// re-apply the emphasis to the following paragraph. Regression guard for
    /// the reopen-cancel path.
    #[test]
    fn bold_close_in_paragraph_gap_ends_emphasis() {
        // Single: the close cancels the pending reopen, so 後 is plain.
        let html = render("前[#太字]中\n\n[#太字終わり]後");
        assert_eq!(
            html.matches("<b").count(),
            html.matches("</b>").count(),
            "bold balanced: {html}",
        );
        assert!(
            html.contains("<p>前<b class=\"aozora-futoji\">中</b></p>")
                && html.contains("<p>後</p>"),
            "後 after a gap-close must be plain, not bold: {html}",
        );
        // Nested: both closes in the gap end both emphases (inner-first),
        // leaving 後 plain — the outer/block markup is untouched.
        let nested = render("前[#太字]あ[#斜体]い\n\n[#斜体終わり][#太字終わり]後");
        assert_eq!(
            nested.matches("<b").count(),
            nested.matches("</b>").count(),
            "nested bold balanced: {nested}",
        );
        assert_eq!(
            nested.matches("<i").count(),
            nested.matches("</i>").count(),
            "nested italic balanced: {nested}",
        );
        assert!(
            nested.contains("<p>後</p>"),
            "後 after nested gap-closes must be plain: {nested}",
        );
    }

    #[test]
    fn block_container_flushes_paragraph_then_wraps_body() {
        let html = render("前文\n\n[#ここから2字下げ]\n本文\n[#ここで字下げ終わり]");
        assert!(html.contains("<p>前文</p>\n"), "leading paragraph: {html}");
        assert!(
            html.contains(
                "<div class=\"aozora-container aozora-container-indent aozora-container-indent-2\""
            ),
            "indent container open: {html}"
        );
        assert!(
            html.contains("<p>本文</p>"),
            "wrapped body paragraph: {html}"
        );
        assert!(html.contains("</div>"), "container close: {html}");
    }

    #[test]
    fn heading_container_holds_content_inline_without_inner_paragraph() {
        let html = render("[#ここから大見出し]\n章題\n[#ここで大見出し終わり]");
        assert!(
            html.contains("<h1 class=\"aozora-heading aozora-heading-large\">章題</h1>"),
            "heading must hold text inline without a <p>: {html}"
        );
        assert!(
            !html.contains("<h1 class=\"aozora-heading aozora-heading-large\"><p>"),
            "heading must not wrap content in <p>: {html}"
        );
    }

    #[test]
    fn section_break_block_flushes_surrounding_paragraphs() {
        let html = render("\n\n[#改丁]\n\n");
        assert!(
            html.contains("<p>前</p>\n"),
            "paragraph before break: {html}"
        );
        assert!(
            html.contains("<div class=\"aozora-section-break aozora-section-break-kaicho\"></div>"),
            "section break div: {html}"
        );
        assert!(
            html.contains("<p>後</p>\n"),
            "paragraph after break: {html}"
        );
    }

    #[test]
    fn single_trailing_newline_emits_no_break_outside_paragraph() {
        let html = render("a\n");
        assert_eq!(html, "<p>a</p>\n", "trailing newline must not add <br />");
    }

    #[test]
    fn quote_and_apostrophe_chunk_take_the_slow_escape_path() {
        let html = render(r#"x"y'z<&>"#);
        assert_eq!(
            html, "<p>x&quot;y&#x27;z&lt;&amp;&gt;</p>\n",
            "all five unsafe chars must escape in document order",
        );
    }

    #[test]
    fn apostrophe_only_chunk_escapes_via_byte_loop() {
        let html = render("it's");
        assert_eq!(html, "<p>it&#x27;s</p>\n", "lone apostrophe must escape");
    }

    #[test]
    fn referenced_contiguous_forward_styles_referent_once() {
        // #333: the non-adjacent referent 青空 is now styled in place (a
        // `Detached` decoration spliced into the plain run), while the bracket
        // stays `Referenced` and renders nothing. 青空 still appears exactly
        // once — the styling is added, the #228 no-double-render invariant holds.
        let html = render("青空の下を歩く[#「青空」に傍点]");
        assert_eq!(
            html,
            "<p><em class=\"aozora-bouten aozora-bouten-goma aozora-bouten-right\">青空</em>の下を歩く</p>\n"
        );
        assert_eq!(html.matches("青空").count(), 1, "青空 must not duplicate");
        assert!(html.contains("<em"), "referent now styled: {html}");
    }

    #[test]
    fn referenced_ruby_base_forward_styles_base_once() {
        // #384: the forward target 我 is a ruby base, so it cannot be pulled into
        // a plain forward leaf; the lowering pass instead decorates the ruby's
        // base (render-only `base_emphasis`). The bracket stays `Referenced` and
        // renders nothing, so 我 appears exactly once — now styled inside the
        // `<ruby>`, before the `<rt>` — and the #228 no-double-render invariant
        // still holds.
        let html = render("我《われ》の名は[#「我」に傍点]");
        assert_eq!(
            html,
            "<p><ruby><em class=\"aozora-bouten aozora-bouten-goma aozora-bouten-right\">我</em><rp>(</rp><rt>われ</rt><rp>)</rp></ruby>の名は</p>\n"
        );
        assert_eq!(html.matches("").count(), 1, "我 must not duplicate");
        assert!(html.contains("<em"), "ruby base now styled (#384): {html}");
    }

    #[test]
    fn reclaimed_adjacent_forward_still_renders_emphasis() {
        let html = render("青空[#「青空」に傍点]を見上げる。");
        assert_eq!(
            html,
            "<p><em class=\"aozora-bouten aozora-bouten-goma aozora-bouten-right\">青空</em>を見上げる。</p>\n"
        );
    }

    /// A block emit routed through [`RenderState::before_block_emit`] must close
    /// the open paragraph (and its trailing newline) before the block markup —
    /// stubbing the method to a no-op would leave `<p>X` dangling.
    #[test]
    fn before_block_emit_closes_open_paragraph() {
        let mut st = RenderState::default();
        let mut out = String::new();
        st.ensure_in_paragraph(&mut out).expect("infallible");
        out.push('X');
        st.before_block_emit(&mut out).expect("infallible");
        assert_eq!(out, "<p>X</p>\n");
    }

    /// Pin the exact opening markup of every `RegionFormat` container arm whose
    /// byte spelling no other test fixes. Deleting an arm falls through to a
    /// sibling / the generic `<div class="aozora-container">`, so each expected
    /// string must be unique.
    #[test]
    fn container_open_pins_each_region_markup() {
        let cases = [
            (
                RegionFormat::LineWidth(LineWidth(NonZeroU8::new(7).unwrap())),
                r#"<div class="aozora-container aozora-container-line-width" data-width="7">"#,
            ),
            (
                RegionFormat::Framed(EnclosureKind::Rule),
                r#"<div class="aozora-container aozora-container-keigakomi">"#,
            ),
            (
                RegionFormat::Gothic { padded: true },
                r#"<div class="aozora-container aozora-container-goshikku">"#,
            ),
            (
                RegionFormat::Horizontal,
                r#"<div class="aozora-container aozora-container-yokogumi">"#,
            ),
            (
                RegionFormat::SmallScript(BoutenPosition::Left),
                r#"<span class="aozora-kogaki-left">"#,
            ),
            (
                RegionFormat::SmallScript(BoutenPosition::Right),
                r#"<span class="aozora-kogaki-right">"#,
            ),
            (
                RegionFormat::Caption { padded: false },
                r#"<span class="aozora-caption">"#,
            ),
            (
                RegionFormat::Caption { padded: true },
                r#"<div class="aozora-container aozora-caption">"#,
            ),
        ];
        for (kind, expected) in cases {
            assert_eq!(open_tag(kind), expected, "open {kind:?}");
        }
    }

    /// Pin the exact closing markup of the `RegionFormat` container arms whose
    /// close tag is not `</div>`. Deleting either arm falls through to the
    /// generic `</div>`, so `</em>` / `</span>` must be observed.
    #[test]
    fn container_close_pins_each_region_markup() {
        let cases = [
            (
                RegionFormat::Bouten {
                    kind: BoutenKind::Goma,
                    position: BoutenPosition::Right,
                },
                "</em>",
            ),
            (RegionFormat::SmallScript(BoutenPosition::Right), "</span>"),
            (RegionFormat::SmallScript(BoutenPosition::Left), "</span>"),
            (RegionFormat::Caption { padded: false }, "</span>"),
        ];
        for (kind, expected) in cases {
            assert_eq!(close_tag(kind), expected, "close {kind:?}");
        }
    }

    /// The 大 / 中 / 小 outline levels map to `<h1>`/`<h2>`/`<h3>`; the 窓 style
    /// is an inset block (`<div>`) at any level. Deleting the Medium / Small arm
    /// wrongly collapses them to the `h1` default.
    #[test]
    fn heading_tag_maps_level_and_window_style() {
        assert_eq!(
            heading_tag(HeadingKind::Large, HeadingStyle::Standard),
            "h1"
        );
        assert_eq!(
            heading_tag(HeadingKind::Medium, HeadingStyle::Standard),
            "h2"
        );
        assert_eq!(
            heading_tag(HeadingKind::Small, HeadingStyle::Standard),
            "h3"
        );
        assert_eq!(
            heading_tag(HeadingKind::Medium, HeadingStyle::Window),
            "div"
        );
    }

    /// Pin the exact hook-span markup of each `LineFormat` arm. The plain indent
    /// and the both-margin compound differ; the flush `AlignEnd { offset: 0 }`
    /// (no `-0` class) differs from the general `AlignEnd { offset }`. Deleting
    /// any arm yields a different (or empty) span.
    #[test]
    fn render_line_pins_each_line_directive_markup() {
        let cases = [
            (
                LineFormat::Indent {
                    amount: 2,
                    end_offset: None,
                },
                r#"<span class="aozora-indent aozora-indent-2" data-amount="2"></span>"#,
            ),
            (
                LineFormat::Indent {
                    amount: 2,
                    end_offset: Some(3),
                },
                r#"<span class="aozora-indent aozora-indent-2 aozora-align-end aozora-align-end-3" data-amount="2" data-offset="3"></span>"#,
            ),
            (
                LineFormat::AlignEnd { offset: 0 },
                r#"<span class="aozora-align-end" data-offset="0"></span>"#,
            ),
            (
                LineFormat::AlignEnd { offset: 5 },
                r#"<span class="aozora-align-end aozora-align-end-5" data-offset="5"></span>"#,
            ),
        ];
        for (lf, expected) in cases {
            assert_eq!(line_tag(lf), expected, "line {lf:?}");
        }
    }

    /// `parse_sashie_dimensions` returns the two digit runs only for a
    /// well-formed `横W×縦H` note. Every malformed shape (missing separator /
    /// prefix, non-digit run, empty run) returns `None`. Pins the return value,
    /// the `!s.is_empty()` guard, and both `&&` conjunctions.
    #[test]
    fn parse_sashie_dimensions_pins_digit_pairs() {
        assert_eq!(parse_sashie_dimensions("横100×縦200"), Some(("100", "200")));
        // Missing `×` separator.
        assert_eq!(parse_sashie_dimensions("横100縦200"), None);
        // Missing `横` prefix.
        assert_eq!(parse_sashie_dimensions("100×縦200"), None);
        // Missing `縦` prefix.
        assert_eq!(parse_sashie_dimensions("横100×200"), None);
        // Non-digit width run: digits(w) is false, digits(h) true — pins the
        // per-run `!is_empty && all_digit` and the joining `digits(w) && digits(h)`.
        assert_eq!(parse_sashie_dimensions("横10a×縦200"), None);
        // Non-digit height run: the mirror of the above.
        assert_eq!(parse_sashie_dimensions("横100×縦20b"), None);
        // Empty width run: pins `!s.is_empty()` (an empty run is not all-digit).
        assert_eq!(parse_sashie_dimensions("横×縦200"), None);
    }

    /// `escape_text` must advance the cursor one byte *past* an escaped char
    /// (`pos + len`, not `pos * len`) so the following text is emitted once.
    #[test]
    fn escape_text_advances_cursor_past_escaped_char() {
        let mut out = String::new();
        escape_text("a<b", &mut out).expect("infallible");
        assert_eq!(out, "a&lt;b");
    }

    /// Every one of the five HTML-unsafe ASCII characters maps to its exact
    /// entity; apostrophe uses the hex form `&#x27;`. Pins each match arm and
    /// rules out the `""` / `"xyzzy"` return stubs.
    #[test]
    fn html_entity_pins_each_escape() {
        assert_eq!(html_entity('<'), "&lt;");
        assert_eq!(html_entity('>'), "&gt;");
        assert_eq!(html_entity('&'), "&amp;");
        assert_eq!(html_entity('"'), "&quot;");
        assert_eq!(html_entity('\''), "&#x27;");
    }
}