rust/src/rustdoc/markdown_writer.rs

304 lines
7.8 KiB
Rust
Raw Normal View History

2012-09-18 18:48:40 -05:00
use doc::ItemUtils;
2012-09-05 12:41:47 -05:00
use io::ReaderUtil;
2012-09-18 18:48:40 -05:00
export WriteInstr;
export Writer;
export WriterFactory;
export WriterUtils;
export make_writer_factory;
export future_writer_factory;
export make_filename;
2012-09-18 18:48:40 -05:00
enum WriteInstr {
Write(~str),
Done
}
2012-09-23 07:39:39 -05:00
type Writer = fn~(+v: WriteInstr);
2012-09-18 18:48:40 -05:00
type WriterFactory = fn~(page: doc::Page) -> Writer;
2012-09-18 18:48:40 -05:00
trait WriterUtils {
fn write_str(str: ~str);
fn write_line(str: ~str);
fn write_done();
}
2012-09-18 18:48:40 -05:00
impl Writer: WriterUtils {
fn write_str(str: ~str) {
2012-09-18 18:48:40 -05:00
self(Write(str));
}
fn write_line(str: ~str) {
self.write_str(str + ~"\n");
}
fn write_done() {
2012-09-18 18:48:40 -05:00
self(Done)
}
}
2012-09-18 18:48:40 -05:00
fn make_writer_factory(config: config::Config) -> WriterFactory {
2012-08-06 14:34:08 -05:00
match config.output_format {
2012-09-18 18:48:40 -05:00
config::Markdown => {
markdown_writer_factory(config)
2012-03-01 16:21:14 -06:00
}
2012-09-18 18:48:40 -05:00
config::PandocHtml => {
pandoc_writer_factory(config)
2012-03-01 16:21:14 -06:00
}
}
}
2012-09-18 18:48:40 -05:00
fn markdown_writer_factory(config: config::Config) -> WriterFactory {
fn~(page: doc::Page) -> Writer {
markdown_writer(config, page)
}
}
2012-09-18 18:48:40 -05:00
fn pandoc_writer_factory(config: config::Config) -> WriterFactory {
fn~(page: doc::Page) -> Writer {
pandoc_writer(config, page)
}
}
fn markdown_writer(
2012-09-18 18:48:40 -05:00
config: config::Config,
page: doc::Page
) -> Writer {
let filename = make_local_filename(config, page);
2012-06-30 18:19:07 -05:00
do generic_writer |markdown| {
write_file(&filename, markdown);
2012-03-01 16:21:14 -06:00
}
}
fn pandoc_writer(
2012-09-18 18:48:40 -05:00
config: config::Config,
page: doc::Page
) -> Writer {
2012-09-21 21:37:57 -05:00
assert config.pandoc_cmd.is_some();
let pandoc_cmd = config.pandoc_cmd.get();
let filename = make_local_filename(config, page);
2012-03-01 16:21:14 -06:00
let pandoc_args = ~[
~"--standalone",
~"--section-divs",
~"--from=markdown",
~"--to=html",
~"--css=rust.css",
~"--output=" + filename.to_str()
];
2012-03-01 16:21:14 -06:00
2012-06-30 18:19:07 -05:00
do generic_writer |markdown| {
use io::WriterUtil;
2012-03-01 16:21:14 -06:00
2012-08-22 19:24:52 -05:00
debug!("pandoc cmd: %s", pandoc_cmd);
debug!("pandoc args: %s", str::connect(pandoc_args, ~" "));
2012-03-01 16:21:14 -06:00
let pipe_in = os::pipe();
let pipe_out = os::pipe();
let pipe_err = os::pipe();
let pid = run::spawn_process(
2012-08-20 14:23:37 -05:00
pandoc_cmd, pandoc_args, &None, &None,
2012-03-01 16:21:14 -06:00
pipe_in.in, pipe_out.out, pipe_err.out);
let writer = io::fd_writer(pipe_in.out, false);
writer.write_str(markdown);
2012-03-01 16:21:14 -06:00
os::close(pipe_in.in);
os::close(pipe_out.out);
os::close(pipe_err.out);
os::close(pipe_in.out);
2012-08-27 16:22:25 -05:00
let stdout_po = comm::Port();
let stdout_ch = comm::Chan(stdout_po);
2012-08-15 16:10:46 -05:00
do task::spawn_sched(task::SingleThreaded) {
comm::send(stdout_ch, readclose(pipe_out.in));
}
let stdout = comm::recv(stdout_po);
2012-08-27 16:22:25 -05:00
let stderr_po = comm::Port();
let stderr_ch = comm::Chan(stderr_po);
2012-08-15 16:10:46 -05:00
do task::spawn_sched(task::SingleThreaded) {
comm::send(stderr_ch, readclose(pipe_err.in));
}
let stderr = comm::recv(stderr_po);
2012-03-01 16:21:14 -06:00
let status = run::waitpid(pid);
2012-08-22 19:24:52 -05:00
debug!("pandoc result: %i", status);
2012-03-01 16:21:14 -06:00
if status != 0 {
2012-08-22 19:24:52 -05:00
error!("pandoc-out: %s", stdout);
error!("pandoc-err: %s", stderr);
fail ~"pandoc failed";
2012-03-01 16:21:14 -06:00
}
}
}
fn readclose(fd: libc::c_int) -> ~str {
// Copied from run::program_output
let file = os::fdopen(fd);
let reader = io::FILE_reader(file, false);
let mut buf = ~"";
while !reader.eof() {
let bytes = reader.read_bytes(4096u);
buf += str::from_bytes(bytes);
}
os::fclose(file);
2012-08-01 19:30:05 -05:00
return buf;
}
2012-09-18 18:48:40 -05:00
fn generic_writer(+process: fn~(markdown: ~str)) -> Writer {
let ch = do task::spawn_listener |po: comm::Port<WriteInstr>| {
let mut markdown = ~"";
let mut keep_going = true;
while keep_going {
2012-08-06 14:34:08 -05:00
match comm::recv(po) {
2012-09-18 18:48:40 -05:00
Write(s) => markdown += s,
Done => keep_going = false
}
}
2012-03-01 16:21:14 -06:00
process(markdown);
};
2012-09-18 18:48:40 -05:00
fn~(+instr: WriteInstr) {
comm::send(ch, instr);
}
}
fn make_local_filename(
2012-09-18 18:48:40 -05:00
config: config::Config,
page: doc::Page
) -> Path {
let filename = make_filename(config, page);
config.output_dir.push_rel(&filename)
}
fn make_filename(
2012-09-18 18:48:40 -05:00
config: config::Config,
page: doc::Page
) -> Path {
let filename = {
2012-08-06 14:34:08 -05:00
match page {
2012-09-18 18:48:40 -05:00
doc::CratePage(doc) => {
if config.output_format == config::PandocHtml &&
config.output_style == config::DocPerMod {
~"index"
} else {
assert doc.topmod.name() != ~"";
doc.topmod.name()
}
}
2012-09-18 18:48:40 -05:00
doc::ItemPage(doc) => {
str::connect(doc.path() + ~[doc.name()], ~"_")
}
}
};
2012-08-06 14:34:08 -05:00
let ext = match config.output_format {
2012-09-18 18:48:40 -05:00
config::Markdown => ~"md",
config::PandocHtml => ~"html"
};
Path(filename).with_filetype(ext)
}
#[test]
fn should_use_markdown_file_name_based_off_crate() {
let config = {
output_dir: Path("output/dir"),
2012-09-18 18:48:40 -05:00
output_format: config::Markdown,
output_style: config::DocPerCrate,
2012-09-04 15:29:32 -05:00
.. config::default_config(&Path("input/test.rc"))
};
let doc = test::mk_doc(~"test", ~"");
2012-09-18 18:48:40 -05:00
let page = doc::CratePage(doc.CrateDoc());
let filename = make_local_filename(config, page);
assert filename.to_str() == ~"output/dir/test.md";
}
#[test]
fn should_name_html_crate_file_name_index_html_when_doc_per_mod() {
let config = {
output_dir: Path("output/dir"),
2012-09-18 18:48:40 -05:00
output_format: config::PandocHtml,
output_style: config::DocPerMod,
2012-09-04 15:29:32 -05:00
.. config::default_config(&Path("input/test.rc"))
};
let doc = test::mk_doc(~"", ~"");
2012-09-18 18:48:40 -05:00
let page = doc::CratePage(doc.CrateDoc());
let filename = make_local_filename(config, page);
assert filename.to_str() == ~"output/dir/index.html";
}
#[test]
fn should_name_mod_file_names_by_path() {
let config = {
output_dir: Path("output/dir"),
2012-09-18 18:48:40 -05:00
output_format: config::PandocHtml,
output_style: config::DocPerMod,
2012-09-04 15:29:32 -05:00
.. config::default_config(&Path("input/test.rc"))
};
let doc = test::mk_doc(~"", ~"mod a { mod b { } }");
let modb = doc.cratemod().mods()[0].mods()[0];
2012-09-18 18:48:40 -05:00
let page = doc::ItemPage(doc::ModTag(modb));
let filename = make_local_filename(config, page);
assert filename == Path("output/dir/a_b.html");
}
#[cfg(test)]
mod test {
#[legacy_exports];
2012-09-18 18:48:40 -05:00
fn mk_doc(name: ~str, source: ~str) -> doc::Doc {
2012-06-30 18:19:07 -05:00
do astsrv::from_str(source) |srv| {
let doc = extract::from_srv(srv, name);
let doc = path_pass::mk_pass().f(srv, doc);
doc
}
}
}
fn write_file(path: &Path, s: ~str) {
use io::WriterUtil;
2012-08-14 15:38:35 -05:00
match io::file_writer(path, ~[io::Create, io::Truncate]) {
2012-08-26 18:54:31 -05:00
result::Ok(writer) => {
writer.write_str(s);
}
2012-08-26 18:54:31 -05:00
result::Err(e) => fail e
}
}
fn future_writer_factory(
2012-09-18 18:48:40 -05:00
) -> (WriterFactory, comm::Port<(doc::Page, ~str)>) {
2012-08-27 16:22:25 -05:00
let markdown_po = comm::Port();
let markdown_ch = comm::Chan(markdown_po);
2012-09-18 18:48:40 -05:00
let writer_factory = fn~(page: doc::Page) -> Writer {
2012-08-27 16:22:25 -05:00
let writer_po = comm::Port();
let writer_ch = comm::Chan(writer_po);
do task::spawn {
let (writer, future) = future_writer();
comm::send(writer_ch, writer);
2012-08-13 21:06:57 -05:00
let s = future::get(&future);
comm::send(markdown_ch, (page, s));
}
comm::recv(writer_po)
};
(writer_factory, markdown_po)
}
2012-09-18 18:48:40 -05:00
fn future_writer() -> (Writer, future::Future<~str>) {
let (chan, port) = pipes::stream();
2012-09-18 18:48:40 -05:00
let writer = fn~(+instr: WriteInstr) {
chan.send(copy instr);
};
let future = do future::from_fn {
let mut res = ~"";
loop {
match port.recv() {
2012-09-18 18:48:40 -05:00
Write(s) => res += s,
Done => break
}
}
res
};
(writer, future)
}