surf-parse 0.10.0

Parser for the SurfDoc format — typed document format with block directives, Markdown-compatible
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
//! Citation & bibliography engine (CSL-style, deterministic).
//!
//! This module is the load-bearing deliverable of Chunk 4: a *pure*,
//! deterministic formatting engine that produces correct in-text citations and
//! reference-list entries for the academic styles carried by [`Format`]
//! (MLA, APA 7, Chicago author-date, IEEE, plus ACM ≈ IEEE numbering and the
//! generic `article` paper style ≈ APA author-date).
//!
//! The data model ([`Reference`], [`Author`], [`RefType`]) is consumed both by
//! the `::cite` block (a stored definition) and by Chunk 6 (paper/report
//! rendering). The formatting functions ([`format_in_text`],
//! [`format_reference`], [`reference_list`], [`reference_list_keyed`]) are pure:
//! same input → byte-identical output, no time / randomness / hashmap-iteration.
//!
//! Inline citations (`[@key]`, `[@key, p. 12]`, `[@key1; @key2]`) are scanned by
//! [`crate::inline::find_inline_cites`] into [`CiteRef`] values, resolved against
//! a [`CiteContext`] (the active style + the document's references + IEEE
//! numbering). Renderers install a context via [`install_context`] and read it
//! through [`with_active`] / [`substitute_text_cites`].

use std::collections::{BTreeMap, BTreeSet};
use std::cell::RefCell;

use serde::{Deserialize, Serialize};

use crate::types::{Block, Format};

// ───────────────────────────────────────────────────────────────────────────
// Data model
// ───────────────────────────────────────────────────────────────────────────

/// A single author/editor name, split into family (surname) + optional given.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
pub struct Author {
    /// Surname / family name (e.g. "Smith").
    pub family: String,
    /// Given name(s) as authored (e.g. "John", "Mary Jane"). `None` for
    /// mononyms / organisations.
    pub given: Option<String>,
}

/// Source category for a [`Reference`]; selects per-style entry shape.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "lowercase")]
pub enum RefType {
    /// Journal / periodical article (default).
    #[default]
    Article,
    /// Whole book.
    Book,
    /// Chapter in an edited book.
    Chapter,
    /// Web page / online resource.
    Web,
    /// Technical report / white paper.
    Report,
    /// Conference / proceedings paper.
    Conference,
}

impl RefType {
    /// Parse a `type=` value (case-insensitive). Unknown → `Article`.
    pub fn from_str_lossy(s: &str) -> RefType {
        match s.trim().to_ascii_lowercase().as_str() {
            "book" => RefType::Book,
            "chapter" | "incollection" | "bookchapter" => RefType::Chapter,
            "web" | "website" | "online" | "webpage" => RefType::Web,
            "report" | "techreport" | "whitepaper" => RefType::Report,
            "conference" | "proceedings" | "inproceedings" | "paper" => RefType::Conference,
            _ => RefType::Article,
        }
    }
}

/// A bibliographic reference. Optional fields use `Option`; the `key` is the
/// stable id used by inline `[@key]` citations and bibliography anchors.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
pub struct Reference {
    /// Citation key / id (e.g. `smith2020`).
    pub key: String,
    /// Source category (article/book/web/…).
    pub ref_type: RefType,
    /// Authors in listed order.
    pub authors: Vec<Author>,
    /// Editors (for edited books / chapters).
    pub editors: Vec<Author>,
    /// Title of the work.
    pub title: Option<String>,
    /// Container: journal / book / website / proceedings name.
    pub container: Option<String>,
    /// Publisher (books / reports).
    pub publisher: Option<String>,
    /// Year of publication (kept as a string to preserve `n.d.`/`2020a`).
    pub year: Option<String>,
    /// Volume number.
    pub volume: Option<String>,
    /// Issue number.
    pub issue: Option<String>,
    /// Page range (e.g. `45-67`).
    pub pages: Option<String>,
    /// URL.
    pub url: Option<String>,
    /// DOI (without the `https://doi.org/` prefix).
    pub doi: Option<String>,
    /// Access date (ISO `YYYY-MM-DD`) for web sources.
    pub accessed: Option<String>,
    /// Edition (e.g. `2nd`).
    pub edition: Option<String>,
}

// ───────────────────────────────────────────────────────────────────────────
// Inline citation references (produced by inline.rs)
// ───────────────────────────────────────────────────────────────────────────

/// One key inside an inline citation, with an optional locator (`p. 12`).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CiteItem {
    /// Reference key (matches [`Reference::key`]).
    pub key: String,
    /// Locator text exactly as authored (e.g. `p. 12`, `pp. 3-4`, `sec. 2`).
    pub locator: Option<String>,
}

/// A single inline citation group: `[@a]` is one item, `[@a; @b]` is two.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CiteRef {
    /// One or more cited items, in authored order.
    pub items: Vec<CiteItem>,
}

// ───────────────────────────────────────────────────────────────────────────
// Author parsing helpers
// ───────────────────────────────────────────────────────────────────────────

fn opt(s: &str) -> Option<String> {
    let t = s.trim();
    if t.is_empty() {
        None
    } else {
        Some(t.to_string())
    }
}

/// Parse a single author. Accepts `Last, First` (preferred) or `First Last`.
/// A single token becomes a family-only name (mononym / organisation).
pub fn parse_author(s: &str) -> Author {
    let s = s.trim();
    if let Some((last, first)) = s.split_once(',') {
        Author {
            family: last.trim().to_string(),
            given: opt(first),
        }
    } else if let Some(idx) = s.rfind(' ') {
        let (given, family) = s.split_at(idx);
        Author {
            family: family.trim().to_string(),
            given: opt(given),
        }
    } else {
        Author {
            family: s.to_string(),
            given: None,
        }
    }
}

/// Parse a `;`-separated author list (`Last, First; Last2, First2`).
pub fn parse_authors(s: &str) -> Vec<Author> {
    s.split(';')
        .map(parse_author)
        .filter(|a| !a.family.is_empty())
        .collect()
}

/// Initials from a given name: `John` → `J.`, `Mary Jane` → `M. J.`.
fn initials(given: &Option<String>) -> String {
    match given {
        None => String::new(),
        Some(g) => g
            .split_whitespace()
            .filter_map(|w| w.chars().next())
            .map(|c| format!("{}.", c.to_uppercase()))
            .collect::<Vec<_>>()
            .join(" "),
    }
}

/// `Family` (no given) or `Family, Given` (inverted form).
fn inverted(a: &Author) -> String {
    match &a.given {
        Some(g) => format!("{}, {}", a.family, g),
        None => a.family.clone(),
    }
}

/// `Family` (no given) or `Given Family` (natural form).
fn natural(a: &Author) -> String {
    match &a.given {
        Some(g) => format!("{} {}", g, a.family),
        None => a.family.clone(),
    }
}

/// Strip a leading page-locator prefix (`p.`/`pp.`/`page`/`pages`) so styles
/// that want a bare number in-text (MLA, Chicago) render `12` not `p. 12`.
fn strip_page_prefix(s: &str) -> String {
    let t = s.trim();
    for p in ["pp.", "pages", "page", "pg.", "p."] {
        if let Some(rest) = t.strip_prefix(p) {
            return rest.trim().to_string();
        }
    }
    t.to_string()
}

/// MLA access-date format: `2021-05-01` → `1 May 2021`. Falls back to the input
/// when it is not an ISO date.
fn fmt_date_mla(iso: &str) -> String {
    const MONTHS: [&str; 12] = [
        "January", "February", "March", "April", "May", "June", "July", "August",
        "September", "October", "November", "December",
    ];
    let parts: Vec<&str> = iso.split('-').collect();
    if parts.len() == 3 {
        if let (Ok(y), Ok(m), Ok(d)) = (
            parts[0].parse::<i32>(),
            parts[1].parse::<usize>(),
            parts[2].parse::<i32>(),
        ) {
            if (1..=12).contains(&m) {
                return format!("{} {} {}", d, MONTHS[m - 1], y);
            }
        }
        // fall through to raw on out-of-range month
    }
    iso.to_string()
}

// ───────────────────────────────────────────────────────────────────────────
// Style helpers
// ───────────────────────────────────────────────────────────────────────────

/// True for numbered styles (IEEE / ACM): in-text `[n]`, reference list in
/// citation order with `[n]` prefixes.
pub fn is_numbered(style: Format) -> bool {
    matches!(style, Format::Ieee | Format::Acm)
}

/// The bibliography section heading for a style.
pub fn bibliography_heading(style: Format) -> &'static str {
    match style {
        Format::Mla => "Works Cited",
        Format::Chicago => "Bibliography",
        // APA / IEEE / ACM / generic article
        _ => "References",
    }
}

/// The active style: the document's `format:` if set, else APA author-date.
pub fn active_style(format: Option<Format>) -> Format {
    format.unwrap_or(Format::Apa)
}

// ── in-text author label ────────────────────────────────────────────────────

/// In-text name label for author-date / author-page styles.
/// 1 → `Smith`, 2 → `Smith & Jones` (APA) / `Smith and Jones` (MLA, Chicago),
/// 3+ → `Smith et al.`.
fn intext_names(r: &Reference, style: Format) -> String {
    let fams: Vec<&str> = r.authors.iter().map(|a| a.family.as_str()).collect();
    let amp = matches!(style, Format::Apa);
    match fams.len() {
        0 => r.title.clone().unwrap_or_else(|| r.key.clone()),
        1 => fams[0].to_string(),
        2 => {
            if amp {
                format!("{} & {}", fams[0], fams[1])
            } else {
                format!("{} and {}", fams[0], fams[1])
            }
        }
        _ => format!("{} et al.", fams[0]),
    }
}

// ───────────────────────────────────────────────────────────────────────────
// In-text citation formatting
// ───────────────────────────────────────────────────────────────────────────

/// Format a complete inline citation group per `style`.
///
/// Pure. `numbers` maps reference keys → IEEE/ACM citation numbers and is only
/// consulted for numbered styles. Unknown keys degrade gracefully (the key text
/// for author styles, `[0]` for numbered styles).
///
/// Examples (APA): `[@smith2020]` → `(Smith, 2020)`,
/// `[@smith2020, p. 12]` → `(Smith, 2020, p. 12)`,
/// `[@a; @b]` → `(A, 2020; B, 2019)`. (IEEE): `[1], [2]`.
pub fn format_in_text(
    refs: &[Reference],
    cite: &CiteRef,
    style: Format,
    numbers: &BTreeMap<String, usize>,
) -> String {
    if is_numbered(style) {
        let parts: Vec<String> = cite
            .items
            .iter()
            .map(|it| {
                let n = numbers.get(&it.key).copied().unwrap_or(0);
                match &it.locator {
                    Some(loc) => format!("[{}, {}]", n, loc),
                    None => format!("[{}]", n),
                }
            })
            .collect();
        return parts.join(", ");
    }

    let find = |key: &str| refs.iter().find(|r| r.key == key);
    let parts: Vec<String> = cite
        .items
        .iter()
        .map(|it| {
            let r = find(&it.key);
            let names = r
                .map(|r| intext_names(r, style))
                .unwrap_or_else(|| it.key.clone());
            match style {
                Format::Mla => {
                    // (Author page)
                    match &it.locator {
                        Some(loc) => format!("{} {}", names, strip_page_prefix(loc)),
                        None => names,
                    }
                }
                Format::Chicago => {
                    // (Author Year, page)
                    let year = r
                        .and_then(|r| r.year.clone())
                        .unwrap_or_else(|| "n.d.".to_string());
                    match &it.locator {
                        Some(loc) => format!("{} {}, {}", names, year, strip_page_prefix(loc)),
                        None => format!("{} {}", names, year),
                    }
                }
                // APA + generic article: (Author, Year, locator)
                _ => {
                    let year = r
                        .and_then(|r| r.year.clone())
                        .unwrap_or_else(|| "n.d.".to_string());
                    match &it.locator {
                        Some(loc) => format!("{}, {}, {}", names, year, loc),
                        None => format!("{}, {}", names, year),
                    }
                }
            }
        })
        .collect();
    format!("({})", parts.join("; "))
}

// ───────────────────────────────────────────────────────────────────────────
// Reference-list entry formatting (per style)
// ───────────────────────────────────────────────────────────────────────────

// Italics use markdown `*...*`; HTML renderers convert to `<em>`, text renderers
// can keep or strip them. This keeps the pure functions renderer-agnostic.

fn join_apa(names: &[String]) -> String {
    match names.len() {
        0 => String::new(),
        1 => names[0].clone(),
        2 => format!("{}, & {}", names[0], names[1]),
        _ => {
            let (last, rest) = names.split_last().unwrap();
            format!("{}, & {}", rest.join(", "), last)
        }
    }
}

fn join_and(parts: &[String]) -> String {
    match parts.len() {
        0 => String::new(),
        1 => parts[0].clone(),
        2 => format!("{}, and {}", parts[0], parts[1]),
        _ => {
            let (last, rest) = parts.split_last().unwrap();
            format!("{}, and {}", rest.join(", "), last)
        }
    }
}

fn apa_authors(authors: &[Author]) -> String {
    let names: Vec<String> = authors
        .iter()
        .map(|a| {
            let ini = initials(&a.given);
            if ini.is_empty() {
                a.family.clone()
            } else {
                format!("{}, {}", a.family, ini)
            }
        })
        .collect();
    join_apa(&names)
}

fn mla_authors(authors: &[Author]) -> String {
    match authors.len() {
        0 => String::new(),
        1 => inverted(&authors[0]),
        2 => format!("{}, and {}", inverted(&authors[0]), natural(&authors[1])),
        _ => format!("{}, et al.", inverted(&authors[0])),
    }
}

fn chicago_authors(authors: &[Author]) -> String {
    match authors.len() {
        0 => String::new(),
        1 => inverted(&authors[0]),
        _ => {
            let mut parts = vec![inverted(&authors[0])];
            for a in &authors[1..] {
                parts.push(natural(a));
            }
            join_and(&parts)
        }
    }
}

fn ieee_authors(authors: &[Author]) -> String {
    let names: Vec<String> = authors
        .iter()
        .map(|a| {
            let ini = initials(&a.given);
            if ini.is_empty() {
                a.family.clone()
            } else {
                format!("{} {}", ini, a.family)
            }
        })
        .collect();
    match names.len() {
        0 => String::new(),
        1 => names[0].clone(),
        2 => format!("{} and {}", names[0], names[1]),
        _ => {
            let (last, rest) = names.split_last().unwrap();
            format!("{}, and {}", rest.join(", "), last)
        }
    }
}

