minify-html/python/src/lib.template.rs

54 lines
1.5 KiB
Rust
Raw Normal View History

2021-08-07 05:52:38 -04:00
use minify_html::{Cfg, minify as minify_html_native};
use pyo3::prelude::*;
use pyo3::wrap_pyfunction;
2021-08-07 05:52:38 -04:00
use std::string::String;
2021-08-07 05:52:38 -04:00
#[pyfunction(
py_args = "*",
do_not_minify_doctype = "false",
ensure_spec_compliant_unquoted_attribute_values = "false",
keep_closing_tags = "false",
keep_comments = "false",
keep_html_and_head_opening_tags = "false",
keep_spaces_between_attributes = "false",
minify_css = "false",
minify_js = "false",
remove_bangs = "false",
remove_processing_instructions = "false",
2021-08-07 05:52:38 -04:00
)]
fn minify(
code: String,
do_not_minify_doctype: bool,
ensure_spec_compliant_unquoted_attribute_values: bool,
2021-08-07 05:52:38 -04:00
keep_closing_tags: bool,
keep_comments: bool,
keep_html_and_head_opening_tags: bool,
keep_spaces_between_attributes: bool,
minify_css: bool,
minify_js: bool,
remove_bangs: bool,
remove_processing_instructions: bool,
) -> PyResult<String> {
let code = code.into_bytes();
let out_code = minify_html_native(&code, &Cfg {
do_not_minify_doctype,
ensure_spec_compliant_unquoted_attribute_values,
2021-08-07 05:52:38 -04:00
keep_closing_tags,
keep_comments,
keep_html_and_head_opening_tags,
keep_spaces_between_attributes,
2021-01-07 08:26:02 -05:00
minify_css,
2021-08-07 05:52:38 -04:00
minify_js,
remove_bangs,
remove_processing_instructions,
});
Ok(String::from_utf8(out_code).unwrap())
}
#[pymodule]
2021-04-07 06:50:05 -04:00
fn REPLACE_WITH_MODULE_NAME(_py: Python, m: &PyModule) -> PyResult<()> {
m.add_wrapped(wrap_pyfunction!(minify))?;
Ok(())
}