crowbook_text_processing/
caps.rs1use regex::{Regex, Captures};
11use std::borrow::Cow;
12
13pub fn latex<'a, S: Into<Cow<'a, str>>>(input: S) -> Cow<'a, str> {
31 let mut res = input.into();
32
33 lazy_static! {
34 static ref REGEX: Regex = Regex::new(r"\b\p{Lu}{2,}\b").unwrap();
35 static ref REGEX_DOTS: Regex = Regex::new(r"\b((\p{Lu}\.){1,}\p{Lu})\b").unwrap();
36 }
37
38 for cap in REGEX.captures_iter(&res) {
39 println!("capture: {:?}", cap);
40 }
41
42 if REGEX.is_match(&res) {
43 let tmp = REGEX.replace_all(&res, |caps: &Captures| {
44 format!("\\textsc{{{}}}",
45 caps[0].to_lowercase())
46 });
47 res = Cow::Owned(tmp.into_owned())
48 }
49 if REGEX_DOTS.is_match(&res) {
50 let tmp = REGEX_DOTS.replace_all(&res, |caps: &Captures| {
51 format!("\\textsc{{{}}}",
52 caps[0].to_lowercase())
53 });
54 res = Cow::Owned(tmp.into_owned())
55 }
56 res
57}
58
59
60#[test]
61fn latex_1() {
62 use crate::caps;
63
64
65 let s = caps::latex("Some ACRONYM or SCREAMING or whatever.");
66 assert_eq!(&s, "Some \\textsc{acronym} or \\textsc{screaming} or whatever.");
67
68 let s = caps::latex("Nothing to change.");
69 assert_eq!(&s, "Nothing to change.");
70
71 let s = caps::latex("A single letter is not capitalized. TWO or more are.");
72 assert_eq!(&s, "A single letter is not capitalized. \\textsc{two} or more are.");
73
74 let s = caps::latex("BEGIN with caps");
75 assert_eq!(&s, "\\textsc{begin} with caps");
76
77 let s = caps::latex("BEGINning with caps");
78 assert_eq!(&s, "BEGINning with caps");
79
80 let s = caps::latex("Ending with CAPS");
81 assert_eq!(&s, "Ending with \\textsc{caps}");
82
83 let s = caps::latex("Some A.W.D (Acronym With Dots)");
84 assert_eq!(&s, "Some \\textsc{a.w.d} (Acronym With Dots)");
85
86 let s = caps::latex("Sentence ennding with A.W.D.");
87 assert_eq!(&s, "Sentence ennding with \\textsc{a.w.d}.");
88}