citum-engine 0.79.0

Citum citation and bibliography processor
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
/*
SPDX-License-Identifier: MIT OR Apache-2.0
SPDX-FileCopyrightText: © 2023-2026 Bruce D'Arcus and Citum contributors
*/

//! Title text-case transforms.
//!
//! Implements structured-title-aware casing for bibliography output.
//! All transforms operate on Djot-markup-bearing strings and respect
//! `.nocase` span protection via the rich-text renderer.

use citum_schema::NoteStartTextCase;
use citum_schema::options::titles::TextCase;
use icu_casemap::CaseMapper;
use icu_locale::LanguageIdentifier;

/// Apply a text-case transform to a single plain-text segment.
///
/// This function handles the core casing logic for a single string.
/// For structured titles with subtitles, use [`apply_to_structured_parts`].
///
/// `.nocase`-protected spans are handled at the Djot rendering layer,
/// not here — this function operates on already-resolved text segments.
#[must_use]
pub fn apply_text_case(text: &str, case: TextCase) -> String {
    apply_text_case_with_language(text, case, None)
}

/// Apply a text-case transform using locale-tailored Unicode case mappings.
///
/// `language` accepts a BCP 47 language tag. Missing or malformed tags use
/// the Unicode root locale, preserving deterministic default behavior.
#[must_use]
pub fn apply_text_case_with_language(text: &str, case: TextCase, language: Option<&str>) -> String {
    let language = language_identifier_for_tag(language);
    apply_text_case_with_language_id(text, case, &language)
}

pub(crate) fn apply_text_case_with_language_id(
    text: &str,
    case: TextCase,
    language: &LanguageIdentifier,
) -> String {
    match case {
        TextCase::AsIs => text.to_string(),
        TextCase::Lowercase => lowercase(text, language),
        TextCase::Uppercase => uppercase(text, language),
        TextCase::CapitalizeFirst => capitalize_first_word_with_language_id(text, language),
        TextCase::Sentence | TextCase::SentenceApa | TextCase::SentenceNlm => {
            to_sentence_case_with_language_id(text, language)
        }
        TextCase::Title => to_title_case_with_language_id(text, language),
    }
}

pub(crate) fn language_identifier_for_tag(language: Option<&str>) -> LanguageIdentifier {
    super::parse_language_identifier(language).unwrap_or_else(root_language_identifier)
}

fn root_language_identifier() -> LanguageIdentifier {
    icu_locale::langid!("und")
}

fn lowercase(text: &str, language: &LanguageIdentifier) -> String {
    CaseMapper::new()
        .lowercase_to_string(text, language)
        .into_owned()
}

fn uppercase(text: &str, language: &LanguageIdentifier) -> String {
    CaseMapper::new()
        .uppercase_to_string(text, language)
        .into_owned()
}

/// Apply text-case to a structured title (main + subtitles).
///
/// The key difference between sentence-case variants:
/// - `SentenceApa`: capitalize first word of main title AND each subtitle
/// - `SentenceNlm`: capitalize first word of main title only
/// - Other variants: applied uniformly to each part
#[must_use]
pub fn apply_to_structured_parts(
    main: &str,
    subtitles: &[&str],
    case: TextCase,
) -> (String, Vec<String>) {
    apply_to_structured_parts_with_language(main, subtitles, case, None)
}

/// Apply locale-tailored text casing to a structured title (main + subtitles).
#[must_use]
pub fn apply_to_structured_parts_with_language(
    main: &str,
    subtitles: &[&str],
    case: TextCase,
    language: Option<&str>,
) -> (String, Vec<String>) {
    let language = language_identifier_for_tag(language);
    match case {
        TextCase::SentenceApa => {
            let main_cased = to_sentence_case_with_language_id(main, &language);
            let subs_cased = subtitles
                .iter()
                .map(|s| to_sentence_case_with_language_id(s, &language))
                .collect();
            (main_cased, subs_cased)
        }
        TextCase::SentenceNlm => {
            let main_cased = to_sentence_case_with_language_id(main, &language);
            // NLM: subtitles keep only explicit/protected capitals (lowercase the rest)
            let subs_cased = subtitles.iter().map(|s| lowercase(s, &language)).collect();
            (main_cased, subs_cased)
        }
        _ => {
            let main_cased = apply_text_case_with_language_id(main, case, &language);
            let subs_cased = subtitles
                .iter()
                .map(|s| apply_text_case_with_language_id(s, case, &language))
                .collect();
            (main_cased, subs_cased)
        }
    }
}

