mirror of
https://github.com/lise-henry/crowbook
synced 2024-05-28 01:36:17 +02:00
added a way to set epub/html template and css
This commit is contained in:
parent
90504e2acc
commit
559cc8fac7
|
@ -7,8 +7,6 @@ lang: en
|
|||
# Generic options (though not used with LaTeX)
|
||||
numbering: true
|
||||
autoclean: true
|
||||
numbering_template: Chapter {{number}}: {{title}}
|
||||
verbose: true
|
||||
|
||||
# Latex option
|
||||
tex_command: pdflatex
|
||||
|
|
|
@ -5,11 +5,13 @@ use token::Token;
|
|||
use epub::EpubRenderer;
|
||||
use html::HtmlRenderer;
|
||||
use latex::LatexRenderer;
|
||||
use templates::{epub,html};
|
||||
|
||||
use std::fs::File;
|
||||
use std::io::{Write,Read};
|
||||
use std::env;
|
||||
use std::path::Path;
|
||||
use std::borrow::Cow;
|
||||
|
||||
use mustache;
|
||||
use mustache::MapBuilder;
|
||||
|
@ -310,4 +312,24 @@ impl Book {
|
|||
self.chapters.push((number, v));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Returns the template (default or modified version)
|
||||
pub fn get_template(&self, template: &str) -> Result<Cow<'static, str>> {
|
||||
let (option, fallback) = match template {
|
||||
"epub_css" => (&self.epub_css, epub::CSS),
|
||||
"epub_template" => (&self.epub_template, epub::TEMPLATE),
|
||||
"html_css" => (&self.html_css, html::CSS),
|
||||
"html_template" => (&self.html_template, html::TEMPLATE),
|
||||
_ => return Err(Error::ConfigParser("invalid template", template.to_owned())),
|
||||
};
|
||||
if let &Some (ref 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))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -61,7 +61,8 @@ impl<'a> EpubRenderer<'a> {
|
|||
}
|
||||
|
||||
// Write CSS file
|
||||
try!(zipper.write("stylesheet.css", CSS.as_bytes()));
|
||||
try!(zipper.write("stylesheet.css",
|
||||
&try!(self.book.get_template("epub_css")).as_bytes()));
|
||||
|
||||
// Write titlepage
|
||||
try!(zipper.write("title_page.xhtml", &try!(self.render_titlepage()).as_bytes()));
|
||||
|
@ -277,7 +278,7 @@ impl<'a> EpubRenderer<'a> {
|
|||
}
|
||||
self.toc.push(title.clone());
|
||||
|
||||
let template = mustache::compile_str(TEMPLATE);
|
||||
let template = mustache::compile_str(try!(self.book.get_template("epub_template")).as_ref());
|
||||
let data = self.book.get_mapbuilder()
|
||||
.insert_str("content", content)
|
||||
.insert_str("chapter_title", title)
|
||||
|
|
|
@ -43,11 +43,11 @@ impl<'a> HtmlRenderer<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
let template = mustache::compile_str(TEMPLATE);
|
||||
|
||||
let template = mustache::compile_str(try!(self.book.get_template("html_template")).as_ref());
|
||||
let data = self.book.get_mapbuilder()
|
||||
.insert_str("content", content)
|
||||
.insert_str("style", CSS)
|
||||
.insert_str("style",
|
||||
&try!(self.book.get_template("html_css")))
|
||||
.build();
|
||||
|
||||
let mut res:Vec<u8> = vec!();
|
||||
|
@ -120,3 +120,5 @@ impl<'a> HtmlRenderer<'a> {
|
|||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue