boko 0.5.0

Fast native ebook converter for EPUB, KFX, AZW3, and MOBI — the only KFX writer that needs no Kindle Previewer
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
//! Core IR → Markdown rendering.
//!
//! This module provides pure rendering logic that transforms the book IR
//! into Markdown strings. No I/O is performed here - the export layer
//! handles writing to files/writers.

use std::collections::HashMap;

use crate::import::ChapterId;
use crate::model::{AnchorTarget, Chapter, GlobalNodeId, NodeId, ResolvedLinks, Role};
use crate::style::Display;
use crate::util::strip_ebook_chars;

use super::escape::escape_markdown_at;
use super::escape::{calculate_fence_length, calculate_inline_code_ticks};

/// Result of rendering a chapter to markdown.
#[derive(Debug, Clone)]
pub struct RenderResult {
    /// The rendered markdown content.
    pub content: String,
    /// Accumulated footnotes for end-of-document rendering.
    pub footnotes: Vec<Footnote>,
}

/// Footnote collected during rendering.
#[derive(Debug, Clone)]
pub struct Footnote {
    /// Footnote number (1-based).
    pub number: usize,
    /// The collected text content.
    pub content: String,
}

/// Tracks list context for numbering.
#[derive(Debug, Clone)]
struct ListContext {
    /// Whether this is an ordered list.
    is_ordered: bool,
    /// Current item counter.
    counter: usize,
    /// Whether this is a tight list (no blank lines between items).
    is_tight: bool,
}

/// Context for rendering (pure string accumulation, no I/O).
pub struct RenderContext<'a> {
    chapter: &'a Chapter,
    chapter_id: ChapterId,
    resolved: &'a ResolvedLinks,
    heading_slugs: &'a HashMap<GlobalNodeId, String>,
    // Accumulated output
    output: String,
    footnotes: Vec<Footnote>,
    // Formatting state
    line_prefix: String,
    list_stack: Vec<ListContext>,
    at_line_start: bool,
    has_line_content: bool,
    pending_newline: bool,
    last_block_role: Option<Role>,
    // Current recursion depth, to bound stack usage on hostile trees.
    depth: usize,
    // Number of footnotes emitted by earlier chapters, so labels stay unique
    // once chapters are concatenated into one markdown document.
    footnote_start: usize,
}

impl<'a> RenderContext<'a> {
    /// Create a new render context for a chapter.
    pub fn new(
        chapter: &'a Chapter,
        chapter_id: ChapterId,
        resolved: &'a ResolvedLinks,
        heading_slugs: &'a HashMap<GlobalNodeId, String>,
        footnote_start: usize,
    ) -> Self {
        Self {
            chapter,
            chapter_id,
            resolved,
            heading_slugs,
            output: String::new(),
            footnotes: Vec::new(),
            line_prefix: String::new(),
            list_stack: Vec::new(),
            at_line_start: true,
            has_line_content: false,
            pending_newline: false,
            last_block_role: None,
            depth: 0,
            footnote_start,
        }
    }

    /// Render the chapter, consuming the context and returning the result.
    pub fn render(mut self) -> RenderResult {
        // Walk children of root
        for child_id in self.chapter.children(NodeId::ROOT) {
            self.walk_node(child_id);
        }

        // Ensure final newline
        if !self.at_line_start {
            self.output.push('\n');
        }

        RenderResult {
            content: self.output,
            footnotes: self.footnotes,
        }
    }

    /// Check if a list is "tight" (items are single paragraphs, no blank lines between).
    fn is_tight_list(&self, list_id: NodeId) -> bool {
        for item_id in self.chapter.children(list_id) {
            let Some(item) = self.chapter.node(item_id) else {
                continue;
            };
            if item.role != Role::ListItem {
                continue;
            }

            // Count block-level children
            let mut block_count = 0;
            for child_id in self.chapter.children(item_id) {
                let Some(child) = self.chapter.node(child_id) else {
                    continue;
                };
                match child.role {
                    Role::Paragraph => {
                        block_count += 1;
                    }
                    Role::BlockQuote
                    | Role::OrderedList
                    | Role::UnorderedList
                    | Role::DefinitionList
                    | Role::CodeBlock
                    | Role::Table
                    | Role::Figure => {
                        // Nested block elements make it loose
                        return false;
                    }
                    _ => {}
                }
            }

            // More than one block-level child means loose
            if block_count > 1 {
                return false;
            }
        }
        true
    }