/// Returns true if the given language tag indicates English.
#[must_use]
pub fn is_english_language(lang: Option<&str>) -> bool {
    match lang {
        Some(tag) => {
            let primary = tag.split('-').next().unwrap_or(tag);
            primary.eq_ignore_ascii_case("en")
        }
        // Default: assume English for backward compatibility
        None => true,
    }
}

/// Resolve the effective text-case, applying language fallback.
///
/// For non-English languages without defined transforms, returns `AsIs`.
#[must_use]
pub fn resolve_text_case(case: TextCase, language: Option<&str>) -> TextCase {
    if is_english_language(language) {
        case
    } else {
        // Non-English: only explicit as-is, lowercase, uppercase pass through.
        // All English-specific transforms fall back to as-is.
        match case {
            TextCase::AsIs | TextCase::Lowercase | TextCase::Uppercase => case,
            _ => TextCase::AsIs,
        }
    }
}

/// Apply a note-start text-case transform using the same language fallback rules
/// as other locale-backed casing behavior.
#[must_use]
pub(crate) fn apply_note_start_text_case(
    value: &str,
    text_case: NoteStartTextCase,
    language: Option<&str>,
) -> String {
    let case = match text_case {
        NoteStartTextCase::CapitalizeFirst => TextCase::CapitalizeFirst,
        NoteStartTextCase::Lowercase => TextCase::Lowercase,
    };
    apply_text_case_with_language(value, resolve_text_case(case, language), language)
}

/// Returns true if any character in `word` other than the first is uppercase.
///
/// Per CSL 1.0 / citeproc-js, a word carrying internal capitalization (an
/// all-caps acronym like "DNA", or a mixed-case brand/name like "McDonald" or
/// "iPhone") is presumed deliberately cased and is left untouched by the
/// sentence- and title-case transforms; only words whose casing is limited to
/// (at most) a leading capital are transformed. Uses `char::is_uppercase` so
/// non-ASCII scripts with case are handled correctly.
fn has_internal_uppercase(word: &str) -> bool {
    let mut chars = word.chars();
    chars.next();
    chars.any(char::is_uppercase)
}

/// Rebuild `text` with each whitespace-delimited word replaced by the
/// corresponding entry in `parts`, preserving the original whitespace runs
/// (leading/trailing/internal spacing) exactly as written.
fn rebuild_with_original_whitespace(text: &str, parts: &[String]) -> String {
    let mut result = String::with_capacity(text.len());
    let mut word_iter = parts.iter();
    let mut in_word = false;
    let mut current_word = word_iter.next();

    for ch in text.chars() {
        if ch.is_whitespace() {
            if in_word {
                in_word = false;
                current_word = word_iter.next();
            }
            result.push(ch);
        } else if !in_word && let Some(word) = current_word {
            result.push_str(word);
            in_word = true;
        }
    }

    result
}

/// Convert text to sentence case: lowercase every word and capitalize the
/// first, except words carrying internal capitalization (acronyms, mixed-case
/// names), which are preserved exactly as written — including the first word,
/// which is left unmodified rather than force-capitalized.
#[cfg(test)]
fn to_sentence_case(text: &str) -> String {
    let language = root_language_identifier();
    to_sentence_case_with_language_id(text, &language)
}

