diff --git a/src/renderer/html_handlebars/hbs_renderer.rs b/src/renderer/html_handlebars/hbs_renderer.rs
index 26f1432c33..8039295957 100644
--- a/src/renderer/html_handlebars/hbs_renderer.rs
+++ b/src/renderer/html_handlebars/hbs_renderer.rs
@@ -903,29 +903,37 @@ fn hide_lines(content: &str) -> String {
}
let mut result = String::with_capacity(content.len());
- for line in content.lines() {
+ let mut lines = content.lines().peekable();
+ while let Some(line) = lines.next() {
+ // Don't include newline on the last line.
+ let newline = if lines.peek().is_none() { "" } else { "\n" };
+
if let Some(caps) = BORING_LINES_REGEX.captures(line) {
if &caps[2] == "#" {
result += &caps[1];
result += &caps[2];
result += &caps[3];
- result += "\n";
+ result += newline;
continue;
- } else if &caps[2] != "!" && &caps[2] != "[" {
+ }
+
+ if &caps[2] != "!" && &caps[2] != "[" {
result += "";
result += &caps[1];
if &caps[2] != " " {
result += &caps[2];
}
result += &caps[3];
- result += "\n";
+ result += newline;
result += "";
continue;
}
}
+
result += line;
- result += "\n";
+ result += newline;
}
+
result
}
@@ -1004,19 +1012,19 @@ mod tests {
fn add_playground() {
let inputs = [
("x()
",
- "
#![allow(unused)]\nfn main() {\nx()\n}\n
"),
+ "#![allow(unused)]\nfn main() {\nx()\n}
"),
("fn main() {}
",
- "fn main() {}\n
"),
+ "fn main() {}
"),
("let s = \"foo\n # bar\n\";
",
- "let s = \"foo\n bar\n\";\n
"),
+ "let s = \"foo\n bar\n\";
"),
("let s = \"foo\n ## bar\n\";
",
- "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\";\n
"),
+ "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\";
"),
("#![no_std]\nlet s = \"foo\";\n #[some_attr]
",
- "#![no_std]\nlet s = \"foo\";\n #[some_attr]\n
"),
+ "#![no_std]\nlet s = \"foo\";\n #[some_attr]
"),
];
for (src, should_be) in &inputs {
let got = add_playground_pre(
@@ -1034,13 +1042,13 @@ mod tests {
fn add_playground_edition2015() {
let inputs = [
("x()
",
- "#![allow(unused)]\nfn main() {\nx()\n}\n
"),
+ "#![allow(unused)]\nfn main() {\nx()\n}
"),
("fn main() {}
",
- "fn main() {}\n
"),
+ "fn main() {}
"),
("fn main() {}
",
- "fn main() {}\n
"),
+ "fn main() {}
"),
("fn main() {}
",
- "fn main() {}\n
"),
+ "fn main() {}
"),
];
for (src, should_be) in &inputs {
let got = add_playground_pre(
@@ -1058,13 +1066,13 @@ mod tests {
fn add_playground_edition2018() {
let inputs = [
("x()
",
- "#![allow(unused)]\nfn main() {\nx()\n}\n
"),
+ "#![allow(unused)]\nfn main() {\nx()\n}
"),
("fn main() {}
",
- "fn main() {}\n
"),
+ "fn main() {}
"),
("fn main() {}
",
- "fn main() {}\n
"),
+ "fn main() {}
"),
("fn main() {}
",
- "fn main() {}\n
"),
+ "fn main() {}
"),
];
for (src, should_be) in &inputs {
let got = add_playground_pre(
@@ -1082,13 +1090,13 @@ mod tests {
fn add_playground_edition2021() {
let inputs = [
("x()
",
- "#![allow(unused)]\nfn main() {\nx()\n}\n
"),
+ "#![allow(unused)]\nfn main() {\nx()\n}
"),
("fn main() {}
",
- "fn main() {}\n
"),
+ "fn main() {}
"),
("fn main() {}
",
- "fn main() {}\n
"),
+ "fn main() {}
"),
("fn main() {}
",
- "fn main() {}\n
"),
+ "fn main() {}
"),
];
for (src, should_be) in &inputs {
let got = add_playground_pre(