Change images to be embedded as base64

feature/epub-rendering
trivernis 4 years ago
parent 8715ca7b41
commit 038a9ef554

841
Cargo.lock generated

File diff suppressed because it is too large Load Diff

@ -1,6 +1,6 @@
[package]
name = "snekdown"
version = "0.20.0"
version = "0.21.0"
authors = ["trivernis <trivernis@protonmail.com>"]
edition = "2018"
license-file = "LICENSE"
@ -32,4 +32,9 @@ notify = "4.0.12"
toml = "0.5.6"
serde ="1.0.111"
serde_derive = "1.0.111"
asciimath-rs = "0.4.8"
asciimath-rs = "0.4.8"
reqwest = {version = "0.10", features=["blocking"]}
mime_guess = "2.0.3"
mime = "0.3.16"
base64 = "0.12.3"
rayon = "1.3.1"

@ -45,6 +45,7 @@ Extended syntax with metadata and no description
!(url)[metadata]
```
When generating the html file the images are base64 embedded.
### Quotes
@ -68,6 +69,10 @@ Imports are parsed via multithreading.
```md
<[path]
<[document.md]
<[style.css]
```

@ -6,6 +6,8 @@ use crate::references::placeholders::ProcessPlaceholders;
use crate::references::templates::{Template, TemplateVariable};
use asciimath_rs::elements::special::Expression;
use std::collections::HashMap;
use std::fs::read;
use std::path::PathBuf;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, RwLock};
@ -615,3 +617,30 @@ impl Metadata for InlineMetadata {
}
}
}
impl Image {
pub fn get_content(&self) -> Option<Vec<u8>> {
let path = PathBuf::from(&self.url.url);
if path.exists() {
if let Ok(content) = read(path) {
Some(content)
} else {
None
}
} else {
self.download_content()
}
}
fn download_content(&self) -> Option<Vec<u8>> {
if let Ok(content) = reqwest::blocking::get(&self.url.url) {
if let Ok(bytes) = content.bytes() {
Some(bytes.to_vec())
} else {
None
}
} else {
None
}
}
}

@ -6,6 +6,7 @@ use crate::references::templates::{Template, TemplateVariable};
use asciimath_rs::format::mathml::ToMathML;
use htmlescape::{encode_attribute, encode_minimal};
use minify::html::minify;
use rayon::prelude::*;
use std::cell::RefCell;
use syntect::highlighting::ThemeSet;
use syntect::html::highlighted_html_for_string;
@ -105,8 +106,15 @@ impl ToHtml for Document {
fn to_html(&self) -> String {
let inner = self
.elements
.iter()
.fold("".to_string(), |a, b| format!("{}{}", a, b.to_html()));
.par_iter()
.fold(|| String::new(), |a, b| format!("{}{}", a, b.to_html()))
.reduce(
|| String::new(),
|mut a: String, b: String| {
a.push_str(&b);
a
},
);
let path = if let Some(path) = &self.path {
format!("path='{}'", encode_attribute(path.as_str()))
} else {
@ -334,6 +342,17 @@ impl ToHtml for TextLine {
impl ToHtml for Image {
fn to_html(&self) -> String {
let mut style = String::new();
let url = if let Some(content) = self.get_content() {
let mime_type = mime_guess::from_path(&self.url.url).first_or(mime::IMAGE_PNG);
format!(
"data:{};base64,{}",
mime_type.to_string(),
base64::encode(content)
)
} else {
encode_attribute(self.url.url.as_str())
};
if let Some(meta) = &self.metadata {
if let Some(width) = meta.data.get("width") {
style = format!("{}width: {};", style, width.to_html())
@ -347,11 +366,12 @@ impl ToHtml for Image {
format!(
"<div class='figure'>\
<a href={0}>\
<img src='{0}' alt='{1}' style='{2}'/>\
<img src='{1}' alt='{2}' style='{3}'/>\
</a>\
<label class='imageDescription'>{1}</label>\
<label class='imageDescription'>{2}</label>\
</div>",
encode_attribute(self.url.url.clone().as_str()),
encode_attribute(self.url.url.as_str()),
url,
encode_attribute(
description
.iter()
@ -363,11 +383,7 @@ impl ToHtml for Image {
.as_str(),
)
} else {
format!(
"<a href={0}><img src='{0}' style='{1}'/></a>",
self.url.url.clone(),
style
)
format!("<a href={0}><img src='{0}' style='{1}'/></a>", url, style)
}
}
}

Loading…
Cancel
Save