rust/src/rustdoc/rustdoc.rs

144 lines
3.6 KiB
Rust
Raw Normal View History

// Some utility interfaces
2012-09-05 12:41:47 -05:00
use doc::item_utils;
use doc::item;
use doc::util;
/// 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 {
let mut passno = 0;
2012-06-30 18:19:07 -05:00
do vec::foldl(doc, passes) |doc, pass| {
2012-08-22 19:24:52 -05:00
log(debug, fmt!("pass #%d", passno));
2012-01-25 22:55:55 -06:00
passno += 1;
log(debug, doc);
do time(pass.name) {
2012-02-27 20:07:16 -06:00
pass.f(srv, doc)
}
}
}
#[test]
fn test_run_passes() {
fn pass1(
_srv: astsrv::srv,
doc: doc::doc
) -> doc::doc {
doc::doc_({
pages: ~[
doc::cratepage({
topmod: doc::moddoc_({
item: {
2012-09-04 15:29:32 -05:00
name: doc.cratemod().name() + ~"two",
.. doc.cratemod().item
},
items: ~[],
2012-08-20 14:23:37 -05:00
index: None
})
})
]
})
}
fn pass2(
_srv: astsrv::srv,
doc: doc::doc
) -> doc::doc {
doc::doc_({
pages: ~[
doc::cratepage({
topmod: doc::moddoc_({
item: {
2012-09-04 15:29:32 -05:00
name: doc.cratemod().name() + ~"three",
.. doc.cratemod().item
},
items: ~[],
2012-08-20 14:23:37 -05:00
index: None
})
})
]
})
}
let source = ~"";
2012-06-30 18:19:07 -05:00
do astsrv::from_str(source) |srv| {
let passes = ~[
2012-02-27 20:07:16 -06:00
{
name: ~"",
2012-02-27 20:07:16 -06:00
f: pass1
},
{
name: ~"",
2012-02-27 20:07:16 -06:00
f: pass2
}
];
let doc = extract::from_srv(srv, ~"one");
let doc = run_passes(srv, doc, passes);
assert doc.cratemod().name() == ~"onetwothree";
}
}
fn main(args: ~[~str]) {
if vec::contains(args, ~"-h") {
2012-02-28 23:27:38 -06:00
config::usage();
2012-08-01 19:30:05 -05:00
return;
2012-02-28 23:27:38 -06:00
}
2012-08-06 14:34:08 -05:00
let config = match config::parse_config(args) {
2012-08-26 18:54:31 -05:00
result::Ok(config) => config,
result::Err(err) => {
2012-08-22 19:24:52 -05:00
io::println(fmt!("error: %s", err));
2012-08-01 19:30:05 -05:00
return;
2012-02-28 23:31:55 -06:00
}
};
2012-02-29 20:10:40 -06:00
run(config);
}
fn time<T>(what: ~str, f: fn() -> T) -> T {
2012-02-27 20:07:16 -06:00
let start = std::time::precise_time_s();
let rv = f();
let end = std::time::precise_time_s();
2012-08-22 19:24:52 -05:00
info!("time: %3.3f s %s", end - start, what);
2012-08-01 19:30:05 -05:00
return rv;
2012-02-27 20:07:16 -06:00
}
/// 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;
do astsrv::from_file(source_file.to_str()) |srv| {
do time(~"wait_ast") {
2012-06-30 18:19:07 -05:00
do astsrv::exec(srv) |_ctxt| { }
2012-02-27 20:07:16 -06:00
};
let doc = time(~"extract", || {
2012-02-29 20:10:40 -06:00
let default_name = source_file;
extract::from_srv(srv, default_name.to_str())
});
run_passes(srv, doc, ~[
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)
)
]);
}
}