{"id":4039,"library":"htmlmin","title":"HTML Minifier","description":"Htmlmin is a configurable Python library designed for minifying HTML, offering safety features and various options to control the minification process. It removes extra whitespace, comments, and other unneeded characters to reduce page size and improve load times. The current version is 0.1.12, released in December 2017. The original project appears to be in maintenance mode or largely inactive, leading to the creation of several forks (e.g., `htmlmin2`, `htmlmin4`) to address compatibility with newer Python versions and continued development.","status":"maintenance","version":"0.1.12","language":"en","source_language":"en","source_url":"https://github.com/mankyd/htmlmin","tags":["html","minifier","web","frontend","optimization"],"install":[{"cmd":"pip install htmlmin","lang":"bash","label":"Install `htmlmin`"}],"dependencies":[],"imports":[{"note":"The `minify` function is a direct entry point for one-off minification.","symbol":"minify","correct":"import htmlmin\nhtmlmin.minify(html_string)"},{"note":"Use the `Minifier` class for persistent options and more efficient repeated minification.","symbol":"Minifier","correct":"from htmlmin import Minifier\nminifier = Minifier()\nminified_html = minifier.minify(html_string)"}],"quickstart":{"code":"import htmlmin\n\ninput_html = '''\n<html>\n    <head>\n        <title> My Page </title>\n    </head>\n    <body>\n        <!-- This is a comment -->\n        <h1> Welcome! </h1>\n        <p> This is some      text. </p>\n        <pre> Preformatted  text </pre>\n    </body>\n</html>\n'''\n\nminified_html = htmlmin.minify(\n    input_html,\n    remove_comments=True,\n    remove_empty_space=True\n)\n\nprint(minified_html)\n# Expected output: <html><head><title>My Page</title></head><body><h1>Welcome!</h1><p>This is some text.</p><pre> Preformatted  text </pre></body></html>","lang":"python","description":"This example demonstrates how to use the `htmlmin.minify` function to minify a basic HTML string, removing comments and empty spaces. The `pre` tag content is preserved by default."},"warnings":[{"fix":"Consider using a maintained fork like `htmlmin2` or `htmlmin4` which address this compatibility issue, or port your code to use an alternative, actively maintained HTML minification library.","message":"The `htmlmin` library implicitly relies on Python's built-in `cgi` module, which was deprecated in Python 3.11 and removed in Python 3.13. Attempting to use `htmlmin` on Python 3.13 or newer will result in a `ModuleNotFoundError`.","severity":"breaking","affected_versions":"Python >= 3.13"},{"fix":"Use `remove_empty_space=True` (which is safer as it primarily targets newlines) or carefully test with `remove_all_empty_space=True` and only use if you are confident in the result. Ensure essential spaces are preserved within content or explicitly protected.","message":"Using `remove_all_empty_space=True` can lead to unexpected visual or functional breakage, especially with inline HTML elements. This option removes all whitespace between tags, potentially collapsing space where it is semantically important (e.g., between `<i>X</i> <i>Y</i>` becoming `<i>X</i><i>Y</i>`).","severity":"gotcha","affected_versions":"All versions"},{"fix":"For new projects or if encountering issues with modern Python, evaluate actively maintained forks such as `htmlmin2` (latest 0.1.13, March 2023) or `htmlmin4` (latest 0.2.15, March 2025) or other HTML minification alternatives.","message":"The original `htmlmin` library has not seen updates since late 2017, indicating it is no longer actively maintained. This means it may not receive bug fixes, security patches, or compatibility updates for newer Python versions or HTML standards.","severity":"gotcha","affected_versions":"All versions (due to lack of future maintenance)"},{"fix":"Wrap the content you wish to protect in an element with the `pre` attribute (e.g., `<div pre>...</div>`). If you need to keep the `pre` attribute in the output, use the `keep_pre=True` option in the `minify` function or `Minifier` class. You can also specify custom attributes via `pre_attr` and additional pre-defined tags via `pre_tags`.","message":"To prevent `htmlmin` from minifying specific blocks of HTML, you can add a `pre` attribute to the HTML tag wrapping the content. By default, `htmlmin` will remove this `pre` attribute from the output.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}