Skip to content

Commit

Permalink
Sanitize more invalid characters in the module ID; #33
Browse files Browse the repository at this point in the history
  • Loading branch information
msuchane committed May 24, 2022
1 parent 28c768a commit 49f4ad3
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ impl Input {
pub fn id(&self) -> String {
let title = &self.title;
// The ID is all lower-case
let mut title_with_replacements = String::from(title).to_lowercase();
let mut title_with_replacements: String = String::from(title).to_lowercase();

// Replace characters that aren't allowed in the ID, usually with a dash or an empty string
let substitutions = [
Expand Down Expand Up @@ -125,7 +125,7 @@ impl Input {
("]", ""),
// TODO: Curly braces shouldn't appear in the title in the first place.
// They'd be interpreted as attributes there.
// Print an error in that case? Escape them with AciiDoc escapes?
// Print an error in that case? Escape them with AsciiDoc escapes?
("{", ""),
("}", ""),
];
Expand All @@ -135,12 +135,24 @@ impl Input {
title_with_replacements = title_with_replacements.replace(old, new);
}

// Make sure the converted ID doesn't contain double dashes ("--"), because
// Replace remaining characters that aren't ASCII, or that are non-alphanumeric ASCII,
// with dashes. For example, this replaces diacritics and typographic quotation marks.
title_with_replacements = title_with_replacements.chars()
.map(|c| if c.is_ascii_alphanumeric() { c } else { '-' })
.collect();

// Ensure the converted ID doesn't contain double dashes ("--"), because
// that breaks references to the ID
while title_with_replacements.contains("--") {
title_with_replacements = title_with_replacements.replace("--", "-");
}

// Ensure that the ID doesn't end with a dash
if title_with_replacements.ends_with("-") {
let len = title_with_replacements.len();
title_with_replacements = title_with_replacements[..len-1].to_string();
}

let prefix = self.prefix();

format!("{}{}", prefix, title_with_replacements)
Expand Down

0 comments on commit 49f4ad3

Please sign in to comment.