Quellcode durchsuchen

【新增】网页压缩

PJ568 vor 1 Jahr
Ursprung
Commit
ed503a93f6
7 geänderte Dateien mit 81 neuen und 1 gelöschten Zeilen
  1. 2 0
      .gitignore
  2. 9 0
      Cargo.toml
  3. 1 1
      index.html
  4. 0 0
      index.min.html
  5. 24 0
      scripts/python/gen_min_html.py
  6. 1 0
      scripts/python/requirements.txt
  7. 44 0
      src/main.rs

+ 2 - 0
.gitignore

@@ -0,0 +1,2 @@
+/target
+/Cargo.lock

+ 9 - 0
Cargo.toml

@@ -0,0 +1,9 @@
+[package]
+name = "markdown-html"
+version = "1.5.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"

+ 1 - 1
index.html

@@ -1,7 +1,7 @@
 <!doctype html>
 
 <html>
-<markdown-html version="1.4.2" author="PJ568" repo="https://github.com/PJ-568/markdown.html"
+<markdown-html version="1.5.0" author="PJ568" repo="https://github.com/PJ-568/markdown.html"
   license="CC BY-SA 4.0 International"></markdown-html>
 
 <head>

Datei-Diff unterdrückt, da er zu groß ist
+ 0 - 0
index.min.html


+ 24 - 0
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)

+ 1 - 0
scripts/python/requirements.txt

@@ -0,0 +1 @@
+htmlmin==0.1.12

+ 44 - 0
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: {} <path_to_html_file> <output_path>", 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);
+    }
+}

Einige Dateien werden nicht angezeigt, da zu viele Dateien in diesem Diff geändert wurden.