fn to_sentence_case_with_language_id(text: &str, language: &LanguageIdentifier) -> String {
    if text.is_empty() {
        return String::new();
    }

    let words: Vec<&str> = text.split_whitespace().collect();
    if words.is_empty() {
        return text.to_string();
    }

    let mut parts: Vec<String> = Vec::with_capacity(words.len());
    for (i, word) in words.iter().enumerate() {
        if has_internal_uppercase(word) {
            parts.push((*word).to_string());
        } else if i == 0 {
            parts.push(capitalize_first_word_with_language_id(
                &lowercase(word, language),
                language,
            ));
        } else {
            parts.push(lowercase(word, language));
        }
    }

    rebuild_with_original_whitespace(text, &parts)
}

/// Capitalize the first alphabetic character of the string,
/// preserving leading whitespace and punctuation.
///
/// A leading digit blocks capitalization entirely rather than being skipped
/// over: a string like `"35 mm film"` has no leading word to capitalize (the
/// first token is a numeral, not a word), so it is left as-is instead of
/// capitalizing the first letter found later in the string (which would
/// wrongly produce `"35 Mm film"`).
#[cfg(test)]
pub(crate) fn capitalize_first_word(text: &str) -> String {
    let language = root_language_identifier();
    capitalize_first_word_with_language_id(text, &language)
}

fn capitalize_first_word_with_language_id(text: &str, language: &LanguageIdentifier) -> String {
    let mut result = String::with_capacity(text.len());
    let mut found_first = false;
    let mut blocked_by_digit = false;
    for ch in text.chars() {
        if !found_first && !blocked_by_digit && ch.is_alphabetic() {
            result.push_str(&uppercase(&ch.to_string(), language));
            found_first = true;
        } else {
            if !found_first && ch.is_ascii_digit() {
                blocked_by_digit = true;
            }
            result.push(ch);
        }
    }
    result
}

/// Capitalize the first alphabetic character of the string, skipping over
/// HTML tags, LaTeX command prefixes, and Typst command prefixes.
///
/// Use this variant when the input may already contain rendered markup from a
/// pre-formatted component. For plain-text input, behaviour is identical to
/// [`capitalize_first_word`].
#[cfg(test)]
pub(crate) fn capitalize_first_word_markup_aware(text: &str) -> String {
    capitalize_first_word_markup_aware_with_language(text, None)
}

pub(crate) fn capitalize_first_word_markup_aware_with_language(
    text: &str,
    language: Option<&str>,
) -> String {
    let language = language_identifier_for_tag(language);
    let bytes = text.as_bytes();
    let len = bytes.len();
    let mut i = 0;

    while i < len {
        let Some(&b) = bytes.get(i) else { break };

        // Skip HTML tag: <...>
        // `i` always points to an ASCII byte here, so the slice is on a char boundary.
        if b == b'<'
            && let Some(end) = text.get(i..).and_then(|s| s.find('>'))
        {
            i += end + 1;
            continue;
        }

        // Skip LaTeX command prefix: \letters{ or \letters[...]{
        if b == b'\\' {
            let cmd_start = i + 1;
            let cmd_len = bytes
                .get(cmd_start..)
                .unwrap_or_default()
                .iter()
                .take_while(|&&c| c.is_ascii_alphabetic())
                .count();
            if cmd_len > 0 {
                let after_cmd = cmd_start + cmd_len;
                // Skip optional [...]
                let after_opt = if bytes.get(after_cmd) == Some(&b'[') {
                    text.get(after_cmd..)
                        .and_then(|s| s.find(']'))
                        .map(|e| after_cmd + e + 1)
                        .unwrap_or(after_cmd)
                } else {
                    after_cmd
                };
                if bytes.get(after_opt) == Some(&b'{') {
                    i = after_opt + 1;
                    continue;
                }
            }
        }

        // Skip Typst command prefix: #letters[
        if b == b'#' {
            let cmd_start = i + 1;
            let cmd_len = bytes
                .get(cmd_start..)
                .unwrap_or_default()
                .iter()
                .take_while(|&&c| c.is_ascii_alphabetic())
                .count();
            if cmd_len > 0 {
                let after_cmd = cmd_start + cmd_len;
                if bytes.get(after_cmd) == Some(&b'[') {
                    i = after_cmd + 1;
                    continue;
                }
            }
        }

        // Decode the next Unicode character. `i` is always on a char boundary:
        // the markup-skip branches only advance past ASCII bytes.
        let ch = text.get(i..).and_then(|s| s.chars().next()).unwrap_or('\0');
        if ch.is_alphabetic() {
            let ch_len = ch.len_utf8();
            let mut result = String::with_capacity(text.len());
            result.push_str(text.get(..i).unwrap_or_default());
            result.push_str(&uppercase(&ch.to_string(), &language));
            result.push_str(text.get(i + ch_len..).unwrap_or_default());
            return result;
        }

        i += ch.len_utf8().max(1);
    }

    text.to_string()
}

