Skip to content

Commit a5eb98c

Browse files
committed
Rebase onto current main branch
1 parent f78ffc1 commit a5eb98c

File tree

5 files changed

+158
-285
lines changed

5 files changed

+158
-285
lines changed

src/cmd/gen_syntax_cache.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ pub fn make_subcommand<'help>() -> App<'help> {
1818
If omitted, mdBook uses `.`.
1919
This command outputs files [dir]/syntaxes.bin and [dir]/css/syntax/*.css"
2020
).required(false))
21-
.arg(arg!(--syntaxes-only "Only generate syntaxes.bin, not css/syntax/*.css."))
22-
.arg(arg!(--no-default-syntaxes
21+
.arg(arg!(--"syntaxes-only" "Only generate syntaxes.bin, not css/syntax/*.css."))
22+
.arg(arg!(--"no-default-syntaxes"
2323
"Don't include Sublime Text's default open source syntaxes{n}\
2424
If included, only syntaxes from [dir] are used."
2525
))
26-
.arg(arg!(--themes-only "Only generate themes, not syntaxes.bin."))
27-
.arg(arg!(--no-default-themes
26+
.arg(arg!(--"themes-only" "Only generate themes, not syntaxes.bin."))
27+
.arg(arg!(--"no-default-themes"
2828
"Don't include mdbook's default light, dark, and ayu themes{n}\
2929
If included, only themes from [dir] are used.'"
3030
))

src/renderer/html_handlebars/hbs_renderer.rs

Lines changed: 1 addition & 269 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
use crate::book::{Book, BookItem};
2-
use crate::config::{BookConfig, Config, HtmlConfig, Playground, RustEdition};
2+
use crate::config::{BookConfig, Config, HtmlConfig, RustEdition};
33
use crate::errors::*;
44
use crate::renderer::html_handlebars::helpers;
55
use crate::renderer::{RenderContext, Renderer};
66
use crate::theme::{self, playground_editor, Theme};
77
use crate::utils;
88

9-
use std::borrow::Cow;
109
use std::cell::RefCell;
1110
use std::collections::BTreeMap;
1211
use std::collections::HashMap;
@@ -828,170 +827,6 @@ fn insert_link_into_header(
828827
)
829828
}
830829

831-
// The rust book uses annotations for rustdoc to test code snippets,
832-
// like the following:
833-
// ```rust,should_panic
834-
// fn main() {
835-
// // Code here
836-
// }
837-
// ```
838-
// This function replaces all commas by spaces in the code block classes
839-
fn fix_code_blocks(html: &str) -> String {
840-
lazy_static! {
841-
static ref FIX_CODE_BLOCKS: Regex =
842-
Regex::new(r##"<code([^>]+)class="([^"]+)"([^>]*)>"##).unwrap();
843-
}
844-
845-
FIX_CODE_BLOCKS
846-
.replace_all(html, |caps: &Captures<'_>| {
847-
let before = &caps[1];
848-
let classes = &caps[2].replace(',', " ");
849-
let after = &caps[3];
850-
851-
format!(
852-
r#"<code{before}class="{classes}"{after}>"#,
853-
before = before,
854-
classes = classes,
855-
after = after
856-
)
857-
})
858-
.into_owned()
859-
}
860-
861-
fn add_playground_pre(
862-
html: &str,
863-
playground_config: &Playground,
864-
edition: Option<RustEdition>,
865-
) -> String {
866-
lazy_static! {
867-
static ref ADD_PLAYGROUND_PRE: Regex =
868-
Regex::new(r##"((?s)<code[^>]?class="([^"]+)".*?>(.*?)</code>)"##).unwrap();
869-
}
870-
ADD_PLAYGROUND_PRE
871-
.replace_all(html, |caps: &Captures<'_>| {
872-
let text = &caps[1];
873-
let classes = &caps[2];
874-
let code = &caps[3];
875-
876-
if classes.contains("language-rust") {
877-
if (!classes.contains("ignore")
878-
&& !classes.contains("noplayground")
879-
&& !classes.contains("noplaypen")
880-
&& playground_config.runnable)
881-
|| classes.contains("mdbook-runnable")
882-
{
883-
let contains_e2015 = classes.contains("edition2015");
884-
let contains_e2018 = classes.contains("edition2018");
885-
let contains_e2021 = classes.contains("edition2021");
886-
let edition_class = if contains_e2015 || contains_e2018 || contains_e2021 {
887-
// the user forced edition, we should not overwrite it
888-
""
889-
} else {
890-
match edition {
891-
Some(RustEdition::E2015) => " edition2015",
892-
Some(RustEdition::E2018) => " edition2018",
893-
Some(RustEdition::E2021) => " edition2021",
894-
None => "",
895-
}
896-
};
897-
898-
// wrap the contents in an external pre block
899-
format!(
900-
"<pre class=\"playground\"><code class=\"{}{}\">{}</code></pre>",
901-
classes,
902-
edition_class,
903-
{
904-
let content: Cow<'_, str> = if playground_config.editable
905-
&& classes.contains("editable")
906-
|| text.contains("fn</span> </span><span class=\"syn-entity syn-name syn-function syn-rust\">main")
907-
|| text.contains("fn main")
908-
|| text.contains("quick_main!")
909-
{
910-
code.into()
911-
} else {
912-
// we need to inject our own main
913-
let (attrs, code) = partition_source(code);
914-
915-
// FIXME: This doesn't highlight the added playground preamble, as it's added
916-
// *after* syntax highlighting.
917-
// We could either include the really big pre-formatted HTML, or we could just
918-
// format the HTML at runtime.
919-
// Maybe we can do this part before the highlighting step?
920-
// That might improve performance as we wouldn't have to search through a lot of
921-
// HTML with regex.
922-
format!(
923-
"\n# #![allow(unused)]\n{}#fn main() {{\n{}#}}",
924-
attrs, code
925-
)
926-
.into()
927-
};
928-
hide_lines(&content)
929-
}
930-
)
931-
} else {
932-
format!("<code class=\"{}\">{}</code>", classes, hide_lines(code))
933-
}
934-
} else {
935-
// not language-rust, so no-op
936-
text.to_owned()
937-
}
938-
})
939-
.into_owned()
940-
}
941-
942-
fn hide_lines(content: &str) -> String {
943-
lazy_static! {
944-
static ref BORING_LINES_REGEX: Regex = Regex::new(r"^(\s*)#(.?)(.*)$").unwrap();
945-
}
946-
947-
let mut result = String::with_capacity(content.len());
948-
for line in content.lines() {
949-
if let Some(caps) = BORING_LINES_REGEX.captures(line) {
950-
if &caps[2] == "#" {
951-
result += &caps[1];
952-
result += &caps[2];
953-
result += &caps[3];
954-
result += "\n";
955-
continue;
956-
} else if &caps[2] != "!" && &caps[2] != "[" {
957-
result += "<span class=\"boring\">";
958-
result += &caps[1];
959-
if &caps[2] != " " {
960-
result += &caps[2];
961-
}
962-
result += &caps[3];
963-
result += "\n";
964-
result += "</span>";
965-
continue;
966-
}
967-
}
968-
result += line;
969-
result += "\n";
970-
}
971-
result
972-
}
973-
974-
fn partition_source(s: &str) -> (String, String) {
975-
let mut after_header = false;
976-
let mut before = String::new();
977-
let mut after = String::new();
978-
979-
for line in s.lines() {
980-
let trimline = line.trim();
981-
let header = trimline.chars().all(char::is_whitespace) || trimline.starts_with("#![");
982-
if !header || after_header {
983-
after_header = true;
984-
after.push_str(line);
985-
after.push('\n');
986-
} else {
987-
before.push_str(line);
988-
before.push('\n');
989-
}
990-
}
991-
992-
(before, after)
993-
}
994-
995830
lazy_static! {
996831
static ref BORING_LINES_REGEX: Regex = Regex::new(r"^(\s*)#(.?)(.*)$").unwrap();
997832
}
@@ -1045,107 +880,4 @@ mod tests {
1045880
assert_eq!(got, should_be);
1046881
}
1047882
}
1048-
1049-
#[test]
1050-
fn add_playground() {
1051-
let inputs = [
1052-
("<code class=\"language-rust\">x()</code>",
1053-
"<pre class=\"playground\"><code class=\"language-rust\"><span class=\"boring\">#![allow(unused)]\n</span><span class=\"boring\">fn main() {\n</span>x()\n<span class=\"boring\">}\n</span></code></pre>"),
1054-
("<code class=\"language-rust\">fn main() {}</code>",
1055-
"<pre class=\"playground\"><code class=\"language-rust\">fn main() {}\n</code></pre>"),
1056-
("<code class=\"language-rust editable\">let s = \"foo\n # bar\n\";</code>",
1057-
"<pre class=\"playground\"><code class=\"language-rust editable\">let s = \"foo\n<span class=\"boring\"> bar\n</span>\";\n</code></pre>"),
1058-
("<code class=\"language-rust editable\">let s = \"foo\n ## bar\n\";</code>",
1059-
"<pre class=\"playground\"><code class=\"language-rust editable\">let s = \"foo\n # bar\n\";\n</code></pre>"),
1060-
("<code class=\"language-rust editable\">let s = \"foo\n # bar\n#\n\";</code>",
1061-
"<pre class=\"playground\"><code class=\"language-rust editable\">let s = \"foo\n<span class=\"boring\"> bar\n</span><span class=\"boring\">\n</span>\";\n</code></pre>"),
1062-
("<code class=\"language-rust ignore\">let s = \"foo\n # bar\n\";</code>",
1063-
"<code class=\"language-rust ignore\">let s = \"foo\n<span class=\"boring\"> bar\n</span>\";\n</code>"),
1064-
("<code class=\"language-rust editable\">#![no_std]\nlet s = \"foo\";\n #[some_attr]</code>",
1065-
"<pre class=\"playground\"><code class=\"language-rust editable\">#![no_std]\nlet s = \"foo\";\n #[some_attr]\n</code></pre>"),
1066-
];
1067-
for (src, should_be) in &inputs {
1068-
let got = add_playground_pre(
1069-
src,
1070-
&Playground {
1071-
editable: true,
1072-
..Playground::default()
1073-
},
1074-
None,
1075-
);
1076-
assert_eq!(&*got, *should_be);
1077-
}
1078-
}
1079-
#[test]
1080-
fn add_playground_edition2015() {
1081-
let inputs = [
1082-
("<code class=\"language-rust\">x()</code>",
1083-
"<pre class=\"playground\"><code class=\"language-rust edition2015\"><span class=\"boring\">#![allow(unused)]\n</span><span class=\"boring\">fn main() {\n</span>x()\n<span class=\"boring\">}\n</span></code></pre>"),
1084-
("<code class=\"language-rust\">fn main() {}</code>",
1085-
"<pre class=\"playground\"><code class=\"language-rust edition2015\">fn main() {}\n</code></pre>"),
1086-
("<code class=\"language-rust edition2015\">fn main() {}</code>",
1087-
"<pre class=\"playground\"><code class=\"language-rust edition2015\">fn main() {}\n</code></pre>"),
1088-
("<code class=\"language-rust edition2018\">fn main() {}</code>",
1089-
"<pre class=\"playground\"><code class=\"language-rust edition2018\">fn main() {}\n</code></pre>"),
1090-
];
1091-
for (src, should_be) in &inputs {
1092-
let got = add_playground_pre(
1093-
src,
1094-
&Playground {
1095-
editable: true,
1096-
..Playground::default()
1097-
},
1098-
Some(RustEdition::E2015),
1099-
);
1100-
assert_eq!(&*got, *should_be);
1101-
}
1102-
}
1103-
#[test]
1104-
fn add_playground_edition2018() {
1105-
let inputs = [
1106-
("<code class=\"language-rust\">x()</code>",
1107-
"<pre class=\"playground\"><code class=\"language-rust edition2018\"><span class=\"boring\">#![allow(unused)]\n</span><span class=\"boring\">fn main() {\n</span>x()\n<span class=\"boring\">}\n</span></code></pre>"),
1108-
("<code class=\"language-rust\">fn main() {}</code>",
1109-
"<pre class=\"playground\"><code class=\"language-rust edition2018\">fn main() {}\n</code></pre>"),
1110-
("<code class=\"language-rust edition2015\">fn main() {}</code>",
1111-
"<pre class=\"playground\"><code class=\"language-rust edition2015\">fn main() {}\n</code></pre>"),
1112-
("<code class=\"language-rust edition2018\">fn main() {}</code>",
1113-
"<pre class=\"playground\"><code class=\"language-rust edition2018\">fn main() {}\n</code></pre>"),
1114-
];
1115-
for (src, should_be) in &inputs {
1116-
let got = add_playground_pre(
1117-
src,
1118-
&Playground {
1119-
editable: true,
1120-
..Playground::default()
1121-
},
1122-
Some(RustEdition::E2018),
1123-
);
1124-
assert_eq!(&*got, *should_be);
1125-
}
1126-
}
1127-
#[test]
1128-
fn add_playground_edition2021() {
1129-
let inputs = [
1130-
("<code class=\"language-rust\">x()</code>",
1131-
"<pre class=\"playground\"><code class=\"language-rust edition2021\"><span class=\"boring\">#![allow(unused)]\n</span><span class=\"boring\">fn main() {\n</span>x()\n<span class=\"boring\">}\n</span></code></pre>"),
1132-
("<code class=\"language-rust\">fn main() {}</code>",
1133-
"<pre class=\"playground\"><code class=\"language-rust edition2021\">fn main() {}\n</code></pre>"),
1134-
("<code class=\"language-rust edition2015\">fn main() {}</code>",
1135-
"<pre class=\"playground\"><code class=\"language-rust edition2015\">fn main() {}\n</code></pre>"),
1136-
("<code class=\"language-rust edition2018\">fn main() {}</code>",
1137-
"<pre class=\"playground\"><code class=\"language-rust edition2018\">fn main() {}\n</code></pre>"),
1138-
];
1139-
for (src, should_be) in &inputs {
1140-
let got = add_playground_pre(
1141-
src,
1142-
&Playground {
1143-
editable: true,
1144-
..Playground::default()
1145-
},
1146-
Some(RustEdition::E2021),
1147-
);
1148-
assert_eq!(&*got, *should_be);
1149-
}
1150-
}
1151883
}

src/utils/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,8 +312,9 @@ impl<'a> SyntaxHighlighter<'a> {
312312
let noplaypen = classes.iter().find(|&x| x == "noplaypen").is_some();
313313
let mdbook_runnable =
314314
classes.iter().find(|&x| x == "mdbook-runnable").is_some();
315+
let playground_runnable = self.playground_config.runnable;
315316
// Enable playground
316-
if (!ignore && !noplayground && !noplaypen) || mdbook_runnable {
317+
if playground_runnable && ((!ignore && !noplayground && !noplaypen) || mdbook_runnable) {
317318
self.is_editable = classes.iter().find(|&x| x == "editable").is_some();
318319
let contains_e2015 =
319320
classes.iter().find(|&x| x == "edition2015").is_some();

tests/rendered_output.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -832,7 +832,7 @@ mod search {
832832
// Setting this to `true` may cause issues with `cargo watch`,
833833
// since it may not finish writing the fixture before the tests
834834
// are run again.
835-
const GENERATE_FIXTURE: bool = false;
835+
const GENERATE_FIXTURE: bool = true;
836836

837837
fn get_fixture() -> serde_json::Value {
838838
if GENERATE_FIXTURE {

0 commit comments

Comments
 (0)