Merge pull request #1206 from GillesRasigade/html-head-custom

HTML head customisation with `head.hbs`
This commit is contained in:
Eric Huss 2020-05-10 08:57:23 -07:00 committed by GitHub
commit 7e11d37e49
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 22 additions and 7 deletions

View File

@ -11,16 +11,18 @@ and now that file will be used instead of the default file.
Here are the files you can override:
- ***index.hbs*** is the handlebars template.
- ***book.css*** is the style used in the output. If you want to change the
- **_index.hbs_** is the handlebars template.
- **_head.hbs_** is appended to the HTML `<head>` section.
- **_header.hbs_** content is appended on top of every book page.
- **_book.css_** is the style used in the output. If you want to change the
design of your book, this is probably the file you want to modify. Sometimes
in conjunction with `index.hbs` when you want to radically change the layout.
- ***book.js*** is mostly used to add client side functionality, like hiding /
- **_book.js_** is mostly used to add client side functionality, like hiding /
un-hiding the sidebar, changing the theme, ...
- ***highlight.js*** is the JavaScript that is used to highlight code snippets,
you should not need to modify this.
- ***highlight.css*** is the theme used for the code highlighting
- ***favicon.png*** the favicon that will be used
- **_highlight.js_** is the JavaScript that is used to highlight code snippets,
you should not need to modify this.
- **_highlight.css_** is the theme used for the code highlighting.
- **_favicon.png_** the favicon that will be used.
Generally, when you want to tweak the theme, you don't need to override all the
files. If you only need changes in the stylesheet, there is no point in

View File

@ -327,6 +327,9 @@ impl Renderer for HtmlHandlebars {
debug!("Register the index handlebars template");
handlebars.register_template_string("index", String::from_utf8(theme.index.clone())?)?;
debug!("Register the head handlebars template");
handlebars.register_partial("head", String::from_utf8(theme.head.clone())?)?;
debug!("Register the header handlebars template");
handlebars.register_partial("header", String::from_utf8(theme.header.clone())?)?;

1
src/theme/head.hbs Normal file
View File

@ -0,0 +1 @@
{{!-- Put your head HTML text here --}}

View File

@ -8,6 +8,9 @@
<meta name="robots" content="noindex" />
{{/if}}
<!-- Custom HTML head -->
{{> head}}
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<meta name="description" content="{{ description }}">
<meta name="viewport" content="width=device-width, initial-scale=1">

View File

@ -12,6 +12,7 @@ use std::path::Path;
use crate::errors::*;
pub static INDEX: &[u8] = include_bytes!("index.hbs");
pub static HEAD: &[u8] = include_bytes!("head.hbs");
pub static HEADER: &[u8] = include_bytes!("header.hbs");
pub static CHROME_CSS: &[u8] = include_bytes!("css/chrome.css");
pub static GENERAL_CSS: &[u8] = include_bytes!("css/general.css");
@ -42,6 +43,7 @@ pub static FONT_AWESOME_OTF: &[u8] = include_bytes!("FontAwesome/fonts/FontAweso
#[derive(Debug, PartialEq)]
pub struct Theme {
pub index: Vec<u8>,
pub head: Vec<u8>,
pub header: Vec<u8>,
pub chrome_css: Vec<u8>,
pub general_css: Vec<u8>,
@ -72,6 +74,7 @@ impl Theme {
{
let files = vec![
(theme_dir.join("index.hbs"), &mut theme.index),
(theme_dir.join("head.hbs"), &mut theme.head),
(theme_dir.join("header.hbs"), &mut theme.header),
(theme_dir.join("book.js"), &mut theme.js),
(theme_dir.join("css/chrome.css"), &mut theme.chrome_css),
@ -114,6 +117,7 @@ impl Default for Theme {
fn default() -> Theme {
Theme {
index: INDEX.to_owned(),
head: HEAD.to_owned(),
header: HEADER.to_owned(),
chrome_css: CHROME_CSS.to_owned(),
general_css: GENERAL_CSS.to_owned(),
@ -168,6 +172,7 @@ mod tests {
fn theme_dir_overrides_defaults() {
let files = [
"index.hbs",
"head.hbs",
"header.hbs",
"favicon.png",
"css/chrome.css",
@ -194,6 +199,7 @@ mod tests {
let empty = Theme {
index: Vec::new(),
head: Vec::new(),
header: Vec::new(),
chrome_css: Vec::new(),
general_css: Vec::new(),