/// Apply a text-case transform to a pre-formatted string that may contain
/// rendered markup.
///
/// Delegates to locale-aware markup capitalization for `CapitalizeFirst` and
/// applies the requested locale-aware transform for all other cases.
pub(crate) fn apply_text_case_markup_aware_with_language(
    text: &str,
    case: TextCase,
    language: Option<&str>,
) -> String {
    match case {
        TextCase::CapitalizeFirst => {
            capitalize_first_word_markup_aware_with_language(text, language)
        }
        _ => apply_text_case_with_language(text, case, language),
    }
}

// English title-case stop words (articles, short conjunctions, short prepositions).
const TITLE_CASE_STOP_WORDS: &[&str] = &[
    "a", "an", "and", "as", "at", "but", "by", "for", "from", "in", "nor", "of", "on", "or", "so",
    "the", "to", "up", "yet", "along", "between", "during", "with", "v", "vs",
];

/// Hyphen-like characters that join compound words for title-case purposes.
///
/// Includes the ASCII hyphen-minus and the en dash: bibliographic titles
/// commonly use an en dash as a hyphen substitute in compounds like
/// "Aging–Disability Nexus" (as-typed source data), and CMOS title-cases
/// each component the same way it would for an ASCII-hyphenated compound.
const HYPHEN_LIKE_CHARS: [char; 2] = ['-', '\u{2013}'];

fn contains_hyphen_like(text: &str) -> bool {
    text.contains(HYPHEN_LIKE_CHARS)
}

/// Capitalize non-stop-word components of a hyphen-joined compound for title case.
///
/// Splits on both the ASCII hyphen and the en dash (see [`HYPHEN_LIKE_CHARS`]),
/// preserving whichever separator character was actually used.
fn capitalize_hyphenated(word: &str, language: &LanguageIdentifier) -> String {
    let mut result = String::with_capacity(word.len());
    let mut last_end = 0;
    let part_count = word.matches(HYPHEN_LIKE_CHARS).count() + 1;
    for (part_index, (idx, sep)) in word.match_indices(HYPHEN_LIKE_CHARS).enumerate() {
        let part = word.get(last_end..idx).unwrap_or_default();
        result.push_str(&capitalize_hyphen_part(
            part,
            part_index == 0 || part_index + 1 == part_count,
            language,
        ));
        result.push_str(sep);
        last_end = idx + sep.len();
    }
    let tail = word.get(last_end..).unwrap_or_default();
    result.push_str(&capitalize_hyphen_part(tail, false, language));
    result
}

fn capitalize_hyphen_part(part: &str, force_all: bool, language: &LanguageIdentifier) -> String {
    if force_all {
        capitalize_first_word_with_language_id(part, language)
    } else {
        let alpha_core = part.trim_matches(|c: char| !c.is_alphanumeric());
        if TITLE_CASE_STOP_WORDS.contains(&alpha_core) {
            part.to_string()
        } else {
            capitalize_first_word_with_language_id(part, language)
        }
    }
}

fn trim_trailing_closing_punctuation(word: &str) -> &str {
    word.trim_end_matches(['"', '\'', ')', ']', '}', '»', '', ''])
}

/// Convert text to English headline-style title case.
///
/// Capitalizes the first and last word unconditionally.
/// Interior stop words (articles, short prepositions, conjunctions) stay lowercase.
/// The first word after `:`, `?`, or `!` is always capitalized.
/// Hyphenated compounds capitalize each non-stop-word component.
#[cfg(test)]
fn to_title_case(text: &str) -> String {
    let language = root_language_identifier();
    to_title_case_with_language_id(text, &language)
}

