Skip to content

Commit b614b0f

Browse files
Merge pull request #454 from projektir/header_partial
Adding a header partial integration #453
2 parents 1aa9c92 + 32df76d commit b614b0f

File tree

5 files changed

+35
-13
lines changed

5 files changed

+35
-13
lines changed

src/book/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,10 @@ impl MDBook {
263263
let mut index = File::create(themedir.join("index.hbs"))?;
264264
index.write_all(theme::INDEX)?;
265265

266+
// header.hbs
267+
let mut header = File::create(themedir.join("header.hbs"))?;
268+
header.write_all(theme::HEADER)?;
269+
266270
// book.css
267271
let mut css = File::create(themedir.join("book.css"))?;
268272
css.write_all(theme::CSS)?;

src/renderer/html_handlebars/hbs_renderer.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,8 +253,17 @@ impl Renderer for HtmlHandlebars {
253253

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

256-
debug!("[*]: Register handlebars template");
257-
handlebars.register_template_string("index", String::from_utf8(theme.index.clone())?)?;
256+
debug!("[*]: Register the index handlebars template");
257+
handlebars.register_template_string(
258+
"index",
259+
String::from_utf8(theme.index.clone())?,
260+
)?;
261+
262+
debug!("[*]: Register the header handlebars template");
263+
handlebars.register_partial(
264+
"header",
265+
String::from_utf8(theme.header.clone())?,
266+
)?;
258267

259268
debug!("[*]: Register handlebars helpers");
260269
self.register_hbs_helpers(&mut handlebars);

src/theme/header.hbs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{{!-- Put your header HTML text here --}}

src/theme/index.hbs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
<div id="page-wrapper" class="page-wrapper">
7575

7676
<div class="page" tabindex="-1">
77+
{{> header}}
7778
<div id="menu-bar" class="menu-bar">
7879
<div class="left-buttons">
7980
<i id="sidebar-toggle" class="fa fa-bars" title="Toggle sidebar"></i>

src/theme/mod.rs

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use std::io::Read;
77
use errors::*;
88

99
pub static INDEX: &'static [u8] = include_bytes!("index.hbs");
10+
pub static HEADER: &'static [u8] = include_bytes!("header.hbs");
1011
pub static CSS: &'static [u8] = include_bytes!("book.css");
1112
pub static FAVICON: &'static [u8] = include_bytes!("favicon.png");
1213
pub static JS: &'static [u8] = include_bytes!("book.js");
@@ -40,6 +41,7 @@ pub static FONT_AWESOME_OTF: &'static [u8] = include_bytes!("_FontAwesome/fonts/
4041
#[derive(Debug, PartialEq)]
4142
pub struct Theme {
4243
pub index: Vec<u8>,
44+
pub header: Vec<u8>,
4345
pub css: Vec<u8>,
4446
pub favicon: Vec<u8>,
4547
pub js: Vec<u8>,
@@ -64,17 +66,20 @@ impl Theme {
6466

6567
// Check for individual files, if they exist copy them across
6668
{
67-
let files = vec![(theme_dir.join("index.hbs"), &mut theme.index),
68-
(theme_dir.join("book.js"), &mut theme.js),
69-
(theme_dir.join("book.css"), &mut theme.css),
70-
(theme_dir.join("favicon.png"), &mut theme.favicon),
71-
(theme_dir.join("highlight.js"), &mut theme.highlight_js),
72-
(theme_dir.join("clipboard.min.js"), &mut theme.clipboard_js),
73-
(theme_dir.join("store.js"), &mut theme.store_js),
74-
(theme_dir.join("highlight.css"), &mut theme.highlight_css),
75-
(theme_dir.join("tomorrow-night.css"), &mut theme.tomorrow_night_css),
76-
(theme_dir.join("ayu-highlight.css"), &mut theme.ayu_highlight_css),
77-
(theme_dir.join("jquery.js"), &mut theme.jquery)];
69+
let files = vec![
70+
(theme_dir.join("index.hbs"), &mut theme.index),
71+
(theme_dir.join("header.hbs"), &mut theme.header),
72+
(theme_dir.join("book.js"), &mut theme.js),
73+
(theme_dir.join("book.css"), &mut theme.css),
74+
(theme_dir.join("favicon.png"), &mut theme.favicon),
75+
(theme_dir.join("highlight.js"), &mut theme.highlight_js),
76+
(theme_dir.join("clipboard.min.js"), &mut theme.clipboard_js),
77+
(theme_dir.join("store.js"), &mut theme.store_js),
78+
(theme_dir.join("highlight.css"), &mut theme.highlight_css),
79+
(theme_dir.join("tomorrow-night.css"), &mut theme.tomorrow_night_css),
80+
(theme_dir.join("ayu-highlight.css"), &mut theme.ayu_highlight_css),
81+
(theme_dir.join("jquery.js"), &mut theme.jquery),
82+
];
7883

7984
for (filename, dest) in files {
8085
if !filename.exists() {
@@ -95,6 +100,7 @@ impl Default for Theme {
95100
fn default() -> Theme {
96101
Theme {
97102
index: INDEX.to_owned(),
103+
header: HEADER.to_owned(),
98104
css: CSS.to_owned(),
99105
favicon: FAVICON.to_owned(),
100106
js: JS.to_owned(),
@@ -166,6 +172,7 @@ mod tests {
166172

167173
let empty = Theme {
168174
index: Vec::new(),
175+
header: Vec::new(),
169176
css: Vec::new(),
170177
favicon: Vec::new(),
171178
js: Vec::new(),

0 commit comments

Comments
 (0)