mirror of
https://github.com/rust-lang/mdBook
synced 2024-12-13 22:32:35 +00:00
Fix code blocks with comma separated classes
This commit is contained in:
parent
0c624d0f74
commit
01341a7705
1 changed files with 25 additions and 1 deletions
|
@ -94,10 +94,13 @@ impl Renderer for HtmlHandlebars {
|
|||
// Render the handlebars template with the data
|
||||
debug!("[*]: Render template");
|
||||
let rendered = try!(handlebars.render("index", &data));
|
||||
|
||||
|
||||
// create links for headers
|
||||
let rendered = build_header_links(rendered);
|
||||
|
||||
// fix code blocks
|
||||
let rendered = fix_code_blocks(rendered);
|
||||
|
||||
// Write to file
|
||||
let filename = Path::new(&ch.path).with_extension("html");
|
||||
info!("[*] Creating {:?} ✓", filename.display());
|
||||
|
@ -143,6 +146,9 @@ impl Renderer for HtmlHandlebars {
|
|||
let rendered = try!(handlebars.render("index", &data));
|
||||
let rendered = build_header_links(rendered);
|
||||
|
||||
// fix code blocks
|
||||
let rendered = fix_code_blocks(rendered);
|
||||
|
||||
try!(book.write_file(Path::new("print").with_extension("html"), &rendered.into_bytes()));
|
||||
info!("[*] Creating print.html ✓");
|
||||
|
||||
|
@ -248,3 +254,21 @@ fn build_header_links(html: String) -> String {
|
|||
format!("<a class=\"header\" href=\"#{id}\" name=\"{id}\"><h{level}>{text}</h{level}></a>", level=level, id=id, text=text)
|
||||
}).into_owned()
|
||||
}
|
||||
|
||||
// The rust book uses annotations for rustdoc to test code snippets, like the following:
|
||||
// ```rust,should_panic
|
||||
// fn main() {
|
||||
// // Code here
|
||||
// }
|
||||
// ```
|
||||
// This function replaces all commas by spaces in the code block classes
|
||||
fn fix_code_blocks(html: String) -> String {
|
||||
let regex = Regex::new(r##"<code([^>]+)class="([^"]+)"([^>]*)>"##).unwrap();
|
||||
regex.replace_all(&html, |caps: &Captures| {
|
||||
let before = &caps[1];
|
||||
let classes = &caps[2].replace(",", " ");
|
||||
let after = &caps[3];
|
||||
|
||||
format!("<code{before}class=\"{classes}\"{after}>", before=before, classes=classes, after=after)
|
||||
}).into_owned()
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue