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

25 lines
847 B
Rust
Raw Normal View History

use minify_html::{Cfg, Error, in_place as minify_html_native};
use pyo3::prelude::*;
2021-01-06 00:16:40 -05:00
use pyo3::exceptions::PySyntaxError;
use pyo3::wrap_pyfunction;
use std::str::from_utf8_unchecked;
2021-01-07 08:26:02 -05:00
#[pyfunction(py_args="*", minify_js="false", minify_css="false")]
fn minify(code: String, minify_js: bool, minify_css: bool) -> PyResult<String> {
let mut code = code.into_bytes();
2020-07-11 11:29:34 -04:00
match minify_html_native(&mut code, &Cfg {
minify_js,
2021-01-07 08:26:02 -05:00
minify_css,
}) {
Ok(out_len) => Ok(unsafe { from_utf8_unchecked(&code[0..out_len]).to_string() }),
2021-01-06 00:16:40 -05:00
Err(Error { error_type, position }) => Err(PySyntaxError::new_err(format!("{} [Character {}]", error_type.message(), position))),
}
}
#[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(())
}