Skip to content

Commit

Permalink
The stylesheet location is now passed into the template
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael-F-Bryan committed Jan 10, 2018
1 parent 6127bb0 commit bed53fe
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 14 deletions.
26 changes: 13 additions & 13 deletions src/generator.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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(())
}
Expand Down Expand Up @@ -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
Expand All @@ -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::<Vec<_>>()
.join("/");

let ctx = json!({ "title": ch.name, "body": body, "stylesheet": stylesheet_path });

self.hbs.render("index", &ctx)
}
Expand Down
2 changes: 1 addition & 1 deletion src/index.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head>
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>{{ title }}</title>
<link rel="stylesheet" href="stylesheet.css" />
<link rel="stylesheet" href="{{ stylesheet }}" />
</head>

<body>
Expand Down
23 changes: 23 additions & 0 deletions tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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]
Expand Down

0 comments on commit bed53fe

Please sign in to comment.