From 15876389e94e61be4cc93c09879d5bf0fc143f22 Mon Sep 17 00:00:00 2001 From: PJ568 Date: Mon, 16 Dec 2024 19:54:26 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E6=96=B0=E5=A2=9E=E3=80=91=E7=BD=91?= =?UTF-8?q?=E9=A1=B5=E5=8E=8B=E7=BC=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 2 ++ Cargo.toml | 9 +++++++ index.html | 2 +- index.min.html | 1 + scripts/python/gen_min_html.py | 24 ++++++++++++++++++ scripts/python/requirements.txt | 1 + src/main.rs | 44 +++++++++++++++++++++++++++++++++ 7 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 .gitignore create mode 100644 Cargo.toml create mode 100644 index.min.html create mode 100644 scripts/python/gen_min_html.py create mode 100644 scripts/python/requirements.txt create mode 100644 src/main.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2ebc5ea --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/target +/Cargo.lock \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..7a6a46c --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "markdown-html" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +minify-html = "0.15.0" diff --git a/index.html b/index.html index ed434df..8805cd2 100644 --- a/index.html +++ b/index.html @@ -1,7 +1,7 @@ - diff --git a/index.min.html b/index.min.html new file mode 100644 index 0000000..2ebea3c --- /dev/null +++ b/index.min.html @@ -0,0 +1 @@ +加载中

加载中……

请稍等。

\ No newline at end of file diff --git a/scripts/python/gen_min_html.py b/scripts/python/gen_min_html.py new file mode 100644 index 0000000..a92c325 --- /dev/null +++ b/scripts/python/gen_min_html.py @@ -0,0 +1,24 @@ +from htmlmin import minify +import sys + +def conpress(file_path, target_path): + # 读取 HTML 文件 + with open(file_path, 'r', encoding='utf-8') as f: + # 写入压缩后的 HTML 文件 + with open(target_path, 'w', encoding='utf-8') as t: + t.write(minify(f.read(), remove_comments=True, remove_empty_space=True)) + +if __name__ == "__main__": + """ + 压缩指定的 HTML 文件。 + + :param path/to/file: 欲读取的源文件路径 + :param path/to/file: 欲写入的目标文件路径 + """ + if len(sys.argv) != 3: + print("使用方法:python gen_min_html.py <源文件路径> <目标文件路径>") + sys.exit(1) + + file_path = sys.argv[1] + target_path = sys.argv[2] + conpress(file_path, target_path) \ No newline at end of file diff --git a/scripts/python/requirements.txt b/scripts/python/requirements.txt new file mode 100644 index 0000000..a73bb8f --- /dev/null +++ b/scripts/python/requirements.txt @@ -0,0 +1 @@ +htmlmin==0.1.12 \ No newline at end of file diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..0ae4adc --- /dev/null +++ b/src/main.rs @@ -0,0 +1,44 @@ +use minify_html::{minify, Cfg}; +use std::fs::write; + +fn main() { + if std::env::args().len() < 3 { + eprintln!("Usage: {} ", std::env::args().next().unwrap()); + return; + } + + let input_path = std::env::args().nth(1).expect("Expected a path to an HTML file"); + let output_path = std::env::args().nth(2).expect("Expected an output file path"); + + let code = match std::fs::read(input_path) { + Ok(data) => data, + Err(e) => { + eprintln!("Error reading file: {}", e); + return; + } + }; + + let mut cfg = Cfg::new(); + cfg.do_not_minify_doctype = true; + cfg.ensure_spec_compliant_unquoted_attribute_values = true; + cfg.keep_closing_tags = true; + cfg.keep_html_and_head_opening_tags = true; + cfg.keep_spaces_between_attributes = true; + cfg.keep_comments = false; + cfg.keep_input_type_text_attr = true; + cfg.keep_ssi_comments = false; + // cfg.preserve_brace_template_syntax = false; + // cfg.preserve_chevron_percent_template_syntax = false; + cfg.minify_css = true; + cfg.minify_js = true; + cfg.remove_bangs = false; + cfg.remove_processing_instructions = false; + + let minified = minify(&code, &cfg); + + if let Err(e) = write(&output_path, minified) { + eprintln!("Error writing to file: {}", e); + } else { + println!("Minified HTML written to {}", output_path); + } +} \ No newline at end of file