fn to_title_case_with_language_id(text: &str, language: &LanguageIdentifier) -> String {
    if text.is_empty() {
        return String::new();
    }

    let words: Vec<&str> = text.split_whitespace().collect();
    if words.is_empty() {
        return text.to_string();
    }

    let last_idx = words.len() - 1;
    let mut parts: Vec<String> = Vec::with_capacity(words.len());
    let mut capitalize_next = false;

    for (i, word) in words.iter().enumerate() {
        if has_internal_uppercase(word) {
            // Acronym or mixed-case name (e.g. "DNA", "McDonald", "iPhone"):
            // preserve exactly as written, regardless of position.
            parts.push((*word).to_string());
        } else {
            let lower = lowercase(word, language);
            if i == 0 || i == last_idx || capitalize_next {
                if contains_hyphen_like(&lower) {
                    parts.push(capitalize_hyphenated(&lower, language));
                } else {
                    parts.push(capitalize_first_word_with_language_id(&lower, language));
                }
            } else {
                // Strip leading/trailing punctuation when checking stop words so that
                // words like "(and" or "and)" are still treated as the stop word "and".
                let alpha_core = lower.trim_matches(|c: char| !c.is_alphanumeric());
                if TITLE_CASE_STOP_WORDS.contains(&alpha_core) {
                    parts.push(lower);
                } else if contains_hyphen_like(&lower) {
                    parts.push(capitalize_hyphenated(&lower, language));
                } else {
                    parts.push(capitalize_first_word_with_language_id(&lower, language));
                }
            }
        }
        // Capitalize the next word after sentence-ending punctuation or a colon,
        // even when that punctuation is followed by a closing quote or bracket.
        let punctuation_core = trim_trailing_closing_punctuation(word);
        capitalize_next = punctuation_core.ends_with(':')
            || punctuation_core.ends_with('?')
            || punctuation_core.ends_with('!');
    }

    rebuild_with_original_whitespace(text, &parts)
}

#[cfg(test)]
#[allow(
    clippy::unwrap_used,
    clippy::expect_used,
    clippy::panic,
    clippy::indexing_slicing,
    clippy::todo,
    clippy::unimplemented,
    clippy::unreachable,
    clippy::get_unwrap,
    reason = "Panicking is acceptable and often desired in tests."
)]
mod tests {
    use super::*;

    // --- capitalize_first_word ---

    #[test]
    fn test_capitalize_first_word_basic() {
        assert_eq!(capitalize_first_word("hello world"), "Hello world");
    }

    #[test]
    fn test_capitalize_first_word_leading_space() {
        assert_eq!(capitalize_first_word("  hello"), "  Hello");
    }

    #[test]
    fn test_capitalize_first_word_empty() {
        assert_eq!(capitalize_first_word(""), "");
    }

    #[test]
    fn test_capitalize_first_word_already_upper() {
        assert_eq!(capitalize_first_word("Hello"), "Hello");
    }

    #[test]
    fn given_leading_numeral_when_capitalize_first_word_then_left_as_is() {
        // "35 mm film" has no leading word to capitalize; the first letter
        // ("m" in "mm") must not be hunted down and capitalized instead.
        assert_eq!(capitalize_first_word("35 mm film"), "35 mm film");
    }

    #[test]
    fn given_leading_letter_when_capitalize_first_word_then_capitalized() {
        assert_eq!(capitalize_first_word("in Korean"), "In Korean");
    }

    #[test]
    fn turkish_case_mapping_handles_dotted_and_dotless_i() {
        assert_eq!(
            apply_text_case_with_language("istanbul ızmir", TextCase::Uppercase, Some("tr-TR"),),
            "İSTANBUL IZMİR"
        );
        assert_eq!(
            apply_text_case_with_language("İSTANBUL IĞDIR", TextCase::Lowercase, Some("tr-TR"),),
            "istanbul ığdır"
        );
        assert_eq!(
            apply_text_case_with_language("istanbul", TextCase::CapitalizeFirst, Some("tr")),
            "İstanbul"
        );
        assert_eq!(
            apply_text_case_with_language("istanbul ızmir", TextCase::Uppercase, Some("tr_TR")),
            "İSTANBUL IZMİR"
        );
    }

    #[test]
    fn azerbaijani_case_mapping_uses_turkic_rules() {
        assert_eq!(
            apply_text_case_with_language("izmir ı", TextCase::Uppercase, Some("az-Latn-AZ")),
            "İZMİR I"
        );
    }

    #[test]
    fn sentence_case_uses_the_supplied_language() {
        assert_eq!(
            apply_text_case_with_language("istanbul Izmir", TextCase::Sentence, Some("tr-TR"),),
            "İstanbul ızmir"
        );
    }

    #[test]
    fn structured_subtitles_use_the_supplied_language() {
        let (main, subtitles) = apply_to_structured_parts_with_language(
            "istanbul tarihi",
            &["izmir incelemesi"],
            TextCase::SentenceApa,
            Some("tr-TR"),
        );

        assert_eq!(main, "İstanbul tarihi");
        assert_eq!(subtitles, ["İzmir incelemesi"]);
    }

    #[test]
    fn malformed_language_tag_uses_root_case_mapping() {
        assert_eq!(
            apply_text_case_with_language("istanbul", TextCase::Uppercase, Some("not_a_language"),),
            "ISTANBUL"
        );
    }

    #[test]
    fn locale_override_suffix_preserves_base_language_mapping() {
        assert_eq!(
            apply_text_case_with_language(
                "istanbul",
                TextCase::Uppercase,
                Some("tr-TR-citum_override"),
            ),
            "İSTANBUL"
        );
    }

    // --- capitalize_first_word_markup_aware ---

    #[test]
    fn test_capitalize_markup_aware_plain_text() {
        assert_eq!(
            capitalize_first_word_markup_aware("the collected essays"),
            "The collected essays"
        );
    }

    #[test]
    fn test_capitalize_markup_aware_html_tag() {
        assert_eq!(
            capitalize_first_word_markup_aware("<em>the collected essays</em>"),
            "<em>The collected essays</em>"
        );
    }

    #[test]
    fn capitalize_markup_aware_uses_the_supplied_language() {
        assert_eq!(
            apply_text_case_markup_aware_with_language(
                "<em>istanbul</em>",
                TextCase::CapitalizeFirst,
                Some("tr-TR"),
            ),
            "<em>İstanbul</em>"
        );
    }

