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
|
use std::fmt::Display;
use convert_case::{Case, Casing};
use deunicode::deunicode;
pub mod replacement;
pub mod chapter;
pub mod section;
pub mod figure;
#[derive(PartialEq, Eq, PartialOrd, Ord)]
pub struct MangledName(String);
impl MangledName {
pub fn new(name: &str) -> Self {
let new_name = deunicode(&name.to_case(Case::Snake));
let safe_name = new_name
// TeX fails to include/input stuff with a colon in it.
.replace(':', "_")
// This obviously creates weird extra directories
.replace('/', "_")
// These are just difficult to use on the command line
.replace('"', "_")
.replace('\'', "_");
let ascii_name = safe_name.to_case(Case::Snake);
Self(ascii_name)
}
pub fn from_str_unsafe(name: &str) -> Self {
Self(name.to_owned())
}
pub fn check_mangled(name: &str) -> bool {
let mangled = Self::new(name);
let normal = Self::from_str_unsafe(name);
mangled == normal
}
pub fn as_str(&self) -> &str {
&self.0
}
pub fn try_unmangle(self) -> String {
self.0.to_case(Case::Title)
}
}
impl Display for MangledName {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(&self.0)
}
}
#[cfg(test)]
mod test {
use crate::new::MangledName;
#[test]
fn test_names() {
let name = MangledName::new("Æneid");
assert_eq!(name.to_string().as_str(), "aeneid");
let name = MangledName::new("étude");
assert_eq!(name.to_string().as_str(), "etude");
let name = MangledName::new("Ästhetik");
assert_eq!(name.to_string().as_str(), "asthetik");
let name = MangledName::new("北亰");
assert_eq!(name.to_string().as_str(), "bei_jing");
let name = MangledName::new("ᔕᓇᓇ");
assert_eq!(name.to_string().as_str(), "shanana");
let name = MangledName::new("げんまい茶");
assert_eq!(name.to_string().as_str(), "genmai_cha");
let name = MangledName::new("🦄☣");
assert_eq!(name.to_string().as_str(), "unicorn_biohazard");
}
}
|