diff options
author | Benedikt Peetz <benedikt.peetz@b-peetz.de> | 2024-09-29 10:35:23 +0200 |
---|---|---|
committer | Benedikt Peetz <benedikt.peetz@b-peetz.de> | 2024-09-29 10:35:23 +0200 |
commit | 0f9dcd571465a338fb0a0928f4cba430aabac343 (patch) | |
tree | feef2f1563cba72ba7861c38bde44274221fde94 /src | |
parent | feat(templates): Provide a consistent syntax for replacements (diff) | |
download | lpm-0f9dcd571465a338fb0a0928f4cba430aabac343.tar.gz lpm-0f9dcd571465a338fb0a0928f4cba430aabac343.zip |
fix(MangledName): Ensure that the mangled names are actually correct
Diffstat (limited to 'src')
-rw-r--r-- | src/new/chapter.rs | 7 | ||||
-rw-r--r-- | src/new/mod.rs | 40 |
2 files changed, 44 insertions, 3 deletions
diff --git a/src/new/chapter.rs b/src/new/chapter.rs index fcc075d..7628db6 100644 --- a/src/new/chapter.rs +++ b/src/new/chapter.rs @@ -113,7 +113,12 @@ fn get_last_chapter_name(project_root: &Path) -> anyhow::Result<Option<ChapterNa let number: u32 = raw_components.0.parse().expect("Will be a number"); // The name is already mangled - assert!(MangledName::check_mangled(raw_components.1)); + assert!( + MangledName::check_mangled(raw_components.1), + "'{}' is not yet mangled?! It should be: '{}'", + raw_components.1, + MangledName::new(raw_components.1) + ); let name: MangledName = MangledName::from_str_unsafe(raw_components.1); Ok(Some(ChapterName::from_components(name, number))) diff --git a/src/new/mod.rs b/src/new/mod.rs index 04b75ef..0c4114c 100644 --- a/src/new/mod.rs +++ b/src/new/mod.rs @@ -12,8 +12,15 @@ pub struct MangledName(String); impl MangledName { pub fn new(name: &str) -> Self { - let ascii_name = deunicode(&name); - Self(ascii_name.to_case(Case::Snake)) + let safe_name = name + // TeX fails to include/input stuff with a colon in it. + .replace(':', "_") + // This obviously creates weird extra directories + .replace('/', "_"); + + let ascii_name = deunicode(&safe_name.to_case(Case::Snake)).to_case(Case::Snake); + + Self(ascii_name) } pub fn from_str_unsafe(name: &str) -> Self { @@ -40,3 +47,32 @@ impl Display for MangledName { 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"); + } +} |