1
0
Fork 0
mirror of https://github.com/lise-henry/crowbook synced 2024-05-28 14:06:29 +02:00

read config from file

This commit is contained in:
Elisabeth Henry 2016-02-18 22:32:43 +01:00
parent eea9a7d0f6
commit 9173e2f4e8
2 changed files with 28 additions and 5 deletions

View File

@ -1,6 +1,7 @@
extern crate crowbook;
use crowbook::{HtmlRenderer, Parser, French, Book};
use crowbook::{HtmlRenderer, Book};
use std::env;
@ -18,8 +19,15 @@ cover: book-example/cover.png
+ book_example/chapitre_03.md
";
let mut book = Book::new();
book.set_from_config(config).unwrap();
let mut html = HtmlRenderer::new(&book);
println!("{}", html.render_book().unwrap());
let mut args = env::args();
args.next(); //discard program name
match args.next() {
None => println!("Needs the name of a book config file"),
Some(ref s) => {
let mut book = Book::new_from_file(s).unwrap();
let mut html = HtmlRenderer::new(&book);
println!("{}", html.render_book().unwrap());
}
}
}

View File

@ -1,6 +1,9 @@
use error::{Error,Result};
use cleaner::{Cleaner, French};
use std::fs::File;
use std::io::Read;
use mustache::MapBuilder;
// Numbering for a given chapter
@ -40,6 +43,18 @@ impl Book {
}
}
/// Creates a new book from a file
pub fn new_from_file(filename: &str) -> Result<Book> {
let mut f = try!(File::open(filename).map_err(|_| Error::FileNotFound(String::from(filename))));
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();
try!(book.set_from_config(&s));
Ok(book)
}
/// Returns a MapBuilder, to be used (and completed) for templating
pub fn get_mapbuilder(&self) -> MapBuilder {
MapBuilder::new()