@@ -26,11 +26,79 @@ impl writer_util for writer {
26
26
}
27
27
28
28
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
+ }
30
37
}
31
38
32
39
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 {
34
102
let ch = task:: spawn_listener { |po : comm:: port<writeinstr>|
35
103
let markdown = "" ;
36
104
let keep_going = true ;
@@ -40,19 +108,19 @@ fn markdown_writer(config: config::config) -> writer {
40
108
done { keep_going = false ; }
41
109
}
42
110
}
43
- write_file ( filename , markdown) ;
111
+ process ( markdown) ;
44
112
} ;
45
113
46
114
fn ~( +instr: writeinstr) {
47
115
comm:: send ( ch , instr ) ;
48
116
}
49
117
}
50
118
51
- fn make_filename ( config : config:: config ) -> str {
119
+ fn make_filename ( config : config:: config , ext : str ) -> str {
52
120
import std:: fs;
53
121
let cratefile = fs:: basename ( config. input_crate ) ;
54
122
let cratename = tuple:: first ( fs:: splitext ( cratefile) ) ;
55
- fs:: connect ( config. output_dir , cratename + ".md" )
123
+ fs:: connect ( config. output_dir , cratename + "." + ext )
56
124
}
57
125
58
126
fn write_file ( path : str , s : str ) {
@@ -73,7 +141,7 @@ fn should_use_markdown_file_name_based_off_crate() {
73
141
output_dir: "output/dir"
74
142
with config:: default_config ( "input/test.rc" )
75
143
} ;
76
- assert make_filename ( config) == "output/dir/test.md" ;
144
+ assert make_filename ( config, "md" ) == "output/dir/test.md" ;
77
145
}
78
146
79
147
fn future_writer ( ) -> ( writer , future:: future < str > ) {
0 commit comments