diff --git a/book-example/src/SUMMARY.md b/book-example/src/SUMMARY.md index 3ae143fb87..4ac08ccb15 100644 --- a/book-example/src/SUMMARY.md +++ b/book-example/src/SUMMARY.md @@ -14,7 +14,7 @@ - [Configuration](format/config.md) - [Theme](format/theme/README.md) - [index.hbs](format/theme/index-hbs.md) - - [Syntax highlighting](format/theme/syntax-highlighting.md) + - [Code Blocks](format/theme/code-blocks.md) - [Editor](format/theme/editor.md) - [MathJax Support](format/mathjax.md) - [mdBook specific features](format/mdbook.md) diff --git a/book-example/src/format/theme/syntax-highlighting.md b/book-example/src/format/theme/code-blocks.md similarity index 69% rename from book-example/src/format/theme/syntax-highlighting.md rename to book-example/src/format/theme/code-blocks.md index 42039cdd41..f9f33656cc 100644 --- a/book-example/src/format/theme/syntax-highlighting.md +++ b/book-example/src/format/theme/code-blocks.md @@ -1,17 +1,51 @@ -# Syntax Highlighting - -For syntax highlighting I use [Highlight.js](https://highlightjs.org) with a -custom theme. +# Code Blocks +Syntax highlighted code blocks are supported using [Highlight.js](https://highlightjs.org) +with a custom theme. Automatic language detection has been turned off, so you will probably want to specify the programming language you use like this -
```rust
fn main() {
// Some code
}
```
+## Run Button
+Rust code blocks are runnable by default with a run button, for example:
+```rust
+fn main() {
+ println!("hello");
+}
+```
+Other languages are currently not supported.
+
+You can also diable the run button with `no_run`:
+```rust,no_run
+fn main() {
+ println!("Not runnable.");
+}
+```
+
+```rust,no_run
+fn main() {
+ println!("Not runnable.");
+}
+```
+### Show Warnings
+
+It is possible to show warning when Rust code is run via adding `warn`:
+```rust,warn
+fn main() {
+ let unused = 7;
+}
+```
+
+```rust,warn
+fn main() {
+ let unused = 7;
+}
+```
+
## Custom theme
Like the rest of the theme, the files used for syntax highlighting can be
overridden with your own.
diff --git a/src/theme/book.js b/src/theme/book.js
index 5e386369f7..a46fb88d16 100644
--- a/src/theme/book.js
+++ b/src/theme/book.js
@@ -98,10 +98,17 @@ function playground_text(playground) {
}
function run_rust_code(code_block) {
- var result_block = code_block.querySelector(".result");
+ var result_stderr_block = code_block.querySelector(".result.stderr");
+ if (!result_stderr_block) {
+ result_stderr_block = document.createElement('code');
+ result_stderr_block.className = 'result stderr hljs nohighlight';
+
+ code_block.append(result_stderr_block);
+ }
+ var result_block = code_block.querySelector(".result.stdout");
if (!result_block) {
result_block = document.createElement('code');
- result_block.className = 'result hljs language-bash';
+ result_block.className = 'result stdout hljs nohighlight';
code_block.append(result_block);
}
@@ -109,22 +116,27 @@ function playground_text(playground) {
let text = playground_text(code_block);
let classes = code_block.querySelector('code').classList;
let has_2018 = classes.contains("edition2018");
+ let should_warn = classes.contains("warn")
let edition = has_2018 ? "2018" : "2015";
var params = {
- version: "stable",
- optimize: "0",
+ backtrace: true,
+ channel: "stable",
code: text,
- edition: edition
+ edition: edition,
+ mode: "debug",
+ tests: false,
+ crateType: "bin",
};
if (text.indexOf("#![feature") !== -1) {
params.version = "nightly";
}
+ result_stderr_block.classList.add("hidden");
result_block.innerText = "Running...";
- fetch_with_timeout("https://play.rust-lang.org/evaluate.json", {
+ fetch_with_timeout("https://play.rust-lang.org/execute", {
headers: {
'Content-Type': "application/json",
},
@@ -133,7 +145,15 @@ function playground_text(playground) {
body: JSON.stringify(params)
})
.then(response => response.json())
- .then(response => result_block.innerText = response.result)
+ .then(response => {
+ result_block.innerText = response.stdout;
+ var show_warnings = should_warn && response.stderr.includes("warning");
+ // show stderr block if there is compile error or warning
+ if (!response.success || show_warnings) {
+ result_stderr_block.innerText = response.stderr;
+ result_stderr_block.classList.remove("hidden");
+ }
+ })
.catch(error => result_block.innerText = "Playground Communication: " + error.message);
}
diff --git a/tests/summary_md_files/example_book.md b/tests/summary_md_files/example_book.md
index ff3911c72c..e6068e4974 100644
--- a/tests/summary_md_files/example_book.md
+++ b/tests/summary_md_files/example_book.md
@@ -12,7 +12,7 @@
- [Configuration](format/config.md)
- [Theme](format/theme/theme.md)
- [index.hbs](format/theme/index-hbs.md)
- - [Syntax highlighting](format/theme/syntax-highlighting.md)
+ - [Code Blocks](format/theme/code-blocks.md)
- [MathJax Support](format/mathjax.md)
- [Rust code specific features](format/rust.md)
- [Rust Library](lib/lib.md)