    /// Write an HTML anchor if this node is targeted by internal links.
    fn write_anchor_if_targeted(&mut self, node_id: NodeId) {
        let global_id = GlobalNodeId::new(self.chapter_id, node_id);
        if self.resolved.is_internal_target(global_id) {
            // Skip headings - they get automatic slug-based IDs
            if let Some(node) = self.chapter.node(node_id)
                && matches!(node.role, Role::Heading(_))
            {
                return;
            }
            self.ensure_line_started();
            self.output.push_str(&format!(
                "<a id=\"c{}n{}\"></a>",
                self.chapter_id.0, node_id.0
            ));
        }
    }

    /// Ensure we're ready to write content (write prefix if at line start).
    fn ensure_line_started(&mut self) {
        if self.at_line_start {
            self.output.push_str(&self.line_prefix);
            self.at_line_start = false;
        }
    }

    /// Write a newline.
    fn write_newline(&mut self) {
        self.output.push('\n');
        self.at_line_start = true;
        self.has_line_content = false;
    }

    /// Write a hard line break (backslash in markdown).
    fn write_hard_break(&mut self) {
        self.output.push('\\');
        self.write_newline();
    }

    /// Start a new block element.
    fn start_block(&mut self) {
        if self.pending_newline {
            if !self.at_line_start {
                self.write_newline();
            }
            self.write_newline();
            self.pending_newline = false;
        }
        self.ensure_line_started();
    }

    /// End a block element.
    fn end_block(&mut self, role: Role) {
        self.pending_newline = true;
        self.last_block_role = Some(role);
    }

    /// Check if we need a separator between adjacent lists.
    fn needs_list_separator(&self, current_role: Role) -> bool {
        matches!(
            (self.last_block_role, current_role),
            (Some(Role::OrderedList), Role::OrderedList)
                | (Some(Role::UnorderedList), Role::UnorderedList)
                | (Some(Role::DefinitionList), Role::DefinitionList)
        )
    }

    /// Write a list separator comment (for adjacent lists).
    fn write_list_separator(&mut self) {
        if !self.at_line_start {
            self.write_newline();
        }
        self.write_newline();
        self.ensure_line_started();
        self.output.push_str("<!-- -->\n");
        self.at_line_start = true;
    }

    fn walk_node(&mut self, id: NodeId) {
        let Some(node) = self.chapter.node(id) else {
            return;
        };

        // Output anchor if this node is a link target
        self.write_anchor_if_targeted(id);

        let role = node.role;

        match role {
            Role::Text => {
                if !node.text.is_empty() {
                    let text = self.chapter.text(node.text);
                    self.write_text(text);
                }
            }

            Role::Paragraph => {
                self.start_block();
                self.walk_children(id);
                self.end_block(role);
            }

            Role::Heading(level) => {
                self.start_block();
                // Clamp defensively: CommonMark only knows ATX levels 1..=6.
                // Level 0 would emit a bare paragraph and 7+ a literal
                // `#######` run, silently demoting the heading.
                for _ in 0..level.clamp(1, 6) {
                    self.output.push('#');
                }
                self.output.push(' ');
                self.walk_children(id);
                self.end_block(role);
            }

            Role::OrderedList => {
                if self.needs_list_separator(role) {
                    self.write_list_separator();
                }
                self.start_block();
                let start = self.chapter.semantics.list_start(id).unwrap_or(1) as usize;
                let is_tight = self.is_tight_list(id);
                self.list_stack.push(ListContext {
                    is_ordered: true,
                    counter: start.saturating_sub(1),
                    is_tight,
                });
                self.walk_children(id);
                self.list_stack.pop();
                self.end_block(role);
            }

            Role::UnorderedList => {
                if self.needs_list_separator(role) {
                    self.write_list_separator();
                }
                self.start_block();
                let is_tight = self.is_tight_list(id);
                self.list_stack.push(ListContext {
                    is_ordered: false,
                    counter: 0,
                    is_tight,
                });
                self.walk_children(id);
                self.list_stack.pop();
                self.end_block(role);
            }

            Role::ListItem => {
                let (is_tight, counter) = self
                    .list_stack
                    .last()
                    .map(|ctx| (ctx.is_tight, ctx.counter))
                    .unwrap_or((true, 0));

                // For loose lists, add blank line before items (except the first)
                if !is_tight && counter > 0 {
                    if !self.at_line_start {
                        self.write_newline();
                    }
                    self.write_newline();
                } else if !self.at_line_start {
                    self.write_newline();
                }

                self.ensure_line_started();

                // Get bullet/number from parent list
                let bullet = if let Some(list_ctx) = self.list_stack.last_mut() {
                    list_ctx.counter += 1;
                    if list_ctx.is_ordered {
                        format!("{}. ", list_ctx.counter)
                    } else {
                        "- ".to_string()
                    }
                } else {
                    String::new()
                };

                self.output.push_str(&bullet);

                // Set continuation indent for subsequent lines
                let old_prefix = self.line_prefix.clone();
                self.line_prefix.push_str(&" ".repeat(bullet.len()));

                self.walk_children(id);

                self.line_prefix = old_prefix;
                self.pending_newline = false;
            }

            Role::BlockQuote => {
                if self.pending_newline {
                    if !self.at_line_start {
                        self.write_newline();
                    }
                    self.write_newline();
                    self.pending_newline = false;
                }

                let prefix = "> ";

                if !self.at_line_start {
                    self.output.push_str(prefix);
                }

                let old_prefix = self.line_prefix.clone();
                self.line_prefix.push_str(prefix);

                self.walk_children(id);

                self.line_prefix = old_prefix;
                self.end_block(role);
            }

            Role::Link => {
                self.ensure_line_started();

                // Look up resolved target
                let global_id = GlobalNodeId::new(self.chapter_id, id);
                let anchor = match self.resolved.get(global_id) {
                    Some(AnchorTarget::External(url)) => url.clone(),
                    Some(AnchorTarget::Internal(target)) => {
                        if let Some(slug) = self.heading_slugs.get(target) {
                            format!("#{}", slug)
                        } else {
                            format!("#c{}n{}", target.chapter.0, target.node.0)
                        }
                    }
                    Some(AnchorTarget::Chapter(chapter_id)) => {
                        format!("#c{}", chapter_id.0)
                    }
                    None => self.chapter.semantics.href(id).unwrap_or("").to_string(),
                };

                // Skip link formatting if anchor is empty
                if anchor.is_empty() {
                    self.walk_children(id);
                } else {
                    self.output.push('[');
                    self.walk_children(id);
                    self.output.push_str("](");
                    self.output.push_str(&format_link_destination(&anchor));
                    self.output.push(')');
                }
            }

            Role::Image => {
                self.start_block();
                let alt = self.chapter.semantics.alt(id).unwrap_or("image");
                let src = self.chapter.semantics.src(id).unwrap_or("");
                // The alt goes inside `[...]`, so escape `]`/`[`/backslash or
                // it terminates the label early; the src goes inside `(...)`.
                self.output.push_str("![");
                self.output.push_str(&escape_link_text(alt));
                self.output.push_str("](");
                self.output.push_str(&format_link_destination(src));
                self.output.push(')');
                self.end_block(role);
            }

            Role::Break => {
                self.write_hard_break();
            }

            Role::Rule => {
                self.start_block();
                self.output.push_str("---");
                self.end_block(role);
            }

            Role::Table => {
                self.start_block();
                // The optimizer (normalize_table_structure) wraps rows in
                // TableHead/TableBody, so descend one level into section
                // wrappers when collecting rows. Header rows are emitted
                // first regardless of section order in the tree.
                let mut header_rows: Vec<NodeId> = Vec::new();
                let mut body_rows: Vec<NodeId> = Vec::new();
                for child_id in self.chapter.children(id) {
                    let Some(child) = self.chapter.node(child_id) else {
                        continue;
                    };
                    match child.role {
                        Role::TableHead => {
                            header_rows.extend(self.chapter.children(child_id));
                        }
                        Role::TableBody => {
                            body_rows.extend(self.chapter.children(child_id));
                        }
                        Role::TableRow => body_rows.push(child_id),
                        _ => {}
                    }
                }
                // GFM requires a delimiter row after the header so parsers
                // recognize the block as a table rather than plain text.
                // Rows from TableHead are the header; without a head, the
                // first row serves as the header.
                let header_count = if header_rows.is_empty() {
                    1
                } else {
                    header_rows.len()
                };
                let rows: Vec<NodeId> = header_rows.into_iter().chain(body_rows).collect();
                for (i, row_id) in rows.iter().enumerate() {
                    let cells = self.table_cells(*row_id);
                    self.ensure_line_started();
                    self.output.push_str("| ");
                    self.output.push_str(&cells.join(" | "));
                    self.output.push_str(" |");
                    self.write_newline();
                    if i + 1 == header_count {
                        self.ensure_line_started();
                        self.output.push('|');
                        for _ in 0..cells.len().max(1) {
                            self.output.push_str(" --- |");
                        }
                        self.write_newline();
                    }
                }
                self.end_block(role);
            }

            Role::TableRow => {
                // Reached only for a stray row outside a Table; render the cells
                // as a GFM row (escaped) without a delimiter.
                let cells = self.table_cells(id);
                self.ensure_line_started();
                self.output.push_str("| ");
                self.output.push_str(&cells.join(" | "));
                self.output.push_str(" |");
                self.write_newline();
            }

            Role::TableCell => {
                self.walk_children(id);
            }

            Role::Figure => {
                self.start_block();
                self.walk_children(id);
                self.end_block(role);
            }

            Role::Caption => {
                self.start_block();
                self.output.push('*');
                self.walk_children(id);
                self.output.push('*');
                self.end_block(role);
            }

            Role::Footnote => {
                self.ensure_line_started();
                // Escape like body text: the content is emitted verbatim after
                // `[^n]:`, so unescaped `*`/`` ` ``/`[` would be interpreted as
                // markdown. Newlines are flattened since the definition is one
                // line.
                let text = escape_markdown_at(&self.collect_text(id), false).replace('\n', " ");
                let note_num = self.footnote_start + self.footnotes.len() + 1;
                self.footnotes.push(Footnote {
                    number: note_num,
                    content: text,
                });
                self.output.push_str(&format!("[^{}]", note_num));
            }

            Role::Sidebar => {
                self.start_block();
                self.output.push_str("> **Sidebar**");
                self.write_newline();
                self.ensure_line_started();
                self.walk_children(id);
                self.end_block(role);
            }

            Role::Inline => {
                let style = self.chapter.styles.get(node.style);
                let is_bold = style.map(|s| s.is_bold()).unwrap_or(false);
                let is_italic = style.map(|s| s.is_italic()).unwrap_or(false);
                let is_code = style.map(|s| s.is_monospace()).unwrap_or(false);
                let is_block = node.style.0 != 0
                    && style.map(|s| s.display == Display::Block).unwrap_or(false);

                // Handle block-display inlines (e.g., verse lines)
                if is_block && self.has_line_content {
                    self.write_hard_break();
                }

                if is_code {
                    self.ensure_line_started();
                    let content = self.collect_text(id);
                    let tick_count = calculate_inline_code_ticks(&content);
                    let ticks: String = std::iter::repeat_n('`', tick_count).collect();

                    let spacer = if content.starts_with('`') || content.ends_with('`') {
                        " "
                    } else {
                        ""
                    };

                    self.output.push_str(&format!(
                        "{}{}{}{}{}",
                        ticks, spacer, content, spacer, ticks
                    ));
                } else {
                    self.ensure_line_started();
                    if is_bold {
                        self.output.push_str("**");
                    }
                    if is_italic {
                        self.output.push('*');
                    }

                    self.walk_children(id);

                    if is_italic {
                        self.output.push('*');
                    }
                    if is_bold {
                        self.output.push_str("**");
                    }
                }
            }

            Role::DefinitionList => {
                if self.needs_list_separator(role) {
                    self.write_list_separator();
                }
                self.start_block();
                self.walk_children(id);
                self.end_block(role);
            }

            Role::DefinitionTerm => {
                self.start_block();
                self.output.push_str("**");
                self.walk_children(id);
                self.output.push_str("**");
                self.pending_newline = false;
            }

            Role::DefinitionDescription => {
                if !self.at_line_start {
                    self.write_newline();
                }
                self.ensure_line_started();
                self.output.push_str(": ");
                self.walk_children(id);
                self.end_block(role);
            }

            Role::CodeBlock => {
                self.start_block();
                let text = self.collect_text_verbatim(id);

                if !self.at_line_start {
                    self.write_newline();
                }
                let lang = self.chapter.semantics.language(id).unwrap_or("");
                let fence_len = calculate_fence_length(&text, '`');
                let fence: String = std::iter::repeat_n('`', fence_len).collect();
                self.ensure_line_started();
                self.output.push_str(&format!("{}{}\n", fence, lang));
                self.at_line_start = true;

                for line in text.lines() {
                    self.ensure_line_started();
                    self.output.push_str(&format!("{}\n", line));
                    self.at_line_start = true;
                }

                self.ensure_line_started();
                self.output.push_str(&fence);
                self.end_block(role);
            }

            Role::Container | Role::Root | Role::TableHead | Role::TableBody => {
                self.walk_children(id);
            }
        }
    }

