1
0

main.rs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. use minify_html::{minify, Cfg};
  2. use std::fs::write;
  3. fn main() {
  4. if std::env::args().len() < 3 {
  5. eprintln!("Usage: {} <path_to_html_file> <output_path>", std::env::args().next().unwrap());
  6. return;
  7. }
  8. let input_path = std::env::args().nth(1).expect("Expected a path to an HTML file");
  9. let output_path = std::env::args().nth(2).expect("Expected an output file path");
  10. let code = match std::fs::read(input_path) {
  11. Ok(data) => data,
  12. Err(e) => {
  13. eprintln!("Error reading file: {}", e);
  14. return;
  15. }
  16. };
  17. let mut cfg = Cfg::new();
  18. cfg.do_not_minify_doctype = true;
  19. cfg.ensure_spec_compliant_unquoted_attribute_values = true;
  20. cfg.keep_closing_tags = true;
  21. cfg.keep_html_and_head_opening_tags = true;
  22. cfg.keep_spaces_between_attributes = true;
  23. cfg.keep_comments = false;
  24. cfg.keep_input_type_text_attr = true;
  25. cfg.keep_ssi_comments = false;
  26. // cfg.preserve_brace_template_syntax = false;
  27. // cfg.preserve_chevron_percent_template_syntax = false;
  28. cfg.minify_css = true;
  29. cfg.minify_js = true;
  30. cfg.remove_bangs = false;
  31. cfg.remove_processing_instructions = false;
  32. let minified = minify(&code, &cfg);
  33. if let Err(e) = write(&output_path, minified) {
  34. eprintln!("Error writing to file: {}", e);
  35. } else {
  36. println!("Minified HTML written to {}", output_path);
  37. }
  38. }