Debian Codegraph @

guide/src/format/mdbook.md

raw
1 # mdBook-specific features
2
3 ## Hiding code lines
4
5 There is a feature in mdBook that lets you hide code lines by prepending them with a specific prefix.
6
7 For the Rust language, you can prefix lines with `# ` (`#` followed by a space) to hide them [like you would with Rustdoc][rustdoc-hide].
8 This prefix can be escaped with `##` to prevent the hiding of a line that should begin with the literal string `# ` (see [Rustdoc's docs][rustdoc-hide] for more details)
9
10 [rustdoc-hide]: https://doc.rust-lang.org/stable/rustdoc/write-documentation/documentation-tests.html#hiding-portions-of-the-example
11
12 ```bash
13 # fn main() {
14 let x = 5;
15 let y = 6;
16
17 println!("{}", x + y);
18 # }
19 ```
20
21 Will render as
22
23 ```rust
24 # fn main() {
25 let x = 5;
26 let y = 6;
27
28 println!("{}", x + y);
29 # }
30 ```
31
32 When you tap or hover the mouse over the code block, there will be an eyeball icon (<i class="fa fa-eye"></i>) which will toggle the visibility of the hidden lines.
33
34 By default, this only works for code examples that are annotated with `rust`.
35 However, you can define custom prefixes for other languages by adding a new line-hiding prefix in your `book.toml` with the language name and prefix character(s):
36
37 ```toml
38 [output.html.code.hidelines]
39 python = "~"
40 ```
41
42 The prefix will hide any lines that begin with the given prefix. With the python prefix shown above, this:
43
44 ```bash
45 ~hidden()
46 nothidden():
47 ~ hidden()
48 ~hidden()
49 nothidden()
50 ```
51
52 will render as
53
54 ```python
55 ~hidden()
56 nothidden():
57 ~ hidden()
58 ~hidden()
59 nothidden()
60 ```
61
62 This behavior can be overridden locally with a different prefix. This has the same effect as above:
63
64 ~~~markdown
65 ```python,hidelines=!!!
66 !!!hidden()
67 nothidden():
68 !!! hidden()
69 !!!hidden()
70 nothidden()
71 ```
72 ~~~
73
74 ## Rust playground
75
76 Rust language code blocks will automatically get a play button (<i class="fas fa-play"></i>) which will execute the code and display the output just below the code block.
77 This works by sending the code to the [Rust Playground].
78
79 ```rust
80 println!("Hello, World!");
81 ```
82
83 If there is no `main` function, then the code is automatically wrapped inside one.
84
85 If you wish to disable the play button for a code block, you can include the `noplayground` option on the code block like this:
86
87 ~~~markdown
88 ```rust,noplayground
89 let mut name = String::new();
90 std::io::stdin().read_line(&mut name).expect("failed to read line");
91 println!("Hello {}!", name);
92 ```
93 ~~~
94
95 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.
96
97 ```toml
98 [output.html.playground]
99 runnable = false
100 ```
101
102 ## Rust code block attributes
103
104 Additional attributes can be included in Rust code blocks with comma, space, or tab-separated terms just after the language term. For example:
105
106 ~~~markdown
107 ```rust,ignore
108 # This example won't be tested.
109 panic!("oops!");
110 ```
111 ~~~
112
113 These are particularly important when using [`mdbook test`] to test Rust examples.
114 These use the same attributes as [rustdoc attributes], with a few additions:
115
116 * `editable` --- Enables the [editor].
117 * `noplayground` --- Removes the play button, but will still be tested.
118 * `mdbook-runnable` --- Forces the play button to be displayed.
119 This is intended to be combined with the `ignore` attribute for examples that should not be tested, but you want to allow the reader to run.
120 * `ignore` --- Will not be tested and no play button is shown, but it is still highlighted as Rust syntax.
121 * `should_panic` --- When executed, it should produce a panic.
122 * `no_run` --- The code is compiled when tested, but it is not run.
123 The play button is also not shown.
124 * `compile_fail` --- The code should fail to compile.
125 * `edition2015`, `edition2018`, `edition2021`, `edition2024` --- Forces the use of a specific Rust edition.
126 See [`rust.edition`] to set this globally.
127
128 [`mdbook test`]: ../cli/test.md
129 [rustdoc attributes]: https://doc.rust-lang.org/rustdoc/documentation-tests.html#attributes
130 [editor]: theme/editor.md
131 [`rust.edition`]: configuration/general.md#rust-options
132
133 ## Including files
134
135 With the following syntax, you can include files into your book:
136
137 ```hbs
138 \{{#include file.rs}}
139 ```
140
141 The path to the file has to be relative from the current source file.
142
143 mdBook will interpret included files as Markdown. Since the include command
144 is usually used for inserting code snippets and examples, you will often
145 wrap the command with ```` ``` ```` to display the file contents without
146 interpreting them.
147
148 ````hbs
149 ```
150 \{{#include file.rs}}
151 ```
152 ````
153
154 ## Including portions of a file
155 Often you only need a specific part of the file, e.g. relevant lines for an
156 example. We support four different modes of partial includes:
157
158 ```hbs
159 \{{#include file.rs:2}}
160 \{{#include file.rs::10}}
161 \{{#include file.rs:2:}}
162 \{{#include file.rs:2:10}}
163 ```
164
165 The first command only includes the second line from file `file.rs`. The second
166 command includes all lines up to line 10, i.e. the lines from 11 till the end of
167 the file are omitted. The third command includes all lines from line 2, i.e. the
168 first line is omitted. The last command includes the excerpt of `file.rs`
169 consisting of lines 2 to 10.
170
171 To avoid breaking your book when modifying included files, you can also
172 include a specific section using anchors instead of line numbers.
173 An anchor is a pair of matching lines. The line beginning an anchor must
174 match the regex `ANCHOR:\s*[\w_-]+` and similarly the ending line must match
175 the regex `ANCHOR_END:\s*[\w_-]+`. This allows you to put anchors in
176 any kind of commented line.
177
178 Consider the following file to include:
179 ```rs
180 /* ANCHOR: all */
181
182 // ANCHOR: component
183 struct Paddle {
184 hello: f32,
185 }
186 // ANCHOR_END: component
187
188 ////////// ANCHOR: system
189 impl System for MySystem { ... }
190 ////////// ANCHOR_END: system
191
192 /* ANCHOR_END: all */
193 ```
194
195 Then in the book, all you have to do is:
196 ````hbs
197 Here is a component:
198 ```rust,no_run,noplayground
199 \{{#include file.rs:component}}
200 ```
201
202 Here is a system:
203 ```rust,no_run,noplayground
204 \{{#include file.rs:system}}
205 ```
206
207 This is the full file.
208 ```rust,no_run,noplayground
209 \{{#include file.rs:all}}
210 ```
211 ````
212
213 Lines containing anchor patterns inside the included anchor are ignored.
214
215 ## Including a file but initially hiding all except specified lines
216
217 The `rustdoc_include` helper is for including code from external Rust files that contain complete
218 examples, but only initially showing particular lines specified with line numbers or anchors in the
219 same way as with `include`.
220
221 The lines not in the line number range or between the anchors will still be included, but they will
222 be prefaced with `#`. This way, a reader can expand the snippet to see the complete example, and
223 Rustdoc will use the complete example when you run `mdbook test`.
224
225 For example, consider a file named `file.rs` that contains this Rust program:
226
227 ```rust
228 fn main() {
229 let x = add_one(2);
230 assert_eq!(x, 3);
231 }
232
233 fn add_one(num: i32) -> i32 {
234 num + 1
235 }
236 ```
237
238 We can include a snippet that initially shows only line 2 by using this syntax:
239
240 ````hbs
241 To call the `add_one` function, we pass it an `i32` and bind the returned value to `x`:
242
243 ```rust
244 \{{#rustdoc_include file.rs:2}}
245 ```
246 ````
247
248 This would have the same effect as if we had manually inserted the code and hidden all but line 2
249 using `#`:
250
251 ````hbs
252 To call the `add_one` function, we pass it an `i32` and bind the returned value to `x`:
253
254 ```rust
255 # fn main() {
256 let x = add_one(2);
257 # assert_eq!(x, 3);
258 # }
259 #
260 # fn add_one(num: i32) -> i32 {
261 # num + 1
262 # }
263 ```
264 ````
265
266 That is, it looks like this (click the "expand" icon to see the rest of the file):
267
268 ```rust
269 # fn main() {
270 let x = add_one(2);
271 # assert_eq!(x, 3);
272 # }
273 #
274 # fn add_one(num: i32) -> i32 {
275 # num + 1
276 # }
277 ```
278
279 ## Inserting runnable Rust files
280
281 With the following syntax, you can insert runnable Rust files into your book:
282
283 ```hbs
284 \{{#playground file.rs}}
285 ```
286
287 The path to the Rust file has to be relative from the current source file.
288
289 When play is clicked, the code snippet will be sent to the [Rust Playground] to be
290 compiled and run. The result is sent back and displayed directly underneath the
291 code.
292
293 Here is what a rendered code snippet looks like:
294
295 {{#playground example.rs}}
296
297 Any additional values passed after the filename will be included as attributes of the code block.
298 For example `\{{#playground example.rs editable}}` will create the code block like the following:
299
300 ~~~markdown
301 ```rust,editable
302 # Contents of example.rs here.
303 ```
304 ~~~
305
306 And the `editable` attribute will enable the [editor] as described at [Rust code block attributes](#rust-code-block-attributes).
307
308 [Rust Playground]: https://play.rust-lang.org/
309
310 ## Controlling page \<title\>
311
312 A chapter can set a \<title\> that is different from its entry in the table of
313 contents (sidebar) by including a `\{{#title ...}}` near the top of the page.
314
315 ```hbs
316 \{{#title My Title}}
317 ```
318
319 ## HTML classes provided by mdBook
320
321 <img class="right" src="images/rust-logo-blk.svg" alt="The Rust logo">
322
323 ### `class="left"` and `"right"`
324
325 These classes are provided by default, for inline HTML to float images.
326
327 ```html
328 <img class="right" src="images/rust-logo-blk.svg" alt="The Rust logo">
329 ```
330
331 ### `class="hidden"`
332
333 HTML tags with class `hidden` will not be shown.
334
335 ```html
336 <div class="hidden">This will not be seen.</div>
337 ```
338
339 <div class="hidden">This will not be seen.</div>
340
341 ## Font-Awesome icons
342
343 mdBook includes a copy of version 6 of [Font Awesome Free's](https://fontawesome.com)
344 MIT-licensed SVG files. It emulates the `<i>` syntax, but converts the results
345 to inline SVG. Only the regular, solid, and brands icons are included; paid
346 features like the light icons are not.
347
348 For example, given this HTML syntax:
349
350 ```hbs
351 The result looks like this: <i class="fa-solid fa-print"></i>
352 ```
353
354 The result looks like this: <i class="fa-solid fa-print"></i>
355
356 See the [free icon set](https://fontawesome.com/v6/search) for the available icons.