    /// Collect the cell texts of a table row, escaped for a GFM table cell:
    /// full markdown escaping (so `_italic_`, `` `code` ``, `<tag>`, `#` are
    /// all handled like body text) plus the table-specific pipe escape and
    /// newline flattening.
    fn table_cells(&mut self, row_id: NodeId) -> Vec<String> {
        let cell_ids: Vec<NodeId> = self.chapter.children(row_id).collect();
        cell_ids
            .into_iter()
            .map(|cell| {
                // escape_markdown_at already escapes `|`; only the newline
                // flatten is table-specific.
                escape_markdown_at(&self.collect_text(cell), false).replace('\n', " ")
            })
            .collect()
    }

    fn walk_children(&mut self, id: NodeId) {
        // Bound recursion depth: a hostile chapter can nest arbitrarily deep.
        // All descent flows through here, so guarding this one site suffices.
        if self.depth > crate::util::MAX_TREE_DEPTH {
            return;
        }
        self.depth += 1;
        for child_id in self.chapter.children(id) {
            self.walk_node(child_id);
        }
        self.depth -= 1;
    }

    fn write_text(&mut self, text: &str) {
        // Whether this chunk lands at a block-start position: no inline
        // content has been written on the current line yet (only the line
        // prefix and/or block markers like `- ` or `> `, after which
        // markdown still recognizes list/heading markers). Line-start-only
        // characters are escaped only in that position.
        let at_line_start = !self.has_line_content;
        self.ensure_line_started();

        let text = strip_ebook_chars(text);

        // Normalize internal whitespace while preserving leading/trailing
        let has_leading = text.starts_with(char::is_whitespace);
        let has_trailing = text.ends_with(char::is_whitespace);

        let words: Vec<&str> = text.split_whitespace().collect();
        if words.is_empty() {
            if !text.is_empty() {
                self.output.push(' ');
            }
            return;
        }

        if has_leading {
            self.output.push(' ');
        }

        let joined = words.join(" ");
        let escaped = escape_markdown_at(&joined, at_line_start);
        self.output.push_str(&escaped);
        self.has_line_content = true;

        if has_trailing {
            self.output.push(' ');
        }
    }

