From 6880df3efb862d37fc342a3b98d7cf95a488e7b0 Mon Sep 17 00:00:00 2001 From: Riccardo Magliocchetti Date: Wed, 12 May 2021 12:59:08 +0200 Subject: [PATCH] Add before_body_end handlebars partial Add a partial just before the body closing tag to add custom html code. The use case is for adding javascript snippet that involves more than just importing external scripts, e.g. web analytics. --- guide/src/format/theme/README.md | 1 + src/renderer/html_handlebars/hbs_renderer.rs | 3 +++ src/theme/before_body_end.hbs | 1 + src/theme/index.hbs | 2 ++ src/theme/mod.rs | 6 ++++++ 5 files changed, 13 insertions(+) create mode 100644 src/theme/before_body_end.hbs diff --git a/guide/src/format/theme/README.md b/guide/src/format/theme/README.md index b327b1d4..3d38b23d 100644 --- a/guide/src/format/theme/README.md +++ b/guide/src/format/theme/README.md @@ -14,6 +14,7 @@ Here are the files you can override: - **_index.hbs_** is the handlebars template. - **_head.hbs_** is appended to the HTML `` section. - **_header.hbs_** content is appended on top of every book page. +- **_before_body_end.hbs_** content is appended before body closing tag. - **_css/_** contains the CSS files for styling the book. - **_css/chrome.css_** is for UI elements. - **_css/general.css_** is the base styles. diff --git a/src/renderer/html_handlebars/hbs_renderer.rs b/src/renderer/html_handlebars/hbs_renderer.rs index 56324331..2bae2622 100644 --- a/src/renderer/html_handlebars/hbs_renderer.rs +++ b/src/renderer/html_handlebars/hbs_renderer.rs @@ -502,6 +502,9 @@ impl Renderer for HtmlHandlebars { debug!("Register the header handlebars template"); handlebars.register_partial("header", String::from_utf8(theme.header.clone())?)?; + debug!("Register the before_body_end handlebars template"); + handlebars.register_partial("before_body_end", String::from_utf8(theme.before_body_end.clone())?)?; + debug!("Register handlebars helpers"); self.register_hbs_helpers(&mut handlebars, &html_config); diff --git a/src/theme/before_body_end.hbs b/src/theme/before_body_end.hbs new file mode 100644 index 00000000..686d31d2 --- /dev/null +++ b/src/theme/before_body_end.hbs @@ -0,0 +1 @@ +{{!-- Put your before_body_end HTML text here --}} diff --git a/src/theme/index.hbs b/src/theme/index.hbs index 966eedbc..76e9d864 100644 --- a/src/theme/index.hbs +++ b/src/theme/index.hbs @@ -308,5 +308,7 @@ {{/if}} {{/if}} + {{> before_body_end}} + diff --git a/src/theme/mod.rs b/src/theme/mod.rs index a1ee18af..553537e0 100644 --- a/src/theme/mod.rs +++ b/src/theme/mod.rs @@ -17,6 +17,7 @@ pub static INDEX: &[u8] = include_bytes!("index.hbs"); pub static HEAD: &[u8] = include_bytes!("head.hbs"); pub static REDIRECT: &[u8] = include_bytes!("redirect.hbs"); pub static HEADER: &[u8] = include_bytes!("header.hbs"); +pub static BEFORE_BODY_END: &[u8] = include_bytes!("before_body_end.hbs"); pub static CHROME_CSS: &[u8] = include_bytes!("css/chrome.css"); pub static GENERAL_CSS: &[u8] = include_bytes!("css/general.css"); pub static PRINT_CSS: &[u8] = include_bytes!("css/print.css"); @@ -50,6 +51,7 @@ pub struct Theme { pub head: Vec, pub redirect: Vec, pub header: Vec, + pub before_body_end: Vec, pub chrome_css: Vec, pub general_css: Vec, pub print_css: Vec, @@ -83,6 +85,7 @@ impl Theme { (theme_dir.join("head.hbs"), &mut theme.head), (theme_dir.join("redirect.hbs"), &mut theme.redirect), (theme_dir.join("header.hbs"), &mut theme.header), + (theme_dir.join("before_body_end.hbs"), &mut theme.before_body_end), (theme_dir.join("book.js"), &mut theme.js), (theme_dir.join("css/chrome.css"), &mut theme.chrome_css), (theme_dir.join("css/general.css"), &mut theme.general_css), @@ -149,6 +152,7 @@ impl Default for Theme { head: HEAD.to_owned(), redirect: REDIRECT.to_owned(), header: HEADER.to_owned(), + before_body_end: BEFORE_BODY_END.to_owned(), chrome_css: CHROME_CSS.to_owned(), general_css: GENERAL_CSS.to_owned(), print_css: PRINT_CSS.to_owned(), @@ -206,6 +210,7 @@ mod tests { "head.hbs", "redirect.hbs", "header.hbs", + "before_body_end.hbs", "favicon.png", "favicon.svg", "css/chrome.css", @@ -236,6 +241,7 @@ mod tests { head: Vec::new(), redirect: Vec::new(), header: Vec::new(), + before_body_end: Vec::new(), chrome_css: Vec::new(), general_css: Vec::new(), print_css: Vec::new(),