2012-01-16 15:22:25 -06:00
|
|
|
import std::io;
|
|
|
|
import std::io::writer_util;
|
|
|
|
|
2012-01-17 18:12:50 -06:00
|
|
|
export mk_pass;
|
|
|
|
|
|
|
|
fn mk_pass(
|
|
|
|
writer: fn~() -> io::writer
|
|
|
|
) -> pass {
|
|
|
|
ret fn~(
|
|
|
|
_srv: astsrv::srv,
|
|
|
|
doc: doc::cratedoc
|
|
|
|
) -> doc::cratedoc {
|
|
|
|
write_markdown(doc, writer());
|
|
|
|
doc
|
|
|
|
};
|
|
|
|
}
|
2012-01-16 15:23:42 -06:00
|
|
|
|
2012-01-16 01:01:05 -06:00
|
|
|
type ctxt = {
|
2012-01-16 23:37:29 -06:00
|
|
|
w: io::writer,
|
|
|
|
mutable depth: uint
|
2012-01-16 00:58:57 -06:00
|
|
|
};
|
|
|
|
|
2012-01-16 02:33:36 -06:00
|
|
|
fn write_markdown(
|
|
|
|
doc: doc::cratedoc,
|
|
|
|
writer: io::writer
|
|
|
|
) {
|
|
|
|
let ctxt = {
|
2012-01-16 23:37:29 -06:00
|
|
|
w: writer,
|
|
|
|
mutable depth: 1u
|
2012-01-16 02:33:36 -06:00
|
|
|
};
|
|
|
|
|
2012-01-16 23:37:29 -06:00
|
|
|
write_crate(ctxt, doc);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn write_header(ctxt: ctxt, title: str) {
|
|
|
|
let hashes = str::from_chars(vec::init_elt('#', ctxt.depth));
|
|
|
|
ctxt.w.write_line(#fmt("%s %s", hashes, title));
|
2012-01-16 02:59:18 -06:00
|
|
|
}
|
|
|
|
|
2012-01-17 00:01:25 -06:00
|
|
|
fn subsection(ctxt: ctxt, f: fn&()) {
|
|
|
|
ctxt.depth += 1u;
|
|
|
|
f();
|
|
|
|
ctxt.depth -= 1u;
|
|
|
|
}
|
|
|
|
|
2012-01-16 23:37:29 -06:00
|
|
|
fn write_crate(
|
|
|
|
ctxt: ctxt,
|
|
|
|
doc: doc::cratedoc
|
|
|
|
) {
|
|
|
|
write_header(ctxt, #fmt("Crate %s", doc.topmod.name));
|
|
|
|
write_top_module(ctxt, doc.topmod);
|
2012-01-16 17:44:10 -06:00
|
|
|
}
|
|
|
|
|
2012-01-16 02:59:18 -06:00
|
|
|
fn write_top_module(
|
|
|
|
ctxt: ctxt,
|
|
|
|
moddoc: doc::moddoc
|
|
|
|
) {
|
2012-01-16 17:38:40 -06:00
|
|
|
write_mod_contents(ctxt, moddoc);
|
2012-01-16 02:59:18 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn write_mod(
|
|
|
|
ctxt: ctxt,
|
|
|
|
moddoc: doc::moddoc
|
|
|
|
) {
|
2012-01-17 01:10:25 -06:00
|
|
|
write_header(ctxt, #fmt("Module `%s`", moddoc.name));
|
2012-01-16 17:38:40 -06:00
|
|
|
write_mod_contents(ctxt, moddoc);
|
2012-01-16 02:59:18 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn write_mod_contents(
|
|
|
|
ctxt: ctxt,
|
|
|
|
moddoc: doc::moddoc
|
|
|
|
) {
|
|
|
|
for fndoc in *moddoc.fns {
|
2012-01-17 00:01:25 -06:00
|
|
|
subsection(ctxt) {||
|
|
|
|
write_fn(ctxt, fndoc);
|
|
|
|
}
|
2012-01-16 02:59:18 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
for moddoc in *moddoc.mods {
|
2012-01-17 00:01:25 -06:00
|
|
|
subsection(ctxt) {||
|
|
|
|
write_mod(ctxt, moddoc);
|
|
|
|
}
|
2012-01-16 02:59:18 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn write_fn(
|
|
|
|
ctxt: ctxt,
|
2012-01-16 17:44:10 -06:00
|
|
|
doc: doc::fndoc
|
2012-01-16 02:59:18 -06:00
|
|
|
) {
|
2012-01-16 23:37:29 -06:00
|
|
|
write_header(ctxt, #fmt("Function `%s`", doc.name));
|
2012-01-16 18:01:33 -06:00
|
|
|
alt doc.brief {
|
|
|
|
some(brief) {
|
|
|
|
ctxt.w.write_line(brief);
|
|
|
|
}
|
|
|
|
none. { }
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
alt doc.return {
|
2012-01-16 17:38:40 -06:00
|
|
|
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-15 17:23:07 -06:00
|
|
|
}
|
|
|
|
}
|
2012-01-16 02:33:36 -06:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
fn write_markdown_str(
|
2012-01-16 17:38:40 -06:00
|
|
|
doc: doc::cratedoc
|
2012-01-16 02:33:36 -06:00
|
|
|
) -> str {
|
|
|
|
let buffer = io::mk_mem_buffer();
|
|
|
|
let writer = io::mem_buffer_writer(buffer);
|
2012-01-16 17:38:40 -06:00
|
|
|
write_markdown(doc, writer);
|
2012-01-16 02:33:36 -06: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");
|
2012-01-16 17:38:40 -06:00
|
|
|
let markdown = write_markdown_str(doc);
|
2012-01-16 02:33:36 -06:00
|
|
|
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, "");
|
2012-01-16 17:38:40 -06:00
|
|
|
let markdown = write_markdown_str(doc);
|
2012-01-16 02:59:18 -06:00
|
|
|
assert str::contains(markdown, "## Function `func`");
|
|
|
|
}
|
2012-01-17 01:10:25 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn write_markdown_should_write_mod_headers() {
|
|
|
|
let source = "mod moo { }";
|
|
|
|
let ast = parse::from_str(source);
|
|
|
|
let doc = extract::extract(ast, "");
|
|
|
|
let markdown = write_markdown_str(doc);
|
|
|
|
assert str::contains(markdown, "## Module `moo`");
|
|
|
|
}
|
2012-01-16 02:33:36 -06:00
|
|
|
}
|