    /// Collect all text content from a node and its children.
    fn collect_text(&self, id: NodeId) -> String {
        self.collect_text_inner(id, false)
    }

    /// Collect text content preserving literal whitespace (for code blocks).
    fn collect_text_verbatim(&self, id: NodeId) -> String {
        self.collect_text_inner(id, true)
    }

    fn collect_text_inner(&self, id: NodeId, verbatim: bool) -> String {
        let mut result = String::new();
        self.collect_text_recursive(id, &mut result, verbatim);
        strip_ebook_chars(&result).into_owned()
    }

    fn collect_text_recursive(&self, id: NodeId, result: &mut String, verbatim: bool) {
        let Some(node) = self.chapter.node(id) else {
            return;
        };

        if node.role == Role::Text && !node.text.is_empty() {
            let text = self.chapter.text(node.text);

            if verbatim {
                result.push_str(text);
            } else {
                let has_leading = text.starts_with(char::is_whitespace);
                let has_trailing = text.ends_with(char::is_whitespace);
                let words: Vec<&str> = text.split_whitespace().collect();

                if !words.is_empty() {
                    if has_leading && !result.is_empty() && !result.ends_with(' ') {
                        result.push(' ');
                    }
                    result.push_str(&words.join(" "));
                    if has_trailing {
                        result.push(' ');
                    }
                } else if !text.is_empty() && !result.is_empty() && !result.ends_with(' ') {
                    result.push(' ');
                }
            }
        }

        for child_id in self.chapter.children(id) {
            self.collect_text_recursive(child_id, result, verbatim);
        }
    }
}

/// Escape text destined for a link/image label (`[...]`): the brackets and
/// backslash would otherwise terminate the label early.
fn escape_link_text(text: &str) -> String {
    let mut out = String::with_capacity(text.len());
    for c in text.chars() {
        if matches!(c, '[' | ']' | '\\') {
            out.push('\\');
        }
        if c == '\n' {
            out.push(' ');
        } else {
            out.push(c);
        }
    }
    out
}

