Cleaned up the filter_map for normalizing id's using a more readable procedural style

This commit is contained in:
Michael Bryan 2017-06-20 11:23:53 +08:00
parent 8c30de16d6
commit 33f3bec301
1 changed files with 17 additions and 16 deletions

View File

@ -434,6 +434,8 @@ fn wrap_header_with_link(level: usize, content: &str, id_counter: &mut HashMap<S
) )
} }
/// Generate an id for use with anchors which is derived from a "normalised"
/// string.
fn id_from_content(content: &str) -> String { fn id_from_content(content: &str) -> String {
let mut content = content.to_string(); let mut content = content.to_string();
@ -455,19 +457,17 @@ fn id_from_content(content: &str) -> String {
content = content.replace(sub, ""); content = content.replace(sub, "");
} }
content.chars() let mut id = String::new();
.filter_map(|c| if c.is_alphanumeric() || c == '-' || c == '_' {
if c.is_ascii() { for c in content.chars() {
Some(c.to_ascii_lowercase()) if c.is_alphanumeric() || c == '-' || c == '_' {
} else { id.push(c.to_ascii_lowercase());
Some(c) } else if c.is_whitespace() {
} id.push(c);
} else if c.is_whitespace() && c.is_ascii() { }
Some('-') }
} else {
None id
})
.collect()
} }
// anchors to the same page (href="#anchor") do not work because of // anchors to the same page (href="#anchor") do not work because of
@ -509,7 +509,7 @@ fn fix_code_blocks(html: String) -> String {
let classes = &caps[2].replace(",", " "); let classes = &caps[2].replace(",", " ");
let after = &caps[3]; let after = &caps[3];
format!("<code{before}class=\"{classes}\"{after}>", before = before, classes = classes, after = after) format!(r#"<code{before}class="{classes}"{after}>"#, before = before, classes = classes, after = after)
}) })
.into_owned() .into_owned()
} }
@ -593,7 +593,8 @@ mod tests {
("<h3>Foo^bar</h3>", r#"<a class="header" href="bar.rs#foobar" id="foobar"><h3>Foo^bar</h3></a>"#), ("<h3>Foo^bar</h3>", r#"<a class="header" href="bar.rs#foobar" id="foobar"><h3>Foo^bar</h3></a>"#),
("<h4></h4>", r#"<a class="header" href="bar.rs#" id=""><h4></h4></a>"#), ("<h4></h4>", r#"<a class="header" href="bar.rs#" id=""><h4></h4></a>"#),
("<h4><em>Hï</em></h4>", r#"<a class="header" href="bar.rs#hï" id="hï"><h4><em>Hï</em></h4></a>"#), ("<h4><em>Hï</em></h4>", r#"<a class="header" href="bar.rs#hï" id="hï"><h4><em>Hï</em></h4></a>"#),
("<h1>Foo</h1><h3>Foo</h3>", r#"<a class="header" href="bar.rs#foo" id="foo"><h1>Foo</h1></a><a class="header" href="bar.rs#foo-1" id="foo-1"><h3>Foo</h3></a>"#), ("<h1>Foo</h1><h3>Foo</h3>",
r#"<a class="header" href="bar.rs#foo" id="foo"><h1>Foo</h1></a><a class="header" href="bar.rs#foo-1" id="foo-1"><h3>Foo</h3></a>"#),
]; ];
for (src, should_be) in inputs { for (src, should_be) in inputs {