Skip to content

Commit 4b0dcdb

Browse files
committed
---
yaml --- r: 10885 b: refs/heads/master c: 361f90e h: refs/heads/master i: 10883: c9eb626 v: v3
1 parent cb6cb81 commit 4b0dcdb

File tree

3 files changed

+82
-10
lines changed

3 files changed

+82
-10
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
refs/heads/master: 07ac2e1043c3a4e9ba0058f0b67798821d0118ee
2+
refs/heads/master: 361f90e618c081afe2fa8b0a67370610781e413e
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: 4a81779abd786ff22d71434c6d9a5917ea4cdfff
55
refs/heads/try: 2898dcc5d97da9427ac367542382b6239d9c0bbf

trunk/src/rustdoc/markdown_pass.rs

Lines changed: 80 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,65 @@ import std::io::writer_util;
55

66
export mk_pass;
77

8+
// FIXME: This is a really convoluted interface to work around trying
9+
// to get a writer into a unique closure and then being able to test
10+
// what was written afterward
811
fn mk_pass(
9-
writer: fn~() -> io::writer
12+
give_writer: fn~(fn(io::writer))
1013
) -> pass {
11-
ret fn~(
12-
_srv: astsrv::srv,
14+
fn~(
15+
srv: astsrv::srv,
1316
doc: doc::cratedoc
1417
) -> doc::cratedoc {
15-
write_markdown(doc, writer());
18+
19+
fn mods_last(item1: doc::itemtag, item2: doc::itemtag) -> bool {
20+
fn is_mod(item: doc::itemtag) -> bool {
21+
alt item {
22+
doc::modtag(_) { true }
23+
_ { false }
24+
}
25+
}
26+
27+
let lteq = !is_mod(item1) || is_mod(item2);
28+
lteq
29+
}
30+
31+
give_writer {|writer|
32+
// Sort the items so mods come last. All mods will be
33+
// output at the same header level so sorting mods last
34+
// makes the headers come out nested correctly.
35+
let sorted_doc = sort_pass::mk_pass(mods_last)(srv, doc);
36+
37+
write_markdown(sorted_doc, writer);
38+
}
1639
doc
17-
};
40+
}
41+
}
42+
43+
#[test]
44+
fn should_write_modules_last() {
45+
/*
46+
Because the markdown pass writes all modules at the same level of
47+
indentation (it doesn't 'nest' them), we need to make sure that we
48+
write all of the modules contained in each module after all other
49+
types of items, or else the header nesting will end up wrong, with
50+
modules appearing to contain items that they do not.
51+
*/
52+
let markdown = test::render(
53+
"mod a { }\
54+
fn b() { }\
55+
mod c { }\
56+
fn d() { }"
57+
);
58+
59+
let idx_a = str::find(markdown, "# Module `a`");
60+
let idx_b = str::find(markdown, "## Function `b`");
61+
let idx_c = str::find(markdown, "# Module `c`");
62+
let idx_d = str::find(markdown, "## Function `d`");
63+
64+
assert idx_b < idx_d;
65+
assert idx_d < idx_a;
66+
assert idx_a < idx_c;
1867
}
1968

2069
type ctxt = {
@@ -487,13 +536,13 @@ fn should_write_resource_args() {
487536
#[cfg(test)]
488537
mod test {
489538
fn render(source: str) -> str {
490-
let doc = create_doc(source);
491-
let markdown = write_markdown_str(doc);
539+
let (srv, doc) = create_doc_srv(source);
540+
let markdown = write_markdown_str_srv(srv, doc);
492541
#debug("markdown: %s", markdown);
493542
markdown
494543
}
495544

496-
fn create_doc(source: str) -> doc::cratedoc {
545+
fn create_doc_srv(source: str) -> (astsrv::srv, doc::cratedoc) {
497546
let srv = astsrv::mk_srv_from_str(source);
498547
let doc = extract::from_srv(srv, "");
499548
#debug("doc (extract): %?", doc);
@@ -503,6 +552,11 @@ mod test {
503552
#debug("doc (path): %?", doc);
504553
let doc = attr_pass::mk_pass()(srv, doc);
505554
#debug("doc (attr): %?", doc);
555+
(srv, doc)
556+
}
557+
558+
fn create_doc(source: str) -> doc::cratedoc {
559+
let (_, doc) = create_doc_srv(source);
506560
doc
507561
}
508562

@@ -515,6 +569,24 @@ mod test {
515569
ret io::mem_buffer_str(buffer);
516570
}
517571

572+
fn write_markdown_str_srv(
573+
srv: astsrv::srv,
574+
doc: doc::cratedoc
575+
) -> str {
576+
let port = comm::port();
577+
let chan = comm::chan(port);
578+
579+
let pass = mk_pass {|f|
580+
let buffer = io::mk_mem_buffer();
581+
let writer = io::mem_buffer_writer(buffer);
582+
f(writer);
583+
let result = io::mem_buffer_str(buffer);
584+
comm::send(chan, result);
585+
};
586+
pass(srv, doc);
587+
ret comm::recv(port);
588+
}
589+
518590
#[test]
519591
fn write_markdown_should_write_crate_header() {
520592
let srv = astsrv::mk_srv_from_str("");

trunk/src/rustdoc/rustdoc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,6 @@ fn run(source_file: str) {
105105
desc_to_brief_pass::mk_pass(),
106106
trim_pass::mk_pass(),
107107
unindent_pass::mk_pass(),
108-
markdown_pass::mk_pass {|| std::io:: stdout()}
108+
markdown_pass::mk_pass {|f| f(std::io:: stdout()) }
109109
]);
110110
}

0 commit comments

Comments
 (0)