2018-02-01 03:29:39 +02:00
|
|
|
# mdBook-specific markdown
|
2018-01-31 11:57:47 +01:00
|
|
|
|
|
|
|
|
## Hiding code lines
|
|
|
|
|
|
2018-08-02 21:34:26 -05:00
|
|
|
There is a feature in mdBook that lets you hide code lines by prepending them
|
|
|
|
|
with a `#`.
|
2018-01-31 11:57:47 +01:00
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
# fn main() {
|
|
|
|
|
let x = 5;
|
|
|
|
|
let y = 6;
|
|
|
|
|
|
|
|
|
|
println!("{}", x + y);
|
|
|
|
|
# }
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
Will render as
|
|
|
|
|
|
|
|
|
|
```rust
|
|
|
|
|
# fn main() {
|
|
|
|
|
let x = 5;
|
|
|
|
|
let y = 7;
|
|
|
|
|
|
|
|
|
|
println!("{}", x + y);
|
|
|
|
|
# }
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## Including files
|
|
|
|
|
|
|
|
|
|
With the following syntax, you can include files into your book:
|
|
|
|
|
|
|
|
|
|
```hbs
|
2018-08-02 19:04:35 -05:00
|
|
|
\{{#include file.rs}}
|
2018-01-31 11:57:47 +01:00
|
|
|
```
|
|
|
|
|
|
|
|
|
|
The path to the file has to be relative from the current source file.
|
|
|
|
|
|
2019-06-12 22:38:12 -07:00
|
|
|
mdBook will interpret included files as markdown. Since the include command
|
|
|
|
|
is usually used for inserting code snippets and examples, you will often
|
|
|
|
|
wrap the command with ```` ``` ```` to display the file contents without
|
|
|
|
|
interpretting them.
|
|
|
|
|
|
|
|
|
|
````hbs
|
|
|
|
|
```
|
|
|
|
|
\{{#include file.rs}}
|
|
|
|
|
```
|
|
|
|
|
````
|
|
|
|
|
|
|
|
|
|
## Including portions of a file
|
|
|
|
|
Often you only need a specific part of the file e.g. relevant lines for an
|
|
|
|
|
example. We support four different modes of partial includes:
|
2018-01-31 11:57:47 +01:00
|
|
|
|
|
|
|
|
```hbs
|
2018-08-02 19:04:35 -05:00
|
|
|
\{{#include file.rs:2}}
|
|
|
|
|
\{{#include file.rs::10}}
|
|
|
|
|
\{{#include file.rs:2:}}
|
|
|
|
|
\{{#include file.rs:2:10}}
|
2018-01-31 11:57:47 +01:00
|
|
|
```
|
|
|
|
|
|
2018-08-02 21:34:26 -05:00
|
|
|
The first command only includes the second line from file `file.rs`. The second
|
|
|
|
|
command includes all lines up to line 10, i.e. the lines from 11 till the end of
|
|
|
|
|
the file are omitted. The third command includes all lines from line 2, i.e. the
|
|
|
|
|
first line is omitted. The last command includes the excerpt of `file.rs`
|
|
|
|
|
consisting of lines 2 to 10.
|
2018-01-31 11:57:47 +01:00
|
|
|
|
2019-07-14 21:51:25 -04:00
|
|
|
To avoid breaking your book when modifying included files, you can also
|
|
|
|
|
include a specific section using anchors instead of line numbers.
|
|
|
|
|
An anchor is a pair of matching lines. The line beginning an anchor must
|
|
|
|
|
match the regex "ANCHOR:\s*[\w_-]+" and similarly the ending line must match
|
|
|
|
|
the regex "ANCHOR_END:\s*[\w_-]+". This allows you to put anchors in
|
|
|
|
|
any kind of commented line.
|
|
|
|
|
|
|
|
|
|
Consider the following file to include:
|
|
|
|
|
```rs
|
|
|
|
|
/* ANCHOR: all */
|
|
|
|
|
|
|
|
|
|
// ANCHOR: component
|
|
|
|
|
struct Paddle {
|
|
|
|
|
hello: f32,
|
|
|
|
|
}
|
|
|
|
|
// ANCHOR_END: component
|
|
|
|
|
|
|
|
|
|
////////// ANCHOR: system
|
|
|
|
|
impl System for MySystem { ... }
|
|
|
|
|
////////// ANCHOR_END: system
|
|
|
|
|
|
|
|
|
|
/* ANCHOR_END: all */
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
Then in the book, all you have to do is:
|
|
|
|
|
````hbs
|
|
|
|
|
Here is a component:
|
|
|
|
|
```rust,no_run,noplaypen
|
|
|
|
|
\{{#include file.rs:component}}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
Here is a system:
|
|
|
|
|
```rust,no_run,noplaypen
|
|
|
|
|
\{{#include file.rs:system}}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
This is the full file.
|
|
|
|
|
```rust,no_run,noplaypen
|
|
|
|
|
\{{#include file.rs:all}}
|
|
|
|
|
```
|
|
|
|
|
````
|
|
|
|
|
|
|
|
|
|
Lines containing anchor patterns inside the included anchor are ignored.
|
|
|
|
|
|
2018-01-31 11:57:47 +01:00
|
|
|
## Inserting runnable Rust files
|
|
|
|
|
|
|
|
|
|
With the following syntax, you can insert runnable Rust files into your book:
|
|
|
|
|
|
|
|
|
|
```hbs
|
2018-08-02 19:04:35 -05:00
|
|
|
\{{#playpen file.rs}}
|
2018-01-31 11:57:47 +01:00
|
|
|
```
|
|
|
|
|
|
|
|
|
|
The path to the Rust file has to be relative from the current source file.
|
|
|
|
|
|
2018-08-02 21:34:26 -05:00
|
|
|
When play is clicked, the code snippet will be sent to the [Rust Playpen] to be
|
|
|
|
|
compiled and run. The result is sent back and displayed directly underneath the
|
|
|
|
|
code.
|
2018-01-31 11:57:47 +01:00
|
|
|
|
|
|
|
|
Here is what a rendered code snippet looks like:
|
|
|
|
|
|
|
|
|
|
{{#playpen example.rs}}
|
|
|
|
|
|
|
|
|
|
[Rust Playpen]: https://play.rust-lang.org/
|