2012-01-16 15:22:25 -06:00
|
|
|
import std::io;
|
|
|
|
import std::io::writer_util;
|
|
|
|
import rustc::syntax::ast;
|
|
|
|
import rustc::syntax::print::pprust;
|
|
|
|
|
2012-01-16 15:23:42 -06:00
|
|
|
export write_markdown;
|
|
|
|
|
2012-01-16 01:01:05 -06:00
|
|
|
type ctxt = {
|
2012-01-16 00:58:57 -06:00
|
|
|
ps: pprust::ps,
|
|
|
|
w: io::writer
|
|
|
|
};
|
|
|
|
|
2012-01-16 02:33:36 -06:00
|
|
|
fn write_markdown(
|
|
|
|
doc: doc::cratedoc,
|
2012-01-16 02:59:18 -06:00
|
|
|
crate: @ast::crate,
|
2012-01-16 02:33:36 -06:00
|
|
|
writer: io::writer
|
|
|
|
) {
|
|
|
|
let ctxt = {
|
|
|
|
ps: pprust::rust_printer(writer),
|
|
|
|
w: writer
|
|
|
|
};
|
|
|
|
|
|
|
|
write_header(ctxt, doc.topmod.name);
|
2012-01-16 02:59:18 -06:00
|
|
|
write_top_module(ctxt, crate, doc.topmod);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn write_top_module(
|
|
|
|
ctxt: ctxt,
|
|
|
|
crate: @ast::crate,
|
|
|
|
moddoc: doc::moddoc
|
|
|
|
) {
|
|
|
|
write_mod_contents(ctxt, crate, moddoc);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn write_mod(
|
|
|
|
ctxt: ctxt,
|
|
|
|
crate: @ast::crate,
|
|
|
|
moddoc: doc::moddoc
|
|
|
|
) {
|
|
|
|
write_mod_contents(ctxt, crate, moddoc);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn write_mod_contents(
|
|
|
|
ctxt: ctxt,
|
|
|
|
crate: @ast::crate,
|
|
|
|
moddoc: doc::moddoc
|
|
|
|
) {
|
|
|
|
for fndoc in *moddoc.fns {
|
|
|
|
write_fn(ctxt, crate, fndoc);
|
|
|
|
}
|
|
|
|
|
|
|
|
for moddoc in *moddoc.mods {
|
|
|
|
write_mod(ctxt, crate, moddoc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn write_fn(
|
|
|
|
ctxt: ctxt,
|
|
|
|
crate: @ast::crate,
|
|
|
|
fndoc: doc::fndoc
|
|
|
|
) {
|
|
|
|
import rustc::middle::ast_map;
|
|
|
|
|
|
|
|
let map = ast_map::map_crate(*crate);
|
|
|
|
let decl = alt map.get(fndoc.id) {
|
|
|
|
ast_map::node_item(@{
|
|
|
|
node: ast::item_fn(decl, _, _), _
|
|
|
|
}) { decl }
|
|
|
|
};
|
|
|
|
|
|
|
|
write_fndoc(ctxt, fndoc.name, fndoc, decl);
|
2012-01-16 02:33:36 -06:00
|
|
|
}
|
|
|
|
|
2012-01-15 17:23:07 -06:00
|
|
|
#[doc(
|
|
|
|
brief = "Generate a crate document header.",
|
|
|
|
args(rd = "Rustdoc context",
|
|
|
|
name = "Crate name")
|
|
|
|
)]
|
2012-01-16 01:01:05 -06:00
|
|
|
fn write_header(ctxt: ctxt, name: str) {
|
|
|
|
ctxt.w.write_line("# Crate " + name);
|
2012-01-15 17:23:07 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[doc(
|
|
|
|
brief = "Documents a single function.",
|
|
|
|
args(rd = "Rustdoc context",
|
|
|
|
ident = "Identifier for this function",
|
|
|
|
doc = "Function docs extracted from attributes",
|
|
|
|
_fn = "AST object representing this function")
|
|
|
|
)]
|
2012-01-16 01:01:05 -06:00
|
|
|
fn write_fndoc(ctxt: ctxt, ident: str, doc: doc::fndoc, decl: ast::fn_decl) {
|
|
|
|
ctxt.w.write_line("## Function `" + ident + "`");
|
|
|
|
ctxt.w.write_line(doc.brief);
|
2012-01-15 17:23:07 -06:00
|
|
|
alt doc.desc {
|
|
|
|
some(_d) {
|
2012-01-16 01:01:05 -06:00
|
|
|
ctxt.w.write_line("");
|
|
|
|
ctxt.w.write_line(_d);
|
|
|
|
ctxt.w.write_line("");
|
2012-01-15 17:23:07 -06:00
|
|
|
}
|
|
|
|
none. { }
|
|
|
|
}
|
2012-01-16 15:53:17 -06:00
|
|
|
for (arg, desc) in doc.args {
|
|
|
|
ctxt.w.write_str("### Argument `" + arg + "`: ");
|
2012-01-16 16:02:46 -06:00
|
|
|
ctxt.w.write_str(desc)
|
2012-01-15 17:23:07 -06:00
|
|
|
}
|
2012-01-16 01:01:05 -06:00
|
|
|
ctxt.w.write_line("### Returns `" + pprust::ty_to_str(decl.output) + "`");
|
2012-01-15 17:23:07 -06:00
|
|
|
alt doc.return {
|
2012-01-16 16:02:46 -06:00
|
|
|
some({desc: some(d), _}) { ctxt.w.write_line(d); }
|
|
|
|
_ { }
|
2012-01-15 17:23:07 -06:00
|
|
|
}
|
|
|
|
}
|
2012-01-16 02:33:36 -06:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
fn write_markdown_str(
|
|
|
|
doc: doc::cratedoc,
|
|
|
|
crate: @ast::crate
|
|
|
|
) -> str {
|
|
|
|
let buffer = io::mk_mem_buffer();
|
|
|
|
let writer = io::mem_buffer_writer(buffer);
|
|
|
|
write_markdown(doc, crate, writer);
|
|
|
|
ret io::mem_buffer_str(buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn write_markdown_should_write_crate_header() {
|
|
|
|
let source = "";
|
|
|
|
let ast = parse::from_str(source);
|
|
|
|
let doc = extract::extract(ast, "belch");
|
|
|
|
let markdown = write_markdown_str(doc, ast);
|
|
|
|
assert str::contains(markdown, "# Crate belch\n");
|
|
|
|
}
|
2012-01-16 02:59:18 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn write_markdown_should_write_function_header() {
|
|
|
|
let source = "fn func() { }";
|
|
|
|
let ast = parse::from_str(source);
|
|
|
|
let doc = extract::extract(ast, "");
|
|
|
|
let markdown = write_markdown_str(doc, ast);
|
|
|
|
assert str::contains(markdown, "## Function `func`");
|
|
|
|
}
|
2012-01-16 02:33:36 -06:00
|
|
|
}
|