Skip to content

Commit 6cab045

Browse files
authored
Merge pull request #1546 from pineapplehunter/master
Config to toggle the run button on codeblocks
2 parents fa0f9df + 2ae7f00 commit 6cab045

File tree

5 files changed

+52
-2
lines changed

5 files changed

+52
-2
lines changed

guide/src/format/configuration/renderers.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,13 +207,15 @@ editable = false # allows editing the source code
207207
copyable = true # include the copy button for copying code snippets
208208
copy-js = true # includes the JavaScript for the code editor
209209
line-numbers = false # displays line numbers for editable code
210+
runnable = true # displays a run button for rust code
210211
```
211212

212213
- **editable:** Allow editing the source code. Defaults to `false`.
213214
- **copyable:** Display the copy button on code snippets. Defaults to `true`.
214215
- **copy-js:** Copy JavaScript files for the editor to the output directory.
215216
Defaults to `true`.
216217
- **line-numbers** Display line numbers on editable sections of code. Requires both `editable` and `copy-js` to be `true`. Defaults to `false`.
218+
- **runnable** Displays a run button for rust code snippets. Changing this to `false` will disable the run in playground feature globally. Defaults to `true`.
217219

218220
[Ace]: https://ace.c9.io/
219221

guide/src/format/mdbook.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ println!("Hello, World!");
4141

4242
If there is no `main` function, then the code is automatically wrapped inside one.
4343

44-
If you wish to disable the play button, you can include the `noplayground` option on the code block like this:
44+
If you wish to disable the play button for a code block, you can include the `noplayground` option on the code block like this:
4545

4646
~~~markdown
4747
```rust,noplayground
@@ -51,6 +51,13 @@ println!("Hello {}!", name);
5151
```
5252
~~~
5353

54+
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.
55+
56+
```toml
57+
[output.html.playground]
58+
runnable = false
59+
```
60+
5461
## Rust code block attributes
5562

5663
Additional attributes can be included in Rust code blocks with comma, space, or tab-separated terms just after the language term. For example:

src/config.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,8 @@ pub struct Playground {
630630
pub copy_js: bool,
631631
/// Display line numbers on playground snippets. Default: `false`.
632632
pub line_numbers: bool,
633+
/// Display the run button. Default: `true`
634+
pub runnable: bool,
633635
}
634636

635637
impl Default for Playground {
@@ -639,6 +641,7 @@ impl Default for Playground {
639641
copyable: true,
640642
copy_js: true,
641643
line_numbers: false,
644+
runnable: true,
642645
}
643646
}
644647
}
@@ -781,6 +784,7 @@ mod tests {
781784
copyable: true,
782785
copy_js: true,
783786
line_numbers: false,
787+
runnable: true,
784788
};
785789
let html_should_be = HtmlConfig {
786790
curly_quotes: true,
@@ -811,6 +815,22 @@ mod tests {
811815
assert_eq!(got.html_config().unwrap(), html_should_be);
812816
}
813817

818+
#[test]
819+
fn disable_runnable() {
820+
let src = r#"
821+
[book]
822+
title = "Some Book"
823+
description = "book book book"
824+
authors = ["Shogo Takata"]
825+
826+
[output.html.playground]
827+
runnable = false
828+
"#;
829+
830+
let got = Config::from_str(src).unwrap();
831+
assert_eq!(got.html_config().unwrap().playground.runnable, false);
832+
}
833+
814834
#[test]
815835
fn edition_2015() {
816836
let src = r#"

src/renderer/html_handlebars/hbs_renderer.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -828,7 +828,8 @@ fn add_playground_pre(
828828
if classes.contains("language-rust") {
829829
if (!classes.contains("ignore")
830830
&& !classes.contains("noplayground")
831-
&& !classes.contains("noplaypen"))
831+
&& !classes.contains("noplaypen")
832+
&& playground_config.runnable)
832833
|| classes.contains("mdbook-runnable")
833834
{
834835
let contains_e2015 = classes.contains("edition2015");

tests/rendered_output.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use std::ffi::OsStr;
1717
use std::fs;
1818
use std::io::Write;
1919
use std::path::{Component, Path, PathBuf};
20+
use std::str::FromStr;
2021
use tempfile::Builder as TempFileBuilder;
2122
use walkdir::{DirEntry, WalkDir};
2223

@@ -150,6 +151,25 @@ fn rendered_code_has_playground_stuff() {
150151
assert_contains_strings(book_js, &[".playground"]);
151152
}
152153

154+
#[test]
155+
fn rendered_code_does_not_have_playground_stuff_in_html_when_disabled_in_config() {
156+
let temp = DummyBook::new().build().unwrap();
157+
let config = Config::from_str(
158+
"
159+
[output.html.playground]
160+
runnable = false
161+
",
162+
)
163+
.unwrap();
164+
let md = MDBook::load_with_config(temp.path(), config).unwrap();
165+
md.build().unwrap();
166+
167+
let nested = temp.path().join("book/first/nested.html");
168+
let playground_class = vec![r#"class="playground""#];
169+
170+
assert_doesnt_contain_strings(nested, &playground_class);
171+
}
172+
153173
#[test]
154174
fn anchors_include_text_between_but_not_anchor_comments() {
155175
let temp = DummyBook::new().build().unwrap();

0 commit comments

Comments
 (0)