mirror of
https://github.com/getzola/zola
synced 2024-12-12 21:32:29 +00:00
parent
aa03a7fec5
commit
1a36c20bd2
6 changed files with 55 additions and 24 deletions
2
Cargo.lock
generated
2
Cargo.lock
generated
|
@ -2423,7 +2423,6 @@ dependencies = [
|
|||
"lazy_static",
|
||||
"library",
|
||||
"link_checker",
|
||||
"minify-html",
|
||||
"rayon",
|
||||
"relative-path",
|
||||
"sass-rs",
|
||||
|
@ -2976,6 +2975,7 @@ version = "0.1.0"
|
|||
dependencies = [
|
||||
"errors",
|
||||
"filetime",
|
||||
"minify-html",
|
||||
"percent-encoding",
|
||||
"serde",
|
||||
"serde_derive",
|
||||
|
|
|
@ -9,7 +9,6 @@ include = ["src/**/*"]
|
|||
tera = "1"
|
||||
glob = "0.3"
|
||||
walkdir = "2"
|
||||
minify-html = "0.3.8"
|
||||
rayon = "1"
|
||||
serde = "1"
|
||||
serde_derive = "1"
|
||||
|
|
|
@ -10,7 +10,6 @@ use std::path::{Path, PathBuf};
|
|||
use std::sync::{Arc, Mutex, RwLock};
|
||||
|
||||
use lazy_static::lazy_static;
|
||||
use minify_html::{with_friendly_error, Cfg};
|
||||
use rayon::prelude::*;
|
||||
use tera::{Context, Tera};
|
||||
use walkdir::{DirEntry, WalkDir};
|
||||
|
@ -25,6 +24,7 @@ use templates::render_redirect_template;
|
|||
use utils::fs::{
|
||||
copy_directory, copy_file_if_needed, create_directory, create_file, ensure_directory_exists,
|
||||
};
|
||||
use utils::minify;
|
||||
use utils::net::get_available_port;
|
||||
use utils::templates::render_template;
|
||||
|
||||
|
@ -490,26 +490,6 @@ impl Site {
|
|||
html
|
||||
}
|
||||
|
||||
/// Minifies html content
|
||||
fn minify(&self, html: String) -> Result<String> {
|
||||
let cfg = &Cfg { minify_js: false };
|
||||
let mut input_bytes = html.as_bytes().to_vec();
|
||||
match with_friendly_error(&mut input_bytes, cfg) {
|
||||
Ok(_len) => match std::str::from_utf8(&input_bytes) {
|
||||
Ok(result) => Ok(result.to_string()),
|
||||
Err(err) => bail!("Failed to convert bytes to string : {}", err),
|
||||
},
|
||||
Err(minify_error) => {
|
||||
bail!(
|
||||
"Failed to truncate html at character {}: {} \n {}",
|
||||
minify_error.position,
|
||||
minify_error.message,
|
||||
minify_error.code_context
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Copy the main `static` folder and the theme `static` folder if a theme is used
|
||||
pub fn copy_static_directories(&self) -> Result<()> {
|
||||
// The user files will overwrite the theme files
|
||||
|
@ -581,7 +561,7 @@ impl Site {
|
|||
let final_content = if !filename.ends_with("html") || !self.config.minify_html {
|
||||
content
|
||||
} else {
|
||||
match self.minify(content) {
|
||||
match minify::html(content) {
|
||||
Ok(minified_content) => minified_content,
|
||||
Err(error) => bail!(error),
|
||||
}
|
||||
|
|
|
@ -15,6 +15,7 @@ serde_derive = "1"
|
|||
slug = "0.1"
|
||||
percent-encoding = "2"
|
||||
filetime = "0.2.12"
|
||||
minify-html = "0.3.8"
|
||||
|
||||
errors = { path = "../errors" }
|
||||
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
pub mod de;
|
||||
pub mod fs;
|
||||
pub mod minify;
|
||||
pub mod net;
|
||||
pub mod site;
|
||||
pub mod slugs;
|
||||
|
|
50
components/utils/src/minify.rs
Normal file
50
components/utils/src/minify.rs
Normal file
|
@ -0,0 +1,50 @@
|
|||
use errors::{bail, Result};
|
||||
use minify_html::{with_friendly_error, Cfg};
|
||||
|
||||
pub fn html(html: String) -> Result<String> {
|
||||
let cfg = &Cfg { minify_js: false };
|
||||
let mut input_bytes = html.as_bytes().to_vec();
|
||||
|
||||
match with_friendly_error(&mut input_bytes, cfg) {
|
||||
Ok(len) => match std::str::from_utf8(&input_bytes) {
|
||||
Ok(result) => Ok(result[..len].to_string()),
|
||||
Err(err) => bail!("Failed to convert bytes to string : {}", err),
|
||||
},
|
||||
Err(minify_error) => {
|
||||
bail!(
|
||||
"Failed to truncate html at character {}: {} \n {}",
|
||||
minify_error.position,
|
||||
minify_error.message,
|
||||
minify_error.code_context
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
// https://github.com/getzola/zola/issues/1292
|
||||
#[test]
|
||||
fn can_minify_html() {
|
||||
let input = r#"
|
||||
<!doctype html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
</head>
|
||||
<body>
|
||||
|
||||
|
||||
<p>Example blog post</p>
|
||||
|
||||
FOO BAR
|
||||
</body>
|
||||
</html>
|
||||
"#;
|
||||
let expected = r#"<!doctype html><html><head><meta charset=utf-8><body><p>Example blog post</p> FOO BAR"#;
|
||||
let res = html(input.to_owned()).unwrap();
|
||||
assert_eq!(res, expected);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue