Skip to content

Instantly share code, notes, and snippets.

@stefanpejcic
Created January 24, 2024 23:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stefanpejcic/b6661068ab7af90cd4d21f0c51b2d707 to your computer and use it in GitHub Desktop.
Save stefanpejcic/b6661068ab7af90cd4d21f0c51b2d707 to your computer and use it in GitHub Desktop.
import os
import sys
from htmlmin import minify
from css_html_js_minify import process_single_html_file
def minify_files(input_dir, output_dir):
if not os.path.exists(output_dir):
os.makedirs(output_dir)
for root, dirs, files in os.walk(input_dir):
for file in files:
if file.endswith(".html"):
input_path = os.path.join(root, file)
output_path = os.path.join(output_dir, file)
with open(input_path, 'r', encoding='utf-8') as f:
html_content = f.read()
# Minify inline CSS and JS
minified_content = process_single_html_file(html_content)
# Minify HTML
minified_content = minify(minified_content, remove_comments=True, remove_empty_space=True)
with open(output_path, 'w', encoding='utf-8') as f:
f.write(minified_content)
if __name__ == "__main__":
if len(sys.argv) != 2:
print("Usage: python script_name.py path_to_html_folder")
sys.exit(1)
input_folder = sys.argv[1]
output_folder = "templates_min"
minify_files(input_folder, output_folder)
print("Minification completed. Check 'templates_min' folder for the modified templates.")
@stefanpejcic
Copy link
Author

pip install htmlmin css-html-js-minify

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment