mdbook-bib 0.5.3

mdbook plugin allowing to load and present a bibliography in BibLaTex format in your books and cite its references
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
//! Tests for citation replacement and regex pattern matching.
//!
//! This module covers:
//! - Citation placeholder replacement (`{{#cite key}}` and `@@key`)
//! - Regex pattern matching
//! - Subfolder linking
//! - BibLaTeX-compliant citation keys

use super::common::{
    create_citation_backend, create_citation_backend_with_template, dummy_bibliography,
    BibItemBuilder, DUMMY_TEXT_WITH_2_VALID_CITE_PLACEHOLDERS,
    DUMMY_TEXT_WITH_A_VALID_AND_AN_INVALID_CITE_PLACEHOLDERS,
};
use crate::config::CitationSyntax;
use indexmap::IndexMap;
use mdbook_preprocessor::book::Chapter;
use rstest::rstest;
use std::collections::HashSet;

// =============================================================================
// Citation Replacement Tests
// =============================================================================

#[test]
fn valid_and_invalid_citations_are_replaced_properly_in_book_text() {
    let mut bibliography = dummy_bibliography();
    let mut cited: HashSet<String> = HashSet::new();

    // Check valid references included in a dummy text
    let chapter = Chapter::new(
        "",
        DUMMY_TEXT_WITH_2_VALID_CITE_PLACEHOLDERS.into(),
        "source.md",
        vec![],
    );

    let backend = create_citation_backend();
    let mut last_index = 0;
    let text_with_citations = crate::citation::replace_all_placeholders(
        &chapter,
        &mut bibliography,
        &mut cited,
        &backend,
        &mut last_index,
        &CitationSyntax::Default,
    );

    assert!(text_with_citations.contains(r#"href="bibliography.html#fps""#));
    assert!(text_with_citations.contains(r#"href="bibliography.html#rust_book""#));

    // Check a mix of valid and invalid references
    let chapter = Chapter::new(
        "",
        DUMMY_TEXT_WITH_A_VALID_AND_AN_INVALID_CITE_PLACEHOLDERS.into(),
        "source.md",
        vec![],
    );
    let mut last_index = 0;
    let text_with_citations = crate::citation::replace_all_placeholders(
        &chapter,
        &mut bibliography,
        &mut cited,
        &backend,
        &mut last_index,
        &CitationSyntax::Default,
    );
    assert!(text_with_citations.contains(">fps</a>"));
    assert!(text_with_citations.contains("[Unknown bib ref:"));
}

#[rstest]
#[case::root_chapter("source.md", "bibliography.html")]
#[case::one_level_down("dir1/source.md", "../bibliography.html")]
#[case::two_levels_down("dir1/dir2/source.md", "../../bibliography.html")]
#[case::non_canonical_path("dir1/dir2/../source.md", "../bibliography.html")]
fn citations_in_subfolders_link_properly(#[case] chapter_path: &str, #[case] expected_link: &str) {
    let mut bibliography = dummy_bibliography();
    let backend = create_citation_backend();

    let chapter = Chapter::new(
        "",
        DUMMY_TEXT_WITH_2_VALID_CITE_PLACEHOLDERS.into(),
        chapter_path,
        vec![],
    );

    let mut last_index = 0;
    let text_with_citations = crate::citation::replace_all_placeholders(
        &chapter,
        &mut bibliography,
        &mut HashSet::new(),
        &backend,
        &mut last_index,
        &CitationSyntax::Default,
    );

    assert!(
        text_with_citations.contains(&format!(r#"href="{expected_link}#fps""#)),
        "Expecting link to '{expected_link}' in '{text_with_citations}'",
    );
    assert!(
        text_with_citations.contains(&format!(r#"href="{expected_link}#rust_book""#)),
        "Expecting link to '{expected_link}' in '{text_with_citations}'",
    );
}

#[test]
fn citations_in_draft_chapter_link_properly() {
    let mut bibliography = dummy_bibliography();
    let backend = create_citation_backend();

    let mut draft_chapter = Chapter::new_draft("", vec![]);
    draft_chapter.content = DUMMY_TEXT_WITH_2_VALID_CITE_PLACEHOLDERS.into();

    let mut last_index = 0;
    let text_with_citations = crate::citation::replace_all_placeholders(
        &draft_chapter,
        &mut bibliography,
        &mut HashSet::new(),
        &backend,
        &mut last_index,
        &CitationSyntax::Default,
    );

    assert!(text_with_citations.contains(r#"href="bibliography.html#fps""#));
    assert!(text_with_citations.contains(r#"href="bibliography.html#rust_book""#));
}

#[test]
fn debug_replace_all_placeholders() {
    let content = r#"
This is a reference {{#cite mdBook}} that has to be resolved to the right bibliography file.
This is a reference to a non-existing book that reports a bug @@mdBookWithAuthorsWithANDInTheirName that was resolved.
This is a reference to {{#cite DUMMY:1}}
"#;

    let mut bibliography = IndexMap::new();
    bibliography.insert(
        "mdBook".to_string(),
        BibItemBuilder::misc("mdBook")
            .title("mdBook Documentation")
            .authors(&["Various Contributors"])
            .year("2015")
            .summary("mdBook is a command line tool.")
            .url("https://rust-lang.github.io/mdBook/")
            .build(),
    );
    bibliography.insert(
        "mdBookWithAuthorsWithANDInTheirName".to_string(),
        BibItemBuilder::misc("mdBookWithAuthorsWithANDInTheirName")
            .title("Bug when rendering authors that include the `and` substring in their names")
            .authors_parts(vec![vec![
                "Jane A. Doeander".to_string(),
                "John B. Doeanderson".to_string(),
            ]])
            .year("2023")
            .summary("What a book about nothing...")
            .url("https://github.com/francisco-perez-sorrosal/mdbook-bib/issues/44")
            .build(),
    );
    bibliography.insert(
        "DUMMY:1".to_string(),
        BibItemBuilder::misc("DUMMY:1")
            .title("The Book without Title")
            .authors_parts(vec![vec!["John".to_string(), "Doe".to_string()]])
            .year("2100")
            .summary("N/A")
            .build(),
    );

    let chapter = Chapter::new(
        "Test",
        content.to_string(),
        std::path::PathBuf::new(),
        vec![],
    );
    let mut cited = HashSet::new();
    let backend = create_citation_backend_with_template("{{item.citation_key}}");
    let mut last_index = 0;
    let _ = crate::citation::replace_all_placeholders(
        &chapter,
        &mut bibliography,
        &mut cited,
        &backend,
        &mut last_index,
        &CitationSyntax::Default,
    );
}

#[test]
fn test_citation_with_dots_replacement() {
    let content = r#"
This is a reference to a paper with DOI @@10.1145/3508461 that should be properly resolved.
This is another reference @@simple_key that should also work.
"#;

    let mut bibliography = IndexMap::new();
    bibliography.insert(
        "10.1145/3508461".to_string(),
        BibItemBuilder::article("10.1145/3508461")
            .title("Some Paper with DOI")
            .authors(&["Author Name"])
            .year("2023")
            .summary("A paper with a DOI citation key")
            .url("https://doi.org/10.1145/3508461")
            .build(),
    );
    bibliography.insert(
        "simple_key".to_string(),
        BibItemBuilder::article("simple_key")
            .title("Simple Paper")
            .authors(&["Another Author"])
            .year("2023")
            .summary("A paper with a simple citation key")
            .build(),
    );

    let chapter = Chapter::new(
        "Test",
        content.to_string(),
        std::path::PathBuf::new(),
        vec![],
    );
    let mut cited = HashSet::new();
    let backend = create_citation_backend_with_template("{{item.citation_key}}");
    let mut last_index = 0;

    let result = crate::citation::replace_all_placeholders(
        &chapter,
        &mut bibliography,
        &mut cited,
        &backend,
        &mut last_index,
        &CitationSyntax::Default,
    );

    // Check that both citations were found and added to cited set
    assert!(cited.contains("10.1145/3508461"));
    assert!(cited.contains("simple_key"));

    // Check that the replacements were made correctly
    assert!(result.contains("10.1145/3508461"));
    assert!(result.contains("simple_key"));
    assert!(!result.contains("@@10.1145/3508461"));
    assert!(!result.contains("@@simple_key"));
}

// =============================================================================
// Regex Pattern Tests (Parametrized)
// =============================================================================

#[rstest]
#[case::simple_key("{{#cite mdBook}}", "mdBook")]
#[case::key_with_colon("{{#cite DUMMY:1}}", "DUMMY:1")]
#[case::key_with_hyphen("{{#cite test-key}}", "test-key")]
#[case::key_with_underscore("{{#cite test_key}}", "test_key")]
#[case::doi_format("{{#cite 10.1145/3508461}}", "10.1145/3508461")]
fn test_ref_pattern_captures_key(#[case] input: &str, #[case] expected_key: &str) {
    use crate::REF_PATTERN;
    use regex::Regex;

    let re = Regex::new(REF_PATTERN).unwrap();
    let captures = re
        .captures(input)
        .unwrap_or_else(|| panic!("Should match: {input}"));
    let captured_key = captures.get(1).expect("Should capture key").as_str().trim();
    assert_eq!(captured_key, expected_key);
}

#[rstest]
#[case::doi_with_dots("@@10.1145/3508461", "10.1145/3508461")]
#[case::simple_key("@@simple_key", "simple_key")]
#[case::key_with_dots("@@key.with.dots", "key.with.dots")]
#[case::key_with_dashes("@@key-with-dashes", "key-with-dashes")]
#[case::key_with_underscores("@@key_with_underscores", "key_with_underscores")]
fn test_at_ref_pattern_captures_key(#[case] input: &str, #[case] expected_key: &str) {
    use crate::AT_REF_PATTERN;
    use regex::Regex;

    let re = Regex::new(AT_REF_PATTERN).unwrap();
    let captures = re
        .captures(input)
        .unwrap_or_else(|| panic!("Should match: {input}"));
    let captured_key = captures.get(2).expect("Should capture key").as_str();
    assert_eq!(captured_key, expected_key);
}

#[test]
fn test_at_ref_followed_by_punctuation() {
    let content = r#"
This book is written in Rust @@Klabnik2018.
Another citation at end of sentence @@fps!
What about questions @@simple_key?
Or maybe colons @@another_key: it should work.
We reference this paper @@10.1145/3508461.
See @@ref1, @@ref2, and @@ref3.
Citations in parentheses (see @@Jones2019).
"#;

    let mut bibliography = IndexMap::new();
    let keys = [
        ("Klabnik2018", "The Rust Programming Language", "2018"),
        ("fps", "Test Entry", "2020"),
        ("simple_key", "Simple Paper", "2023"),
        ("another_key", "Another Paper", "2024"),
        ("10.1145/3508461", "Paper with DOI", "2023"),
        ("ref1", "First Reference", "2020"),
        ("ref2", "Second Reference", "2021"),
        ("ref3", "Third Reference", "2022"),
        ("Jones2019", "Jones Paper", "2019"),
    ];

    for (key, title, year) in keys {
        bibliography.insert(
            key.to_string(),
            BibItemBuilder::article(key)
                .title(title)
                .authors(&["Author"])
                .year(year)
                .summary("Test")
                .build(),
        );
    }

    let chapter = Chapter::new(
        "Test",
        content.to_string(),
        std::path::PathBuf::new(),
        vec![],
    );
    let mut cited = HashSet::new();
    let backend = create_citation_backend_with_template("{{item.citation_key}}");
    let mut last_index = 0;

    let result = crate::citation::replace_all_placeholders(
        &chapter,
        &mut bibliography,
        &mut cited,
        &backend,
        &mut last_index,
        &CitationSyntax::Default,
    );

    // Check that all citations were found
    for (key, _, _) in &keys {
        assert!(cited.contains(*key), "Should contain {key}");
        assert!(result.contains(*key), "Result should contain {key}");
        assert!(
            !result.contains(&format!("@@{key}")),
            "Original @@ pattern should be gone for {key}"
        );
    }

    // Check that punctuation is preserved after the citation
    assert!(result.contains("Klabnik2018."));
    assert!(result.contains("fps!"));
    assert!(result.contains("simple_key?"));
    assert!(result.contains("another_key:"));
    assert!(result.contains("10.1145/3508461."));

    // Check multiple citations with commas are handled correctly
    assert!(result.contains("ref1,"));
    assert!(result.contains("ref2,"));
    assert!(result.contains("ref3."));

    // Check citation in parentheses is handled correctly
    assert!(result.contains("Jones2019)"));
}

#[test]
fn test_biblatex_compliant_citation_keys() {
    use crate::{AT_REF_PATTERN, REF_PATTERN};
    use regex::Regex;

    let ref_re = Regex::new(REF_PATTERN).unwrap();
    let at_re = Regex::new(AT_REF_PATTERN).unwrap();

    // BibLaTeX-compliant citation keys with various safe characters
    let keys = vec![
        "Klabnik2018",
        "10.1145/3508461",           // DOI with dots and slash
        "doi:10.5555/12345",         // DOI with colon prefix
        "arXiv:2301.12345",          // arXiv with colon
        "key_with_underscores",      // underscores
        "key-with-hyphens",          // hyphens
        "user@domain",               // at symbol
        "mixed:key_2023.1-final@v2", // combination
    ];

    // Test REF_PATTERN ({{#cite key}})
    for key in &keys {
        let text = format!("{{{{#cite {key}}}}}");
        assert!(
            ref_re.is_match(&text),
            "REF_PATTERN should match BibLaTeX-compliant key: {key}"
        );
        if let Some(caps) = ref_re.captures(&text) {
            if let Some(captured) = caps.get(1) {
                assert_eq!(
                    captured.as_str(),
                    *key,
                    "REF_PATTERN should capture the full key"
                );
            }
        }
    }

    // Test AT_REF_PATTERN (@@ syntax) with trailing punctuation
    for key in &keys {
        let text = format!("See @@{key}.");
        assert!(
            at_re.is_match(&text),
            "AT_REF_PATTERN should match BibLaTeX-compliant key: {key}"
        );
        if let Some(caps) = at_re.captures(&text) {
            if let Some(captured) = caps.get(2) {
                assert_eq!(
                    captured.as_str(),
                    *key,
                    "AT_REF_PATTERN should capture the full key without trailing punctuation"
                );
            }
        }
    }

    // Test with actual replacement to ensure keys work end-to-end
    let content = r#"
Test {{#cite doi:10.5555/12345}} and @@arXiv:2301.12345.
User citation @@user@domain is valid.
"#;

    let mut bibliography = IndexMap::new();
    bibliography.insert(
        "doi:10.5555/12345".to_string(),
        BibItemBuilder::article("doi:10.5555/12345")
            .title("DOI Paper")
            .authors(&["Author"])
            .year("2023")
            .url("https://doi.org/10.5555/12345")
            .build(),
    );
    bibliography.insert(
        "arXiv:2301.12345".to_string(),
        BibItemBuilder::article("arXiv:2301.12345")
            .title("arXiv Paper")
            .authors(&["Researcher"])
            .year("2023")
            .url("https://arxiv.org/abs/2301.12345")
            .build(),
    );
    bibliography.insert(
        "user@domain".to_string(),
        BibItemBuilder::misc("user@domain")
            .title("User Citation")
            .authors(&["User"])
            .year("2024")
            .build(),
    );

    let chapter = Chapter::new(
        "Test",
        content.to_string(),
        std::path::PathBuf::new(),
        vec![],
    );
    let mut cited = HashSet::new();
    let backend = create_citation_backend_with_template("{{item.citation_key}}");
    let mut last_index = 0;

    let result = crate::citation::replace_all_placeholders(
        &chapter,
        &mut bibliography,
        &mut cited,
        &backend,
        &mut last_index,
        &CitationSyntax::Default,
    );

    // Verify all citations were found
    assert!(cited.contains("doi:10.5555/12345"));
    assert!(cited.contains("arXiv:2301.12345"));
    assert!(cited.contains("user@domain"));

    // Verify replacements occurred
    assert!(result.contains("doi:10.5555/12345"));
    assert!(result.contains("arXiv:2301.12345"));
    assert!(result.contains("user@domain"));

    // Verify original patterns are gone
    assert!(!result.contains("{{#cite doi:10.5555/12345}}"));
    assert!(!result.contains("@@arXiv:2301.12345"));
    assert!(!result.contains("@@user@domain"));
}

#[rstest]
#[case::include("{{#include file.rs}}")]
#[case::title("{{#title My Custom Title}}")]
#[case::playground("{{#playground example.rs}}")]
#[case::rustdoc_include("{{#rustdoc_include file.rs:2}}")]
#[case::include_with_range("{{#include file.rs:2:10}}")]
fn test_ref_pattern_excludes_mdbook_expressions(#[case] input: &str) {
    use crate::REF_PATTERN;
    use regex::Regex;

    let re = Regex::new(REF_PATTERN).unwrap();
    assert!(
        !re.is_match(input),
        "Pattern should NOT match mdBook expression: {input}"
    );
}

// =============================================================================
// Pandoc Citation Syntax Tests
// =============================================================================

#[test]
fn test_pandoc_author_in_text_citation() {
    use crate::citation::PANDOC_CITE_PATTERN;
    use regex::Regex;

    let re = Regex::new(PANDOC_CITE_PATTERN).unwrap();

    // Should match @key at word boundaries
    // Pattern captures: group 1 = prefix (empty or char), group 2 = key
    let cases = vec![
        ("@Smith2024", "Smith2024"),
        ("See @Jones2020 for details", "Jones2020"),
        ("As @Author_Name argues", "Author_Name"),
    ];

    for (input, expected_key) in cases {
        let caps = re.captures(input);
        assert!(caps.is_some(), "Should match: {input}");
        let key = caps.unwrap().get(2).unwrap().as_str();
        assert_eq!(key, expected_key, "Should capture key from: {input}");
    }
}

#[test]
fn test_pandoc_pattern_does_not_match_emails() {
    use crate::citation::PANDOC_CITE_PATTERN;
    use regex::Regex;

    let re = Regex::new(PANDOC_CITE_PATTERN).unwrap();

    // Email addresses should NOT be matched
    // The pattern requires a non-word char before @, so "user@..." won't match
    let non_matches = vec![
        "user@example.com",
        "email@domain.org",
        "test@email.co.uk",
        "name@company.io",
    ];

    for input in non_matches {
        assert!(!re.is_match(input), "Should NOT match email: {input}");
    }
}

#[test]
fn test_pandoc_pattern_does_not_match_url_mentions() {
    use crate::citation::PANDOC_CITE_PATTERN;
    use regex::Regex;

    let re = Regex::new(PANDOC_CITE_PATTERN).unwrap();

    // URLs with @ mentions should NOT be matched
    // The pattern excludes / before @ to handle URL paths
    let non_matches = vec![
        "https://twitter.com/@rustlang",
        "https://github.com/@user",
        "http://example.com/@mention",
        "git@github.com:user/repo.git", // Also git URLs
    ];

    for input in non_matches {
        assert!(!re.is_match(input), "Should NOT match URL mention: {input}");
    }
}

#[test]
fn test_pandoc_pattern_does_not_match_double_at() {
    use crate::citation::PANDOC_CITE_PATTERN;
    use regex::Regex;

    let re = Regex::new(PANDOC_CITE_PATTERN).unwrap();

    // @@key should NOT be matched by Pandoc single-@ pattern
    // The pattern requires non-@ char before @, so @@ won't match
    let non_matches = vec!["@@Smith2024", "@@key"];

    for input in non_matches {
        assert!(!re.is_match(input), "Should NOT match double-at: {input}");
    }

    // "text @@citation" - the @ before @citation excludes matching @citation
    let input = "text @@citation more";
    assert!(
        !re.is_match(input),
        "Should NOT match @citation after @@ prefix"
    );
}

#[test]
fn test_pandoc_bracketed_citation() {
    use crate::citation::PANDOC_BRACKETED_PATTERN;
    use regex::Regex;

    let re = Regex::new(PANDOC_BRACKETED_PATTERN).unwrap();

    let cases = vec![
        ("[@Smith2024]", "Smith2024"),
        ("see [@Jones2020]", "Jones2020"),
        ("documented [@Author_Name].", "Author_Name"),
    ];

    for (input, expected_key) in cases {
        let caps = re.captures(input);
        assert!(caps.is_some(), "Should match: {input}");
        let key = caps.unwrap().get(1).unwrap().as_str();
        assert_eq!(key, expected_key, "Should capture key from: {input}");
    }
}

#[test]
fn test_pandoc_suppress_author_citation() {
    use crate::citation::PANDOC_SUPPRESS_AUTHOR_PATTERN;
    use regex::Regex;

    let re = Regex::new(PANDOC_SUPPRESS_AUTHOR_PATTERN).unwrap();

    let cases = vec![
        ("[-@Smith2024]", "Smith2024"),
        ("Smith argues [-@Smith2024]", "Smith2024"),
        ("As noted [-@Author_Name].", "Author_Name"),
    ];

    for (input, expected_key) in cases {
        let caps = re.captures(input);
        assert!(caps.is_some(), "Should match: {input}");
        let key = caps.unwrap().get(1).unwrap().as_str();
        assert_eq!(key, expected_key, "Should capture key from: {input}");
    }
}

#[test]
fn test_pandoc_bracketed_does_not_match_markdown_links() {
    use crate::citation::PANDOC_BRACKETED_PATTERN;
    use regex::Regex;

    let re = Regex::new(PANDOC_BRACKETED_PATTERN).unwrap();

    // Markdown links [text](url) should NOT be matched
    let non_matches = vec![
        "[link text](https://example.com)",
        "[Documentation](../docs/index.html)",
        "[See here](./local.md)",
    ];

    for input in non_matches {
        assert!(
            !re.is_match(input),
            "Should NOT match markdown link: {input}"
        );
    }
}

#[test]
fn test_pandoc_syntax_full_replacement() {
    let content = r#"
According to @Smith2024, this is important.
This has been documented [@Jones2020].
Smith argues [-@Smith2024] that...
Legacy syntax still works: @@legacy_key and {{#cite another_key}}.
"#;

    let mut bibliography = IndexMap::new();
    for key in ["Smith2024", "Jones2020", "legacy_key", "another_key"] {
        bibliography.insert(
            key.to_string(),
            BibItemBuilder::article(key)
                .title(&format!("Title for {key}"))
                .authors(&["Author"])
                .year("2024")
                .build(),
        );
    }

    let chapter = Chapter::new(
        "Test",
        content.to_string(),
        std::path::PathBuf::new(),
        vec![],
    );
    let mut cited = HashSet::new();
    let backend = create_citation_backend_with_template("{{item.citation_key}}");
    let mut last_index = 0;

    let result = crate::citation::replace_all_placeholders(
        &chapter,
        &mut bibliography,
        &mut cited,
        &backend,
        &mut last_index,
        &CitationSyntax::Pandoc,
    );

    // All citations should be found
    assert!(cited.contains("Smith2024"), "Should find Smith2024");
    assert!(cited.contains("Jones2020"), "Should find Jones2020");
    assert!(cited.contains("legacy_key"), "Should find legacy_key");
    assert!(cited.contains("another_key"), "Should find another_key");

    // Original patterns should be gone
    assert!(!result.contains("@Smith2024"), "Should replace @Smith2024");
    assert!(
        !result.contains("[@Jones2020]"),
        "Should replace [@Jones2020]"
    );
    assert!(
        !result.contains("[-@Smith2024]"),
        "Should replace [-@Smith2024]"
    );
    assert!(
        !result.contains("@@legacy_key"),
        "Should replace @@legacy_key"
    );
    assert!(
        !result.contains("{{#cite another_key}}"),
        "Should replace {{#cite}}"
    );
}

#[test]
fn test_code_block_protection() {
    let content = r#"
Regular citation @Smith2024 should be replaced.

```rust
// This @NotACitation should NOT be replaced
let x = 1;
```

Inline code `@AlsoNotACitation` should be protected.

~~~python
# @AnotherNonCitation in fenced block
print("hello")
~~~

Back to regular text with @Jones2020.
"#;

    let mut bibliography = IndexMap::new();
    for key in [
        "Smith2024",
        "Jones2020",
        "NotACitation",
        "AlsoNotACitation",
        "AnotherNonCitation",
    ] {
        bibliography.insert(
            key.to_string(),
            BibItemBuilder::article(key)
                .title(&format!("Title for {key}"))
                .authors(&["Author"])
                .year("2024")
                .build(),
        );
    }

    let chapter = Chapter::new(
        "Test",
        content.to_string(),
        std::path::PathBuf::new(),
        vec![],
    );
    let mut cited = HashSet::new();
    let backend = create_citation_backend_with_template("[CITE:{{item.citation_key}}]");
    let mut last_index = 0;

    let result = crate::citation::replace_all_placeholders(
        &chapter,
        &mut bibliography,
        &mut cited,
        &backend,
        &mut last_index,
        &CitationSyntax::Pandoc,
    );

    // Only regular citations should be found
    assert!(cited.contains("Smith2024"), "Should find Smith2024");
    assert!(cited.contains("Jones2020"), "Should find Jones2020");

    // Code block citations should NOT be found
    assert!(
        !cited.contains("NotACitation"),
        "Should NOT find citation in code block"
    );
    assert!(
        !cited.contains("AlsoNotACitation"),
        "Should NOT find citation in inline code"
    );
    assert!(
        !cited.contains("AnotherNonCitation"),
        "Should NOT find citation in ~~~ block"
    );

    // Code blocks should be preserved with original @ symbols
    assert!(
        result.contains("@NotACitation"),
        "Code block content should be preserved"
    );
    assert!(
        result.contains("`@AlsoNotACitation`"),
        "Inline code should be preserved"
    );
    assert!(
        result.contains("@AnotherNonCitation"),
        "Fenced block content should be preserved"
    );

    // Regular citations should be replaced
    assert!(
        result.contains("[CITE:Smith2024]"),
        "Regular citation should be replaced"
    );
    assert!(
        result.contains("[CITE:Jones2020]"),
        "Regular citation should be replaced"
    );
}

#[test]
fn test_escaped_at_symbol() {
    let content = r#"
Contact me at user\@example.com for more info.
Citation @Smith2024 should still work.
Another escaped: admin\@server.local
"#;

    let mut bibliography = IndexMap::new();
    bibliography.insert(
        "Smith2024".to_string(),
        BibItemBuilder::article("Smith2024")
            .title("Test Paper")
            .authors(&["Smith"])
            .year("2024")
            .build(),
    );

    let chapter = Chapter::new(
        "Test",
        content.to_string(),
        std::path::PathBuf::new(),
        vec![],
    );
    let mut cited = HashSet::new();
    let backend = create_citation_backend_with_template("[CITE:{{item.citation_key}}]");
    let mut last_index = 0;

    let result = crate::citation::replace_all_placeholders(
        &chapter,
        &mut bibliography,
        &mut cited,
        &backend,
        &mut last_index,
        &CitationSyntax::Pandoc,
    );

    // Citation should be found and replaced
    assert!(cited.contains("Smith2024"), "Should find Smith2024");
    assert!(
        result.contains("[CITE:Smith2024]"),
        "Should replace citation"
    );

    // Escaped @ should become literal @
    assert!(
        result.contains("user@example.com"),
        "Escaped @ should become literal: {result}"
    );
    assert!(
        result.contains("admin@server.local"),
        "Escaped @ should become literal"
    );

    // No \@ should remain
    assert!(
        !result.contains(r"\@"),
        "No escaped @ patterns should remain"
    );
}

#[test]
fn test_pandoc_syntax_disabled_by_default() {
    let content = r#"
This @citation should NOT be replaced when Pandoc syntax is disabled.
But @@legacy_key should still work.
And {{#cite another_key}} too.
"#;

    let mut bibliography = IndexMap::new();
    for key in ["citation", "legacy_key", "another_key"] {
        bibliography.insert(
            key.to_string(),
            BibItemBuilder::article(key)
                .title(&format!("Title for {key}"))
                .authors(&["Author"])
                .year("2024")
                .build(),
        );
    }

    let chapter = Chapter::new(
        "Test",
        content.to_string(),
        std::path::PathBuf::new(),
        vec![],
    );
    let mut cited = HashSet::new();
    let backend = create_citation_backend_with_template("[{{item.citation_key}}]");
    let mut last_index = 0;

    let result = crate::citation::replace_all_placeholders(
        &chapter,
        &mut bibliography,
        &mut cited,
        &backend,
        &mut last_index,
        &CitationSyntax::Default, // Default syntax, Pandoc disabled
    );

    // @citation should NOT be replaced (Pandoc syntax disabled)
    assert!(
        !cited.contains("citation"),
        "Pandoc @citation should NOT be processed in default mode"
    );
    assert!(
        result.contains("@citation"),
        "@citation should remain unchanged"
    );

    // Legacy syntax should still work
    assert!(cited.contains("legacy_key"), "@@legacy_key should work");
    assert!(cited.contains("another_key"), "{{#cite}} should work");
}