Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conditionally show compile warnings when running code #1315

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion book-example/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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

<pre><code class="language-markdown">```rust
fn main() {
// Some code
}
```</code></pre>

## 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`:
<pre><code class="language-markdown">```rust,no_run
fn main() {
println!("Not runnable.");
}
```</code></pre>

```rust,no_run
fn main() {
println!("Not runnable.");
}
```
### Show Warnings

It is possible to show warning when Rust code is run via adding `warn`:
<pre><code class="language-markdown">```rust,warn
fn main() {
let unused = 7;
}
```</code></pre>

```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.
Expand Down
34 changes: 27 additions & 7 deletions src/theme/book.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,33 +98,45 @@ 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);
}

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",
},
Expand All @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/summary_md_files/example_book.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down