Skip to content

Commit

Permalink
Merge pull request #454 from projektir/header_partial
Browse files Browse the repository at this point in the history
Adding a header partial integration #453
  • Loading branch information
Michael-F-Bryan authored Dec 2, 2017
2 parents 1aa9c92 + 32df76d commit b614b0f
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 13 deletions.
4 changes: 4 additions & 0 deletions src/book/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,10 @@ impl MDBook {
let mut index = File::create(themedir.join("index.hbs"))?;
index.write_all(theme::INDEX)?;

// header.hbs
let mut header = File::create(themedir.join("header.hbs"))?;
header.write_all(theme::HEADER)?;

// book.css
let mut css = File::create(themedir.join("book.css"))?;
css.write_all(theme::CSS)?;
Expand Down
13 changes: 11 additions & 2 deletions src/renderer/html_handlebars/hbs_renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,17 @@ impl Renderer for HtmlHandlebars {

let theme = theme::Theme::new(theme_dir);

debug!("[*]: Register handlebars template");
handlebars.register_template_string("index", String::from_utf8(theme.index.clone())?)?;
debug!("[*]: Register the index handlebars template");
handlebars.register_template_string(
"index",
String::from_utf8(theme.index.clone())?,
)?;

debug!("[*]: Register the header handlebars template");
handlebars.register_partial(
"header",
String::from_utf8(theme.header.clone())?,
)?;

debug!("[*]: Register handlebars helpers");
self.register_hbs_helpers(&mut handlebars);
Expand Down
1 change: 1 addition & 0 deletions src/theme/header.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{!-- Put your header HTML text here --}}
1 change: 1 addition & 0 deletions src/theme/index.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
<div id="page-wrapper" class="page-wrapper">

<div class="page" tabindex="-1">
{{> header}}
<div id="menu-bar" class="menu-bar">
<div class="left-buttons">
<i id="sidebar-toggle" class="fa fa-bars" title="Toggle sidebar"></i>
Expand Down
29 changes: 18 additions & 11 deletions src/theme/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::io::Read;
use errors::*;

pub static INDEX: &'static [u8] = include_bytes!("index.hbs");
pub static HEADER: &'static [u8] = include_bytes!("header.hbs");
pub static CSS: &'static [u8] = include_bytes!("book.css");
pub static FAVICON: &'static [u8] = include_bytes!("favicon.png");
pub static JS: &'static [u8] = include_bytes!("book.js");
Expand Down Expand Up @@ -40,6 +41,7 @@ pub static FONT_AWESOME_OTF: &'static [u8] = include_bytes!("_FontAwesome/fonts/
#[derive(Debug, PartialEq)]
pub struct Theme {
pub index: Vec<u8>,
pub header: Vec<u8>,
pub css: Vec<u8>,
pub favicon: Vec<u8>,
pub js: Vec<u8>,
Expand All @@ -64,17 +66,20 @@ impl Theme {

// Check for individual files, if they exist copy them across
{
let files = vec![(theme_dir.join("index.hbs"), &mut theme.index),
(theme_dir.join("book.js"), &mut theme.js),
(theme_dir.join("book.css"), &mut theme.css),
(theme_dir.join("favicon.png"), &mut theme.favicon),
(theme_dir.join("highlight.js"), &mut theme.highlight_js),
(theme_dir.join("clipboard.min.js"), &mut theme.clipboard_js),
(theme_dir.join("store.js"), &mut theme.store_js),
(theme_dir.join("highlight.css"), &mut theme.highlight_css),
(theme_dir.join("tomorrow-night.css"), &mut theme.tomorrow_night_css),
(theme_dir.join("ayu-highlight.css"), &mut theme.ayu_highlight_css),
(theme_dir.join("jquery.js"), &mut theme.jquery)];
let files = vec![
(theme_dir.join("index.hbs"), &mut theme.index),
(theme_dir.join("header.hbs"), &mut theme.header),
(theme_dir.join("book.js"), &mut theme.js),
(theme_dir.join("book.css"), &mut theme.css),
(theme_dir.join("favicon.png"), &mut theme.favicon),
(theme_dir.join("highlight.js"), &mut theme.highlight_js),
(theme_dir.join("clipboard.min.js"), &mut theme.clipboard_js),
(theme_dir.join("store.js"), &mut theme.store_js),
(theme_dir.join("highlight.css"), &mut theme.highlight_css),
(theme_dir.join("tomorrow-night.css"), &mut theme.tomorrow_night_css),
(theme_dir.join("ayu-highlight.css"), &mut theme.ayu_highlight_css),
(theme_dir.join("jquery.js"), &mut theme.jquery),
];

for (filename, dest) in files {
if !filename.exists() {
Expand All @@ -95,6 +100,7 @@ impl Default for Theme {
fn default() -> Theme {
Theme {
index: INDEX.to_owned(),
header: HEADER.to_owned(),
css: CSS.to_owned(),
favicon: FAVICON.to_owned(),
js: JS.to_owned(),
Expand Down Expand Up @@ -166,6 +172,7 @@ mod tests {

let empty = Theme {
index: Vec::new(),
header: Vec::new(),
css: Vec::new(),
favicon: Vec::new(),
js: Vec::new(),
Expand Down

0 comments on commit b614b0f

Please sign in to comment.