From f214c7108fdefab0ef5e9857cf4e016621b0e1ae Mon Sep 17 00:00:00 2001 From: Michal Budzynski Date: Sun, 25 Jun 2017 00:32:26 +0200 Subject: [PATCH 1/2] Make MathJax support optional to enable add following to book.toml ```toml [output.html] mathjax-support = true ``` --- src/book/mod.rs | 17 +++++++++++++++++ src/config/htmlconfig.rs | 14 ++++++++++++++ src/config/tomlconfig.rs | 1 + src/renderer/html_handlebars/hbs_renderer.rs | 4 ++++ src/theme/index.hbs | 2 ++ 5 files changed, 38 insertions(+) diff --git a/src/book/mod.rs b/src/book/mod.rs index 35ed41f7..f33a9187 100644 --- a/src/book/mod.rs +++ b/src/book/mod.rs @@ -498,6 +498,23 @@ impl MDBook { false } + pub fn with_mathjax_support(mut self, mathjax_support: bool) -> Self { + if let Some(htmlconfig) = self.config.get_mut_html_config() { + htmlconfig.set_mathjax_support(mathjax_support); + } else { + error!("There is no HTML renderer set..."); + } + self + } + + pub fn get_mathjax_support(&self) -> bool { + if let Some(htmlconfig) = self.config.get_html_config() { + return htmlconfig.get_mathjax_support(); + } + + false + } + pub fn get_google_analytics_id(&self) -> Option { if let Some(htmlconfig) = self.config.get_html_config() { return htmlconfig.get_google_analytics_id(); diff --git a/src/config/htmlconfig.rs b/src/config/htmlconfig.rs index 0cb2192b..5d5cac94 100644 --- a/src/config/htmlconfig.rs +++ b/src/config/htmlconfig.rs @@ -7,6 +7,7 @@ pub struct HtmlConfig { destination: PathBuf, theme: PathBuf, curly_quotes: bool, + mathjax_support: bool, google_analytics: Option, additional_css: Vec, additional_js: Vec, @@ -30,6 +31,7 @@ impl HtmlConfig { destination: root.clone().join("book"), theme: root.join("theme"), curly_quotes: false, + mathjax_support: false, google_analytics: None, additional_css: Vec::new(), additional_js: Vec::new(), @@ -51,6 +53,10 @@ impl HtmlConfig { self.curly_quotes = curly_quotes; } + if let Some(mathjax_support) = tomlconfig.mathjax_support { + self.mathjax_support = mathjax_support; + } + if tomlconfig.google_analytics.is_some() { self.google_analytics = tomlconfig.google_analytics; } @@ -116,6 +122,14 @@ impl HtmlConfig { self.curly_quotes = curly_quotes; } + pub fn get_mathjax_support(&self) -> bool { + self.mathjax_support + } + + pub fn set_mathjax_support(&mut self, mathjax_support: bool) { + self.mathjax_support = mathjax_support; + } + pub fn get_google_analytics_id(&self) -> Option { self.google_analytics.clone() } diff --git a/src/config/tomlconfig.rs b/src/config/tomlconfig.rs index 90cfa4c6..5fac3a3e 100644 --- a/src/config/tomlconfig.rs +++ b/src/config/tomlconfig.rs @@ -25,6 +25,7 @@ pub struct TomlHtmlConfig { pub theme: Option, pub google_analytics: Option, pub curly_quotes: Option, + pub mathjax_support: Option, pub additional_css: Option>, pub additional_js: Option>, } diff --git a/src/renderer/html_handlebars/hbs_renderer.rs b/src/renderer/html_handlebars/hbs_renderer.rs index fd69ee58..34b240f2 100644 --- a/src/renderer/html_handlebars/hbs_renderer.rs +++ b/src/renderer/html_handlebars/hbs_renderer.rs @@ -318,6 +318,10 @@ fn make_data(book: &MDBook) -> Result data.insert("google_analytics".to_owned(), json!(ga)); } + if book.get_mathjax_support() { + data.insert("mathjax_support".to_owned(), json!(true)); + } + // Add check to see if there is an additional style if book.has_additional_css() { let mut css = Vec::new(); diff --git a/src/theme/index.hbs b/src/theme/index.hbs index 467d2fe6..769d1caa 100644 --- a/src/theme/index.hbs +++ b/src/theme/index.hbs @@ -27,8 +27,10 @@ {{/each}} + {{#if mathjax_support}} + {{/if}} From db94b3d8394851a515672c2617e999d55f8c7fea Mon Sep 17 00:00:00 2001 From: Michal Budzynski Date: Sun, 25 Jun 2017 00:39:57 +0200 Subject: [PATCH 2/2] Add minimal testing for the optional MathJax support --- tests/config.rs | 5 ++++- tests/tomlconfig.rs | 14 ++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/tests/config.rs b/tests/config.rs index 9fd95e60..fd9d86cc 100644 --- a/tests/config.rs +++ b/tests/config.rs @@ -15,7 +15,8 @@ fn do_not_overwrite_unspecified_config_values() { let book = MDBook::new(dir.path()) .with_source("bar") - .with_destination("baz"); + .with_destination("baz") + .with_mathjax_support(true); assert_eq!(book.get_root(), dir.path()); assert_eq!(book.get_source(), dir.path().join("bar")); @@ -27,6 +28,7 @@ fn do_not_overwrite_unspecified_config_values() { assert_eq!(book.get_root(), dir.path()); assert_eq!(book.get_source(), dir.path().join("bar")); assert_eq!(book.get_destination().unwrap(), dir.path().join("baz")); + assert_eq!(book.get_mathjax_support(), true); // Try with a partial config file let file_path = dir.path().join("book.toml"); @@ -39,5 +41,6 @@ fn do_not_overwrite_unspecified_config_values() { assert_eq!(book.get_root(), dir.path()); assert_eq!(book.get_source(), dir.path().join("barbaz")); assert_eq!(book.get_destination().unwrap(), dir.path().join("baz")); + assert_eq!(book.get_mathjax_support(), true); } diff --git a/tests/tomlconfig.rs b/tests/tomlconfig.rs index 0ff62ada..a751b359 100644 --- a/tests/tomlconfig.rs +++ b/tests/tomlconfig.rs @@ -101,6 +101,20 @@ fn from_toml_output_html_curly_quotes() { assert_eq!(htmlconfig.get_curly_quotes(), true); } +// Tests that the `output.html.mathjax-support` key is correctly parsed in the TOML config +#[test] +fn from_toml_output_html_mathjax_support() { + let toml = r#"[output.html] + mathjax-support = true"#; + + let parsed = TomlConfig::from_toml(&toml).expect("This should parse"); + let config = BookConfig::from_tomlconfig("root", parsed); + + let htmlconfig = config.get_html_config().expect("There should be an HtmlConfig"); + + assert_eq!(htmlconfig.get_mathjax_support(), true); +} + // Tests that the `output.html.google-analytics` key is correctly parsed in the TOML config #[test] fn from_toml_output_html_google_analytics() {