From d2565af0006639625fbeb94c8f0c1e38397f0172 Mon Sep 17 00:00:00 2001 From: Jason Liquorish Date: Sat, 13 Oct 2018 14:44:10 +0100 Subject: [PATCH] Add helper to format theme name for theme changer --- src/renderer/html_handlebars/hbs_renderer.rs | 1 + src/renderer/html_handlebars/helpers/mod.rs | 1 + src/renderer/html_handlebars/helpers/theme.rs | 30 +++++++++++++++++++ src/theme/index.hbs | 10 +++---- 4 files changed, 37 insertions(+), 5 deletions(-) create mode 100644 src/renderer/html_handlebars/helpers/theme.rs diff --git a/src/renderer/html_handlebars/hbs_renderer.rs b/src/renderer/html_handlebars/hbs_renderer.rs index fe80259e..aaf51c26 100644 --- a/src/renderer/html_handlebars/hbs_renderer.rs +++ b/src/renderer/html_handlebars/hbs_renderer.rs @@ -214,6 +214,7 @@ impl HtmlHandlebars { ); handlebars.register_helper("previous", Box::new(helpers::navigation::previous)); handlebars.register_helper("next", Box::new(helpers::navigation::next)); + handlebars.register_helper("theme_option", Box::new(helpers::theme::theme_option)); } /// Copy across any additional CSS and JavaScript files which the book diff --git a/src/renderer/html_handlebars/helpers/mod.rs b/src/renderer/html_handlebars/helpers/mod.rs index 62fc6149..e9a8f932 100644 --- a/src/renderer/html_handlebars/helpers/mod.rs +++ b/src/renderer/html_handlebars/helpers/mod.rs @@ -1,2 +1,3 @@ pub mod navigation; pub mod toc; +pub mod theme; \ No newline at end of file diff --git a/src/renderer/html_handlebars/helpers/theme.rs b/src/renderer/html_handlebars/helpers/theme.rs new file mode 100644 index 00000000..24c0dda1 --- /dev/null +++ b/src/renderer/html_handlebars/helpers/theme.rs @@ -0,0 +1,30 @@ +use handlebars::{Context, Handlebars, Helper, Output, RenderContext, RenderError}; + +pub fn theme_option( + h: &Helper, + _r: &Handlebars, + ctx: &Context, + rc: &mut RenderContext, + out: &mut Output, +) -> Result<(), RenderError> { + trace!("theme_option (handlebars helper)"); + + let param = h + .param(0) + .and_then(|v| v.value().as_str()) + .ok_or(RenderError::new( + "Param 0 with String type is required for theme_option helper.", + ))?; + + let theme_name = rc + .evaluate_absolute(ctx, "default_theme", true)? + .as_str() + .ok_or_else(|| RenderError::new("Type error for `default_theme`, string expected"))?; + + out.write(param)?; + if param.to_lowercase() == theme_name.to_lowercase() { + out.write(" (default)")?; + } + + Ok(()) +} diff --git a/src/theme/index.hbs b/src/theme/index.hbs index 744916d4..a390731f 100644 --- a/src/theme/index.hbs +++ b/src/theme/index.hbs @@ -97,11 +97,11 @@ {{#if search_enabled}}