rust/src/rustdoc/rustdoc.rs

161 lines
4.0 KiB
Rust
Raw Normal View History

// Some utility interfaces
import doc::item;
import doc::util;
2012-01-17 19:44:32 -06:00
#[doc = "A single operation on the document model"]
2012-02-27 20:07:16 -06:00
type pass = {
name: str,
f: fn~(srv: astsrv::srv, doc: doc::doc) -> doc::doc
2012-02-27 20:07:16 -06:00
};
fn run_passes(
srv: astsrv::srv,
doc: doc::doc,
passes: [pass]
) -> doc::doc {
2012-01-18 16:06:22 -06:00
2012-01-19 01:11:20 -06:00
#[doc(
brief =
"Run a series of passes over the document",
args(
srv =
"The AST service to provide to the passes",
doc =
"The document to transform",
passes =
"The list of passes used to transform the document"
),
return =
"The transformed document that results from folding the \
original through each pass"
)];
2012-01-18 16:06:22 -06:00
let mut passno = 0;
vec::foldl(doc, passes) {|doc, pass|
2012-01-25 22:55:55 -06:00
log(debug, #fmt("pass #%d", passno));
passno += 1;
log(debug, doc);
2012-02-27 20:07:16 -06:00
time(pass.name) {||
pass.f(srv, doc)
}
}
}
#[test]
fn test_run_passes() {
fn pass1(
_srv: astsrv::srv,
doc: doc::doc
) -> doc::doc {
2012-01-30 15:05:25 -06:00
{
pages: [
doc::cratepage({
topmod: {
item: {
name: doc.cratemod().name() + "two"
with doc.cratemod().item
},
items: [],
index: none
}
})
]
}
}
fn pass2(
_srv: astsrv::srv,
doc: doc::doc
) -> doc::doc {
2012-01-30 15:05:25 -06:00
{
pages: [
doc::cratepage({
topmod: {
item: {
name: doc.cratemod().name() + "three"
with doc.cratemod().item
},
items: [],
index: none
}
})
]
}
}
let source = "";
astsrv::from_str(source) {|srv|
2012-02-27 20:07:16 -06:00
let passes = [
{
name: "",
f: pass1
},
{
name: "",
f: pass2
}
];
let doc = extract::from_srv(srv, "one");
let doc = run_passes(srv, doc, passes);
assert doc.cratemod().name() == "onetwothree";
}
}
2012-02-28 23:31:55 -06:00
fn main(args: [str]) {
2012-02-28 23:31:55 -06:00
if vec::contains(args, "-h") {
2012-02-28 23:27:38 -06:00
config::usage();
ret;
}
2012-02-28 23:31:55 -06:00
let config = alt config::parse_config(args) {
result::ok(config) { config }
result::err(err) {
io::println(#fmt("error: %s", err));
ret;
2012-02-28 23:31:55 -06:00
}
};
2012-02-29 20:10:40 -06:00
run(config);
}
2012-02-27 20:07:16 -06:00
fn time<T>(what: str, f: fn() -> T) -> T {
let start = std::time::precise_time_s();
let rv = f();
let end = std::time::precise_time_s();
#info("time: %3.3f s %s", end - start, what);
ret rv;
}
2012-01-17 19:22:03 -06:00
#[doc = "Runs rustdoc over the given file"]
2012-02-29 20:10:40 -06:00
fn run(config: config::config) {
2012-02-29 20:10:40 -06:00
let source_file = config.input_crate;
astsrv::from_file(source_file) {|srv|
2012-02-27 20:11:12 -06:00
time("wait_ast") {||
2012-02-27 20:07:16 -06:00
astsrv::exec(srv) {|_ctxt| () }
};
let doc = time("extract") {||
2012-02-29 20:10:40 -06:00
let default_name = source_file;
2012-02-27 20:07:16 -06:00
extract::from_srv(srv, default_name)
};
run_passes(srv, doc, [
2012-02-24 17:43:57 -06:00
reexport_pass::mk_pass(),
prune_unexported_pass::mk_pass(),
tystr_pass::mk_pass(),
path_pass::mk_pass(),
attr_pass::mk_pass(),
2012-03-16 14:20:29 -05:00
escape_pass::mk_pass(),
2012-03-07 16:49:52 -06:00
prune_hidden_pass::mk_pass(),
desc_to_brief_pass::mk_pass(),
unindent_pass::mk_pass(),
2012-03-09 13:47:31 -06:00
sectionalize_pass::mk_pass(),
trim_pass::mk_pass(),
sort_item_name_pass::mk_pass(),
sort_item_type_pass::mk_pass(),
markdown_index_pass::mk_pass(config),
page_pass::mk_pass(config.output_style),
markdown_pass::mk_pass(
markdown_writer::make_writer_factory(config)
)
]);
}
}