Skip to content

Commit a4f30bf

Browse files
committed
auto merge of #11185 : huonw/rust/doc-ignore, r=cmr
Currently any line starting with `#` is filtered from the output, including line like `#[deriving]`; this patch makes it so lines are only filtered when followed by a space similar to the current behaviour of the tutorial/manual tester.
2 parents f37b746 + dedc29f commit a4f30bf

File tree

6 files changed

+57
-7
lines changed

6 files changed

+57
-7
lines changed

doc/rustdoc.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,10 @@ specifiers that can be used to dictate how a code block is tested:
132132
~~~
133133

134134
Rustdoc also supplies some extra sugar for helping with some tedious
135-
documentation examples. If a line is prefixed with a `#` character, then the
136-
line will not show up in the HTML documentation, but it will be used when
137-
testing the code block.
135+
documentation examples. If a line is prefixed with `# `, then the line
136+
will not show up in the HTML documentation, but it will be used when
137+
testing the code block (NB. the space after the `#` is required, so
138+
that one can still write things like `#[deriving(Eq)]`).
138139

139140
~~~
140141
```rust

src/librustdoc/html/markdown.rs

+15-4
Original file line numberDiff line numberDiff line change
@@ -94,15 +94,26 @@ extern {
9494

9595
}
9696

97+
/// Returns Some(code) if `s` is a line that should be stripped from
98+
/// documentation but used in example code. `code` is the portion of
99+
/// `s` that should be used in tests. (None for lines that should be
100+
/// left as-is.)
101+
fn stripped_filtered_line<'a>(s: &'a str) -> Option<&'a str> {
102+
let trimmed = s.trim();
103+
if trimmed.starts_with("# ") {
104+
Some(trimmed.slice_from(2))
105+
} else {
106+
None
107+
}
108+
}
109+
97110
pub fn render(w: &mut io::Writer, s: &str) {
98111
extern fn block(ob: *buf, text: *buf, lang: *buf, opaque: *libc::c_void) {
99112
unsafe {
100113
let my_opaque: &my_opaque = cast::transmute(opaque);
101114
vec::raw::buf_as_slice((*text).data, (*text).size as uint, |text| {
102115
let text = str::from_utf8(text);
103-
let mut lines = text.lines().filter(|l| {
104-
!l.trim().starts_with("#")
105-
});
116+
let mut lines = text.lines().filter(|l| stripped_filtered_line(*l).is_none());
106117
let text = lines.to_owned_vec().connect("\n");
107118

108119
let buf = buf {
@@ -169,7 +180,7 @@ pub fn find_testable_code(doc: &str, tests: &mut ::test::Collector) {
169180
vec::raw::buf_as_slice((*text).data, (*text).size as uint, |text| {
170181
let tests: &mut ::test::Collector = intrinsics::transmute(opaque);
171182
let text = str::from_utf8(text);
172-
let mut lines = text.lines().map(|l| l.trim_chars(&'#'));
183+
let mut lines = text.lines().map(|l| stripped_filtered_line(l).unwrap_or(l));
173184
let text = lines.to_owned_vec().connect("\n");
174185
tests.add_test(text, ignore, shouldfail);
175186
})

src/librustdoc/test.rs

+1
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ impl Collector {
173173
let libs = self.libs.borrow();
174174
let libs = (*libs.get()).clone();
175175
let cratename = self.cratename.to_owned();
176+
debug!("Creating test {}: {}", name, test);
176177
self.tests.push(test::TestDescAndFn {
177178
desc: test::TestDesc {
178179
name: test::DynTestName(name),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
-include ../tools.mk
2+
3+
all:
4+
$(RUSTDOC) --test foo.rs
5+
$(RUSTDOC) -w html -o $(TMPDIR)/doc foo.rs
6+
cp verify.sh $(TMPDIR)
7+
$(call RUN,verify.sh) $(TMPDIR)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#[crate_id="foo#0.1"];
2+
3+
/// The '# ' lines should be removed from the output, but the #[deriving] should be
4+
/// retained.
5+
///
6+
/// ```rust
7+
/// mod to_make_deriving_work { // FIXME #4913
8+
///
9+
/// # #[deriving(Eq)] // invisible
10+
/// # struct Foo; // invisible
11+
///
12+
/// #[deriving(Eq)] // Bar
13+
/// struct Bar(Foo);
14+
///
15+
/// fn test() {
16+
/// let x = Bar(Foo);
17+
/// assert!(x == x); // check that the derivings worked
18+
/// }
19+
///
20+
/// }
21+
/// ```
22+
pub fn foo() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/sh
2+
3+
file="$1/doc/foo/fn.foo.html"
4+
5+
grep -v 'invisible' $file &&
6+
grep '#\[deriving(Eq)\] // Bar' $file
7+
8+
exit $?

0 commit comments

Comments
 (0)