Django Smart Spaceless

PyPI version Build status

Django Smart Spaceless is a Django template tag application for minifying block-level HTML elements only.

It’s just like spaceless, but preserves white space between inline HTML elements. Useful for HTML where spaces directly between <a>, <strong>, and other inline elements is likely desired to be preserved. Packages the django-htmlmin project to be used as a template tag.

Install

$ pip install django-smartspaceless

Add to settings.py.

INSTALLED_APPS = [
    # ...
    'smartspaceless',
]

Usage

{% load smartspaceless_tags %}

{% smartspaceless %}
<p><a href="#">Link 1</a></p>
<p><a href="#">Link 2</a> <a href="#">Link 3</a></p>
{% endsmartspaceless %}

Result:

<p><a href="#">Link 1</a></p><p><a href="#">Link 2</a> <a href="#">Link 3</a></p>

The space between <a href="#">Link 2</a> and <a href="#">Link 3</a> is preserved. Removing that space would be bad.

Note

Please note that django-htmlmin by default uses the html5lib parser, which prepends possibly missing <html><head></head><body> and appends possibly missing </body></html> tags in an effort to create valid HTML. The template tag changes this default behavior to use html.parser, the HTML parser in Python’s standard library, which does not alter HTML fragments.

Contents

Install

Install with the pip package manager.

$ mkvirtualenv myvenv -p python3
$ pip install django
$ pip install django-smartspaceless

After creating a project, add smartspaceless to INSTALLED_APPS in settings.py.

INSTALLED_APPS = [
    # ...
    'smartspaceless',
]

Remember to update your requirements.txt file. In your project directory:

$ pip freeze > requirements.txt

Usage

Load the template tag in a template. Run the tag with an opening and closing tag.

{% extends "base.html" %}

{% load smartspaceless_tags %}

{% block content %}

    {% smartspaceless %}
    <p><a href="#">Link 1</a></p>
    <p><a href="#">Link 2</a> <a href="#">Link 3</a></p>
    {% endsmartspaceless %}

{% endblock %}

Result:

<p><a href="#">Link 1</a></p><p><a href="#">Link 2</a> <a href="#">Link 3</a></p>

Documentation

Full documentation is available online.

However, you can also build the documentation from source. Enter your virtual environment.

$ workon myvenv

Clone the code repository.

$ git clone git@github.com:richardcornish/django-smartspaceless.git
$ cd django-smartspaceless/

Install Sphinx, sphinx-autobuild, and sphinx_rtd_theme.

$ pip install sphinx sphinx-autobuild sphinx_rtd_theme

Create an HTML build.

$ (cd docs/ && make html)

Or use sphinx-autobuild to watch for live changes.

$ sphinx-autobuild docs/ docs/_build_html

Open 127.0.0.1:8000.

Tests

Continuous integration test results are available online.

However, you can also test the source code.

$ workon myvenv
$ django-admin test smartspaceless.tests --settings="smartspaceless.tests.settings"

Creating test database for alias 'default'...
..........
----------------------------------------------------------------------
Ran 10 tests in 0.713s

OK
Destroying test database for alias 'default'...

A bundled settings file allows you to test the code without even creating a Django project.

Indices and tables