fn apa_reference(r: &Reference) -> String {
    let authors = apa_authors(&r.authors);
    let year = r.year.clone().unwrap_or_else(|| "n.d.".to_string());
    let head = if authors.is_empty() {
        format!("({}).", year)
    } else {
        format!("{} ({}).", authors, year)
    };
    let title = r.title.clone().unwrap_or_default();
    match r.ref_type {
        RefType::Book => {
            let mut s = format!("{} *{}*.", head, title);
            if let Some(p) = &r.publisher {
                s.push_str(&format!(" {}.", p));
            }
            s
        }
        _ => {
            let mut s = head;
            if !title.is_empty() {
                s.push_str(&format!(" {}.", title));
            }
            if let Some(c) = &r.container {
                s.push_str(&format!(" *{}*", c));
                if let Some(v) = &r.volume {
                    s.push_str(&format!(", *{}*", v));
                    if let Some(i) = &r.issue {
                        s.push_str(&format!("({})", i));
                    }
                }
                if let Some(pg) = &r.pages {
                    s.push_str(&format!(", {}", pg));
                }
                s.push('.');
            }
            if let Some(d) = &r.doi {
                s.push_str(&format!(" https://doi.org/{}", d));
            } else if let Some(u) = &r.url {
                s.push_str(&format!(" {}", u));
            }
            s
        }
    }
}

fn mla_reference(r: &Reference) -> String {
    let authors = mla_authors(&r.authors);
    let mut s = String::new();
    if !authors.is_empty() {
        // Avoid a double period after "et al." (which already ends in '.').
        if authors.ends_with('.') {
            s.push_str(&format!("{} ", authors));
        } else {
            s.push_str(&format!("{}. ", authors));
        }
    }
    let title = r.title.clone().unwrap_or_default();
    match r.ref_type {
        RefType::Book => {
            s.push_str(&format!("*{}*.", title));
            if let Some(p) = &r.publisher {
                s.push_str(&format!(" {},", p));
            }
            if let Some(y) = &r.year {
                s.push_str(&format!(" {}.", y));
            }
        }
        _ => {
            if !title.is_empty() {
                s.push_str(&format!("\"{}.\" ", title));
            }
            if let Some(c) = &r.container {
                s.push_str(&format!("*{}*", c));
                let mut parts: Vec<String> = Vec::new();
                if let Some(v) = &r.volume {
                    parts.push(format!("vol. {}", v));
                }
                if let Some(i) = &r.issue {
                    parts.push(format!("no. {}", i));
                }
                if let Some(y) = &r.year {
                    parts.push(y.clone());
                }
                if let Some(pg) = &r.pages {
                    parts.push(format!("pp. {}", pg));
                }
                if matches!(r.ref_type, RefType::Web) {
                    if let Some(u) = &r.url {
                        parts.push(u.clone());
                    }
                }
                for p in parts {
                    s.push_str(&format!(", {}", p));
                }
                s.push('.');
            }
            if let Some(acc) = &r.accessed {
                s.push_str(&format!(" Accessed {}.", fmt_date_mla(acc)));
            }
        }
    }
    s
}

fn chicago_reference(r: &Reference) -> String {
    let authors = chicago_authors(&r.authors);
    let year = r.year.clone().unwrap_or_else(|| "n.d.".to_string());
    let head = if authors.is_empty() {
        format!("{}.", year)
    } else {
        format!("{}. {}.", authors, year)
    };
    let title = r.title.clone().unwrap_or_default();
    match r.ref_type {
        RefType::Book => {
            let mut s = format!("{} *{}*.", head, title);
            if let Some(p) = &r.publisher {
                s.push_str(&format!(" {}.", p));
            }
            s
        }
        _ => {
            let mut s = format!("{} \"{}.\"", head, title);
            if let Some(c) = &r.container {
                s.push_str(&format!(" *{}*", c));
                let has_vol = r.volume.is_some();
                if let Some(v) = &r.volume {
                    s.push_str(&format!(" {}", v));
                }
                if let Some(i) = &r.issue {
                    s.push_str(&format!(" ({})", i));
                }
                if let Some(pg) = &r.pages {
                    if has_vol {
                        s.push_str(&format!(": {}", pg));
                    } else {
                        s.push_str(&format!(", {}", pg));
                    }
                }
                s.push('.');
            }
            if let Some(u) = &r.url {
                s.push_str(&format!(" {}.", u));
            }
            s
        }
    }
}

