write print.html
This commit is contained in:
parent
5ca380f5a2
commit
da9c57e6f5
|
@ -199,6 +199,9 @@ impl Chapter {
|
|||
self
|
||||
}
|
||||
|
||||
/// FIXME chapter content should be read when parsing SUMMARY.md so that you
|
||||
/// don't have to pass around the Book struct for getting the src dir
|
||||
|
||||
/// Reads in the chapter's content from the markdown file. Chapter doesn't
|
||||
/// know the book's src folder, hence the `book_src_dir` argument.
|
||||
pub fn read_content_using(&self, book_src_dir: &PathBuf) -> Result<String, Box<Error>> {
|
||||
|
|
|
@ -61,8 +61,6 @@ impl Renderer for HtmlHandlebars {
|
|||
));
|
||||
}
|
||||
|
||||
// TODO write print version html
|
||||
|
||||
// Copy book's static assets
|
||||
|
||||
if book_project.get_project_root().join("assets").exists() {
|
||||
|
@ -125,10 +123,6 @@ impl Renderer for HtmlHandlebars {
|
|||
&book_project.get_dest_base()));
|
||||
}
|
||||
|
||||
// Concatenate the content (as rendered from Markdown) of each chapter
|
||||
// for writing print.html in the end
|
||||
let mut print_content: String = String::new();
|
||||
|
||||
debug!("[fn]: render");
|
||||
let mut handlebars = Handlebars::new();
|
||||
|
||||
|
@ -234,6 +228,41 @@ impl Renderer for HtmlHandlebars {
|
|||
|
||||
// Render a file for every entry in the book
|
||||
try!(self.process_items(&book.toc, &book, &book_project.livereload_script, &handlebars));
|
||||
|
||||
// Write print.html
|
||||
// FIXME chapter should have its content so that we don't have to duplicate process_chapter() here
|
||||
if let Some(mut content) = self.collect_print_content_markdown(&book.toc, &book) {
|
||||
|
||||
let mut chapter: Chapter = Chapter::new(book.config.title.to_owned(), PathBuf::from(""));
|
||||
|
||||
chapter.dest_path = Some(PathBuf::from("print.html"));
|
||||
|
||||
// FIXME this is process_chapter() except we replace content in data
|
||||
|
||||
let mut data = try!(make_data(&book, &chapter, &None));
|
||||
|
||||
content = utils::render_markdown(&content);
|
||||
|
||||
if let Some(p) = book.config.get_src().join(&chapter.path).parent() {
|
||||
content = helpers::playpen::render_playpen(&content, p);
|
||||
}
|
||||
|
||||
data.remove("content");
|
||||
data.insert("content".to_owned(), content.to_json());
|
||||
|
||||
let rendered_content = try!(handlebars.render("page", &data));
|
||||
|
||||
let p = match chapter.dest_path.clone() {
|
||||
Some(x) => x,
|
||||
None => chapter.path.with_extension("html")
|
||||
};
|
||||
|
||||
let rendered_path = &book.config.get_dest().join(&p);
|
||||
|
||||
let mut file = try!(utils::fs::create_file(rendered_path));
|
||||
|
||||
try!(file.write_all(&rendered_content.into_bytes()));
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
@ -269,13 +298,46 @@ impl HtmlHandlebars {
|
|||
}
|
||||
|
||||
},
|
||||
_ => {},
|
||||
TocItem::Spacer => {},
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn collect_print_content_markdown(&self, items: &Vec<TocItem>, book: &Book) -> Option<String> {
|
||||
let mut text = "".to_string();
|
||||
|
||||
for item in items.iter() {
|
||||
match *item {
|
||||
TocItem::Numbered(ref i) |
|
||||
TocItem::Unnumbered(ref i) |
|
||||
TocItem::Unlisted(ref i) => {
|
||||
if i.chapter.path.as_os_str().len() > 0 {
|
||||
if let Ok(x) = i.chapter.read_content_using(&book.config.src) {
|
||||
text.push_str(&x);
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(ref subs) = i.sub_items {
|
||||
if let Some(x) = self.collect_print_content_markdown(subs, book) {
|
||||
text.push_str(&x);
|
||||
}
|
||||
}
|
||||
|
||||
},
|
||||
TocItem::Spacer => {},
|
||||
}
|
||||
}
|
||||
|
||||
if text.len() > 0 {
|
||||
Some(text)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fn process_chapter(&self,
|
||||
chapter: &Chapter,
|
||||
book: &Book,
|
||||
|
@ -285,7 +347,7 @@ impl HtmlHandlebars {
|
|||
|
||||
let data = try!(make_data(book, chapter, livereload_script));
|
||||
|
||||
// Rendere the handlebars template with the data
|
||||
// Render the handlebars template with the data
|
||||
debug!("[*]: Render template");
|
||||
let rendered_content = try!(handlebars.render("page", &data));
|
||||
|
||||
|
@ -352,7 +414,8 @@ fn make_data(book: &Book,
|
|||
},
|
||||
}
|
||||
|
||||
let mut content = try!(chapter.read_content_using(&book.config.src));
|
||||
match chapter.read_content_using(&book.config.src) {
|
||||
Ok(mut content) => {
|
||||
content = utils::render_markdown(&content);
|
||||
|
||||
// Parse for playpen links
|
||||
|
@ -361,6 +424,11 @@ fn make_data(book: &Book,
|
|||
}
|
||||
|
||||
data.insert("content".to_owned(), content.to_json());
|
||||
},
|
||||
Err(e) => {
|
||||
debug!("Couldn't read chapter content: {:#?}", e);
|
||||
},
|
||||
}
|
||||
|
||||
data.insert("path_to_root".to_owned(), utils::fs::path_to_root(&path).to_json());
|
||||
|
||||
|
|
Loading…
Reference in New Issue