]?class="([^"]+)".*?>(.*?)
)"##).unwrap();
regex
.replace_all(html, |caps: &Captures<'_>| {
@@ -617,10 +624,24 @@ fn add_playpen_pre(html: &str, playpen_config: &Playpen) -> String {
if (!classes.contains("ignore") && !classes.contains("noplaypen"))
|| classes.contains("mdbook-runnable")
{
+ let contains_e2015 = classes.contains("edition2015");
+ let contains_e2018 = classes.contains("edition2018");
+ let edition_class = if contains_e2015 || contains_e2018 {
+ // the user forced edition, we should not overwrite it
+ ""
+ } else {
+ match edition {
+ Some(RustEdition::E2015) => " edition2015",
+ Some(RustEdition::E2018) => " edition2018",
+ None => "",
+ }
+ };
+
// wrap the contents in an external pre block
format!(
- "{}
",
+ "{}
",
classes,
+ edition_class,
{
let content: Cow<'_, str> = if playpen_config.editable
&& classes.contains("editable")
@@ -711,6 +732,7 @@ struct RenderItemContext<'a> {
data: serde_json::Mapx()
",
+ "\n#![allow(unused_variables)]\nfn main() {\nx()\n}\n
"),
+ ("fn main() {}
",
+ "fn main() {}\n
"),
+ ("fn main() {}
",
+ "fn main() {}\n
"),
+ ("fn main() {}
",
+ "fn main() {}\n
"),
+ ];
+ for (src, should_be) in &inputs {
+ let got = add_playpen_pre(
+ src,
+ &Playpen {
+ editable: true,
+ ..Playpen::default()
+ },
+ Some(RustEdition::E2015),
+ );
+ assert_eq!(&*got, *should_be);
+ }
+ }
+ #[test]
+ fn add_playpen_edition2018() {
+ let inputs = [
+ ("x()
",
+ "\n#![allow(unused_variables)]\nfn main() {\nx()\n}\n
"),
+ ("fn main() {}
",
+ "fn main() {}\n
"),
+ ("fn main() {}
",
+ "fn main() {}\n
"),
+ ("fn main() {}
",
+ "fn main() {}\n
"),
+ ];
+ for (src, should_be) in &inputs {
+ let got = add_playpen_pre(
+ src,
+ &Playpen {
+ editable: true,
+ ..Playpen::default()
+ },
+ Some(RustEdition::E2018),
);
assert_eq!(&*got, *should_be);
}