fn ieee_reference(r: &Reference, number: usize) -> String {
    let prefix = format!("[{}] ", number);
    let authors = ieee_authors(&r.authors);
    let title = r.title.clone().unwrap_or_default();
    match r.ref_type {
        RefType::Book => {
            let mut s = format!("{}{}, *{}*.", prefix, authors, title);
            if let Some(p) = &r.publisher {
                s.push_str(&format!(" {},", p));
            }
            if let Some(y) = &r.year {
                s.push_str(&format!(" {}.", y));
            }
            s
        }
        RefType::Conference => {
            let mut s = format!("{}{}, \"{},\"", prefix, authors, title);
            if let Some(c) = &r.container {
                s.push_str(&format!(" in *{}*", c));
            }
            if let Some(y) = &r.year {
                s.push_str(&format!(", {}", y));
            }
            if let Some(pg) = &r.pages {
                s.push_str(&format!(", pp. {}", pg));
            }
            s.push('.');
            s
        }
        RefType::Web => {
            let mut s = format!("{}{}, \"{},\"", prefix, authors, title);
            if let Some(c) = &r.container {
                s.push_str(&format!(" *{}*", c));
            }
            if let Some(y) = &r.year {
                s.push_str(&format!(", {}", y));
            }
            s.push_str(". [Online]. Available: ");
            if let Some(u) = &r.url {
                s.push_str(u);
            } else if let Some(d) = &r.doi {
                s.push_str(&format!("https://doi.org/{}", d));
            }
            s
        }
        _ => {
            let mut s = format!("{}{}, \"{},\"", prefix, authors, title);
            if let Some(c) = &r.container {
                s.push_str(&format!(" *{}*", c));
            }
            if let Some(v) = &r.volume {
                s.push_str(&format!(", vol. {}", v));
            }
            if let Some(i) = &r.issue {
                s.push_str(&format!(", no. {}", i));
            }
            if let Some(pg) = &r.pages {
                s.push_str(&format!(", pp. {}", pg));
            }
            if let Some(y) = &r.year {
                s.push_str(&format!(", {}", y));
            }
            s.push('.');
            s
        }
    }
}

/// Format ONE reference-list entry for `style`. `number` is the IEEE/ACM entry
/// number (ignored by author styles). Italics use markdown `*...*`.
pub fn format_reference(r: &Reference, style: Format, number: Option<usize>) -> String {
    match style {
        Format::Mla => mla_reference(r),
        Format::Chicago => chicago_reference(r),
        Format::Ieee | Format::Acm => ieee_reference(r, number.unwrap_or(0)),
        // APA + generic article paper style
        _ => apa_reference(r),
    }
}

/// Sort key for author styles: (first-author family ↓case, year, title ↓case).
fn sort_key(r: &Reference) -> (String, String, String) {
    let fam = r
        .authors
        .first()
        .map(|a| a.family.to_lowercase())
        .unwrap_or_else(|| r.title.clone().unwrap_or_default().to_lowercase());
    (
        fam,
        r.year.clone().unwrap_or_default(),
        r.title.clone().unwrap_or_default().to_lowercase(),
    )
}

/// Build the full reference list as `(key, formatted_entry)` pairs in display
/// order. Numbered styles (IEEE/ACM) keep the *input order* (the caller passes
/// references in citation order) and number `1..`; author styles sort
/// alphabetically. Deterministic.
pub fn reference_list_keyed(refs: &[Reference], style: Format) -> Vec<(String, String)> {
    if is_numbered(style) {
        refs.iter()
            .enumerate()
            .map(|(i, r)| (r.key.clone(), format_reference(r, style, Some(i + 1))))
            .collect()
    } else {
        let mut sorted: Vec<&Reference> = refs.iter().collect();
        sorted.sort_by(|a, b| sort_key(a).cmp(&sort_key(b)));
        sorted
            .iter()
            .map(|r| (r.key.clone(), format_reference(r, style, None)))
            .collect()
    }
}

/// Build the full reference list as formatted strings in display order.
pub fn reference_list(refs: &[Reference], style: Format) -> Vec<String> {
    reference_list_keyed(refs, style)
        .into_iter()
        .map(|(_, s)| s)
        .collect()
}

// ───────────────────────────────────────────────────────────────────────────
// Document context: collect references + citation order + numbering
// ───────────────────────────────────────────────────────────────────────────

/// Resolved citation context for a document: the active style, every defined
/// reference (definition order), and the IEEE/ACM number assigned to each key.
#[derive(Debug, Clone)]
pub struct CiteContext {
    /// Active citation style.
    pub style: Format,
    /// All references defined via `::cite`, in definition order.
    pub references: Vec<Reference>,
    /// Key → citation number (cited keys first in citation order, then any
    /// defined-but-uncited references in definition order). Used by IEEE/ACM.
    pub numbers: BTreeMap<String, usize>,
}

/// Child-block accessor for the container variants citations can nest inside.
fn children_of(b: &Block) -> Option<&[Block]> {
    match b {
        Block::Page { children, .. }
        | Block::Section { children, .. }
        | Block::Slide { children, .. }
        | Block::App { children, .. }
        | Block::AppShell { children, .. }
        | Block::Sidebar { children, .. }
        | Block::Panel { children, .. }
        | Block::TabContent { children, .. }
        | Block::Drawer { children, .. }
        | Block::Modal { children, .. } => Some(children),
        _ => None,
    }
}

/// Prose text of a block that may contain inline `[@key]` citations.
fn cite_text_of(b: &Block) -> Option<&str> {
    match b {
        Block::Markdown { content, .. }
        | Block::Callout { content, .. }
        | Block::Summary { content, .. }
        | Block::Quote { content, .. }
        | Block::Section { content, .. }
        | Block::Details { content, .. } => Some(content),
        _ => None,
    }
}

fn collect_refs_rec(blocks: &[Block], out: &mut Vec<Reference>) {
    for b in blocks {
        if let Block::Cite { reference, .. } = b {
            out.push(reference.clone());
        }
        if let Some(children) = children_of(b) {
            collect_refs_rec(children, out);
        }
    }
}

