",
- "
",
- "",
- "",
- "<",
- ">",
- "&",
- "'",
- """];
- for sub in repl_sub {
- id = id.replace(sub, "");
- }
- let id = id.chars()
- .filter_map(|c| if c.is_alphanumeric() || c == '-' || c == '_' {
- if c.is_ascii() {
- Some(c.to_ascii_lowercase())
- } else {
- Some(c)
- }
- } else if c.is_whitespace() && c.is_ascii() {
- Some('-')
- } else {
- None
- })
- .collect::",
+ "
",
+ "",
+ "",
+ "<",
+ ">",
+ "&",
+ "'",
+ """,
+ ];
+ for sub in repl_sub {
+ content = content.replace(sub, "");
+ }
+
+ let mut id = String::new();
+
+ for c in content.chars() {
+ if c.is_alphanumeric() || c == '-' || c == '_' {
+ id.push(c.to_ascii_lowercase());
+ } else if c.is_whitespace() {
+ id.push(c);
+ }
+ }
+
+ id
+}
+
// anchors to the same page (href="#anchor") do not work because of
// ", before = before, classes = classes, after = after)
+ format!(r#""#, before = before, classes = classes, after = after)
})
.into_owned()
}
@@ -412,14 +532,16 @@ fn add_playpen_pre(html: String) -> String {
} else {
// we need to inject our own main
let (attrs, code) = partition_source(code);
- format!("# #![allow(unused_variables)]
+ format!(
+ "# #![allow(unused_variables)]
{}#fn main() {{
\
{}
#}}
",
- classes,
- attrs,
- code)
+ classes,
+ attrs,
+ code
+ )
}
} else {
// not language-rust, so no-op
@@ -449,3 +571,37 @@ fn partition_source(s: &str) -> (String, String) {
(before, after)
}
+
+
+struct RenderItemContext<'a> {
+ handlebars: &'a Handlebars,
+ book: &'a MDBook,
+ destination: PathBuf,
+ data: serde_json::Map,
+ is_index: bool,
+}
+
+
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn original_build_header_links() {
+ let inputs = vec![
+ ("blah blah Foo
", r#"blah blah Foo
"#),
+ ("Foo
", r#"Foo
"#),
+ ("Foo^bar
", r#"Foo^bar
"#),
+ ("", r#""#),
+ ("Hï
", r#"Hï
"#),
+ ("Foo
Foo
",
+ r#"Foo
Foo
"#),
+ ];
+
+ for (src, should_be) in inputs {
+ let got = build_header_links(src.to_string(), "bar.rs");
+ assert_eq!(got, should_be);
+ }
+ }
+}