django-htmlmin


Namedjango-htmlmin JSON
Version 0.11.0 PyPI version JSON
download
home_page
SummaryHTML minifier for Python frameworks (not only Django, despite the name).
upload_time2019-03-18 01:25:14
maintainer
docs_urlNone
authorCobraTeam
requires_python
license
keywords django html minifier minify
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            ++++++++++++++
django-htmlmin
++++++++++++++

.. image:: https://secure.travis-ci.org/cobrateam/django-htmlmin.png
   :target: http://travis-ci.org/cobrateam/django-htmlmin

django-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.

Installing
==========

To install django-htmlmin, run this on the terminal: :

.. code-block:: sh

    $ [sudo] pip install django-htmlmin

Using the middleware
====================

All you need to do is add two middlewares to your ``MIDDLEWARE_CLASSES`` and
enable the ``HTML_MINIFY`` setting:

.. code-block:: python

    MIDDLEWARE_CLASSES = (
        # other middleware classes
        'htmlmin.middleware.HtmlMinifyMiddleware',
        'htmlmin.middleware.MarkRequestMiddleware',
    )

Note that if you're using Django's caching middleware,
``MarkRequestMiddleware`` should go after ``FetchFromCacheMiddleware``, and
``HtmlMinifyMiddleware`` should go after ``UpdateCacheMiddleware``:

.. code-block:: python

    MIDDLEWARE_CLASSES = (
        'django.middleware.cache.UpdateCacheMiddleware',
        'htmlmin.middleware.HtmlMinifyMiddleware',
        # other middleware classes
        'django.middleware.cache.FetchFromCacheMiddleware',
        'htmlmin.middleware.MarkRequestMiddleware',
    )

You can optionally specify the ``HTML_MINIFY`` setting:


.. code-block:: python

    HTML_MINIFY = True

The default value for the ``HTML_MINIFY`` setting is ``not DEBUG``. You only
need to set it to ``True`` if you want to minify your HTML code when ``DEBUG``
is enabled.

Excluding some URLs
-------------------

If you don't want to minify all views in your app and it's under a ``/my_app``
URL, you can tell the middleware to not minify the response of your views by
adding a ``EXCLUDE_FROM_MINIFYING`` setting on your settings.py:

.. code-block:: python

    EXCLUDE_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 comments
----------------

The default behaviour of the middleware is to remove all HTML comments. If you
want to keep the comments, set the setting ``KEEP_COMMENTS_ON_MINIFYING``
to ``True``:

.. code-block:: python

    KEEP_COMMENTS_ON_MINIFYING = True

Using the decorator
===================

django-htmlmin also provides a decorator, that you can use only on views you
want to minify the response:

.. code-block:: python

    from htmlmin.decorators import minified_response

    @minified_response
    def home(request):
        return render_to_response('home.html')

Decorator to avoid response to be minified
------------------------------------------

You can use the ``not_minified_response`` decorator on views if you want to
avoid the minification of any specific response, without using the
``EXCLUDE_FROM_MINIFYING`` setting:

.. code-block:: python

    from htmlmin.decorators import not_minified_response

    @not_minified_response
    def home(request):
        return render_to_response('home.html')

Using the ``html_minify`` function
==================================

If you are not working with Django, you can invoke the ``html_minify`` function
manually:

.. code-block:: python

    from htmlmin.minify import html_minify
    html = '<html>    <body>Hello world</body>    </html>'
    minified_html = html_minify(html)

Here is an example with a `Flask <http://flask.pocoo.org>`_ view:

.. code-block:: python

    from 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 comments
----------------

By default, ``html_minify()`` removes all comments. If you want to keep them,
you can pass ``ignore_comments=False``:

.. code-block:: python

    from 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 tool
=======================

If you are not even using Python, you can use the ``pyminify`` command line
tool to minify HTML files:

.. code-block:: sh

    $ pyminify index.html > index_minified.html

You can also keep the comments, if you want:

.. code-block:: sh

    $ pyminify --keep-comments index.html > index_minified_with_comments.html

development
===========

* Source hosted at `GitHub <http://github.com/cobrateam/django-htmlmin>`_
* Report issues on `GitHub Issues
  <http://github.com/cobrateam/django-htmlmin/issues>`_

Pull requests are very welcome! Make sure your patches are well tested.

Running tests
-------------

If you are using a virtualenv, all you need to do is:

.. code-block:: sh

    $ make test

community
=========

IRC channel
-----------

``#cobrateam`` channel on ``irc.freenode.net``

Changelog
=========

You can see the complete changelog on the
`Github releases page <https://github.com/cobrateam/django-htmlmin/releases>`_.

LICENSE
=======

Unless otherwise noted, the ``django-htmlmin`` source files are distributed
under the BSD-style license found in the LICENSE file.
            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "django-htmlmin",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "django,html,minifier,minify",
    "author": "CobraTeam",
    "author_email": "andrewsmedina@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/52/37/df97e6f60662e2545fa4349c48fbf2166eb8a4902c9bf1f179b7ee5aa0f0/django-htmlmin-0.11.0.tar.gz",
    "platform": "",
    "description": "++++++++++++++\ndjango-htmlmin\n++++++++++++++\n\n.. image:: https://secure.travis-ci.org/cobrateam/django-htmlmin.png\n   :target: http://travis-ci.org/cobrateam/django-htmlmin\n\ndjango-html is an HTML minifier for Python, with full support for HTML 5. It\nsupports Django, Flask and many other Python web frameworks. It also provides a\ncommand line tool, that can be used for static websites or deployment scripts.\n\nWhy minify HTML code?\n=====================\n\nOne of the important points on client side optimization is to minify HTML. With\nminified HTML code, you reduce the size of the data transferred from the server\nto the client, which results in faster load times.\n\nInstalling\n==========\n\nTo install django-htmlmin, run this on the terminal: :\n\n.. code-block:: sh\n\n    $ [sudo] pip install django-htmlmin\n\nUsing the middleware\n====================\n\nAll you need to do is add two middlewares to your ``MIDDLEWARE_CLASSES`` and\nenable the ``HTML_MINIFY`` setting:\n\n.. code-block:: python\n\n    MIDDLEWARE_CLASSES = (\n        # other middleware classes\n        'htmlmin.middleware.HtmlMinifyMiddleware',\n        'htmlmin.middleware.MarkRequestMiddleware',\n    )\n\nNote that if you're using Django's caching middleware,\n``MarkRequestMiddleware`` should go after ``FetchFromCacheMiddleware``, and\n``HtmlMinifyMiddleware`` should go after ``UpdateCacheMiddleware``:\n\n.. code-block:: python\n\n    MIDDLEWARE_CLASSES = (\n        'django.middleware.cache.UpdateCacheMiddleware',\n        'htmlmin.middleware.HtmlMinifyMiddleware',\n        # other middleware classes\n        'django.middleware.cache.FetchFromCacheMiddleware',\n        'htmlmin.middleware.MarkRequestMiddleware',\n    )\n\nYou can optionally specify the ``HTML_MINIFY`` setting:\n\n\n.. code-block:: python\n\n    HTML_MINIFY = True\n\nThe default value for the ``HTML_MINIFY`` setting is ``not DEBUG``. You only\nneed to set it to ``True`` if you want to minify your HTML code when ``DEBUG``\nis enabled.\n\nExcluding some URLs\n-------------------\n\nIf you don't want to minify all views in your app and it's under a ``/my_app``\nURL, you can tell the middleware to not minify the response of your views by\nadding a ``EXCLUDE_FROM_MINIFYING`` setting on your settings.py:\n\n.. code-block:: python\n\n    EXCLUDE_FROM_MINIFYING = ('^my_app/', '^admin/')\n\nRegex patterns are used for URL exclusion. If you want to exclude all URLs of\nyour app, except a specific view, you can use the decorator\n``@minified_response`` (check the next section above).\n\nKeeping comments\n----------------\n\nThe default behaviour of the middleware is to remove all HTML comments. If you\nwant to keep the comments, set the setting ``KEEP_COMMENTS_ON_MINIFYING``\nto ``True``:\n\n.. code-block:: python\n\n    KEEP_COMMENTS_ON_MINIFYING = True\n\nUsing the decorator\n===================\n\ndjango-htmlmin also provides a decorator, that you can use only on views you\nwant to minify the response:\n\n.. code-block:: python\n\n    from htmlmin.decorators import minified_response\n\n    @minified_response\n    def home(request):\n        return render_to_response('home.html')\n\nDecorator to avoid response to be minified\n------------------------------------------\n\nYou can use the ``not_minified_response`` decorator on views if you want to\navoid the minification of any specific response, without using the\n``EXCLUDE_FROM_MINIFYING`` setting:\n\n.. code-block:: python\n\n    from htmlmin.decorators import not_minified_response\n\n    @not_minified_response\n    def home(request):\n        return render_to_response('home.html')\n\nUsing the ``html_minify`` function\n==================================\n\nIf you are not working with Django, you can invoke the ``html_minify`` function\nmanually:\n\n.. code-block:: python\n\n    from htmlmin.minify import html_minify\n    html = '<html>    <body>Hello world</body>    </html>'\n    minified_html = html_minify(html)\n\nHere is an example with a `Flask <http://flask.pocoo.org>`_ view:\n\n.. code-block:: python\n\n    from flask import Flask\n    from htmlmin.minify import html_minify\n\n    app = Flask(__name__)\n\n    @app.route('/')\n    def home():\n        rendered_html = render_template('home.html')\n        return html_minify(rendered_html)\n\nKeeping comments\n----------------\n\nBy default, ``html_minify()`` removes all comments. If you want to keep them,\nyou can pass ``ignore_comments=False``:\n\n.. code-block:: python\n\n    from htmlmin.minify import html_minify\n    html = '<html>  <body>Hello world<!-- comment to keep --></body>  </html>'\n    minified_html = html_minify(html, ignore_comments=False)\n\n\nUsing command line tool\n=======================\n\nIf you are not even using Python, you can use the ``pyminify`` command line\ntool to minify HTML files:\n\n.. code-block:: sh\n\n    $ pyminify index.html > index_minified.html\n\nYou can also keep the comments, if you want:\n\n.. code-block:: sh\n\n    $ pyminify --keep-comments index.html > index_minified_with_comments.html\n\ndevelopment\n===========\n\n* Source hosted at `GitHub <http://github.com/cobrateam/django-htmlmin>`_\n* Report issues on `GitHub Issues\n  <http://github.com/cobrateam/django-htmlmin/issues>`_\n\nPull requests are very welcome! Make sure your patches are well tested.\n\nRunning tests\n-------------\n\nIf you are using a virtualenv, all you need to do is:\n\n.. code-block:: sh\n\n    $ make test\n\ncommunity\n=========\n\nIRC channel\n-----------\n\n``#cobrateam`` channel on ``irc.freenode.net``\n\nChangelog\n=========\n\nYou can see the complete changelog on the\n`Github releases page <https://github.com/cobrateam/django-htmlmin/releases>`_.\n\nLICENSE\n=======\n\nUnless otherwise noted, the ``django-htmlmin`` source files are distributed\nunder the BSD-style license found in the LICENSE file.",
    "bugtrack_url": null,
    "license": "",
    "summary": "HTML minifier for Python frameworks (not only Django, despite the name).",
    "version": "0.11.0",
    "split_keywords": [
        "django",
        "html",
        "minifier",
        "minify"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "md5": "b2be56abc709c8032b9a9af87816cc1d",
                "sha256": "e41b2a2157570846645cc636a9bddde8aa3e03f6834a9211e61a17f2ed42b87e"
            },
            "downloads": -1,
            "filename": "django-htmlmin-0.11.0.tar.gz",
            "has_sig": false,
            "md5_digest": "b2be56abc709c8032b9a9af87816cc1d",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 8017,
            "upload_time": "2019-03-18T01:25:14",
            "upload_time_iso_8601": "2019-03-18T01:25:14.710964Z",
            "url": "https://files.pythonhosted.org/packages/52/37/df97e6f60662e2545fa4349c48fbf2166eb8a4902c9bf1f179b7ee5aa0f0/django-htmlmin-0.11.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2019-03-18 01:25:14",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "lcname": "django-htmlmin"
}
        
Elapsed time: 0.01466s