rust/src/rustdoc/gen.rs

118 lines
2.5 KiB
Rust
Raw Normal View History

import std::io;
import std::io::writer_util;
export write_markdown;
type ctxt = {
w: io::writer
};
2012-01-16 00:33:36 -08:00
fn write_markdown(
doc: doc::cratedoc,
writer: io::writer
) {
let ctxt = {
w: writer
};
write_header(ctxt, doc.topmod.name);
write_top_module(ctxt, doc.topmod);
2012-01-16 00:59:18 -08:00
}
2012-01-16 15:44:10 -08:00
fn write_header(ctxt: ctxt, name: str) {
ctxt.w.write_line("# Crate " + name);
}
2012-01-16 00:59:18 -08:00
fn write_top_module(
ctxt: ctxt,
moddoc: doc::moddoc
) {
write_mod_contents(ctxt, moddoc);
2012-01-16 00:59:18 -08:00
}
fn write_mod(
ctxt: ctxt,
moddoc: doc::moddoc
) {
write_mod_contents(ctxt, moddoc);
2012-01-16 00:59:18 -08:00
}
fn write_mod_contents(
ctxt: ctxt,
moddoc: doc::moddoc
) {
for fndoc in *moddoc.fns {
write_fn(ctxt, fndoc);
2012-01-16 00:59:18 -08:00
}
for moddoc in *moddoc.mods {
write_mod(ctxt, moddoc);
2012-01-16 00:59:18 -08:00
}
}
fn write_fn(
ctxt: ctxt,
2012-01-16 15:44:10 -08:00
doc: doc::fndoc
2012-01-16 00:59:18 -08:00
) {
2012-01-16 15:44:10 -08:00
ctxt.w.write_line("## Function `" + doc.name + "`");
ctxt.w.write_line(doc.brief);
alt doc.desc {
some(_d) {
ctxt.w.write_line("");
ctxt.w.write_line(_d);
ctxt.w.write_line("");
}
none. { }
}
for (arg, desc) in doc.args {
ctxt.w.write_str("### Argument `" + arg + "`: ");
ctxt.w.write_str(desc)
}
alt doc.return {
some(doc) {
alt doc.ty {
some(ty) {
ctxt.w.write_line("### Returns `" + ty + "`");
alt doc.desc {
some(d) {
ctxt.w.write_line(d);
}
none. { }
}
}
none. { fail "unimplemented"; }
}
}
none. { }
}
}
2012-01-16 00:33:36 -08:00
#[cfg(test)]
mod tests {
fn write_markdown_str(
doc: doc::cratedoc
2012-01-16 00:33:36 -08:00
) -> str {
let buffer = io::mk_mem_buffer();
let writer = io::mem_buffer_writer(buffer);
write_markdown(doc, writer);
2012-01-16 00:33:36 -08:00
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);
2012-01-16 00:33:36 -08:00
assert str::contains(markdown, "# Crate belch\n");
}
2012-01-16 00:59:18 -08: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);
2012-01-16 00:59:18 -08:00
assert str::contains(markdown, "## Function `func`");
}
2012-01-16 00:33:36 -08:00
}