Repository navigation
Expand file tree
/
Copy pathlib.rs
More file actions
59 lines (51 loc) · 2.24 KB
/
Copy pathlib.rs
File metadata and controls
59 lines (51 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
use minify_html::{minify as minify_html_native, Cfg};
use rutie::{class, methods, Boolean, Class, Hash, Object, RString, Symbol, VM};
use std::str::from_utf8;
macro_rules! get_cfg_hash_prop {
($cfg_hash:ident, $prop:literal) => {
$cfg_hash
.at(&Symbol::new($prop))
.try_convert_to::<Boolean>()
.map_or(false, |v| v.to_bool())
};
}
class!(MinifyHtml);
methods! {
MinifyHtml,
_itself,
fn minify(source: RString, cfg_hash: Hash) -> RString {
let code = source
.map_err(|e| VM::raise_ex(e) )
.unwrap()
.to_string()
.into_bytes();
let cfg_hash = cfg_hash
.map_err(|e| VM::raise_ex(e) )
.unwrap();
let cfg = &Cfg {
do_not_minify_doctype: get_cfg_hash_prop!(cfg_hash, "do_not_minify_doctype"),
ensure_spec_compliant_unquoted_attribute_values: get_cfg_hash_prop!(cfg_hash, "ensure_spec_compliant_unquoted_attribute_values"),
keep_closing_tags: get_cfg_hash_prop!(cfg_hash, "keep_closing_tags"),
keep_comments: get_cfg_hash_prop!(cfg_hash, "keep_comments"),
keep_html_and_head_opening_tags: get_cfg_hash_prop!(cfg_hash, "keep_html_and_head_opening_tags"),
keep_spaces_between_attributes: get_cfg_hash_prop!(cfg_hash, "keep_spaces_between_attributes"),
minify_css: get_cfg_hash_prop!(cfg_hash, "minify_css"),
minify_css_level_1: get_cfg_hash_prop!(cfg_hash, "minify_css_level_1"),
minify_css_level_2: get_cfg_hash_prop!(cfg_hash, "minify_css_level_2"),
minify_css_level_3: get_cfg_hash_prop!(cfg_hash, "minify_css_level_3"),
minify_js: get_cfg_hash_prop!(cfg_hash, "minify_js"),
remove_bangs: get_cfg_hash_prop!(cfg_hash, "remove_bangs"),
remove_processing_instructions: get_cfg_hash_prop!(cfg_hash, "remove_processing_instructions"),
};
let out_code = minify_html_native(&code, cfg);
let out_str = from_utf8(&out_code).unwrap();
RString::new_utf8(out_str)
}
}
#[allow(non_snake_case)]
#[no_mangle]
pub extern "C" fn Init_minify_html() {
Class::new("MinifyHtml", None).define(|itself| {
itself.def_self("minify", minify);
});
}