/// Collect every `::cite`-defined reference in document order (recursing into
/// container blocks).
pub fn collect_references(blocks: &[Block]) -> Vec<Reference> {
    let mut out = Vec::new();
    collect_refs_rec(blocks, &mut out);
    out
}

fn collect_keys_rec(blocks: &[Block], order: &mut Vec<String>, seen: &mut BTreeSet<String>) {
    for b in blocks {
        if let Some(text) = cite_text_of(b) {
            for (_, _, cr) in crate::inline::find_inline_cites(text) {
                for it in cr.items {
                    if seen.insert(it.key.clone()) {
                        order.push(it.key);
                    }
                }
            }
        }
        if let Some(children) = children_of(b) {
            collect_keys_rec(children, order, seen);
        }
    }
}

/// Collect cited reference keys in first-appearance (citation) order.
pub fn collect_cited_keys(blocks: &[Block]) -> Vec<String> {
    let mut order = Vec::new();
    let mut seen = BTreeSet::new();
    collect_keys_rec(blocks, &mut order, &mut seen);
    order
}

/// Assign citation numbers: cited keys first (citation order), then any
/// defined-but-uncited references in definition order. Deterministic.
fn assign_numbers(refs: &[Reference], order: &[String]) -> BTreeMap<String, usize> {
    let mut nums = BTreeMap::new();
    let mut n = 1usize;
    for k in order {
        if refs.iter().any(|r| &r.key == k) && !nums.contains_key(k) {
            nums.insert(k.clone(), n);
            n += 1;
        }
    }
    for r in refs {
        if !nums.contains_key(&r.key) {
            nums.insert(r.key.clone(), n);
            n += 1;
        }
    }
    nums
}

/// Build a [`CiteContext`] from a document's blocks and its `format:`.
pub fn build_context(blocks: &[Block], format: Option<Format>) -> CiteContext {
    let references = collect_references(blocks);
    let order = collect_cited_keys(blocks);
    let numbers = assign_numbers(&references, &order);
    CiteContext {
        style: active_style(format),
        references,
        numbers,
    }
}

/// References ordered for the bibliography display: number order for numbered
/// styles, definition order otherwise (the keyed list re-sorts author styles).
pub fn ordered_references(ctx: &CiteContext) -> Vec<Reference> {
    if is_numbered(ctx.style) {
        let mut v = ctx.references.clone();
        v.sort_by_key(|r| ctx.numbers.get(&r.key).copied().unwrap_or(usize::MAX));
        v
    } else {
        ctx.references.clone()
    }
}

// ───────────────────────────────────────────────────────────────────────────
// Ambient context for renderers (thread-local, deterministic per render)
// ───────────────────────────────────────────────────────────────────────────

thread_local! {
    static ACTIVE: RefCell<Option<CiteContext>> = const { RefCell::new(None) };
}

/// RAII guard that clears the ambient [`CiteContext`] when dropped.
pub struct CiteScope {
    _private: (),
}

impl Drop for CiteScope {
    fn drop(&mut self) {
        ACTIVE.with(|c| *c.borrow_mut() = None);
    }
}

/// Install `ctx` as the ambient context for the current thread for the lifetime
/// of the returned guard. Renderers call this at the top of their entrypoint so
/// nested `render_block` calls can resolve inline cites + the bibliography
/// without threading the context through every signature.
pub fn install_context(ctx: CiteContext) -> CiteScope {
    ACTIVE.with(|c| *c.borrow_mut() = Some(ctx));
    CiteScope { _private: () }
}

/// Run `f` with a reference to the ambient context (if any).
pub fn with_active<R>(f: impl FnOnce(Option<&CiteContext>) -> R) -> R {
    ACTIVE.with(|c| f(c.borrow().as_ref()))
}

