minify-html/rust/main/src/lib.rs

44 lines
1.1 KiB
Rust
Raw Normal View History

2021-08-06 09:07:55 -04:00
pub use crate::cfg::Cfg;
2021-08-06 02:17:45 -04:00
use crate::minify::content::minify_content;
use crate::parse::content::parse_content;
2021-08-06 02:19:36 -04:00
use crate::parse::Code;
2021-08-06 02:17:45 -04:00
use crate::spec::tag::ns::Namespace;
use crate::spec::tag::EMPTY_SLICE;
2019-12-25 04:44:51 -05:00
2021-08-05 22:07:27 -04:00
mod ast;
mod cfg;
2020-06-19 03:58:16 -04:00
mod gen;
2021-08-06 02:17:45 -04:00
mod minify;
2021-08-05 22:07:27 -04:00
mod parse;
mod pattern;
mod spec;
2021-08-06 03:54:23 -04:00
#[cfg(test)]
2020-07-10 06:40:33 -04:00
mod tests;
mod whitespace;
2021-08-07 05:02:09 -04:00
/// Minifies UTF-8 HTML code, represented as an array of bytes.
2020-08-24 10:36:27 -04:00
///
/// # Arguments
///
/// * `code` - A slice of bytes representing the source code to minify.
/// * `cfg` - Configuration object to adjust minification approach.
///
/// # Examples
///
/// ```
2021-08-05 22:07:27 -04:00
/// use minify_html::{Cfg, minify};
2020-08-24 10:36:27 -04:00
///
/// let mut code: &[u8] = b"<p> Hello, world! </p>";
2021-08-07 00:56:20 -04:00
/// let mut cfg = Cfg::new();
/// cfg.keep_comments = true;
/// let minified = minify(&code, &cfg);
2021-08-05 22:07:27 -04:00
/// assert_eq!(minified, b"<p>Hello, world!".to_vec());
2020-08-24 10:36:27 -04:00
/// ```
2021-08-06 02:17:45 -04:00
pub fn minify(src: &[u8], cfg: &Cfg) -> Vec<u8> {
let mut code = Code::new(src);
let parsed = parse_content(&mut code, Namespace::Html, EMPTY_SLICE, EMPTY_SLICE);
2021-08-06 02:17:45 -04:00
let mut out = Vec::with_capacity(src.len());
minify_content(cfg, &mut out, false, EMPTY_SLICE, parsed.children);
2021-08-06 02:17:45 -04:00
out
}