Package: garpix-htmlmin
++++++++++++++
django-htmlmin
++++++++++++++.. image::https://github.com/cobrateam/django-htmlmin/workflows/Build/badge.svg:target:https://github.com/cobrateam/django-htmlmin/actions?query=branch:master+workflow:Builddjango-html is an HTML minifier for Python, with full support for HTML 5. It
supports Django, Flask and many other Python web frameworks. It also provides a
command line tool, that can be used for static websites or deployment scripts.Why minify HTML code?One of the important points on client side optimization is to minify HTML. With
minified HTML code, you reduce the size of the data transferred from the server
to the client, which results in faster load times.InstallingTo install django-htmlmin, run this on the terminal: :.. code-block:: sh$ [sudo] pip install django-htmlminUsing the middlewareAll you need to do is add two middlewares to yourMIDDLEWARE_CLASSESand
enable theHTML_MINIFYsetting:.. code-block:: pythonMIDDLEWARE_CLASSES = (
    # other middleware classes
    'htmlmin.middleware.HtmlMinifyMiddleware',
    'htmlmin.middleware.MarkRequestMiddleware',
)Note that if you're using Django's caching middleware,MarkRequestMiddlewareshould go afterFetchFromCacheMiddleware, andHtmlMinifyMiddlewareshould go afterUpdateCacheMiddleware:.. code-block:: pythonMIDDLEWARE_CLASSES = (
    'django.middleware.cache.UpdateCacheMiddleware',
    'htmlmin.middleware.HtmlMinifyMiddleware',
    # other middleware classes
    'django.middleware.cache.FetchFromCacheMiddleware',
    'htmlmin.middleware.MarkRequestMiddleware',
)You can optionally specify theHTML_MINIFYsetting:.. code-block:: pythonHTML_MINIFY = TrueThe default value for theHTML_MINIFYsetting isnot DEBUG. You only
need to set it toTrueif you want to minify your HTML code whenDEBUGis enabled.Excluding some URLsIf you don't want to minify all views in your app and it's under a/my_appURL, you can tell the middleware to not minify the response of your views by
adding aEXCLUDE_FROM_MINIFYINGsetting on your settings.py:.. code-block:: pythonEXCLUDE_FROM_MINIFYING = ('^my_app/', '^admin/')Regex patterns are used for URL exclusion. If you want to exclude all URLs of
your app, except a specific view, you can use the decorator@minified_response(check the next section above).Keeping commentsThe default behaviour of the middleware is to remove all HTML comments. If you
want to keep the comments, set the settingKEEP_COMMENTS_ON_MINIFYINGtoTrue:.. code-block:: pythonKEEP_COMMENTS_ON_MINIFYING = TrueConservative whitespace minifyingBy default the minifier will try to intelligently remove whitespace and leave
spaces only as needed for inline text rendering.  Sometimes it may be necessary
to not completely remove whitespace but only reduce spaces to a single space.
If you setCONSERVATIVE_WHITESPACE_ON_MINIFYINGtoFalsethen
whitespace is always reduced to a single space and never completely removed... code-block:: pythonCONSERVATIVE_WHITESPACE_ON_MINIFYING = TrueUsing the decoratordjango-htmlmin also provides a decorator, that you can use only on views you
want to minify the response:.. code-block:: pythonfrom htmlmin.decorators import minified_response

@minified_response
def home(request):
    return render_to_response('home.html')Decorator to avoid response to be minifiedYou can use thenot_minified_responsedecorator on views if you want to
avoid the minification of any specific response, without using theEXCLUDE_FROM_MINIFYINGsetting:.. code-block:: pythonfrom htmlmin.decorators import not_minified_response

@not_minified_response
def home(request):
    return render_to_response('home.html')Using thehtml_minifyfunctionIf you are not working with Django, you can invoke thehtml_minifyfunction
manually:.. code-block:: pythonfrom htmlmin.minify import html_minify
html = '<html>    <body>Hello world</body>    </html>'
minified_html = html_minify(html)Here is an example with aFlask <http://flask.pocoo.org>_ view:.. code-block:: pythonfrom flask import Flask
from htmlmin.minify import html_minify

app = Flask(__name__)

@app.route('/')
def home():
    rendered_html = render_template('home.html')
    return html_minify(rendered_html)Keeping commentsBy default,html_minify()removes all comments. If you want to keep them,
you can passignore_comments=False:.. code-block:: pythonfrom htmlmin.minify import html_minify
html = '<html>  <body>Hello world<!-- comment to keep --></body>  </html>'
minified_html = html_minify(html, ignore_comments=False)Using command line toolIf you are not even using Python, you can use thepyminifycommand line
tool to minify HTML files:.. code-block:: sh$ pyminify index.html > index_minified.htmlYou can also keep the comments, if you want:.. code-block:: sh$ pyminify --keep-comments index.html > index_minified_with_comments.htmldevelopmentSource hosted atGitHub <http://github.com/cobrateam/django-htmlmin>_Report issues onGitHub Issues <http://github.com/cobrateam/django-htmlmin/issues>_Pull requests are very welcome! Make sure your patches are well tested.Running testsIf you are using a virtualenv, all you need to do is:.. code-block:: sh$ make testcommunityIRC channel#cobrateamchannel onirc.freenode.netChangelogYou can see the complete changelog on theGithub releases page <https://github.com/cobrateam/django-htmlmin/releases>_.LICENSEUnless otherwise noted, thedjango-htmlminsource files are distributed
under the BSD-style license found in the LICENSE file.