2020-07-11 11:29:34 -04:00
|
|
|
use minify_html::{Cfg, in_place as minify_html_native};
|
2020-01-19 00:50:21 -05:00
|
|
|
use pyo3::prelude::*;
|
|
|
|
use pyo3::exceptions::SyntaxError;
|
|
|
|
use pyo3::wrap_pyfunction;
|
|
|
|
use std::str::from_utf8_unchecked;
|
|
|
|
|
2020-07-10 11:15:56 -04:00
|
|
|
#[pyfunction(py_args="*", minify_js="false")]
|
|
|
|
fn minify(code: String, minify_js: bool) -> PyResult<String> {
|
2020-01-19 00:50:21 -05:00
|
|
|
let mut code = code.into_bytes();
|
2020-07-11 11:29:34 -04:00
|
|
|
match minify_html_native(&mut code, &Cfg {
|
2020-07-10 11:15:56 -04:00
|
|
|
minify_js,
|
|
|
|
}) {
|
2020-01-19 00:50:21 -05:00
|
|
|
Ok(out_len) => Ok(unsafe { from_utf8_unchecked(&code[0..out_len]).to_string() }),
|
|
|
|
Err((err, pos)) => Err(SyntaxError::py_err(format!("{} [Character {}]", err.message(), pos))),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[pymodule]
|
2020-07-11 10:47:17 -04:00
|
|
|
fn minify_html(_py: Python, m: &PyModule) -> PyResult<()> {
|
2020-01-19 00:50:21 -05:00
|
|
|
m.add_wrapped(wrap_pyfunction!(minify))?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|