diff --git a/src/generator.rs b/src/generator.rs index e72ab2044..aaa379941 100644 --- a/src/generator.rs +++ b/src/generator.rs @@ -1,10 +1,11 @@ +use std::iter; use std::io::{Read, Write}; use std::fmt::{self, Debug, Formatter}; use std::fs::File; use mdbook::renderer::RenderContext; use mdbook::book::{BookItem, Chapter}; -use epub_builder::{EpubBuilder, EpubContent, TocElement, ZipLibrary}; +use epub_builder::{EpubBuilder, EpubContent, ZipLibrary}; use failure::{Error, ResultExt}; use pulldown_cmark::{html, Parser}; use handlebars::{Handlebars, RenderError}; @@ -61,7 +62,7 @@ impl<'a> Generator<'a> { self.builder .metadata("generator", env!("CARGO_PKG_NAME")) .sync()?; - self.builder .metadata("lang", "en") .sync()?; + self.builder.metadata("lang", "en").sync()?; Ok(()) } @@ -103,16 +104,6 @@ impl<'a> Generator<'a> { let level = ch.number.as_ref().map(|n| n.len() as i32 - 1).unwrap_or(0); content = content.level(level); - // unfortunately we need to do two passes through `ch.sub_items` here. - // The first pass will add each sub-item to the current chapter's toc - // and the second pass actually adds the sub-items to the book. - for sub_item in &ch.sub_items { - if let BookItem::Chapter(ref sub_ch) = *sub_item { - let child_path = sub_ch.path.with_extension("html").display().to_string(); - content = content.child(TocElement::new(child_path, format!("{}", sub_ch))); - } - } - self.builder.add_content(content).sync()?; // second pass to actually add the sub-chapters @@ -130,7 +121,16 @@ impl<'a> Generator<'a> { let mut body = String::new(); html::push_html(&mut body, Parser::new(&ch.content)); - let ctx = json!({ "body": body, "title": ch.name }); + let stylesheet_path = ch.path + .parent() + .expect("All chapters have a parent") + .components() + .map(|_| "..") + .chain(iter::once("stylesheet.css")) + .collect::>() + .join("/"); + + let ctx = json!({ "title": ch.name, "body": body, "stylesheet": stylesheet_path }); self.hbs.render("index", &ctx) } diff --git a/src/index.hbs b/src/index.hbs index b965f7374..1b28bfe6a 100644 --- a/src/index.hbs +++ b/src/index.hbs @@ -3,7 +3,7 @@ {{ title }} - + diff --git a/tests/integration_tests.rs b/tests/integration_tests.rs index b7ed46e45..a06c311f8 100644 --- a/tests/integration_tests.rs +++ b/tests/integration_tests.rs @@ -5,6 +5,7 @@ extern crate mdbook_epub; extern crate tempdir; use std::path::Path; +use std::process::Command; use failure::{Error, SyncFailure}; use tempdir::TempDir; use epub::doc::EpubDoc; @@ -41,6 +42,28 @@ fn output_epub_is_valid() { let got = EpubDoc::new(&output_file); assert!(got.is_ok()); + + // also try to run epubcheck, if it's available + epub_check(&output_file).unwrap(); +} + +fn epub_check(path: &Path) -> Result<(), Error> { + let cmd = Command::new("epubcheck").arg(path).output(); + + match cmd { + Ok(output) => { + if output.status.success() { + Ok(()) + } else { + let msg = failure::err_msg(format!("epubcheck failed\n{:?}", output)); + Err(msg) + } + } + Err(_) => { + // failed to launch epubcheck, it's probably not installed + Ok(()) + } + } } #[test]