write print.html

This commit is contained in:
Gambhiro 2017-01-13 07:57:08 +00:00
parent 5ca380f5a2
commit da9c57e6f5
2 changed files with 86 additions and 15 deletions

View File

@ -199,6 +199,9 @@ impl Chapter {
self 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 /// 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. /// 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>> { pub fn read_content_using(&self, book_src_dir: &PathBuf) -> Result<String, Box<Error>> {

View File

@ -61,8 +61,6 @@ impl Renderer for HtmlHandlebars {
)); ));
} }
// TODO write print version html
// Copy book's static assets // Copy book's static assets
if book_project.get_project_root().join("assets").exists() { if book_project.get_project_root().join("assets").exists() {
@ -125,10 +123,6 @@ impl Renderer for HtmlHandlebars {
&book_project.get_dest_base())); &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"); debug!("[fn]: render");
let mut handlebars = Handlebars::new(); let mut handlebars = Handlebars::new();
@ -234,6 +228,41 @@ impl Renderer for HtmlHandlebars {
// Render a file for every entry in the book // Render a file for every entry in the book
try!(self.process_items(&book.toc, &book, &book_project.livereload_script, &handlebars)); 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(()) Ok(())
@ -269,13 +298,46 @@ impl HtmlHandlebars {
} }
}, },
_ => {}, TocItem::Spacer => {},
} }
} }
Ok(()) 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, fn process_chapter(&self,
chapter: &Chapter, chapter: &Chapter,
book: &Book, book: &Book,
@ -285,7 +347,7 @@ impl HtmlHandlebars {
let data = try!(make_data(book, chapter, livereload_script)); 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"); debug!("[*]: Render template");
let rendered_content = try!(handlebars.render("page", &data)); let rendered_content = try!(handlebars.render("page", &data));
@ -352,16 +414,22 @@ fn make_data(book: &Book,
}, },
} }
let mut content = try!(chapter.read_content_using(&book.config.src)); match chapter.read_content_using(&book.config.src) {
content = utils::render_markdown(&content); Ok(mut content) => {
content = utils::render_markdown(&content);
// Parse for playpen links // Parse for playpen links
if let Some(p) = book.config.get_src().join(&chapter.path).parent() { if let Some(p) = book.config.get_src().join(&chapter.path).parent() {
content = helpers::playpen::render_playpen(&content, p); content = helpers::playpen::render_playpen(&content, p);
}
data.insert("content".to_owned(), content.to_json());
},
Err(e) => {
debug!("Couldn't read chapter content: {:#?}", e);
},
} }
data.insert("content".to_owned(), content.to_json());
data.insert("path_to_root".to_owned(), utils::fs::path_to_root(&path).to_json()); data.insert("path_to_root".to_owned(), utils::fs::path_to_root(&path).to_json());
if let Some(ref links) = chapter.translation_links { if let Some(ref links) = chapter.translation_links {