From e2a860071237928f39b97ad0adff04f6d861e1bb Mon Sep 17 00:00:00 2001
From: Benedikt Werner <1benediktwerner@gmail.com>
Date: Mon, 4 Nov 2019 13:34:38 +0100
Subject: [PATCH 1/4] Remove outdated unused var in theme js code
---
src/theme/book.js | 3 ---
1 file changed, 3 deletions(-)
diff --git a/src/theme/book.js b/src/theme/book.js
index 328151ca87..186f9ae111 100644
--- a/src/theme/book.js
+++ b/src/theme/book.js
@@ -16,9 +16,6 @@ function playpen_text(playpen) {
}
(function codeSnippets() {
- // Hide Rust code lines prepended with a specific character
- var hiding_character = "#";
-
function fetch_with_timeout(url, options, timeout = 6000) {
return Promise.race([
fetch(url, options),
From f5549f2267447d046add45c5a032d3b6ccc652f9 Mon Sep 17 00:00:00 2001
From: Benedikt Werner <1benediktwerner@gmail.com>
Date: Mon, 4 Nov 2019 13:58:37 +0100
Subject: [PATCH 2/4] Hide empty lines starting with '#' in playpens
---
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 27d9f61028..041acbf24d 100644
--- a/src/renderer/html_handlebars/hbs_renderer.rs
+++ b/src/renderer/html_handlebars/hbs_renderer.rs
@@ -601,7 +601,7 @@ fn fix_code_blocks(html: &str) -> String {
}
fn add_playpen_pre(html: &str, playpen_config: &Playpen) -> String {
- let boring_line_regex = Regex::new(r"^(\s*)#(#|.)(.*)$").unwrap();
+ let boring_line_regex = Regex::new(r"^(\s*)#(.?)(.*)$").unwrap();
let regex = Regex::new(r##"((?s)]?class="([^"]+)".*?>(.*?)
)"##).unwrap();
regex
.replace_all(html, |caps: &Captures<'_>| {
@@ -747,6 +747,8 @@ mod tests {
"
let s = \"foo\n bar\n\";\n
"),
("let s = \"foo\n ## bar\n\";
",
"let s = \"foo\n # bar\n\";\n
"),
+ ("let s = \"foo\n # bar\n#\n\";
",
+ "let s = \"foo\n bar\n\n\";\n
"),
];
for (src, should_be) in &inputs {
let got = add_playpen_pre(
From 8fffb2a704c0124e314bab34db0dadea22fc3613 Mon Sep 17 00:00:00 2001
From: Benedikt Werner <1benediktwerner@gmail.com>
Date: Thu, 7 Nov 2019 02:20:10 +0100
Subject: [PATCH 3/4] Hide lines in ignored code blocks
---
src/renderer/html_handlebars/hbs_renderer.rs | 109 ++++++++++---------
1 file changed, 60 insertions(+), 49 deletions(-)
diff --git a/src/renderer/html_handlebars/hbs_renderer.rs b/src/renderer/html_handlebars/hbs_renderer.rs
index 041acbf24d..de7c512597 100644
--- a/src/renderer/html_handlebars/hbs_renderer.rs
+++ b/src/renderer/html_handlebars/hbs_renderer.rs
@@ -601,7 +601,6 @@ fn fix_code_blocks(html: &str) -> String {
}
fn add_playpen_pre(html: &str, playpen_config: &Playpen) -> String {
- let boring_line_regex = Regex::new(r"^(\s*)#(.?)(.*)$").unwrap();
let regex = Regex::new(r##"((?s)]?class="([^"]+)".*?>(.*?)
)"##).unwrap();
regex
.replace_all(html, |caps: &Captures<'_>| {
@@ -609,57 +608,37 @@ fn add_playpen_pre(html: &str, playpen_config: &Playpen) -> String {
let classes = &caps[2];
let code = &caps[3];
- if (classes.contains("language-rust")
- && !classes.contains("ignore")
- && !classes.contains("noplaypen"))
- || classes.contains("mdbook-runnable")
- {
- // wrap the contents in an external pre block
- format!(
- "{}
",
- classes,
- {
- let content: Cow<'_, str> = if playpen_config.editable
- && classes.contains("editable")
- || text.contains("fn main")
- || text.contains("quick_main!")
+ if classes.contains("language-rust") {
+ if (!classes.contains("ignore") && !classes.contains("noplaypen"))
+ || classes.contains("mdbook-runnable")
+ {
+ // wrap the contents in an external pre block
+ format!(
+ "{}
",
+ classes,
{
- code.into()
- } else {
- // we need to inject our own main
- let (attrs, code) = partition_source(code);
-
- format!(
- "\n# #![allow(unused_variables)]\n{}#fn main() {{\n{}#}}",
- attrs, code
- )
- .into()
- };
- let mut prev_line_hidden = false;
- let mut result = String::with_capacity(content.len());
- for line in content.lines() {
- if let Some(caps) = boring_line_regex.captures(line) {
- if !prev_line_hidden && &caps[2] != "#" {
- result += "";
- prev_line_hidden = true;
- }
- result += &caps[1];
- if &caps[2] != " " {
- result += &caps[2];
- }
- result += &caps[3];
+ let content: Cow<'_, str> = if playpen_config.editable
+ && classes.contains("editable")
+ || text.contains("fn main")
+ || text.contains("quick_main!")
+ {
+ code.into()
} else {
- if prev_line_hidden {
- result += "";
- prev_line_hidden = false;
- }
- result += line;
- }
- result += "\n";
+ // we need to inject our own main
+ let (attrs, code) = partition_source(code);
+
+ format!(
+ "\n# #![allow(unused_variables)]\n{}#fn main() {{\n{}#}}",
+ attrs, code
+ )
+ .into()
+ };
+ hide_lines(&content)
}
- result
- }
- )
+ )
+ } else {
+ format!("{}
", classes, hide_lines(code))
+ }
} else {
// not language-rust, so no-op
text.to_owned()
@@ -668,6 +647,36 @@ fn add_playpen_pre(html: &str, playpen_config: &Playpen) -> String {
.into_owned()
}
+lazy_static! {
+ static ref BORING_LINES_REGEX: Regex = Regex::new(r"^(\s*)#(.?)(.*)$").unwrap();
+}
+
+fn hide_lines(content: &str) -> String {
+ let mut prev_line_hidden = false;
+ let mut result = String::with_capacity(content.len());
+ for line in content.lines() {
+ if let Some(caps) = BORING_LINES_REGEX.captures(line) {
+ if !prev_line_hidden && &caps[2] != "#" {
+ result += "";
+ prev_line_hidden = true;
+ }
+ result += &caps[1];
+ if &caps[2] != " " {
+ result += &caps[2];
+ }
+ result += &caps[3];
+ } else {
+ if prev_line_hidden {
+ result += "";
+ prev_line_hidden = false;
+ }
+ result += line;
+ }
+ result += "\n";
+ }
+ result
+}
+
fn partition_source(s: &str) -> (String, String) {
let mut after_header = false;
let mut before = String::new();
@@ -749,6 +758,8 @@ mod tests {
"let s = \"foo\n # bar\n\";\n
"),
("let s = \"foo\n # bar\n#\n\";
",
"let s = \"foo\n bar\n\n\";\n
"),
+ ("let s = \"foo\n # bar\n#\n\";
",
+ "let s = \"foo\n bar\n\n\";\n
"),
];
for (src, should_be) in &inputs {
let got = add_playpen_pre(
From 4c1bca168409b05176a4458d3a6b98a4a724cd55 Mon Sep 17 00:00:00 2001
From: Benedikt Werner <1benediktwerner@gmail.com>
Date: Thu, 7 Nov 2019 02:42:02 +0100
Subject: [PATCH 4/4] Don't hide macro lines
---
src/renderer/html_handlebars/hbs_renderer.rs | 38 +++++++++++---------
1 file changed, 21 insertions(+), 17 deletions(-)
diff --git a/src/renderer/html_handlebars/hbs_renderer.rs b/src/renderer/html_handlebars/hbs_renderer.rs
index de7c512597..b184094edf 100644
--- a/src/renderer/html_handlebars/hbs_renderer.rs
+++ b/src/renderer/html_handlebars/hbs_renderer.rs
@@ -652,26 +652,28 @@ lazy_static! {
}
fn hide_lines(content: &str) -> String {
- let mut prev_line_hidden = false;
let mut result = String::with_capacity(content.len());
for line in content.lines() {
if let Some(caps) = BORING_LINES_REGEX.captures(line) {
- if !prev_line_hidden && &caps[2] != "#" {
- result += "";
- prev_line_hidden = true;
- }
- result += &caps[1];
- if &caps[2] != " " {
+ if &caps[2] == "#" {
+ result += &caps[1];
result += &caps[2];
- }
- result += &caps[3];
- } else {
- if prev_line_hidden {
+ result += &caps[3];
+ result += "\n";
+ continue;
+ } else if &caps[2] != "!" && &caps[2] != "[" {
+ result += "";
+ result += &caps[1];
+ if &caps[2] != " " {
+ result += &caps[2];
+ }
+ result += &caps[3];
+ result += "\n";
result += "";
- prev_line_hidden = false;
+ continue;
}
- result += line;
}
+ result += line;
result += "\n";
}
result
@@ -749,7 +751,7 @@ mod tests {
fn add_playpen() {
let inputs = [
("x()
",
- "\n#![allow(unused_variables)]\nfn main() {\nx()\n}\n
"),
+ "\n#![allow(unused_variables)]\nfn main() {\nx()\n}\n
"),
("fn main() {}
",
"fn main() {}\n
"),
("let s = \"foo\n # bar\n\";
",
@@ -757,9 +759,11 @@ mod tests {
("let s = \"foo\n ## bar\n\";
",
"let s = \"foo\n # bar\n\";\n
"),
("let s = \"foo\n # bar\n#\n\";
",
- "let s = \"foo\n bar\n\n\";\n
"),
- ("let s = \"foo\n # bar\n#\n\";
",
- "let s = \"foo\n bar\n\n\";\n
"),
+ "let s = \"foo\n bar\n\n\";\n
"),
+ ("let s = \"foo\n # bar\n\";
",
+ "let s = \"foo\n bar\n\";\n
"),
+ ("#![no_std]\nlet s = \"foo\";\n #[some_attr]
",
+ "#![no_std]\nlet s = \"foo\";\n #[some_attr]\n
"),
];
for (src, should_be) in &inputs {
let got = add_playpen_pre(