Allow to define own HTML <head> attributes

Create a `index.hbs` template file to render additional HTML <head> attributes
This commit is contained in:
Gilles Rasigade 2020-05-01 07:47:50 +02:00 committed by Eric Huss
parent 8ee950e3de
commit 61c8413138
4 changed files with 273 additions and 237 deletions

View File

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

@ -1,6 +1,7 @@
<!DOCTYPE HTML> <!DOCTYPE HTML>
<html lang="{{ language }}" class="sidebar-visible no-js {{ default_theme }}"> <html lang="{{ language }}" class="sidebar-visible no-js {{ default_theme }}">
<head>
<head>
<!-- Book generated using mdBook --> <!-- Book generated using mdBook -->
<meta charset="UTF-8"> <meta charset="UTF-8">
<title>{{ title }}</title> <title>{{ title }}</title>
@ -8,6 +9,9 @@
<meta name="robots" content="noindex" /> <meta name="robots" content="noindex" />
{{/if}} {{/if}}
<!-- Custom HTML head -->
{{> head}}
<meta content="text/html; charset=utf-8" http-equiv="Content-Type"> <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<meta name="description" content="{{ description }}"> <meta name="description" content="{{ description }}">
<meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="viewport" content="width=device-width, initial-scale=1">
@ -21,7 +25,9 @@
<!-- Fonts --> <!-- Fonts -->
<link rel="stylesheet" href="{{ path_to_root }}FontAwesome/css/font-awesome.css"> <link rel="stylesheet" href="{{ path_to_root }}FontAwesome/css/font-awesome.css">
<link href="https://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800" rel="stylesheet" type="text/css"> <link
href="https://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800"
rel="stylesheet" type="text/css">
<link href="https://fonts.googleapis.com/css?family=Source+Code+Pro:500" rel="stylesheet" type="text/css"> <link href="https://fonts.googleapis.com/css?family=Source+Code+Pro:500" rel="stylesheet" type="text/css">
<!-- Highlight.js Stylesheets --> <!-- Highlight.js Stylesheets -->
@ -36,10 +42,12 @@
{{#if mathjax_support}} {{#if mathjax_support}}
<!-- MathJax --> <!-- MathJax -->
<script async type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script> <script async type="text/javascript"
src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
{{/if}} {{/if}}
</head> </head>
<body>
<body>
<!-- Provide site root to javascript --> <!-- Provide site root to javascript -->
<script type="text/javascript"> <script type="text/javascript">
var path_to_root = "{{ path_to_root }}"; var path_to_root = "{{ path_to_root }}";
@ -65,7 +73,7 @@
<!-- Set the theme before any content is loaded, prevents flash --> <!-- Set the theme before any content is loaded, prevents flash -->
<script type="text/javascript"> <script type="text/javascript">
var theme; var theme;
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { } try { theme = localStorage.getItem('mdbook-theme'); } catch (e) { }
if (theme === null || theme === undefined) { theme = default_theme; } if (theme === null || theme === undefined) { theme = default_theme; }
var html = document.querySelector('html'); var html = document.querySelector('html');
html.classList.remove('no-js') html.classList.remove('no-js')
@ -79,7 +87,7 @@
var html = document.querySelector('html'); var html = document.querySelector('html');
var sidebar = 'hidden'; var sidebar = 'hidden';
if (document.body.clientWidth >= 1080) { if (document.body.clientWidth >= 1080) {
try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { } try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch (e) { }
sidebar = sidebar || 'visible'; sidebar = sidebar || 'visible';
} }
html.classList.remove('sidebar-visible'); html.classList.remove('sidebar-visible');
@ -100,21 +108,30 @@
<div id="menu-bar-hover-placeholder"></div> <div id="menu-bar-hover-placeholder"></div>
<div id="menu-bar" class="menu-bar sticky bordered"> <div id="menu-bar" class="menu-bar sticky bordered">
<div class="left-buttons"> <div class="left-buttons">
<button id="sidebar-toggle" class="icon-button" type="button" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar"> <button id="sidebar-toggle" class="icon-button" type="button" title="Toggle Table of Contents"
aria-label="Toggle Table of Contents" aria-controls="sidebar">
<i class="fa fa-bars"></i> <i class="fa fa-bars"></i>
</button> </button>
<button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list"> <button id="theme-toggle" class="icon-button" type="button" title="Change theme"
aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
<i class="fa fa-paint-brush"></i> <i class="fa fa-paint-brush"></i>
</button> </button>
<ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu"> <ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
<li role="none"><button role="menuitem" class="theme" id="light">{{ theme_option "Light" }}</button></li> <li role="none"><button role="menuitem" class="theme"
<li role="none"><button role="menuitem" class="theme" id="rust">{{ theme_option "Rust" }}</button></li> id="light">{{ theme_option "Light" }}</button></li>
<li role="none"><button role="menuitem" class="theme" id="coal">{{ theme_option "Coal" }}</button></li> <li role="none"><button role="menuitem" class="theme"
<li role="none"><button role="menuitem" class="theme" id="navy">{{ theme_option "Navy" }}</button></li> id="rust">{{ theme_option "Rust" }}</button></li>
<li role="none"><button role="menuitem" class="theme" id="ayu">{{ theme_option "Ayu" }}</button></li> <li role="none"><button role="menuitem" class="theme"
id="coal">{{ theme_option "Coal" }}</button></li>
<li role="none"><button role="menuitem" class="theme"
id="navy">{{ theme_option "Navy" }}</button></li>
<li role="none"><button role="menuitem" class="theme" id="ayu">{{ theme_option "Ayu" }}</button>
</li>
</ul> </ul>
{{#if search_enabled}} {{#if search_enabled}}
<button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar"> <button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)"
aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S"
aria-controls="searchbar">
<i class="fa fa-search"></i> <i class="fa fa-search"></i>
</button> </button>
{{/if}} {{/if}}
@ -137,7 +154,9 @@
{{#if search_enabled}} {{#if search_enabled}}
<div id="search-wrapper" class="hidden"> <div id="search-wrapper" class="hidden">
<form id="searchbar-outer" class="searchbar-outer"> <form id="searchbar-outer" class="searchbar-outer">
<input type="search" name="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header"> <input type="search" name="search" id="searchbar" name="searchbar"
placeholder="Search this book ..." aria-controls="searchresults-outer"
aria-describedby="searchresults-header">
</form> </form>
<div id="searchresults-outer" class="searchresults-outer hidden"> <div id="searchresults-outer" class="searchresults-outer hidden">
<div id="searchresults-header" class="searchresults-header"></div> <div id="searchresults-header" class="searchresults-header"></div>
@ -151,7 +170,7 @@
<script type="text/javascript"> <script type="text/javascript">
document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible'); document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible'); document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) { Array.from(document.querySelectorAll('#sidebar a')).forEach(function (link) {
link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1); link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
}); });
</script> </script>
@ -164,13 +183,15 @@
<nav class="nav-wrapper" aria-label="Page navigation"> <nav class="nav-wrapper" aria-label="Page navigation">
<!-- Mobile navigation buttons --> <!-- Mobile navigation buttons -->
{{#previous}} {{#previous}}
<a rel="prev" href="{{ path_to_root }}{{link}}" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left"> <a rel="prev" href="{{ path_to_root }}{{link}}" class="mobile-nav-chapters previous"
title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
<i class="fa fa-angle-left"></i> <i class="fa fa-angle-left"></i>
</a> </a>
{{/previous}} {{/previous}}
{{#next}} {{#next}}
<a rel="next" href="{{ path_to_root }}{{link}}" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right"> <a rel="next" href="{{ path_to_root }}{{link}}" class="mobile-nav-chapters next"
title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
<i class="fa fa-angle-right"></i> <i class="fa fa-angle-right"></i>
</a> </a>
{{/next}} {{/next}}
@ -182,13 +203,15 @@
<nav class="nav-wide-wrapper" aria-label="Page navigation"> <nav class="nav-wide-wrapper" aria-label="Page navigation">
{{#previous}} {{#previous}}
<a rel="prev" href="{{ path_to_root }}{{link}}" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left"> <a rel="prev" href="{{ path_to_root }}{{link}}" class="nav-chapters previous" title="Previous chapter"
aria-label="Previous chapter" aria-keyshortcuts="Left">
<i class="fa fa-angle-left"></i> <i class="fa fa-angle-left"></i>
</a> </a>
{{/previous}} {{/previous}}
{{#next}} {{#next}}
<a rel="next" href="{{ path_to_root }}{{link}}" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right"> <a rel="next" href="{{ path_to_root }}{{link}}" class="nav-chapters next" title="Next chapter"
aria-label="Next chapter" aria-keyshortcuts="Right">
<i class="fa fa-angle-right"></i> <i class="fa fa-angle-right"></i>
</a> </a>
{{/next}} {{/next}}
@ -207,7 +230,7 @@
} }
}; };
window.onbeforeunload = function() { window.onbeforeunload = function () {
socket.close(); socket.close();
} }
</script> </script>
@ -221,10 +244,12 @@
// make sure we don't activate google analytics if the developer is // make sure we don't activate google analytics if the developer is
// inspecting the book locally... // inspecting the book locally...
if (localAddrs.indexOf(document.location.hostname) === -1) { if (localAddrs.indexOf(document.location.hostname) === -1) {
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ (function (i, s, o, g, r, a, m) {
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), i['GoogleAnalyticsObject'] = r; i[r] = i[r] || function () {
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) (i[r].q = i[r].q || []).push(arguments)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga'); }, i[r].l = 1 * new Date(); a = s.createElement(o),
m = s.getElementsByTagName(o)[0]; a.async = 1; a.src = g; m.parentNode.insertBefore(a, m)
})(window, document, 'script', 'https://www.google-analytics.com/analytics.js', 'ga');
ga('create', '{{google_analytics}}', 'auto'); ga('create', '{{google_analytics}}', 'auto');
ga('send', 'pageview'); ga('send', 'pageview');
@ -270,20 +295,21 @@
{{#if is_print}} {{#if is_print}}
{{#if mathjax_support}} {{#if mathjax_support}}
<script type="text/javascript"> <script type="text/javascript">
window.addEventListener('load', function() { window.addEventListener('load', function () {
MathJax.Hub.Register.StartupHook('End', function() { MathJax.Hub.Register.StartupHook('End', function () {
window.setTimeout(window.print, 100); window.setTimeout(window.print, 100);
}); });
}); });
</script> </script>
{{else}} {{else}}
<script type="text/javascript"> <script type="text/javascript">
window.addEventListener('load', function() { window.addEventListener('load', function () {
window.setTimeout(window.print, 100); window.setTimeout(window.print, 100);
}); });
</script> </script>
{{/if}} {{/if}}
{{/if}} {{/if}}
</body> </body>
</html> </html>

View File

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