Skip to content

Commit bb8ac21

Browse files
committed
docs: render rustdoc docs with rustdoc, hack around sundown code-fence
parsing limitations. Sundown parses ``` ~~~ as a valid codeblock (i.e. mismatching delimiters), which made using rustdoc on its own documentation impossible (since it used nested codeblocks to demonstrate how testable codesnippets worked). This modifies those snippets so that they're delimited by indentation, but this then means they're tested by `rustdoc --test` & rendered as Rust code (because there's no way to add `notrust` to indentation-delimited code blocks). A comment is added to stop the compiler reading the text too closely, but this unfortunately has to be visible in the final docs, since that's the text on which the highlighting happens.
1 parent 6d6e288 commit bb8ac21

File tree

2 files changed

+55
-40
lines changed

2 files changed

+55
-40
lines changed

mk/docs.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
DOCS := index tutorial guide-ffi guide-macros guide-lifetimes \
2727
guide-tasks guide-container guide-pointers \
2828
complement-cheatsheet guide-runtime \
29-
rust
29+
rust rustdoc
3030

3131
RUSTDOC_DEPS_rust := doc/full-toc.inc
3232
RUSTDOC_FLAGS_rust := --markdown-in-header=doc/full-toc.inc

src/doc/rustdoc.md

Lines changed: 54 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ comments":
2020
//! the crate index page. The ! makes it apply to the parent of the comment,
2121
//! rather than what follows).
2222
23+
# mod workaround_the_outer_function_rustdoc_inserts {
2324
/// Widgets are very common (this is a doc comment, and will show up on
2425
/// Widget's documentation).
2526
pub struct Widget {
@@ -36,6 +37,7 @@ pub fn recalibrate() {
3637
//! `recalibrate`).
3738
/* ... */
3839
}
40+
# }
3941
~~~
4042

4143
Doc comments are markdown, and are currently parsed with the
@@ -94,7 +96,7 @@ source code.
9496

9597
To test documentation, the `--test` argument is passed to rustdoc:
9698

97-
~~~
99+
~~~ {.notrust}
98100
rustdoc --test crate.rs
99101
~~~
100102

@@ -105,56 +107,68 @@ code blocks as testable-by-default. In order to not run a test over a block of
105107
code, the `ignore` string can be added to the three-backtick form of markdown
106108
code block.
107109

108-
~~~
109-
```
110-
// This is a testable code block
111-
```
110+
/**
111+
# nested codefences confuse sundown => indentation + comment to
112+
# avoid failing tests
113+
```
114+
// This is a testable code block
115+
```
112116

113-
```ignore
114-
// This is not a testable code block
115-
```
117+
```ignore
118+
// This is not a testable code block
119+
```
116120

117-
// This is a testable code block (4-space indent)
118-
~~~
121+
// This is a testable code block (4-space indent)
122+
*/
123+
# fn foo() {}
119124

120125
You can specify that the test's execution should fail with the `should_fail`
121126
directive.
122127

123-
~~~
124-
```should_fail
125-
// This code block is expected to generate a failure when run
126-
```
127-
~~~
128+
/**
129+
# nested codefences confuse sundown => indentation + comment to
130+
# avoid failing tests
131+
```should_fail
132+
// This code block is expected to generate a failure when run
133+
```
134+
*/
135+
# fn foo() {}
128136

129137
You can specify that the code block should be compiled but not run with the
130138
`no_run` directive.
131139

132-
~~~
133-
```no_run
134-
// This code will be compiled but not executed
135-
```
136-
~~~
140+
/**
141+
# nested codefences confuse sundown => indentation + comment to
142+
# avoid failing tests
143+
```no_run
144+
// This code will be compiled but not executed
145+
```
146+
*/
147+
# fn foo() {}
137148

138149
Rustdoc also supplies some extra sugar for helping with some tedious
139150
documentation examples. If a line is prefixed with `# `, then the line
140151
will not show up in the HTML documentation, but it will be used when
141152
testing the code block (NB. the space after the `#` is required, so
142153
that one can still write things like `#[deriving(Eq)]`).
143154

144-
~~~
145-
```rust
146-
# /!\ The three following lines are comments, which are usually stripped off by
147-
# the doc-generating tool. In order to display them anyway in this particular
148-
# case, the character following the leading '#' is not a usual space like in
149-
# these first five lines but a non breakable one.
150-
#
151-
# // showing 'fib' in this documentation would just be tedious and detracts from
152-
# // what's actualy being documented.
153-
# fn fib(n: int) { n + 2 }
154-
155-
do spawn { fib(200); }
156-
```
157-
~~~
155+
/**
156+
# nested codefences confuse sundown => indentation + comment to
157+
# avoid failing tests
158+
```rust
159+
# /!\ The three following lines are comments, which are usually stripped off by
160+
# the doc-generating tool. In order to display them anyway in this particular
161+
# case, the character following the leading '#' is not a usual space like in
162+
# these first five lines but a non breakable one.
163+
#
164+
# // showing 'fib' in this documentation would just be tedious and detracts from
165+
# // what's actualy being documented.
166+
# fn fib(n: int) { n + 2 }
167+
168+
do spawn { fib(200); }
169+
```
170+
*/
171+
# fn foo() {}
158172

159173
The documentation online would look like `do spawn { fib(200); }`, but when
160174
testing this code, the `fib` function will be included (so it can compile).
@@ -167,12 +181,12 @@ uses is build on crate `test`, which is also used when you compile crates with
167181
rustc's `--test` flag. Extra arguments can be passed to rustdoc's test harness
168182
with the `--test-args` flag.
169183

170-
~~~
171-
// Only run tests containing 'foo' in their name
172-
rustdoc --test lib.rs --test-args 'foo'
184+
~~~ {.notrust}
185+
$ # Only run tests containing 'foo' in their name
186+
$ rustdoc --test lib.rs --test-args 'foo'
173187
174-
// See what's possible when running tests
175-
rustdoc --test lib.rs --test-args '--help'
188+
$ # See what's possible when running tests
189+
$ rustdoc --test lib.rs --test-args '--help'
176190
~~~
177191

178192
When testing a library, code examples will often show how functions are used,
@@ -189,6 +203,7 @@ into HTML and testing the code snippets from them. A Markdown file is
189203
detected by a `.md` or `.markdown` extension.
190204

191205
There are 4 options to modify the output that Rustdoc creates.
206+
192207
- `--markdown-css PATH`: adds a `<link rel="stylesheet">` tag pointing to `PATH`.
193208
- `--markdown-in-header FILE`: includes the contents of `FILE` at the
194209
end of the `<head>...</head>` section.

0 commit comments

Comments
 (0)