This library can help you generate and minify your HTML code at the same time. It also supports to minify JS and CSS in <style>, <script> elements, and ignores the minification of <pre>, <code> and <textarea> elements.
HTML is minified by the following rules:
'\x20' or a single '\n', if possible.<input readonly=""> => <input readonly> )<pre><textarea><code> (optionally, minified by default)<style> (if the type attribute is unsupported)<script> (if the type attribute is unsupported)<script> and <style> elements are minified by minifier.The original (non-minified) HTML doesn't need to be completely generated before using this library because this library doesn't do any deserialization to create DOMs.
```rust use html_minifier::HTMLMinifier;
let mut html_minifier = HTMLMinifier::new();
htmlminifier.digest(" minifier.digest(" ").unwrap(); html_minifier.digest("
123456
<b>big</b> 789
ab
c
中文
字
").unwrap(); html_minifier.digest("
asserteq!(" 123456 big 789 ab c 中文 字 ".asbytes(), htmlminifier.gethtml()); ```
```rust use html_minifier::HTMLMinifier;
let mut html_minifier = HTMLMinifier::new();
html_minifier.digest("
asserteq!(b"
Hello world!", htmlminifier.get_html()); ``````rust use html_minifier::HTMLMinifier;
let mut html_minifier = HTMLMinifier::new();
html_minifier.digest("").unwrap();
asserteq!("".asbytes(), htmlminifier.gethtml()); ```
Write HTML to a Writer
If you don't want to store your HTML in memory (e.g. writing to a file instead), you can use the
HTMLMinifierHelperstruct which provides a low-level API that allows you to pass your output instance when invoking thedigestmethod.```rust use html_minifier::HTMLMinifierHelper;
use std::fs::File; use std::io::Read;
let mut inputfile = File::open("tests/data/w3schools.comtryhowcssexamplewebsite.htm").unwrap(); let mut outputfile = File::create("tests/data/index.min.html").unwrap();
let mut buffer = [0u8; 256];
let mut htmlminifierhelper = HTMLMinifierHelper::new();
loop { let c = input_file.read(&mut buffer).unwrap();
if c == 0 { break; } html_minifier_helper.digest(&buffer[..c], &mut output_file).unwrap();} ```
No Std
Disable the default features to compile this crate without std.
toml [dependencies.html-minifier] version = "*" default-features = falseCrates.io
https://crates.io/crates/html-minifier
Documentation
https://docs.rs/html-minifier
License