about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorUltraQbik <no1skill@yandex.ru>2024-08-27 15:44:19 +0300
committerUltraQbik <no1skill@yandex.ru>2024-08-27 15:44:19 +0300
commitfdda4950d518e4ddae8a548f8ff6c8b0d4cba41a (patch)
tree8471fb2b1f657b848507425d74b12bbb8ac018f2 /src
parent3263cfdee15391995532545df84b8e4144684879 (diff)
downloadhttpy-fdda4950d518e4ddae8a548f8ff6c8b0d4cba41a.tar.gz
httpy-fdda4950d518e4ddae8a548f8ff6c8b0d4cba41a.zip
wut?
Diffstat (limited to 'src')
-rw-r--r--src/file_man.py10
-rw-r--r--src/minimizer.py65
2 files changed, 7 insertions, 68 deletions
diff --git a/src/file_man.py b/src/file_man.py
index 3371dd2..1759338 100644
--- a/src/file_man.py
+++ b/src/file_man.py
@@ -80,7 +80,7 @@ def compress_path_map(path_map: dict[str, dict[str, Any]], path_prefix: str = "c
     """
 
     import brotli
-    from src.minimizer import minimize_html
+    import htmlmin
     if not os.path.exists(path_prefix):
         os.mkdir(path_prefix)
     for val in path_map.values():
@@ -95,8 +95,12 @@ def compress_path_map(path_map: dict[str, dict[str, Any]], path_prefix: str = "c
                 with open(filepath, "wb") as comp:
                     with open(val["path"], "rb") as file:
                         comp.write(
-                            brotli.compress(minimize_html(file.read()))
-                        )
+                            brotli.compress(htmlmin.minify(
+                                file.read().decode("utf-8"),
+                                remove_comments=True,
+                                remove_empty_space=True,
+                                remove_all_empty_space=True,
+                                reduce_boolean_attributes=True).encode("utf-8")))
             else:
                 with open(filepath, "wb") as comp:
                     br = brotli.Compressor()
diff --git a/src/minimizer.py b/src/minimizer.py
deleted file mode 100644
index af1c015..0000000
--- a/src/minimizer.py
+++ /dev/null
@@ -1,65 +0,0 @@
-import re
-# import htmlmin
-
-
-def minimize_html(html: bytes) -> bytes:
-    """
-    Minimizes HTML files.
-    Slightly better than htmlmin for my files,
-    but maybe I break something in process and I don't notice
-    """
-
-    html = bytearray(html)
-
-    # remove newlines
-    html = (html
-            .replace(b'\r', b'')
-            .replace(b'\n', b''))
-
-    # remove double spaces
-    size = len(html)
-    while True:
-        html = html.replace(b'  ', b'')
-
-        # if nothing changes -> break
-        if size == len(html):
-            break
-        size = len(html)
-
-    # simplify '> <' to '><'
-    html = html.replace(b'> <', b'><')
-
-    # remove unnecessary quotes
-    index = 0
-    for tag in re.findall(r"<.*?>", html.decode("utf8")):
-        index = html.find(tag.encode("utf8"), index)
-        processed = (tag
-                     .replace("\"", "")
-                     .replace(": ", ":")
-                     .replace("; ", ";"))
-        if len(processed) < len(tag):
-            html[index:index+len(tag)] = (html[index:index+len(tag)]
-                                          .replace(tag.encode("utf8"), processed.encode("utf8"), 1))
-
-    return html
-
-
-def test():
-    with open("../www/about.html", "rb") as file:
-        original = file.read()
-
-    processed = minimize_html(original)
-
-    print(f"Original : {len(original)}\n"
-          f"Processed: {len(processed)}\n"
-          f"Rate     : {(1 - len(processed) / len(original)) * 100:.2f}%", end="\n\n")
-
-    # processed = htmlmin.minify(original.decode("utf8"), True, True, True, True, True)
-    #
-    # print(f"Original : {len(original)}\n"
-    #       f"Processed: {len(processed)}\n"
-    #       f"Rate     : {(1 - len(processed) / len(original)) * 100:.2f}%")
-
-
-if __name__ == '__main__':
-    test()