1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#![deny(missing_docs)]
#[macro_use]
extern crate itertools;
use itertools::Itertools;
#[cfg(windows)]
const PANDOC_PATH: &'static [&'static str] = &[
concat!(env!("LOCALAPPDATA"), r#"\Pandoc\"#),
];
#[cfg(not(windows))]
const PANDOC_PATH: &'static [&'static str] = &[
];
#[cfg(windows)]
const LATEX_PATH: &'static [&'static str] = &[
r#"C:\Program Files (x86)\MiKTeX 2.9\miktex\bin"#,
r#"C:\Program Files\MiKTeX 2.9\miktex\bin"#,
];
#[cfg(not(windows))]
const LATEX_PATH: &'static [&'static str] = &[
r"/usr/local/bin",
r"/usr/local/texlive/2015/bin/i386-linux",
];
use std::process::Command;
use std::env;
#[derive(Debug)]
pub enum DocumentClass {
Article,
Report,
Book,
}
pub use DocumentClass::*;
impl DocumentClass {
fn pandoc_name(&self) -> &'static str {
match *self {
Article => "article",
Report => "report",
Book => "book",
}
}
}
#[derive(Debug, Default)]
pub struct Pandoc {
inputs: Vec<String>,
output: Option<String>,
latex_path_hint: Vec<String>,
pandoc_path_hint: Vec<String>,
document_class: Option<DocumentClass>,
bibliography: Option<String>,
csl: Option<String>,
toc: bool,
number_sections: bool,
template: Option<String>,
variables: Vec<(String, String)>,
}
use std::convert::Into;
use std::borrow::Cow;
pub fn new() -> Pandoc { Default::default() }
impl Pandoc {
pub fn new() -> Pandoc { Default::default() }
pub fn add_latex_path_hint<'a, T: Into<Cow<'a, str>>>(&mut self, path: T) {
self.latex_path_hint.push(path.into().into_owned());
}
pub fn add_pandoc_path_hint<'a, T: Into<Cow<'a, str>>>(&mut self, path: T) {
self.pandoc_path_hint.push(path.into().into_owned());
}
pub fn set_doc_class(&mut self, class: DocumentClass) {
self.document_class = Some(class);
}
pub fn add_input<'a, T: Into<Cow<'a, str>>>(&mut self, filename: T) {
self.inputs.push(filename.into().into_owned());
}
pub fn set_output<'a, T: Into<Cow<'a, str>>>(&mut self, filename: T) {
self.output = Some(filename.into().into_owned());
}
pub fn set_bibliography<'a, T: Into<Cow<'a, str>>>(&mut self, filename: T) {
self.bibliography = Some(filename.into().into_owned());
}
pub fn set_csl<'a, T: Into<Cow<'a, str>>>(&mut self, filename: T) {
self.csl = Some(filename.into().into_owned());
}
pub fn set_toc(&mut self) { self.toc = true; }
pub fn set_number_sections(&mut self) { self.number_sections = true; }
pub fn set_latex_template<'a, T: Into<Cow<'a, str>>>(&mut self, filename: T) {
self.template = Some(filename.into().into_owned());
}
pub fn set_variable<'a, T: Into<Cow<'a, str>>, U: Into<Cow<'a, str>>>(&mut self, key: T, value: U) {
self.variables.push((key.into().into_owned(), value.into().into_owned()));
}
pub fn execute(self) -> Result<(), PandocError> {
let mut cmd = Command::new("pandoc");
for input in self.inputs {
cmd.arg(input);
}
cmd.arg("-o").arg(self.output.expect("no output file specified"));
if let Some(filename) = self.bibliography {
cmd.arg(format!("--bibliography={}", filename));
}
if let Some(filename) = self.csl {
cmd.arg(format!("--csl={}", filename));
}
if self.toc {
cmd.arg("--toc");
}
if self.number_sections {
cmd.arg("--number-sections");
}
if let Some(filename) = self.template {
cmd.arg(format!("--template={}", filename));
}
for (key, val) in self.variables {
cmd.arg("--variable").arg(format!("{}={}", key, val));
}
if let Some(doc_class) = self.document_class {
cmd.arg("--variable").arg(format!("documentclass={}", doc_class.pandoc_name()));
}
let path: String = self.latex_path_hint.iter()
.chain(self.pandoc_path_hint.iter())
.map(std::borrow::Borrow::borrow)
.chain(PANDOC_PATH.into_iter().cloned())
.chain(LATEX_PATH.into_iter().cloned())
.chain([env::var("PATH").unwrap()].iter().map(std::borrow::Borrow::borrow))
.intersperse(";")
.collect();
cmd.env("PATH", path);
match cmd.output() {
Err(e) => Err(PandocError::IoErr(e)),
Ok(ref o) if o.status.success() => Ok(()),
Ok(o) => Err(PandocError::Err(o)),
}
}
}
pub enum PandocError {
IoErr(std::io::Error),
Err(std::process::Output),
}
impl std::fmt::Debug for PandocError {
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
match *self {
PandocError::IoErr(ref e) => write!(fmt, "{:?}", e),
PandocError::Err(ref e) => {
try!(write!(fmt, "exit_code: {:?}", e.status.code()));
try!(write!(fmt, "stdout: {}", String::from_utf8_lossy(&e.stdout)));
write!(fmt, "stderr: {}", String::from_utf8_lossy(&e.stderr))
}
}
}
}