Merge pull request #1546 from pineapplehunter/master

Config to toggle the run button on codeblocks
This commit is contained in:
Eric Huss 2022-03-27 12:32:35 -07:00 committed by GitHub
commit 6cab04554e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 52 additions and 2 deletions

View File

@ -207,6 +207,7 @@ editable = false # allows editing the source code
copyable = true # include the copy button for copying code snippets copyable = true # include the copy button for copying code snippets
copy-js = true # includes the JavaScript for the code editor copy-js = true # includes the JavaScript for the code editor
line-numbers = false # displays line numbers for editable code line-numbers = false # displays line numbers for editable code
runnable = true # displays a run button for rust code
``` ```
- **editable:** Allow editing the source code. Defaults to `false`. - **editable:** Allow editing the source code. Defaults to `false`.
@ -214,6 +215,7 @@ line-numbers = false # displays line numbers for editable code
- **copy-js:** Copy JavaScript files for the editor to the output directory. - **copy-js:** Copy JavaScript files for the editor to the output directory.
Defaults to `true`. Defaults to `true`.
- **line-numbers** Display line numbers on editable sections of code. Requires both `editable` and `copy-js` to be `true`. Defaults to `false`. - **line-numbers** Display line numbers on editable sections of code. Requires both `editable` and `copy-js` to be `true`. Defaults to `false`.
- **runnable** Displays a run button for rust code snippets. Changing this to `false` will disable the run in playground feature globally. Defaults to `true`.
[Ace]: https://ace.c9.io/ [Ace]: https://ace.c9.io/

View File

@ -41,7 +41,7 @@ println!("Hello, World!");
If there is no `main` function, then the code is automatically wrapped inside one. If there is no `main` function, then the code is automatically wrapped inside one.
If you wish to disable the play button, you can include the `noplayground` option on the code block like this: If you wish to disable the play button for a code block, you can include the `noplayground` option on the code block like this:
~~~markdown ~~~markdown
```rust,noplayground ```rust,noplayground
@ -51,6 +51,13 @@ println!("Hello {}!", name);
``` ```
~~~ ~~~
Or, if you wish to disable the play button for all code blocks in your book, you can write the config to the `book.toml` like this.
```toml
[output.html.playground]
runnable = false
```
## Rust code block attributes ## Rust code block attributes
Additional attributes can be included in Rust code blocks with comma, space, or tab-separated terms just after the language term. For example: Additional attributes can be included in Rust code blocks with comma, space, or tab-separated terms just after the language term. For example:

View File

@ -630,6 +630,8 @@ pub struct Playground {
pub copy_js: bool, pub copy_js: bool,
/// Display line numbers on playground snippets. Default: `false`. /// Display line numbers on playground snippets. Default: `false`.
pub line_numbers: bool, pub line_numbers: bool,
/// Display the run button. Default: `true`
pub runnable: bool,
} }
impl Default for Playground { impl Default for Playground {
@ -639,6 +641,7 @@ impl Default for Playground {
copyable: true, copyable: true,
copy_js: true, copy_js: true,
line_numbers: false, line_numbers: false,
runnable: true,
} }
} }
} }
@ -781,6 +784,7 @@ mod tests {
copyable: true, copyable: true,
copy_js: true, copy_js: true,
line_numbers: false, line_numbers: false,
runnable: true,
}; };
let html_should_be = HtmlConfig { let html_should_be = HtmlConfig {
curly_quotes: true, curly_quotes: true,
@ -811,6 +815,22 @@ mod tests {
assert_eq!(got.html_config().unwrap(), html_should_be); assert_eq!(got.html_config().unwrap(), html_should_be);
} }
#[test]
fn disable_runnable() {
let src = r#"
[book]
title = "Some Book"
description = "book book book"
authors = ["Shogo Takata"]
[output.html.playground]
runnable = false
"#;
let got = Config::from_str(src).unwrap();
assert_eq!(got.html_config().unwrap().playground.runnable, false);
}
#[test] #[test]
fn edition_2015() { fn edition_2015() {
let src = r#" let src = r#"

View File

@ -828,7 +828,8 @@ fn add_playground_pre(
if classes.contains("language-rust") { if classes.contains("language-rust") {
if (!classes.contains("ignore") if (!classes.contains("ignore")
&& !classes.contains("noplayground") && !classes.contains("noplayground")
&& !classes.contains("noplaypen")) && !classes.contains("noplaypen")
&& playground_config.runnable)
|| classes.contains("mdbook-runnable") || classes.contains("mdbook-runnable")
{ {
let contains_e2015 = classes.contains("edition2015"); let contains_e2015 = classes.contains("edition2015");

View File

@ -17,6 +17,7 @@ use std::ffi::OsStr;
use std::fs; use std::fs;
use std::io::Write; use std::io::Write;
use std::path::{Component, Path, PathBuf}; use std::path::{Component, Path, PathBuf};
use std::str::FromStr;
use tempfile::Builder as TempFileBuilder; use tempfile::Builder as TempFileBuilder;
use walkdir::{DirEntry, WalkDir}; use walkdir::{DirEntry, WalkDir};
@ -150,6 +151,25 @@ fn rendered_code_has_playground_stuff() {
assert_contains_strings(book_js, &[".playground"]); assert_contains_strings(book_js, &[".playground"]);
} }
#[test]
fn rendered_code_does_not_have_playground_stuff_in_html_when_disabled_in_config() {
let temp = DummyBook::new().build().unwrap();
let config = Config::from_str(
"
[output.html.playground]
runnable = false
",
)
.unwrap();
let md = MDBook::load_with_config(temp.path(), config).unwrap();
md.build().unwrap();
let nested = temp.path().join("book/first/nested.html");
let playground_class = vec![r#"class="playground""#];
assert_doesnt_contain_strings(nested, &playground_class);
}
#[test] #[test]
fn anchors_include_text_between_but_not_anchor_comments() { fn anchors_include_text_between_but_not_anchor_comments() {
let temp = DummyBook::new().build().unwrap(); let temp = DummyBook::new().build().unwrap();