    #[test]
    fn test_capitalize_markup_aware_html_nested_tags() {
        assert_eq!(
            capitalize_first_word_markup_aware(r#"<span class="x"><em>the title</em></span>"#),
            r#"<span class="x"><em>The title</em></span>"#
        );
    }

    #[test]
    fn test_capitalize_markup_aware_latex_command() {
        assert_eq!(
            capitalize_first_word_markup_aware(r"\emph{the collected essays}"),
            r"\emph{The collected essays}"
        );
    }

    #[test]
    fn test_capitalize_markup_aware_latex_number_not_corrupted() {
        // Regression: \emph{521} must not become \Emph{521}
        assert_eq!(
            capitalize_first_word_markup_aware(r"\emph{521}"),
            r"\emph{521}"
        );
    }

    #[test]
    fn test_capitalize_markup_aware_typst_command() {
        assert_eq!(
            capitalize_first_word_markup_aware("#emph[the collected essays]"),
            "#emph[The collected essays]"
        );
    }

    #[test]
    fn test_capitalize_markup_aware_plain_underscore_delimiters() {
        // PlainText emph uses _..._; _ is non-alphabetic so this was already safe
        assert_eq!(
            capitalize_first_word_markup_aware("_the collected essays_"),
            "_The collected essays_"
        );
    }

    #[test]
    fn test_capitalize_markup_aware_empty_string() {
        assert_eq!(capitalize_first_word_markup_aware(""), "");
    }

    #[test]
    fn test_capitalize_markup_aware_all_markup_no_text() {
        assert_eq!(capitalize_first_word_markup_aware("<em></em>"), "<em></em>");
    }

    // --- to_sentence_case ---

    #[test]
    fn test_sentence_case_basic() {
        assert_eq!(
            to_sentence_case("The Quick Brown Fox"),
            "The quick brown fox"
        );
    }

    #[test]
    fn test_sentence_case_all_caps() {
        // All-caps words are presumed deliberately cased (acronyms) and are
        // preserved verbatim, including as the first word.
        assert_eq!(to_sentence_case("DNA REPLICATION"), "DNA REPLICATION");
    }

    #[test]
    fn test_sentence_case_empty() {
        assert_eq!(to_sentence_case(""), "");
    }

    #[test]
    fn given_mixed_case_word_when_sentence_case_then_preserved() {
        assert_eq!(
            to_sentence_case("An Introduction to DNA"),
            "An introduction to DNA"
        );
    }

    #[test]
    fn given_lowercase_iphone_when_sentence_case_then_lowercased() {
        assert_eq!(to_sentence_case("the iphone problem"), "The iphone problem");
    }

    #[test]
    fn given_mixed_case_iphone_when_sentence_case_then_preserved() {
        assert_eq!(to_sentence_case("the iPhone problem"), "The iPhone problem");
    }

    #[test]
    fn given_mixed_case_surname_when_sentence_case_then_preserved() {
        assert_eq!(
            to_sentence_case("a study of McDonald"),
            "A study of McDonald"
        );
    }

    // --- to_title_case ---

    #[test]
    fn test_title_case_basic() {
        assert_eq!(to_title_case("the quick brown fox"), "The Quick Brown Fox");
    }

    #[test]
    fn test_title_case_stop_words() {
        assert_eq!(
            to_title_case("a tale of two cities"),
            "A Tale of Two Cities"
        );
    }

    #[test]
    fn test_title_case_last_word_capitalized() {
        assert_eq!(
            to_title_case("the world we live in"),
            "The World We Live In"
        );
    }

    #[test]
    fn test_title_case_after_colon() {
        assert_eq!(
            to_title_case("the title: a subtitle"),
            "The Title: A Subtitle"
        );
    }

    #[test]
    fn test_title_case_after_colon_stop_word() {
        // First word after colon is a stop word but must still be capitalized
        assert_eq!(
            to_title_case("history of the world: a new perspective"),
            "History of the World: A New Perspective"
        );
    }

    #[test]
    fn test_title_case_after_question_mark() {
        assert_eq!(
            to_title_case("who's black and why? a hidden chapter"),
            "Who's Black and Why? A Hidden Chapter"
        );
    }

    #[test]
    fn test_title_case_after_question_mark_with_closing_quote() {
        assert_eq!(
            to_title_case("who's black and why?\" a hidden chapter"),
            "Who's Black and Why?\" A Hidden Chapter"
        );
    }

    #[test]
    fn test_title_case_from_is_stop_word() {
        assert_eq!(
            to_title_case("a hidden chapter from the eighteenth-century invention of race"),
            "A Hidden Chapter from the Eighteenth-Century Invention of Race"
        );
    }

    #[test]
    fn test_title_case_hyphenated_compound() {
        assert_eq!(
            to_title_case("eighteenth-century studies"),
            "Eighteenth-Century Studies"
        );
    }

    #[test]
    fn test_title_case_hyphenated_stop_word_part() {
        // "well-to-do": "to" is a stop word → stays lowercase in interior position
        assert_eq!(to_title_case("a well-to-do family"), "A Well-to-Do Family");
    }

    #[test]
    fn given_hyphenated_title_with_stop_word_when_title_case_then_interior_stop_word_stays_lowercase()
     {
        assert_eq!(
            to_title_case("text-to-speech systems"),
            "Text-to-Speech Systems"
        );
    }

    #[test]
    fn given_long_title_prepositions_when_title_case_then_interior_stop_words_stay_lowercase() {
        assert_eq!(
            to_title_case(
                "fighting for forests: protection and exploitation during the war with China"
            ),
            "Fighting for Forests: Protection and Exploitation during the War with China"
        );
    }

    #[test]
    fn given_en_dash_compound_when_title_case_then_both_sides_capitalized() {
        // Source data sometimes uses an en dash ("–") as a hyphen substitute
        // in a compound like "Aging–Disability"; CMOS title-cases each side
        // the same way it would for an ASCII-hyphenated compound.
        assert_eq!(
            to_title_case("the aging\u{2013}disability nexus"),
            "The Aging\u{2013}Disability Nexus"
        );
    }

    // --- apply_to_structured_parts ---

    #[test]
    fn test_sentence_apa_structured() {
        let (main, subs) = apply_to_structured_parts(
            "Understanding Citation Systems",
            &["History and Practice", "A Comparative View"],
            TextCase::SentenceApa,
        );
        assert_eq!(main, "Understanding citation systems");
        assert_eq!(subs, vec!["History and practice", "A comparative view"]);
    }

    #[test]
    fn test_sentence_nlm_structured() {
        let (main, subs) = apply_to_structured_parts(
            "Understanding Citation Systems",
            &["History and Practice"],
            TextCase::SentenceNlm,
        );
        assert_eq!(main, "Understanding citation systems");
        // NLM: subtitles lowercased (no first-word capitalization)
        assert_eq!(subs, vec!["history and practice"]);
    }

    #[test]
    fn test_title_case_structured() {
        // "DNA" is already mixed-case in the source data and must be
        // preserved verbatim by the title-case transform.
        let (main, subs) =
            apply_to_structured_parts("the DNA of empire", &["a new perspective"], TextCase::Title);
        assert_eq!(main, "The DNA of Empire");
        assert_eq!(subs, vec!["A New Perspective"]);
    }

    #[test]
    fn given_mixed_case_surname_when_title_case_then_preserved() {
        assert_eq!(to_title_case("a study of McDonald"), "A Study of McDonald");
    }

    // --- resolve_text_case ---

    #[test]
    fn test_english_language_detection() {
        assert!(is_english_language(Some("en")));
        assert!(is_english_language(Some("en-US")));
        assert!(is_english_language(Some("en-GB")));
        assert!(is_english_language(None));
        assert!(!is_english_language(Some("de")));
        assert!(!is_english_language(Some("fr-FR")));
    }

    #[test]
    fn test_resolve_non_english_falls_back() {
        assert_eq!(
            resolve_text_case(TextCase::SentenceApa, Some("de")),
            TextCase::AsIs
        );
        assert_eq!(
            resolve_text_case(TextCase::Title, Some("fr")),
            TextCase::AsIs
        );
        // Explicit lowercase/uppercase pass through for any language
        assert_eq!(
            resolve_text_case(TextCase::Lowercase, Some("de")),
            TextCase::Lowercase
        );
    }

    #[test]
    fn test_resolve_english_passes_through() {
        assert_eq!(
            resolve_text_case(TextCase::SentenceApa, Some("en")),
            TextCase::SentenceApa
        );
        assert_eq!(
            resolve_text_case(TextCase::Title, Some("en-US")),
            TextCase::Title
        );
    }

    #[test]
    fn test_note_start_capitalize_first_uses_english_language_rules() {
        assert_eq!(
            apply_note_start_text_case(
                "edited by",
                NoteStartTextCase::CapitalizeFirst,
                Some("en-US"),
            ),
            "Edited by"
        );
    }

    #[test]
    fn test_note_start_capitalize_first_falls_back_to_as_is_for_non_english() {
        assert_eq!(
            apply_note_start_text_case(
                "hg. von",
                NoteStartTextCase::CapitalizeFirst,
                Some("de-DE"),
            ),
            "hg. von"
        );
    }

    #[test]
    fn test_note_start_capitalize_first_is_no_op_for_uncased_scripts() {
        assert_eq!(
            apply_note_start_text_case("ابن سينا", NoteStartTextCase::CapitalizeFirst, Some("ar"),),
            "ابن سينا"
        );
    }
}