rustdoc: Extract markdown_writer from markdown_pass

This commit is contained in:
Brian Anderson 2012-02-29 22:26:28 -08:00
parent 516f87f860
commit 9208684e74
3 changed files with 103 additions and 90 deletions

View File

@ -1,77 +1,12 @@
#[doc = "Generate markdown from a document tree"];
import markdown_writer::writer;
import markdown_writer::writer_util;
export mk_pass;
fn mk_pass(config: config::config) -> pass {
mk_pass_(config, markdown_writer(config))
}
enum writeinstr {
write(str),
done
}
type writer = fn~(+writeinstr);
impl writer for writer {
fn write_str(str: str) {
self(write(str));
}
fn write_line(str: str) {
self.write_str(str + "\n");
}
fn write_done() {
self(done)
}
}
fn markdown_writer(config: config::config) -> writer {
let filename = make_filename(config);
let ch = task::spawn_listener {|po: comm::port<writeinstr>|
let markdown = "";
let keep_going = true;
while keep_going {
alt comm::recv(po) {
write(s) { markdown += s; }
done { keep_going = false; }
}
}
write_file(filename, markdown);
};
fn~(+instr: writeinstr) {
comm::send(ch, instr);
}
}
fn make_filename(config: config::config) -> str {
import std::fs;
let cratefile = fs::basename(config.input_crate);
let cratename = tuple::first(fs::splitext(cratefile));
fs::connect(config.output_dir, cratename + ".md")
}
fn write_file(path: str, s: str) {
import std::io;
import std::io::writer_util;
alt io::file_writer(path, [io::create, io::truncate]) {
result::ok(writer) {
writer.write_str(s);
}
result::err(e) { fail e }
}
}
#[test]
fn should_use_markdown_file_name_based_off_crate() {
let config = {
output_dir: "output/dir"
with config::default_config("input/test.rc")
};
assert make_filename(config) == "output/dir/test.md";
mk_pass_(config, markdown_writer::make_writer(config))
}
// FIXME: This is a really convoluted interface to work around trying
@ -918,29 +853,10 @@ mod test {
doc
}
fn writer_future() -> (writer, future::future<str>) {
let port = comm::port();
let chan = comm::chan(port);
let writer = fn~(+instr: writeinstr) {
comm::send(chan, copy instr);
};
let future = future::from_fn {||
let res = "";
while true {
alt comm::recv(port) {
write(s) { res += s }
done { break }
}
}
res
};
(writer, future)
}
fn write_markdown_str(
doc: doc::cratedoc
) -> str {
let (writer, future) = writer_future();
let (writer, future) = markdown_writer::future_writer();
write_markdown(doc, writer);
ret future::get(future);
}
@ -953,7 +869,7 @@ mod test {
output_style: config::doc_per_crate
with config::default_config("")
};
let (writer, future) = writer_future();
let (writer, future) = markdown_writer::future_writer();
let pass = mk_pass_(config, writer);
pass.f(srv, doc);
ret future::get(future);

View File

@ -0,0 +1,96 @@
export writeinstr;
export writer;
export writer_util;
export make_writer;
export future_writer;
enum writeinstr {
write(str),
done
}
type writer = fn~(+writeinstr);
impl writer_util for writer {
fn write_str(str: str) {
self(write(str));
}
fn write_line(str: str) {
self.write_str(str + "\n");
}
fn write_done() {
self(done)
}
}
fn make_writer(config: config::config) -> writer {
markdown_writer(config)
}
fn markdown_writer(config: config::config) -> writer {
let filename = make_filename(config);
let ch = task::spawn_listener {|po: comm::port<writeinstr>|
let markdown = "";
let keep_going = true;
while keep_going {
alt comm::recv(po) {
write(s) { markdown += s; }
done { keep_going = false; }
}
}
write_file(filename, markdown);
};
fn~(+instr: writeinstr) {
comm::send(ch, instr);
}
}
fn make_filename(config: config::config) -> str {
import std::fs;
let cratefile = fs::basename(config.input_crate);
let cratename = tuple::first(fs::splitext(cratefile));
fs::connect(config.output_dir, cratename + ".md")
}
fn write_file(path: str, s: str) {
import std::io;
import std::io::writer_util;
alt io::file_writer(path, [io::create, io::truncate]) {
result::ok(writer) {
writer.write_str(s);
}
result::err(e) { fail e }
}
}
#[test]
fn should_use_markdown_file_name_based_off_crate() {
let config = {
output_dir: "output/dir"
with config::default_config("input/test.rc")
};
assert make_filename(config) == "output/dir/test.md";
}
fn future_writer() -> (writer, future::future<str>) {
let port = comm::port();
let chan = comm::chan(port);
let writer = fn~(+instr: writeinstr) {
comm::send(chan, copy instr);
};
let future = future::from_fn {||
let res = "";
while true {
alt comm::recv(port) {
write(s) { res += s }
done { break }
}
}
res
};
(writer, future)
}

View File

@ -18,6 +18,7 @@ mod extract;
mod attr_parser;
mod doc;
mod markdown_pass;
mod markdown_writer;
mod fold;
mod path_pass;
mod attr_pass;