/// Replace inline `[@key]` citations in `text` with their formatted in-text
/// strings using the ambient context. No-op when there is no context, no
/// references, or no cites. Used by text-oriented renderers (markdown/native).
pub fn substitute_text_cites(text: &str) -> String {
    with_active(|ctx| match ctx {
        Some(ctx) if !ctx.references.is_empty() => {
            let cites = crate::inline::find_inline_cites(text);
            if cites.is_empty() {
                return text.to_string();
            }
            let mut out = String::with_capacity(text.len());
            let mut last = 0;
            for (s, e, cr) in cites {
                out.push_str(&text[last..s]);
                out.push_str(&format_in_text(&ctx.references, &cr, ctx.style, &ctx.numbers));
                last = e;
            }
            out.push_str(&text[last..]);
            out
        }
        _ => text.to_string(),
    })
}

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

    fn fixtures() -> Vec<Reference> {
        vec![
            Reference {
                key: "smith2020".into(),
                ref_type: RefType::Article,
                authors: parse_authors("Smith, John"),
                title: Some("Deep Learning for Climate Models".into()),
                container: Some("Journal of Climate AI".into()),
                year: Some("2020".into()),
                volume: Some("12".into()),
                issue: Some("3".into()),
                pages: Some("45-67".into()),
                doi: Some("10.1000/jcai.2020.45".into()),
                ..Default::default()
            },
            Reference {
                key: "jones2019".into(),
                ref_type: RefType::Book,
                authors: parse_authors("Jones, Alice; Brown, Bob"),
                title: Some("Foundations of Data Science".into()),
                publisher: Some("MIT Press".into()),
                year: Some("2019".into()),
                ..Default::default()
            },
            Reference {
                key: "lee2021".into(),
                ref_type: RefType::Web,
                authors: parse_authors("Lee, Carol"),
                title: Some("Understanding Transformers".into()),
                container: Some("AI Weekly".into()),
                year: Some("2021".into()),
                url: Some("https://aiweekly.example/transformers".into()),
                accessed: Some("2021-05-01".into()),
                ..Default::default()
            },
            Reference {
                key: "garcia2022".into(),
                ref_type: RefType::Conference,
                authors: parse_authors("Garcia, David; Patel, Esha; Wong, Fang"),
                title: Some("Scalable Inference".into()),
                container: Some("Proceedings of NeurIPS".into()),
                year: Some("2022".into()),
                pages: Some("100-110".into()),
                ..Default::default()
            },
        ]
    }

    fn cite1(key: &str) -> CiteRef {
        CiteRef {
            items: vec![CiteItem {
                key: key.into(),
                locator: None,
            }],
        }
    }

    // ── L2: author parsing ──────────────────────────────────────────────────

    #[test]
    fn parse_author_inverted_and_natural() {
        assert_eq!(parse_author("Smith, John"), Author { family: "Smith".into(), given: Some("John".into()) });
        assert_eq!(parse_author("John Smith"), Author { family: "Smith".into(), given: Some("John".into()) });
        assert_eq!(parse_author("Plato"), Author { family: "Plato".into(), given: None });
    }

    #[test]
    fn parse_authors_semicolon() {
        let a = parse_authors("Jones, Alice; Brown, Bob");
        assert_eq!(a.len(), 2);
        assert_eq!(a[0].family, "Jones");
        assert_eq!(a[1].family, "Brown");
    }

    #[test]
    fn initials_multiword() {
        assert_eq!(initials(&Some("John".into())), "J.");
        assert_eq!(initials(&Some("Mary Jane".into())), "M. J.");
        assert_eq!(initials(&None), "");
    }

    // ── L6 golden: APA ──────────────────────────────────────────────────────

    #[test]
    fn apa_in_text_and_list() {
        let refs = fixtures();
        let nums = BTreeMap::new();
        assert_eq!(format_in_text(&refs, &cite1("smith2020"), Format::Apa, &nums), "(Smith, 2020)");
        assert_eq!(
            format_in_text(&refs, &CiteRef { items: vec![CiteItem { key: "smith2020".into(), locator: Some("p. 12".into()) }] }, Format::Apa, &nums),
            "(Smith, 2020, p. 12)"
        );
        assert_eq!(format_in_text(&refs, &cite1("jones2019"), Format::Apa, &nums), "(Jones & Brown, 2019)");
        assert_eq!(format_in_text(&refs, &cite1("garcia2022"), Format::Apa, &nums), "(Garcia et al., 2022)");
        assert_eq!(
            format_in_text(&refs, &CiteRef { items: vec![CiteItem { key: "smith2020".into(), locator: None }, CiteItem { key: "jones2019".into(), locator: None }] }, Format::Apa, &nums),
            "(Smith, 2020; Jones & Brown, 2019)"
        );

        let list = reference_list(&refs, Format::Apa);
        // Alphabetical: Garcia, Jones, Lee, Smith
        assert_eq!(list[0], "Garcia, D., Patel, E., & Wong, F. (2022). Scalable Inference. *Proceedings of NeurIPS*, 100-110.");
        assert_eq!(list[1], "Jones, A., & Brown, B. (2019). *Foundations of Data Science*. MIT Press.");
        assert_eq!(list[2], "Lee, C. (2021). Understanding Transformers. *AI Weekly*. https://aiweekly.example/transformers");
        assert_eq!(list[3], "Smith, J. (2020). Deep Learning for Climate Models. *Journal of Climate AI*, *12*(3), 45-67. https://doi.org/10.1000/jcai.2020.45");
    }

    // ── L6 golden: MLA ──────────────────────────────────────────────────────

    #[test]
    fn mla_in_text_and_list() {
        let refs = fixtures();
        let nums = BTreeMap::new();
        assert_eq!(format_in_text(&refs, &cite1("smith2020"), Format::Mla, &nums), "(Smith)");
        assert_eq!(
            format_in_text(&refs, &CiteRef { items: vec![CiteItem { key: "smith2020".into(), locator: Some("p. 12".into()) }] }, Format::Mla, &nums),
            "(Smith 12)"
        );
        assert_eq!(format_in_text(&refs, &cite1("jones2019"), Format::Mla, &nums), "(Jones and Brown)");
        assert_eq!(format_in_text(&refs, &cite1("garcia2022"), Format::Mla, &nums), "(Garcia et al.)");

        let list = reference_list(&refs, Format::Mla);
        assert_eq!(list[0], "Garcia, David, et al. \"Scalable Inference.\" *Proceedings of NeurIPS*, 2022, pp. 100-110.");
        assert_eq!(list[1], "Jones, Alice, and Bob Brown. *Foundations of Data Science*. MIT Press, 2019.");
        assert_eq!(list[2], "Lee, Carol. \"Understanding Transformers.\" *AI Weekly*, 2021, https://aiweekly.example/transformers. Accessed 1 May 2021.");
        assert_eq!(list[3], "Smith, John. \"Deep Learning for Climate Models.\" *Journal of Climate AI*, vol. 12, no. 3, 2020, pp. 45-67.");
    }

    // ── L6 golden: Chicago author-date ──────────────────────────────────────

    #[test]
    fn chicago_in_text_and_list() {
        let refs = fixtures();
        let nums = BTreeMap::new();
        assert_eq!(format_in_text(&refs, &cite1("smith2020"), Format::Chicago, &nums), "(Smith 2020)");
        assert_eq!(
            format_in_text(&refs, &CiteRef { items: vec![CiteItem { key: "smith2020".into(), locator: Some("p. 12".into()) }] }, Format::Chicago, &nums),
            "(Smith 2020, 12)"
        );
        assert_eq!(format_in_text(&refs, &cite1("jones2019"), Format::Chicago, &nums), "(Jones and Brown 2019)");

        let list = reference_list(&refs, Format::Chicago);
        assert_eq!(list[0], "Garcia, David, Esha Patel, and Fang Wong. 2022. \"Scalable Inference.\" *Proceedings of NeurIPS*, 100-110.");
        assert_eq!(list[1], "Jones, Alice, and Bob Brown. 2019. *Foundations of Data Science*. MIT Press.");
        assert_eq!(list[2], "Lee, Carol. 2021. \"Understanding Transformers.\" *AI Weekly*. https://aiweekly.example/transformers.");
        assert_eq!(list[3], "Smith, John. 2020. \"Deep Learning for Climate Models.\" *Journal of Climate AI* 12 (3): 45-67.");
    }

    // ── L6 golden: IEEE (numbered, citation order) ──────────────────────────

    #[test]
    fn ieee_in_text_and_list() {
        let refs = fixtures();
        let mut nums = BTreeMap::new();
        nums.insert("smith2020".to_string(), 1);
        nums.insert("jones2019".to_string(), 2);
        nums.insert("garcia2022".to_string(), 3);
        nums.insert("lee2021".to_string(), 4);

        assert_eq!(format_in_text(&refs, &cite1("smith2020"), Format::Ieee, &nums), "[1]");
        assert_eq!(
            format_in_text(&refs, &CiteRef { items: vec![CiteItem { key: "smith2020".into(), locator: None }, CiteItem { key: "jones2019".into(), locator: None }] }, Format::Ieee, &nums),
            "[1], [2]"
        );
        assert_eq!(
            format_in_text(&refs, &CiteRef { items: vec![CiteItem { key: "smith2020".into(), locator: Some("p. 12".into()) }] }, Format::Ieee, &nums),
            "[1, p. 12]"
        );

        // Reference list in citation order: smith, jones, garcia, lee
        let ordered = vec![refs[0].clone(), refs[1].clone(), refs[3].clone(), refs[2].clone()];
        let list = reference_list(&ordered, Format::Ieee);
        assert_eq!(list[0], "[1] J. Smith, \"Deep Learning for Climate Models,\" *Journal of Climate AI*, vol. 12, no. 3, pp. 45-67, 2020.");
        assert_eq!(list[1], "[2] A. Jones and B. Brown, *Foundations of Data Science*. MIT Press, 2019.");
        assert_eq!(list[2], "[3] D. Garcia, E. Patel, and F. Wong, \"Scalable Inference,\" in *Proceedings of NeurIPS*, 2022, pp. 100-110.");
        assert_eq!(list[3], "[4] C. Lee, \"Understanding Transformers,\" *AI Weekly*, 2021. [Online]. Available: https://aiweekly.example/transformers");
    }

    // ── L6 determinism ──────────────────────────────────────────────────────

    #[test]
    fn reference_list_is_deterministic() {
        let refs = fixtures();
        for style in [Format::Apa, Format::Mla, Format::Chicago, Format::Ieee] {
            assert_eq!(reference_list(&refs, style), reference_list(&refs, style));
        }
    }

    #[test]
    fn bibliography_headings() {
        assert_eq!(bibliography_heading(Format::Mla), "Works Cited");
        assert_eq!(bibliography_heading(Format::Apa), "References");
        assert_eq!(bibliography_heading(Format::Chicago), "Bibliography");
        assert_eq!(bibliography_heading(Format::Ieee), "References");
    }

    #[test]
    fn active_style_defaults_to_apa() {
        assert_eq!(active_style(None), Format::Apa);
        assert_eq!(active_style(Some(Format::Ieee)), Format::Ieee);
    }
}