Merge pull request #222 from steveklabnik/gh29

Implement playpen support for ```rust
This commit is contained in:
Steve Klabnik 2017-03-10 08:59:15 -08:00 committed by GitHub
commit c6a5d12002
1 changed files with 32 additions and 5 deletions

View File

@ -97,12 +97,11 @@ impl Renderer for HtmlHandlebars {
let filename = Path::new(&ch.path).with_extension("html"); let filename = Path::new(&ch.path).with_extension("html");
// create links for headers and fix anchors // Do several kinds of post-processing
let rendered = build_header_links(rendered, filename.to_str().unwrap_or("")); let rendered = build_header_links(rendered, filename.to_str().unwrap_or(""));
let rendered = fix_anchor_links(rendered, filename.to_str().unwrap_or("")); let rendered = fix_anchor_links(rendered, filename.to_str().unwrap_or(""));
// fix code blocks
let rendered = fix_code_blocks(rendered); let rendered = fix_code_blocks(rendered);
let rendered = add_playpen_pre(rendered);
// Write to file // Write to file
info!("[*] Creating {:?} ✓", filename.display()); info!("[*] Creating {:?} ✓", filename.display());
@ -146,11 +145,12 @@ impl Renderer for HtmlHandlebars {
debug!("[*]: Render template"); debug!("[*]: Render template");
let rendered = try!(handlebars.render("index", &data)); let rendered = try!(handlebars.render("index", &data));
// do several kinds of post-processing
let rendered = build_header_links(rendered, "print.html"); let rendered = build_header_links(rendered, "print.html");
let rendered = fix_anchor_links(rendered, "print.html"); let rendered = fix_anchor_links(rendered, "print.html");
// fix code blocks
let rendered = fix_code_blocks(rendered); let rendered = fix_code_blocks(rendered);
let rendered = add_playpen_pre(rendered);
try!(book.write_file(Path::new("print").with_extension("html"), &rendered.into_bytes())); try!(book.write_file(Path::new("print").with_extension("html"), &rendered.into_bytes()));
info!("[*] Creating print.html ✓"); info!("[*] Creating print.html ✓");
@ -292,3 +292,30 @@ fn fix_code_blocks(html: String) -> String {
format!("<code{before}class=\"{classes}\"{after}>", before=before, classes=classes, after=after) format!("<code{before}class=\"{classes}\"{after}>", before=before, classes=classes, after=after)
}).into_owned() }).into_owned()
} }
fn add_playpen_pre(html: String) -> String {
let regex = Regex::new(r##"((?s)<code[^>]?class="([^"]+)".*?>(.*?)</code>)"##).unwrap();
regex.replace_all(&html, |caps: &Captures| {
let text = &caps[1];
let classes = &caps[2];
let code = &caps[3];
if classes.contains("language-rust") && !classes.contains("ignore") {
// wrap the contents in an external pre block
if text.contains("fn main") {
format!("<pre class=\"playpen\">{}</pre>", text)
} else {
// we need to inject our own main
format!("<pre class=\"playpen\"><code class=\"{}\"># #![allow(unused_variables)]
#
#fn main() {{
{}
#}}</code></pre>", classes, code)
}
} else {
// not language-rust, so no-op
format!("{}", text)
}
}).into_owned()
}