Merge pull request #363 from budziq/opt_mathjax

Make MathJax support optional
This commit is contained in:
Mathieu David 2017-06-26 17:01:52 +02:00 committed by GitHub
commit 13ab20ea49
7 changed files with 56 additions and 1 deletions

View File

@ -498,6 +498,23 @@ impl MDBook {
false 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<String> { pub fn get_google_analytics_id(&self) -> Option<String> {
if let Some(htmlconfig) = self.config.get_html_config() { if let Some(htmlconfig) = self.config.get_html_config() {
return htmlconfig.get_google_analytics_id(); return htmlconfig.get_google_analytics_id();

View File

@ -7,6 +7,7 @@ pub struct HtmlConfig {
destination: PathBuf, destination: PathBuf,
theme: PathBuf, theme: PathBuf,
curly_quotes: bool, curly_quotes: bool,
mathjax_support: bool,
google_analytics: Option<String>, google_analytics: Option<String>,
additional_css: Vec<PathBuf>, additional_css: Vec<PathBuf>,
additional_js: Vec<PathBuf>, additional_js: Vec<PathBuf>,
@ -30,6 +31,7 @@ impl HtmlConfig {
destination: root.clone().join("book"), destination: root.clone().join("book"),
theme: root.join("theme"), theme: root.join("theme"),
curly_quotes: false, curly_quotes: false,
mathjax_support: false,
google_analytics: None, google_analytics: None,
additional_css: Vec::new(), additional_css: Vec::new(),
additional_js: Vec::new(), additional_js: Vec::new(),
@ -51,6 +53,10 @@ impl HtmlConfig {
self.curly_quotes = curly_quotes; self.curly_quotes = curly_quotes;
} }
if let Some(mathjax_support) = tomlconfig.mathjax_support {
self.mathjax_support = mathjax_support;
}
if tomlconfig.google_analytics.is_some() { if tomlconfig.google_analytics.is_some() {
self.google_analytics = tomlconfig.google_analytics; self.google_analytics = tomlconfig.google_analytics;
} }
@ -116,6 +122,14 @@ impl HtmlConfig {
self.curly_quotes = curly_quotes; 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<String> { pub fn get_google_analytics_id(&self) -> Option<String> {
self.google_analytics.clone() self.google_analytics.clone()
} }

View File

@ -25,6 +25,7 @@ pub struct TomlHtmlConfig {
pub theme: Option<PathBuf>, pub theme: Option<PathBuf>,
pub google_analytics: Option<String>, pub google_analytics: Option<String>,
pub curly_quotes: Option<bool>, pub curly_quotes: Option<bool>,
pub mathjax_support: Option<bool>,
pub additional_css: Option<Vec<PathBuf>>, pub additional_css: Option<Vec<PathBuf>>,
pub additional_js: Option<Vec<PathBuf>>, pub additional_js: Option<Vec<PathBuf>>,
} }

View File

@ -318,6 +318,10 @@ fn make_data(book: &MDBook) -> Result<serde_json::Map<String, serde_json::Value>
data.insert("google_analytics".to_owned(), json!(ga)); 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 // Add check to see if there is an additional style
if book.has_additional_css() { if book.has_additional_css() {
let mut css = Vec::new(); let mut css = Vec::new();

View File

@ -27,8 +27,10 @@
<link rel="stylesheet" href="{{this}}"> <link rel="stylesheet" href="{{this}}">
{{/each}} {{/each}}
{{#if mathjax_support}}
<!-- MathJax --> <!-- MathJax -->
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script> <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
{{/if}}
<!-- Fetch Clipboard.js from CDN but have a local fallback --> <!-- Fetch Clipboard.js from CDN but have a local fallback -->
<script src="https://cdn.jsdelivr.net/clipboard.js/1.6.1/clipboard.min.js"></script> <script src="https://cdn.jsdelivr.net/clipboard.js/1.6.1/clipboard.min.js"></script>

View File

@ -15,7 +15,8 @@ fn do_not_overwrite_unspecified_config_values() {
let book = MDBook::new(dir.path()) let book = MDBook::new(dir.path())
.with_source("bar") .with_source("bar")
.with_destination("baz"); .with_destination("baz")
.with_mathjax_support(true);
assert_eq!(book.get_root(), dir.path()); assert_eq!(book.get_root(), dir.path());
assert_eq!(book.get_source(), dir.path().join("bar")); 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_root(), dir.path());
assert_eq!(book.get_source(), dir.path().join("bar")); assert_eq!(book.get_source(), dir.path().join("bar"));
assert_eq!(book.get_destination().unwrap(), dir.path().join("baz")); assert_eq!(book.get_destination().unwrap(), dir.path().join("baz"));
assert_eq!(book.get_mathjax_support(), true);
// Try with a partial config file // Try with a partial config file
let file_path = dir.path().join("book.toml"); 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_root(), dir.path());
assert_eq!(book.get_source(), dir.path().join("barbaz")); assert_eq!(book.get_source(), dir.path().join("barbaz"));
assert_eq!(book.get_destination().unwrap(), dir.path().join("baz")); assert_eq!(book.get_destination().unwrap(), dir.path().join("baz"));
assert_eq!(book.get_mathjax_support(), true);
} }

View File

@ -101,6 +101,20 @@ fn from_toml_output_html_curly_quotes() {
assert_eq!(htmlconfig.get_curly_quotes(), true); 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 // Tests that the `output.html.google-analytics` key is correctly parsed in the TOML config
#[test] #[test]
fn from_toml_output_html_google_analytics() { fn from_toml_output_html_google_analytics() {