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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
use error::{Error,Result};
use cleaner::{Cleaner, French};
use bookoption::BookOption;
use parser::Parser;
use token::Token;
use epub::EpubRenderer;
use html::HtmlRenderer;
use latex::LatexRenderer;
use odt::OdtRenderer;
use templates::{epub, html, epub3, latex};
use escape;
use std::fs::File;
use std::io::{self, Write,Read};
use std::env;
use std::path::Path;
use std::borrow::Cow;
use std::collections::HashMap;
use mustache;
use mustache::MapBuilder;
#[derive(Debug, PartialEq, Clone, Copy)]
pub enum Number {
Hidden,
Unnumbered,
Default,
Specified(i32),
}
static OPTIONS:&'static str = "
# Metadata
author:str:Anonymous # The author of the book
title:str:Untitled # The title of the book
lang:str:en # The language of the book
subject:str # Subject of the book (used for EPUB metadata)
description:str # Description of the book (used for EPUB metadata)
cover:str # File name of the cover of the book
# Output options
output.epub:str # Output file name for EPUB rendering
output.html:str # Output file name for HTML rendering
output.tex:str # Output file name for LaTeX rendering
output.pdf:str # Output file name for PDF rendering
output.odt:str # Output file name for ODT rendering
# Misc options
numbering:int:1 # The maximum heading levels to number (0: no numbering, 1: only chapters, ..., 6: all)
display_toc:bool:false # If true, display a table of content in the document
toc_name:str:Table of contents # Name of the table of contents if toc is displayed in line
autoclean:bool:true # Toggles cleaning of input markdown (not used for LaTeX)
verbose:bool:false # Toggle verbose mode
side_notes:bool:false # Display footnotes as side notes in HTML/Epub
nb_char:char:' ' # The non-breaking character to use for autoclean when lang is set to fr
temp_dir:str:. # Path where to create a temporary directory
numbering_template:str:{{number}}. {{title}} # Format of numbered titles
# HTML options
html.template:str # Path of an HTML template
html.css:str # Path of a stylesheet to use with HTML rendering
# EPUB options
epub.version:int:2 # The EPUB version to generate
epub.css:str # Path of a stylesheet to use with EPUB rendering
epub.template:str # Path of an epub template for chapter
# LaTeX options
tex.links_as_footnotes:bool:true # If set to true, will add foontotes to URL of links in LaTeX/PDF output
tex.command:str:pdflatex # LaTeX flavour to use for generating PDF
tex.template:str # Path of a LaTeX template file
";
#[derive(Debug)]
pub struct Book {
pub chapters: Vec<(Number, Vec<Token>)>,
options: HashMap<String, BookOption>,
valid_bools: Vec<&'static str>,
valid_chars: Vec<&'static str>,
valid_strings: Vec<&'static str>,
valid_ints: Vec<&'static str>,
}
impl Book {
pub fn description(md: bool) -> String {
let mut out = String::new();
let mut previous_is_comment = true;
for (comment, key, o_type, default) in Book::options_to_vec() {
if key.is_none() {
if !previous_is_comment {
out.push_str("\n");
previous_is_comment = true;
}
out.push_str(&format!("### {} ###\n", comment));
continue;
}
previous_is_comment = false;
let o_type = match o_type.unwrap() {
"bool" => "boolean",
"int" => "integer",
"char" => "char",
"str" => "string",
_ => unreachable!()
};
let def = if let Some(value) = default {
value
} else {
"not set"
};
if md {
out.push_str(&format!("- **`{}`**
- **type**: {}
- **default value**: `{}`
- {}\n", key.unwrap(), o_type, def, comment));
} else {
out.push_str(&format!("- {} (type: {}) (default: {}) {}\n", key.unwrap(), o_type, def,comment));
}
}
out
}
fn options_to_vec() -> Vec<(&'static str, Option<&'static str>,
Option<&'static str>, Option<&'static str>)> {
let mut out = vec!();
for line in OPTIONS.lines() {
let line = line.trim();
if line.is_empty() {
continue;
}
if line.starts_with('#') {
out.push((&line[1..], None, None, None));
continue;
}
let v:Vec<_> = line.split('#').collect();
let content = v[0];
let comment = v[1];
let v:Vec<_> = content.split(':').collect();
let key = Some(v[0].trim());
let option_type = Some(v[1].trim());
let default_value = if v.len() > 2 {
Some(v[2].trim())
} else {
None
};
out.push((comment, key, option_type, default_value));
}
out
}
pub fn new() -> Book {
let mut book = Book {
chapters: vec!(),
options: HashMap::new(),
valid_bools:vec!(),
valid_chars:vec!(),
valid_ints:vec!(),
valid_strings:vec!(),
};
for (_, key, option_type, default_value) in Book::options_to_vec() {
if key.is_none() {
continue;
}
let key = key.unwrap();
match option_type.unwrap() {
"str" => book.valid_strings.push(key),
"bool" => book.valid_bools.push(key),
"int" => book.valid_ints.push(key),
"char" => book.valid_chars.push(key),
_ => panic!(format!("Ill-formatted OPTIONS string: unrecognized type '{}'", option_type.unwrap())),
}
if let Some(value) = default_value {
book.set_option(key, value).unwrap();
}
}
book
}
pub fn println(&self, s:&str) {
writeln!(&mut io::stderr(), "{}", s).unwrap();
}
pub fn debug(&self, s:&str) {
if self.get_bool("verbose").unwrap() {
writeln!(&mut io::stderr(), "{}", s).unwrap();
}
}
pub fn new_from_file(filename: &str, verbose: bool) -> Result<Book> {
let path = Path::new(filename);
let mut f = try!(File::open(&path).map_err(|_| Error::FileNotFound(String::from(filename))));
if let Some(parent) = path.parent() {
if !parent.to_string_lossy().is_empty() {
if !env::set_current_dir(&parent).is_ok() {
return Err(Error::ConfigParser("could not change current directory to the one of the config file",
format!("{}", parent.display())));
}
}
}
let mut s = String::new();
try!(f.read_to_string(&mut s).map_err(|_| Error::ConfigParser("file contains invalid UTF-8, could not parse it",
String::from(filename))));
let mut book = Book::new();
if verbose {
book.set_option("verbose", "true").unwrap();
}
try!(book.set_from_config(&s));
Ok(book)
}
pub fn get_mapbuilder(&self, format: &str) -> MapBuilder {
fn clone(x:&str) -> String {
x.to_owned()
}
let f:fn(&str)->String = match format {
"none" => clone,
"html" => escape::escape_html,
"tex" => escape::escape_tex,
_ => panic!("get mapbuilder called with invalid escape format")
};
MapBuilder::new()
.insert_str("author", f(self.get_str("author").unwrap()))
.insert_str("title", f(&self.get_str("title").unwrap()))
.insert_str("lang", self.get_str("lang").unwrap().to_owned())
}
pub fn clean(&self, mut text:String) -> String {
if let Some(cleaner) = self.get_cleaner() {
cleaner.clean(&mut text)
}
text
}
pub fn get_cleaner(&self) -> Option<Box<Cleaner>> {
if self.get_bool("autoclean").unwrap() {
let lang = self.get_str("lang").unwrap().to_lowercase();
if lang.starts_with("fr") {
Some(Box::new(French::new(self.get_char("nb_char").unwrap())))
} else {
Some(Box::new(()))
}
} else {
None
}
}
pub fn get_header(&self, n: i32, title: &str) -> Result<String> {
let template = mustache::compile_str(self.get_str("numbering_template").unwrap());
let data = MapBuilder::new()
.insert_str("title", String::from(title))
.insert_str("number", format!("{}", n))
.build();
let mut res:Vec<u8> = vec!();
template.render_data(&mut res, &data);
match String::from_utf8(res) {
Err(_) => Err(Error::Render("header generated by mustache was not valid utf-8")),
Ok(res) => Ok(res)
}
}
pub fn get_option(&self, key: &str) -> Result<&BookOption> {
self.options.get(key).ok_or(Error::InvalidOption(format!("option {} is not present", key)))
}
pub fn get_str(&self, key: &str) -> Result<&str> {
try!(self.get_option(key)).as_str()
}
pub fn get_bool(&self, key: &str) -> Result<bool> {
try!(self.get_option(key)).as_bool()
}
pub fn get_char(&self, key: &str) -> Result<char> {
try!(self.get_option(key)).as_char()
}
pub fn get_i32(&self, key: &str) -> Result<i32> {
try!(self.get_option(key)).as_i32()
}
pub fn set_option(&mut self, key: &str, value: &str) -> Result<()> {
if self.valid_strings.contains(&key) {
self.options.insert(key.to_owned(), BookOption::String(value.to_owned()));
Ok(())
} else if self.valid_chars.contains(&key) {
let words: Vec<_> = value.trim().split('\'').collect();
if words.len() != 3 {
return Err(Error::ConfigParser("could not parse char", String::from(value)));
}
let chars: Vec<_> = words[1].chars().collect();
if chars.len() != 1 {
return Err(Error::ConfigParser("could not parse char", String::from(value)));
}
self.options.insert(key.to_owned(), BookOption::Char(chars[0]));
Ok(())
} else if self.valid_bools.contains(&key) {
match value.parse::<bool>() {
Ok(b) => {
self.options.insert(key.to_owned(), BookOption::Bool(b));
()
},
Err(_) => return Err(Error::ConfigParser("could not parse bool", format!("{}:{}", key, value))),
}
Ok(())
} else if self.valid_ints.contains(&key) {
match value.parse::<i32>() {
Ok(i) => {
self.options.insert(key.to_owned(), BookOption::Int(i));
}
Err(_) => return Err(Error::ConfigParser("could not parse int", format!("{}:{}", key, value))),
}
Ok(())
} else {
Err(Error::ConfigParser("unrecognized key", String::from(key)))
}
}
pub fn set_from_config(&mut self, s: &str) -> Result<()> {
fn get_filename(s: &str) -> Result<&str> {
let words:Vec<&str> = (&s[1..]).split_whitespace().collect();
if words.len() > 1 {
return Err(Error::ConfigParser("chapter filenames must not contain whitespace", String::from(s)));
} else if words.len() < 1 {
return Err(Error::ConfigParser("no chapter name specified", String::from(s)));
}
Ok(words[0])
}
let mut multiline = false;
let mut join_new_line = false;
let mut prev_key = String::new();
let mut prev_value = String::new();
for line in s.lines() {
if multiline {
if line.starts_with(' ') {
prev_value.push_str(line.trim());
if join_new_line {
prev_value.push_str("\n");
} else {
prev_value.push_str(" ");
}
continue;
} else {
try!(self.set_option(&prev_key, &prev_value));
multiline = false;
}
}
let line = line.trim();
if line.is_empty() || line.starts_with('#') {
continue;
}
if line.starts_with('-') {
let file = try!(get_filename(line));
try!(self.add_chapter(Number::Unnumbered, file));
} else if line.starts_with('+') {
let file = try!(get_filename(line));
try!(self.add_chapter(Number::Default, file));
} else if line.starts_with('!') {
let file = try!(get_filename(line));
try!(self.add_chapter(Number::Hidden, file));
} else if line.starts_with(|c: char| c.is_digit(10)) {
let parts:Vec<_> = line.splitn(2, |c: char| c == '.' || c == ':' || c == '+').collect();
if parts.len() != 2 {
return Err(Error::ConfigParser("ill-formatted line specifying chapter number", String::from(line)));
} else {
let file = try!(get_filename(parts[1]));
let number = try!(parts[0].parse::<i32>().map_err(|_| Error::ConfigParser("Error parsing integer", String::from(line))));
try!(self.add_chapter(Number::Specified(number), file));
}
} else {
let parts:Vec<_> = line.splitn(2, ':').collect();
if parts.len() != 2 {
return Err(Error::ConfigParser("option setting must be of the form option: value", String::from(line)));
}
let key = parts[0].trim();
let value = parts[1].trim();
match value {
">" | "|" => {
multiline = true;
join_new_line = value == "|";
prev_key = key.to_owned();
prev_value = String::new();
},
_ => try!(self.set_option(key, value)),
}
}
}
if multiline {
try!(self.set_option(&prev_key, &prev_value));
}
Ok(())
}
pub fn render_pdf(&self) -> Result<()> {
self.debug("Attempting to generate pdf...");
let mut latex = LatexRenderer::new(&self);
let result = try!(latex.render_pdf());
self.debug(&result);
self.println(&format!("Successfully generated pdf file: {}", self.get_str("output.pdf").unwrap()));
Ok(())
}
pub fn render_epub(&self) -> Result<()> {
self.debug("Attempting to generate epub...");
let mut epub = EpubRenderer::new(&self);
let result = try!(epub.render_book());
self.debug(&result);
self.println(&format!("Successfully generated epub file: {}", self.get_str("output.epub").unwrap()));
Ok(())
}
pub fn render_odt(&self) -> Result<()> {
self.debug("Attempting to generate Odt...");
let mut odt = OdtRenderer::new(&self);
let result = try!(odt.render_book());
self.debug(&result);
self.println(&format!("Successfully generated odt file: {}", self.get_str("output.odt").unwrap()));
Ok(())
}
pub fn render_html<T: Write>(&self, f: &mut T) -> Result<()> {
self.debug("Attempting to generate HTML...");
let mut html = HtmlRenderer::new(&self);
let result = try!(html.render_book());
try!(f.write_all(&result.as_bytes()).map_err(|_| Error::Render("problem when writing to HTML file")));
self.println("Successfully generated HTML");
Ok(())
}
pub fn render_tex<T:Write>(&self, f: &mut T) -> Result<()> {
self.debug("Attempting to generate LaTeX...");
let mut latex = LatexRenderer::new(&self);
let result = try!(latex.render_book());
try!(f.write_all(&result.as_bytes()).map_err(|_| Error::Render("problem when writing to LaTeX file")));
self.println("Successfully generated LaTeX");
Ok(())
}
pub fn render_all(&self) -> Result<()> {
let mut did_some_stuff = false;
if self.get_option("output.epub").is_ok() {
did_some_stuff = true;
try!(self.render_epub());
}
if let Ok(file) = self.get_str("output.html") {
did_some_stuff = true;
let mut f = try!(File::create(file).map_err(|_| Error::Render("could not create HTML file")));
try!(self.render_html(&mut f));
}
if let Ok(file) = self.get_str("output.tex") {
did_some_stuff = true;
let mut f = try!(File::create(file).map_err(|_| Error::Render("could not create LaTeX file")));
try!(self.render_tex(&mut f));
}
if self.get_str("output.pdf").is_ok() {
did_some_stuff = true;
try!(self.render_pdf());
}
if self.get_str("output.odt").is_ok() {
did_some_stuff = true;
try!(self.render_odt());
}
if !did_some_stuff {
self.println("Warning: generated no file because no output file speficied. Add output_{{format}} to your config file.");
}
Ok(())
}
pub fn add_chapter(&mut self, number: Number, file: &str) -> Result<()> {
self.debug(&format!("Parsing chapter: {}...", file));
let mut parser = Parser::new();
let v = try!(parser.parse_file(file));
self.chapters.push((number, v));
Ok(())
}
pub fn get_template(&self, template: &str) -> Result<Cow<'static, str>> {
let (option, fallback) = match template {
"epub.css" => (self.get_str("epub.css"), epub::CSS),
"epub.template" => (self.get_str("epub.template"),
if try!(self.get_i32("epub.version")) == 3 {epub3::TEMPLATE} else {epub::TEMPLATE}),
"html.css" => (self.get_str("html.css"), html::CSS),
"html.template" => (self.get_str("html.template"), html::TEMPLATE),
"tex.template" => (self.get_str("tex.template"), latex::TEMPLATE),
_ => return Err(Error::ConfigParser("invalid template", template.to_owned())),
};
if let Ok (s) = option {
let mut f = try!(File::open(s).map_err(|_| Error::FileNotFound(s.to_owned())));
let mut res = String::new();
try!(f.read_to_string(&mut res)
.map_err(|_| Error::ConfigParser("file could not be read", s.to_owned())));
Ok(Cow::Owned(res))
} else {
Ok(Cow::Borrowed(fallback))
}
}
}