rust/src/librustdoc/markdown_writer.rs

320 lines
8.8 KiB
Rust
Raw Normal View History

// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use config;
2012-09-18 18:48:40 -05:00
use doc::ItemUtils;
use doc;
use core::libc;
use core::run;
2013-02-02 05:10:12 -06:00
use core::comm::*;
2012-10-23 16:20:57 -05:00
use std::future;
2012-11-19 20:00:12 -06:00
pub enum WriteInstr {
2012-09-18 18:48:40 -05:00
Write(~str),
Done
}
pub type Writer = ~fn(v: WriteInstr);
pub type WriterFactory = ~fn(page: doc::Page) -> Writer;
2012-11-19 20:00:12 -06:00
pub trait WriterUtils {
fn put_str(&self, str: ~str);
fn put_line(&self, str: ~str);
fn put_done(&self);
}
impl WriterUtils for Writer {
fn put_str(&self, str: ~str) {
2013-01-30 21:56:17 -06:00
(*self)(Write(str));
}
fn put_line(&self, str: ~str) {
self.put_str(str + ~"\n");
}
fn put_done(&self) {
2013-01-30 21:56:17 -06:00
(*self)(Done)
}
}
2013-01-30 21:32:36 -06:00
pub 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
}
}
}
2013-01-30 21:32:36 -06:00
fn markdown_writer_factory(config: config::Config) -> WriterFactory {
let result: ~fn(page: doc::Page) -> Writer = |page| {
2013-01-30 15:14:35 -06:00
markdown_writer(copy config, page)
};
result
}
2013-01-30 21:32:36 -06:00
fn pandoc_writer_factory(config: config::Config) -> WriterFactory {
let result: ~fn(doc::Page) -> Writer = |page| {
2013-01-30 15:14:35 -06:00
pandoc_writer(copy config, page)
};
result
}
fn markdown_writer(
2013-01-30 21:32:36 -06:00
config: config::Config,
page: doc::Page
2012-09-18 18:48:40 -05:00
) -> 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(
2013-01-30 21:32:36 -06:00
config: config::Config,
page: doc::Page
2012-09-18 18:48:40 -05:00
) -> Writer {
2013-03-28 20:39:09 -05:00
assert!(config.pandoc_cmd.is_some());
2013-01-30 15:14:35 -06:00
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 core::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);
2013-02-02 05:10:12 -06:00
let (stdout_po, stdout_ch) = comm::stream();
2013-02-15 02:37:08 -06:00
do task::spawn_sched(task::SingleThreaded) || {
2012-11-28 22:34:40 -06:00
stdout_ch.send(readclose(pipe_out.in));
}
2013-02-02 05:10:12 -06:00
let (stderr_po, stderr_ch) = comm::stream();
2013-02-15 02:37:08 -06:00
do task::spawn_sched(task::SingleThreaded) || {
2012-11-28 22:34:40 -06:00
stderr_ch.send(readclose(pipe_err.in));
}
2012-11-28 22:34:40 -06:00
let stdout = stdout_po.recv();
let stderr = stderr_po.recv();
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
unsafe {
let file = os::fdopen(fd);
let reader = io::FILE_reader(file, false);
let buf = io::with_bytes_writer(|writer| {
let mut bytes = [0, ..4096];
while !reader.eof() {
let nread = reader.read(bytes, bytes.len());
writer.write(bytes.slice(0, nread).to_owned());
}
});
os::fclose(file);
str::from_bytes(buf)
}
}
fn generic_writer(process: ~fn(markdown: ~str)) -> Writer {
2013-02-01 20:03:32 -06:00
let (po, ch) = stream::<WriteInstr>();
2013-02-15 02:37:08 -06:00
do task::spawn || {
let mut markdown = ~"";
let mut keep_going = true;
while keep_going {
2012-11-28 22:34:40 -06:00
match po.recv() {
2012-09-18 18:48:40 -05:00
Write(s) => markdown += s,
Done => keep_going = false
}
}
2013-02-15 02:37:08 -06:00
process(markdown);
};
let result: ~fn(instr: WriteInstr) = |instr| ch.send(instr);
result
}
pub fn make_local_filename(
2013-01-30 21:32:36 -06:00
config: config::Config,
page: doc::Page
) -> Path {
2013-01-30 15:14:35 -06:00
let filename = make_filename(copy config, page);
config.output_dir.push_rel(&filename)
}
2012-11-19 20:00:12 -06:00
pub fn make_filename(
2013-01-30 21:32:36 -06: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 {
2013-03-28 20:39:09 -05:00
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)
}
2013-01-30 21:32:36 -06:00
fn write_file(path: &Path, s: ~str) {
use core::io::WriterUtil;
match io::file_writer(path, [io::Create, io::Truncate]) {
2012-08-26 18:54:31 -05:00
result::Ok(writer) => {
writer.write_str(s);
}
result::Err(e) => fail!(e)
}
}
2012-11-19 20:00:12 -06:00
pub fn future_writer_factory(
2013-02-01 20:19:57 -06:00
) -> (WriterFactory, Port<(doc::Page, ~str)>) {
let (markdown_po, markdown_ch) = stream();
let markdown_ch = SharedChan::new(markdown_ch);
let writer_factory: WriterFactory = |page| {
2013-02-02 05:10:12 -06:00
let (writer_po, writer_ch) = comm::stream();
2013-02-01 20:19:57 -06:00
let markdown_ch = markdown_ch.clone();
2013-02-15 02:37:08 -06:00
do task::spawn || {
let (writer, future) = future_writer();
let mut future = future;
2013-02-15 02:37:08 -06:00
writer_ch.send(writer);
let s = future.get();
2013-02-01 20:19:57 -06:00
markdown_ch.send((copy page, s));
}
2012-11-28 22:34:40 -06:00
writer_po.recv()
};
2013-02-15 02:37:08 -06:00
(writer_factory, markdown_po)
}
2012-09-18 18:48:40 -05:00
fn future_writer() -> (Writer, future::Future<~str>) {
2013-02-02 05:10:12 -06:00
let (port, chan) = comm::stream();
let writer: ~fn(instr: WriteInstr) = |instr| chan.send(copy instr);
2013-02-15 02:37:08 -06:00
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
};
2013-02-15 02:37:08 -06:00
(writer, future)
}
#[cfg(test)]
mod test {
use astsrv;
use doc;
use extract;
use path_pass;
use config;
use super::make_local_filename;
fn mk_doc(name: ~str, source: ~str) -> doc::Doc {
do astsrv::from_str(source) |srv| {
let doc = extract::from_srv(srv.clone(), copy name);
let doc = (path_pass::mk_pass().f)(srv.clone(), doc);
doc
}
}
#[test]
fn should_use_markdown_file_name_based_off_crate() {
let config = config::Config {
output_dir: Path("output/dir"),
output_format: config::Markdown,
output_style: config::DocPerCrate,
.. config::default_config(&Path("input/test.rc"))
};
let doc = mk_doc(~"test", ~"");
let page = doc::CratePage(doc.CrateDoc());
let filename = make_local_filename(config, page);
assert_eq!(filename.to_str(), ~"output/dir/test.md");
}
#[test]
fn should_name_html_crate_file_name_index_html_when_doc_per_mod() {
let config = config::Config {
output_dir: Path("output/dir"),
output_format: config::PandocHtml,
output_style: config::DocPerMod,
.. config::default_config(&Path("input/test.rc"))
};
let doc = mk_doc(~"", ~"");
let page = doc::CratePage(doc.CrateDoc());
let filename = make_local_filename(config, page);
assert_eq!(filename.to_str(), ~"output/dir/index.html");
}
#[test]
fn should_name_mod_file_names_by_path() {
let config = config::Config {
output_dir: Path("output/dir"),
output_format: config::PandocHtml,
output_style: config::DocPerMod,
.. config::default_config(&Path("input/test.rc"))
};
let doc = mk_doc(~"", ~"mod a { mod b { } }");
let modb = copy doc.cratemod().mods()[0].mods()[0];
let page = doc::ItemPage(doc::ModTag(modb));
let filename = make_local_filename(config, page);
assert_eq!(filename, Path("output/dir/a_b.html"));
}
}