/// Format a link destination for `(...)`. CommonMark allows an unwrapped
/// destination only when it has no spaces/control chars and balanced parens;
/// anything else must be `<...>`-wrapped (with `<`, `>`, and `\` escaped
/// inside the wrap).
fn format_link_destination(dest: &str) -> String {
    let needs_wrap =
        dest.chars().any(|c| c.is_whitespace() || c.is_control()) || !parens_balanced(dest);
    if !needs_wrap {
        return dest.to_string();
    }
    let mut out = String::with_capacity(dest.len() + 2);
    out.push('<');
    for c in dest.chars() {
        match c {
            '<' | '>' | '\\' => {
                out.push('\\');
                out.push(c);
            }
            // A literal newline can't appear inside <...>; drop it.
            '\n' | '\r' => {}
            _ => out.push(c),
        }
    }
    out.push('>');
    out
}

fn parens_balanced(s: &str) -> bool {
    let mut depth: i32 = 0;
    for c in s.chars() {
        match c {
            '(' => depth += 1,
            ')' => {
                depth -= 1;
                if depth < 0 {
                    return false;
                }
            }
            _ => {}
        }
    }
    depth == 0
}

/// Render a single chapter to markdown.
///
/// This is the main entry point for chapter rendering. It creates a
/// `RenderContext`, processes all nodes, and returns the result.
///
/// # Arguments
///
/// * `chapter` - The chapter to render
/// * `chapter_id` - The chapter's ID for building GlobalNodeIds
/// * `resolved` - Resolved links for internal link output
/// * `heading_slugs` - Map of heading targets to slugs
///
/// # Returns
///
/// A `RenderResult` containing the rendered markdown and any footnotes.
pub fn render_chapter(
    chapter: &Chapter,
    chapter_id: ChapterId,
    resolved: &ResolvedLinks,
    heading_slugs: &HashMap<GlobalNodeId, String>,
    footnote_start: usize,
) -> RenderResult {
    let ctx = RenderContext::new(chapter, chapter_id, resolved, heading_slugs, footnote_start);
    ctx.render()
}

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

    fn render_to_string(chapter: &Chapter) -> String {
        let resolved = ResolvedLinks::default();
        let heading_slugs = HashMap::new();
        let result = render_chapter(chapter, ChapterId(0), &resolved, &heading_slugs, 0);
        result.content
    }

    #[test]
    fn image_alt_and_src_are_escaped() {
        let mut chapter = Chapter::new();
        let img = chapter.alloc_node(Node::new(Role::Image));
        chapter.append_child(NodeId::ROOT, img);
        chapter.semantics.set_alt(img, "a [bracket] tag");
        chapter.semantics.set_src(img, "images/a b(1).png");

        let out = render_to_string(&chapter);
        // `]` in the alt must be escaped; the spacey/paren src must be wrapped.
        assert!(out.contains(r"![a \[bracket\] tag]"), "{out}");
        assert!(out.contains("(<images/a b(1).png>)"), "{out}");
    }

    #[test]
    fn footnote_content_is_escaped() {
        let mut chapter = Chapter::new();
        let note = chapter.alloc_node(Node::new(Role::Footnote));
        chapter.append_child(NodeId::ROOT, note);
        let t = chapter.append_text("see *ibid* [1]");
        let tn = chapter.alloc_node(Node::text(t));
        chapter.append_child(note, tn);

        let resolved = ResolvedLinks::default();
        let slugs = HashMap::new();
        let result = render_chapter(&chapter, ChapterId(0), &resolved, &slugs, 0);
        assert_eq!(result.footnotes.len(), 1);
        assert_eq!(result.footnotes[0].content, r"see \*ibid\* \[1\]");
    }

    #[test]
    fn format_link_destination_wraps_spaces_and_unbalanced_parens() {
        assert_eq!(format_link_destination("simple.png"), "simple.png");
        assert_eq!(format_link_destination("a b.png"), "<a b.png>");
        assert_eq!(format_link_destination("bad)paren"), "<bad)paren>");
        // Balanced parens are legal bare.
        assert_eq!(format_link_destination("ok(1).png"), "ok(1).png");
    }

    #[test]
    fn test_simple_paragraph() {
        let mut chapter = Chapter::new();

        let para = chapter.alloc_node(Node::new(Role::Paragraph));
        chapter.append_child(NodeId::ROOT, para);

        let text_range = chapter.append_text("Hello, World!");
        let text_node = chapter.alloc_node(Node::text(text_range));
        chapter.append_child(para, text_node);

        let result = render_to_string(&chapter);
        assert!(result.contains("Hello, World!"));
    }

    #[test]
    fn test_heading() {
        let mut chapter = Chapter::new();

        let h1 = chapter.alloc_node(Node::new(Role::Heading(1)));
        chapter.append_child(NodeId::ROOT, h1);

        let text_range = chapter.append_text("Chapter One");
        let text_node = chapter.alloc_node(Node::text(text_range));
        chapter.append_child(h1, text_node);

        let result = render_to_string(&chapter);
        assert!(result.contains("# Chapter One"));
    }

    #[test]
    fn test_unordered_list() {
        let mut chapter = Chapter::new();

        let ul = chapter.alloc_node(Node::new(Role::UnorderedList));
        chapter.append_child(NodeId::ROOT, ul);

        let li = chapter.alloc_node(Node::new(Role::ListItem));
        chapter.append_child(ul, li);

        let text_range = chapter.append_text("Item one");
        let text_node = chapter.alloc_node(Node::text(text_range));
        chapter.append_child(li, text_node);

        let result = render_to_string(&chapter);
        assert!(result.contains("- Item one"));
    }

    #[test]
    fn test_ordered_list() {
        let mut chapter = Chapter::new();

        let ol = chapter.alloc_node(Node::new(Role::OrderedList));
        chapter.append_child(NodeId::ROOT, ol);

        for i in 1..=3 {
            let li = chapter.alloc_node(Node::new(Role::ListItem));
            chapter.append_child(ol, li);

            let text_range = chapter.append_text(&format!("Item {}", i));
            let text_node = chapter.alloc_node(Node::text(text_range));
            chapter.append_child(li, text_node);
        }

        let result = render_to_string(&chapter);
        assert!(result.contains("1. Item 1"));
        assert!(result.contains("2. Item 2"));
        assert!(result.contains("3. Item 3"));
    }

    #[test]
    fn test_footnote_accumulation() {
        let mut chapter = Chapter::new();

        let p = chapter.alloc_node(Node::new(Role::Paragraph));
        chapter.append_child(NodeId::ROOT, p);

        let t1 = chapter.append_text("Main text");
        let tn1 = chapter.alloc_node(Node::text(t1));
        chapter.append_child(p, tn1);

        let note = chapter.alloc_node(Node::new(Role::Footnote));
        chapter.append_child(p, note);
        let t2 = chapter.append_text("This is a footnote");
        let tn2 = chapter.alloc_node(Node::text(t2));
        chapter.append_child(note, tn2);

        let resolved = ResolvedLinks::default();
        let heading_slugs = HashMap::new();
        let result = render_chapter(&chapter, ChapterId(0), &resolved, &heading_slugs, 0);

        assert!(result.content.contains("[^1]"));
        assert_eq!(result.footnotes.len(), 1);
        assert_eq!(result.footnotes[0].number, 1);
        assert_eq!(result.footnotes[0].content, "This is a footnote");
    }

    #[test]
    fn footnote_numbering_honors_start_offset() {
        let mut chapter = Chapter::new();
        let p = chapter.alloc_node(Node::new(Role::Paragraph));
        chapter.append_child(NodeId::ROOT, p);
        let note = chapter.alloc_node(Node::new(Role::Footnote));
        chapter.append_child(p, note);
        let t = chapter.append_text("note");
        let tn = chapter.alloc_node(Node::text(t));
        chapter.append_child(note, tn);

        let resolved = ResolvedLinks::default();
        let heading_slugs = HashMap::new();
        // Simulate two earlier chapters' footnotes: this one starts at 3.
        let result = render_chapter(&chapter, ChapterId(0), &resolved, &heading_slugs, 2);
        assert!(result.content.contains("[^3]"));
        assert_eq!(result.footnotes[0].number, 3);
    }

    #[test]
    fn table_emits_gfm_delimiter_and_escapes_pipes() {
        let mut chapter = Chapter::new();
        let table = chapter.alloc_node(Node::new(Role::Table));
        chapter.append_child(NodeId::ROOT, table);
        for cells in [["A", "B|C"], ["1", "2"]] {
            let row = chapter.alloc_node(Node::new(Role::TableRow));
            chapter.append_child(table, row);
            for cell in cells {
                let c = chapter.alloc_node(Node::new(Role::TableCell));
                chapter.append_child(row, c);
                let t = chapter.append_text(cell);
                let tn = chapter.alloc_node(Node::text(t));
                chapter.append_child(c, tn);
            }
        }

        let resolved = ResolvedLinks::default();
        let heading_slugs = HashMap::new();
        let out = render_chapter(&chapter, ChapterId(0), &resolved, &heading_slugs, 0).content;
        assert!(
            out.contains("| A | B\\|C |"),
            "header + escaped pipe: {out}"
        );
        assert!(out.contains("| --- | --- |"), "delimiter row: {out}");
        assert!(out.contains("| 1 | 2 |"), "body row: {out}");
    }

    #[test]
    fn table_with_head_body_wrappers_renders_rows() {
        // The optimizer (normalize_table_structure) wraps rows in
        // TableHead/TableBody; the renderer must descend into the wrappers
        // rather than treating them as rows.
        let mut chapter = Chapter::new();
        let table = chapter.alloc_node(Node::new(Role::Table));
        chapter.append_child(NodeId::ROOT, table);

        let head = chapter.alloc_node(Node::new(Role::TableHead));
        chapter.append_child(table, head);
        let body = chapter.alloc_node(Node::new(Role::TableBody));
        chapter.append_child(table, body);

        for (section, cells) in [(head, ["Sample", "Voltage"]), (body, ["A", "1.5"])] {
            let row = chapter.alloc_node(Node::new(Role::TableRow));
            chapter.append_child(section, row);
            for cell in cells {
                let c = chapter.alloc_node(Node::new(Role::TableCell));
                chapter.append_child(row, c);
                let t = chapter.append_text(cell);
                let tn = chapter.alloc_node(Node::text(t));
                chapter.append_child(c, tn);
            }
        }

        let out = render_to_string(&chapter);
        let header = out.find("| Sample | Voltage |").expect("header row");
        let delim = out.find("| --- | --- |").expect("delimiter row");
        let body_row = out.find("| A | 1.5 |").expect("body row");
        assert!(header < delim && delim < body_row, "row order: {out}");
    }

    #[test]
    fn table_cells_escape_inline_markers() {
        let mut chapter = Chapter::new();
        let table = chapter.alloc_node(Node::new(Role::Table));
        chapter.append_child(NodeId::ROOT, table);
        let row = chapter.alloc_node(Node::new(Role::TableRow));
        chapter.append_child(table, row);
        for cell in ["*star*", "[link]"] {
            let c = chapter.alloc_node(Node::new(Role::TableCell));
            chapter.append_child(row, c);
            let t = chapter.append_text(cell);
            let tn = chapter.alloc_node(Node::text(t));
            chapter.append_child(c, tn);
        }

        let out = render_to_string(&chapter);
        assert!(out.contains("\\*star\\*"), "star escaped: {out}");
        // Both brackets are now escaped (full body escaping), which is safe.
        assert!(out.contains("\\[link\\]"), "bracket escaped: {out}");
    }

    #[test]
    fn paragraph_text_resembling_list_markers_is_escaped() {
        for (text, escaped) in [
            ("- not a list", "\\- not a list"),
            ("1. not a list", "1\\. not a list"),
        ] {
            let mut chapter = Chapter::new();
            let p = chapter.alloc_node(Node::new(Role::Paragraph));
            chapter.append_child(NodeId::ROOT, p);
            let t = chapter.append_text(text);
            let tn = chapter.alloc_node(Node::text(t));
            chapter.append_child(p, tn);

            let out = render_to_string(&chapter);
            assert!(out.contains(escaped), "expected {escaped:?} in {out:?}");
        }
    }

    #[test]
    fn mid_line_dash_not_escaped_across_chunks() {
        // Two text nodes on one line: the second begins mid-line, so its
        // leading dash must not be treated as a list marker.
        let mut chapter = Chapter::new();
        let p = chapter.alloc_node(Node::new(Role::Paragraph));
        chapter.append_child(NodeId::ROOT, p);
        for text in ["seven ", "- eight"] {
            let t = chapter.append_text(text);
            let tn = chapter.alloc_node(Node::text(t));
            chapter.append_child(p, tn);
        }

        let out = render_to_string(&chapter);
        assert!(out.contains("seven - eight"), "no escape mid-line: {out}");
    }

    #[test]
    fn test_markdown_escaping() {
        let mut chapter = Chapter::new();

        let p = chapter.alloc_node(Node::new(Role::Paragraph));
        chapter.append_child(NodeId::ROOT, p);

        let text_range = chapter.append_text("*bold* and _italic_");
        let text_node = chapter.alloc_node(Node::text(text_range));
        chapter.append_child(p, text_node);

        let result = render_to_string(&chapter);
        assert!(result.contains("\\*bold\\*"));
        assert!(result.contains("\\_italic\\_"));
    }
}