From 79f00eeea3e2db027696de64741bc71fc8e67c39 Mon Sep 17 00:00:00 2001 From: steveklabnik Date: Mon, 6 Mar 2017 12:23:15 -0500 Subject: [PATCH 1/3] Implement playpen support for ```rust Fixes #29 --- src/renderer/html_handlebars/hbs_renderer.rs | 26 ++++++++++++++++---- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/src/renderer/html_handlebars/hbs_renderer.rs b/src/renderer/html_handlebars/hbs_renderer.rs index 7c06fd62..124c8968 100644 --- a/src/renderer/html_handlebars/hbs_renderer.rs +++ b/src/renderer/html_handlebars/hbs_renderer.rs @@ -97,12 +97,11 @@ impl Renderer for HtmlHandlebars { 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 = fix_anchor_links(rendered, filename.to_str().unwrap_or("")); - - // fix code blocks let rendered = fix_code_blocks(rendered); + let rendered = add_playpen_pre(rendered); // Write to file info!("[*] Creating {:?} ✓", filename.display()); @@ -146,11 +145,12 @@ impl Renderer for HtmlHandlebars { debug!("[*]: Render template"); let rendered = try!(handlebars.render("index", &data)); + + // do several kinds of post-processing let rendered = build_header_links(rendered, "print.html"); let rendered = fix_anchor_links(rendered, "print.html"); - - // fix code blocks 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())); info!("[*] Creating print.html ✓"); @@ -292,3 +292,19 @@ fn fix_code_blocks(html: String) -> String { format!("", before=before, classes=classes, after=after) }).into_owned() } + +fn add_playpen_pre(html: String) -> String { + let regex = Regex::new(r##"((?s)]?class="([^"]+)".*?>.*?)"##).unwrap(); + regex.replace_all(&html, |caps: &Captures| { + let text = &caps[1]; + let classes = &caps[2]; + + if classes.contains("language-rust") && !classes.contains("ignore") { + // wrap the contents in an external pre block + format!("
{}
", text) + } else { + // not language-rust, so no-op + format!("{}", text) + } + }).into_owned() +} From c5f9625feb102406324f18ec717b350cd8eaeee6 Mon Sep 17 00:00:00 2001 From: steveklabnik Date: Mon, 6 Mar 2017 13:27:25 -0500 Subject: [PATCH 2/3] inject main --- src/renderer/html_handlebars/hbs_renderer.rs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/renderer/html_handlebars/hbs_renderer.rs b/src/renderer/html_handlebars/hbs_renderer.rs index 124c8968..65ec2d43 100644 --- a/src/renderer/html_handlebars/hbs_renderer.rs +++ b/src/renderer/html_handlebars/hbs_renderer.rs @@ -294,14 +294,23 @@ fn fix_code_blocks(html: String) -> String { } fn add_playpen_pre(html: String) -> String { - let regex = Regex::new(r##"((?s)]?class="([^"]+)".*?>.*?)"##).unwrap(); + let regex = Regex::new(r##"((?s)]?class="([^"]+)".*?>(.*?))"##).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 - format!("
{}
", text) + + if text.contains("fn main") { + format!("
{}
", text) + } else { + // we need to inject our own main + format!("
#fn main() {{
+{}
+#}}
", classes, code) + } } else { // not language-rust, so no-op format!("{}", text) From b120ce739785a115cd1627608d831ac796899d27 Mon Sep 17 00:00:00 2001 From: steveklabnik Date: Fri, 10 Mar 2017 09:46:11 -0500 Subject: [PATCH 3/3] inject allow(unused_variables) --- src/renderer/html_handlebars/hbs_renderer.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/renderer/html_handlebars/hbs_renderer.rs b/src/renderer/html_handlebars/hbs_renderer.rs index 65ec2d43..a36fc83a 100644 --- a/src/renderer/html_handlebars/hbs_renderer.rs +++ b/src/renderer/html_handlebars/hbs_renderer.rs @@ -307,7 +307,9 @@ fn add_playpen_pre(html: String) -> String { format!("
{}
", text) } else { // we need to inject our own main - format!("
#fn main() {{
+                format!("
# #![allow(unused_variables)]
+# 
+#fn main() {{
 {}
 #}}
", classes, code) }