From 693a371efa057d700c0391caa9bbcc755058b083 Mon Sep 17 00:00:00 2001 From: Huck Boles Date: Fri, 2 Jun 2023 17:05:29 -0500 Subject: [PATCH] added: html minifier --- Cargo.lock | 168 ++++++++++++++++++++++++++++----- Cargo.toml | 1 + files/README/source/index.meta | 1 + src/metafile/dir/node.rs | 19 +++- src/tests.rs | 4 +- 5 files changed, 169 insertions(+), 24 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 72cbf58..1f37171 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,15 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "aho-corasick" +version = "0.7.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cc936419f96fa211c1b9166887b38e5e40b19958e5b895be7c1f93adec7071ac" +dependencies = [ + "memchr", +] + [[package]] name = "anes" version = "0.1.6" @@ -154,9 +163,9 @@ dependencies = [ [[package]] name = "clap" -version = "4.3.0" +version = "4.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "93aae7a4192245f70fe75dd9157fc7b4a5bf53e88d30bd4396f7d8f9284d5acc" +checksum = "b4ed2379f8603fa2b7509891660e802b88c70a79a6427a70abb5968054de2c28" dependencies = [ "clap_builder", "clap_derive", @@ -165,9 +174,9 @@ dependencies = [ [[package]] name = "clap_builder" -version = "4.3.0" +version = "4.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4f423e341edefb78c9caba2d9c7f7687d0e72e89df3ce3394554754393ac3990" +checksum = "72394f3339a76daf211e57d4bcb374410f3965dcc606dd0e03738c7888766980" dependencies = [ "anstream", "anstyle", @@ -178,14 +187,14 @@ dependencies = [ [[package]] name = "clap_derive" -version = "4.3.0" +version = "4.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "191d9573962933b4027f932c600cd252ce27a8ad5979418fe78e43c07996f27b" +checksum = "59e9ef9a08ee1c0e1f2e162121665ac45ac3783b0f897db7244ae75ad9a8f65b" dependencies = [ "heck", "proc-macro2", "quote", - "syn", + "syn 2.0.18", ] [[package]] @@ -209,6 +218,12 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7" +[[package]] +name = "convert_case" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6245d59a3e82a7fc217c5828a6692dbc6dfb63a0c8c90495621f7b9d79704a0e" + [[package]] name = "cpufeatures" version = "0.2.7" @@ -307,6 +322,30 @@ dependencies = [ "typenum", ] +[[package]] +name = "css-minify" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "874c6e2d19f8d4a285083b11a3241bfbe01ac3ed85f26e1e6b34888d960552bd" +dependencies = [ + "derive_more", + "indexmap", + "nom", +] + +[[package]] +name = "derive_more" +version = "0.99.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4fb810d30a7c1953f91334de7244731fc3f3c10d7fe163338a35b9f640960321" +dependencies = [ + "convert_case", + "proc-macro2", + "quote", + "rustc_version", + "syn 1.0.109", +] + [[package]] name = "digest" version = "0.10.7" @@ -498,12 +537,15 @@ checksum = "ef53942eb7bf7ff43a617b3e2c1c4a5ecf5944a7c1bc12d7ee39bbb15e5c1519" [[package]] name = "log" -version = "0.4.17" +version = "0.4.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e" -dependencies = [ - "cfg-if", -] +checksum = "518ef76f2f87365916b142844c16d8fefd85039bc5699050210a7778ee1cd1de" + +[[package]] +name = "memchr" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" [[package]] name = "memoffset" @@ -516,11 +558,12 @@ dependencies = [ [[package]] name = "metaforge" -version = "0.1.2" +version = "0.1.3" dependencies = [ - "clap 4.3.0", + "clap 4.3.1", "criterion", "eyre", + "minify-html", "pandoc", "pest", "pest_derive", @@ -528,6 +571,46 @@ dependencies = [ "thiserror", ] +[[package]] +name = "minify-html" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc4d9147754a49e80557df835eb59e743eab1bf75410a134f55dc4b9dbb692ad" +dependencies = [ + "aho-corasick", + "css-minify", + "lazy_static", + "memchr", + "minify-js", + "rustc-hash", +] + +[[package]] +name = "minify-js" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c300f90ba1138b5c5daf5d9441dc9bdc67b808aac22cf638362a2647bc213be4" +dependencies = [ + "lazy_static", + "parse-js", +] + +[[package]] +name = "minimal-lexical" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" + +[[package]] +name = "nom" +version = "7.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a" +dependencies = [ + "memchr", + "minimal-lexical", +] + [[package]] name = "num-traits" version = "0.2.15" @@ -549,9 +632,9 @@ dependencies = [ [[package]] name = "once_cell" -version = "1.17.1" +version = "1.17.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b7e5500299e16ebb147ae15a00a942af264cf3688f47923b8fc2cd5858f23ad3" +checksum = "9670a07f94779e00908f3e686eab508878ebb390ba6e604d3a284c00e8d0487b" [[package]] name = "oorandom" @@ -574,6 +657,17 @@ dependencies = [ "itertools 0.8.2", ] +[[package]] +name = "parse-js" +version = "0.10.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "30534759e6ad87aa144c396544747e1c25b1020bd133356fd758c8facec764e5" +dependencies = [ + "aho-corasick", + "lazy_static", + "memchr", +] + [[package]] name = "pest" version = "2.6.0" @@ -604,7 +698,7 @@ dependencies = [ "pest_meta", "proc-macro2", "quote", - "syn", + "syn 2.0.18", ] [[package]] @@ -701,6 +795,21 @@ version = "0.7.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "436b050e76ed2903236f032a59761c1eb99e1b0aead2c257922771dab1fc8c78" +[[package]] +name = "rustc-hash" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2" + +[[package]] +name = "rustc_version" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bfa0f585226d2e68097d4f95d113b15b83a82e819ab25717ec0590d9584ef366" +dependencies = [ + "semver", +] + [[package]] name = "rustix" version = "0.37.19" @@ -736,6 +845,12 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" +[[package]] +name = "semver" +version = "1.0.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bebd363326d05ec3e2f532ab7660680f3b02130d780c299bca73469d521bc0ed" + [[package]] name = "serde" version = "1.0.163" @@ -753,7 +868,7 @@ checksum = "8c805777e3930c8883389c602315a24224bcc738b63905ef87cd1420353ea93e" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.18", ] [[package]] @@ -784,6 +899,17 @@ version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" +[[package]] +name = "syn" +version = "1.0.109" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + [[package]] name = "syn" version = "2.0.18" @@ -818,7 +944,7 @@ checksum = "f9456a42c5b0d803c8cd86e73dd7cc9edd429499f37a3550d286d5e86720569f" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.18", ] [[package]] @@ -892,7 +1018,7 @@ dependencies = [ "once_cell", "proc-macro2", "quote", - "syn", + "syn 2.0.18", "wasm-bindgen-shared", ] @@ -914,7 +1040,7 @@ checksum = "e128beba882dd1eb6200e1dc92ae6c5dbaa4311aa7bb211ca035779e5efc39f8" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.18", "wasm-bindgen-backend", "wasm-bindgen-shared", ] diff --git a/Cargo.toml b/Cargo.toml index e855186..30560cf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,6 +11,7 @@ eyre = "0.6" pest = "2" pest_derive = "2" rayon = "1.7" +minify-html = "0.11" [dev-dependencies] criterion = "0.4" diff --git a/files/README/source/index.meta b/files/README/source/index.meta index dfbb4bd..fb8a466 100644 --- a/files/README/source/index.meta +++ b/files/README/source/index.meta @@ -26,6 +26,7 @@ you rebuild it. - spaces are correctly preserved between directly adjacent substitutions - copy_only header directive - pandoc header now works on pattern files +- html minification ## versions - 0.1.3: grammar update, new header directives diff --git a/src/metafile/dir/node.rs b/src/metafile/dir/node.rs index 5b878f4..40e3d0c 100644 --- a/src/metafile/dir/node.rs +++ b/src/metafile/dir/node.rs @@ -1,9 +1,26 @@ use crate::{error::*, Options}; use eyre::Result; +use minify_html::{minify, Cfg}; use std::{fs, path::PathBuf}; use super::*; +const HTML_CFG: Cfg = Cfg { + do_not_minify_doctype: false, + ensure_spec_compliant_unquoted_attribute_values: false, + keep_closing_tags: true, + keep_html_and_head_opening_tags: true, + keep_spaces_between_attributes: false, + keep_comments: false, + minify_css: true, + minify_css_level_1: false, + minify_css_level_2: true, + minify_css_level_3: false, + minify_js: true, + remove_bangs: true, + remove_processing_instructions: true, +}; + impl<'a> DirNode<'a> { pub fn build(path: PathBuf, opts: &'a Options) -> Result { assert!(path.is_dir() && path.exists()); @@ -69,7 +86,7 @@ impl<'a> DirNode<'a> { file.merge(&self.global); match file.construct() { Ok(str) => { - fs::write(file.dest()?, str)?; + fs::write(file.dest()?, minify(str.as_bytes(), &HTML_CFG))?; } Err(e) => { // print a line to stderr about failure but continue with other files diff --git a/src/tests.rs b/src/tests.rs index b0086f6..8e69a06 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -189,12 +189,12 @@ fn test_global() -> Result<()> { assert_eq!( fs::read_to_string(dir.join("build/unit_tests/global/pattern.html"))?, - "

GOOD

\n

GOOD

\n\n" + "

GOOD

GOOD

" ); assert_eq!( fs::read_to_string(dir.join("build/unit_tests/global/variable.html"))?, - "

GOOD GOOD

\n\n" + "

GOOD GOOD

" ); Ok(()) -- 2.45.2