From 9424d04c37fabd1354e5e4c0be0754b304c49017 Mon Sep 17 00:00:00 2001 From: trivernis Date: Mon, 7 Sep 2020 08:55:49 +0200 Subject: [PATCH] Add option to turn off embedding of images Signed-off-by: trivernis --- Cargo.lock | 2 +- Cargo.toml | 2 +- README.md | 3 ++- src/format/html/to_html.rs | 26 ++++++++++++++++++++++++-- src/parser/mod.rs | 1 + src/references/configuration/keys.rs | 1 + 6 files changed, 30 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 28096b6..afcdbaa 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1277,7 +1277,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "snekdown" -version = "0.27.1" +version = "0.27.2" dependencies = [ "asciimath-rs 0.5.7 (registry+https://github.com/rust-lang/crates.io-index)", "base64 0.12.3 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/Cargo.toml b/Cargo.toml index a7de3db..56f9a20 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "snekdown" -version = "0.27.1" +version = "0.27.2" authors = ["trivernis "] edition = "2018" license-file = "LICENSE" diff --git a/README.md b/README.md index 8f0701b..c75320a 100644 --- a/README.md +++ b/README.md @@ -46,7 +46,8 @@ Extended syntax with metadata and no description !(url)[metadata] ``` -When generating the html file the images are base64 embedded. +When generating the html file the images are base64 embedded. To turn off this behaviour +set the config parameter `embed-external` to `false`. ### Quotes diff --git a/src/format/html/to_html.rs b/src/format/html/to_html.rs index c08c5e8..ac506cd 100644 --- a/src/format/html/to_html.rs +++ b/src/format/html/to_html.rs @@ -1,7 +1,8 @@ use crate::elements::*; use crate::format::html::html_writer::HTMLWriter; use crate::format::PlaceholderTemplate; -use crate::references::configuration::keys::META_LANG; +use crate::references::configuration::keys::{EMBED_EXTERNAL, META_LANG}; +use crate::references::configuration::Value; use crate::references::templates::{Template, TemplateVariable}; use asciimath_rs::format::mathml::ToMathML; use htmlescape::encode_attribute; @@ -103,12 +104,23 @@ impl ToHtml for Document { .lock() .unwrap() .add_download(MATHJAX_URL.to_string()); - downloads.lock().unwrap().download_all(); + if let Some(Value::Bool(embed)) = self + .config + .get_entry(EMBED_EXTERNAL) + .map(|e| e.get().clone()) + { + if embed { + downloads.lock().unwrap().download_all(); + } + } else { + downloads.lock().unwrap().download_all(); + } let path = if let Some(path) = &self.path { format!("path=\"{}\"", encode_attribute(path.as_str())) } else { "".to_string() }; + if self.is_root { let language = self .config @@ -123,10 +135,16 @@ impl ToHtml for Document { writer.write(path)?; writer.write("/>".to_string())?; writer.write("".to_string())?; + if let Some(data) = std::mem::replace(&mut mathjax.lock().unwrap().data, None) { writer.write("".to_string())?; + } else { + writer.write(format!( + "", + MATHJAX_URL + ))?; } writer.write("".to_string())?; + } else { + writer.write("".to_string())?; } } writer.write("
".to_string())?; diff --git a/src/parser/mod.rs b/src/parser/mod.rs index aad548b..e35a7ab 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -58,6 +58,7 @@ impl ParserOptions { self } + /// If external sources should be cached when after downloaded pub fn use_cache(self, value: bool) -> Self { self.document.downloads.lock().unwrap().use_cache = value; diff --git a/src/references/configuration/keys.rs b/src/references/configuration/keys.rs index 31de74b..0c934ce 100644 --- a/src/references/configuration/keys.rs +++ b/src/references/configuration/keys.rs @@ -5,3 +5,4 @@ pub const IMP_IGNORE: &str = "ignored-imports"; pub const IMP_STYLESHEETS: &str = "included-stylesheets"; pub const IMP_CONFIGS: &str = "included-configs"; pub const IMP_BIBLIOGRAPHY: &str = "included-bibliography"; +pub const EMBED_EXTERNAL: &str = "embed-external";