From 9208684e745b50b511c0fd3645a8ab2f3919fc82 Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Wed, 29 Feb 2012 22:26:28 -0800 Subject: [PATCH] rustdoc: Extract markdown_writer from markdown_pass --- src/rustdoc/markdown_pass.rs | 96 +++------------------------------- src/rustdoc/markdown_writer.rs | 96 ++++++++++++++++++++++++++++++++++ src/rustdoc/rustdoc.rc | 1 + 3 files changed, 103 insertions(+), 90 deletions(-) create mode 100644 src/rustdoc/markdown_writer.rs diff --git a/src/rustdoc/markdown_pass.rs b/src/rustdoc/markdown_pass.rs index 06b1b0f4484..da7472fd065 100644 --- a/src/rustdoc/markdown_pass.rs +++ b/src/rustdoc/markdown_pass.rs @@ -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| - 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) { - 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); diff --git a/src/rustdoc/markdown_writer.rs b/src/rustdoc/markdown_writer.rs new file mode 100644 index 00000000000..ff382a2aca4 --- /dev/null +++ b/src/rustdoc/markdown_writer.rs @@ -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| + 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) { + 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) +} diff --git a/src/rustdoc/rustdoc.rc b/src/rustdoc/rustdoc.rc index e317ade9d16..5f88ca4bdf9 100644 --- a/src/rustdoc/rustdoc.rc +++ b/src/rustdoc/rustdoc.rc @@ -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;