Skip to content

Commit 3cec2d6

Browse files
committed
rustdoc: Teach rustdoc to run pandoc
1 parent 42799a5 commit 3cec2d6

File tree

1 file changed

+74
-6
lines changed

1 file changed

+74
-6
lines changed

src/rustdoc/markdown_writer.rs

Lines changed: 74 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,79 @@ impl writer_util for writer {
2626
}
2727

2828
fn make_writer(config: config::config) -> writer {
29-
markdown_writer(config)
29+
alt config.output_format {
30+
config::markdown {
31+
markdown_writer(config)
32+
}
33+
config::pandoc_html {
34+
pandoc_writer(config)
35+
}
36+
}
3037
}
3138

3239
fn markdown_writer(config: config::config) -> writer {
33-
let filename = make_filename(config);
40+
let filename = make_filename(config, "md");
41+
generic_writer {|markdown|
42+
write_file(filename, markdown);
43+
}
44+
}
45+
46+
fn pandoc_writer(config: config::config) -> writer {
47+
assert option::is_some(config.pandoc_cmd);
48+
let pandoc_cmd = option::get(config.pandoc_cmd);
49+
let filename = make_filename(config, "html");
50+
51+
let pandoc_args = [
52+
"--standalone",
53+
"--toc",
54+
"--section-divs",
55+
"--from=markdown",
56+
"--to=html",
57+
"--css=rust.css",
58+
"--output=" + filename
59+
];
60+
61+
generic_writer {|markdown|
62+
import std::run;
63+
import std::os;
64+
import std::io;
65+
import std::io::writer_util;
66+
67+
#debug("pandoc cmd: %s", pandoc_cmd);
68+
#debug("pandoc args: %s", str::connect(pandoc_args, " "));
69+
70+
let pipe_in = os::pipe();
71+
let pipe_out = os::pipe();
72+
let pipe_err = os::pipe();
73+
let pid = run::spawn_process(
74+
pandoc_cmd, pandoc_args, none, none,
75+
pipe_in.in, pipe_out.out, pipe_err.out);
76+
77+
if pid != -1 as ctypes::pid_t {
78+
let writer = io::fd_writer(pipe_in.out, false);
79+
writer.write_str(markdown);
80+
}
81+
82+
os::close(pipe_in.in);
83+
os::close(pipe_out.out);
84+
os::close(pipe_err.out);
85+
os::close(pipe_in.out);
86+
os::close(pipe_out.in);
87+
os::close(pipe_err.in);
88+
89+
if pid == -1 as ctypes::pid_t {
90+
fail "failed to run pandoc";
91+
}
92+
93+
let status = run::waitpid(pid);
94+
#debug("pandoc result: %i", status);
95+
if status != 0 {
96+
fail "pandoc failed";
97+
}
98+
}
99+
}
100+
101+
fn generic_writer(process: fn~(markdown: str)) -> writer {
34102
let ch = task::spawn_listener {|po: comm::port<writeinstr>|
35103
let markdown = "";
36104
let keep_going = true;
@@ -40,19 +108,19 @@ fn markdown_writer(config: config::config) -> writer {
40108
done { keep_going = false; }
41109
}
42110
}
43-
write_file(filename, markdown);
111+
process(markdown);
44112
};
45113

46114
fn~(+instr: writeinstr) {
47115
comm::send(ch, instr);
48116
}
49117
}
50118

51-
fn make_filename(config: config::config) -> str {
119+
fn make_filename(config: config::config, ext: str) -> str {
52120
import std::fs;
53121
let cratefile = fs::basename(config.input_crate);
54122
let cratename = tuple::first(fs::splitext(cratefile));
55-
fs::connect(config.output_dir, cratename + ".md")
123+
fs::connect(config.output_dir, cratename + "." + ext)
56124
}
57125

58126
fn write_file(path: str, s: str) {
@@ -73,7 +141,7 @@ fn should_use_markdown_file_name_based_off_crate() {
73141
output_dir: "output/dir"
74142
with config::default_config("input/test.rc")
75143
};
76-
assert make_filename(config) == "output/dir/test.md";
144+
assert make_filename(config, "md") == "output/dir/test.md";
77145
}
78146

79147
fn future_writer() -> (writer, future::future<str>) {

0 commit comments

Comments
 (0)