Django template filter for cleaning up whitespace in plain text
Project description
django-clearplaintext
Provides a Django template filter that normalizes plain text while allowing explicit whitespace control.
It collapses excessive whitespace but preserves intentional formatting using escaped sequences like \n, \t, and \s.
Perfect for:
- Plain text emails
- Notification templates
- Text exports
- System messages
- Markdown templates
- Structured text blocks inside Django templates
✨ What It Does
The clean_plaintext filter:
-
Collapses multiple spaces into a single space
-
Limits consecutive blank lines to a maximum of two
-
Strips leading and trailing whitespace on each line
-
Removes empty lines at the beginning and end
-
Converts escaped sequences:
\n→ newline\t→ tab\s→ single space
This allows you to write readable Django templates while still controlling whitespace precisely.
📦 Installation
pip install django-clearplaintext
🔧 Setup
Add it to your Django project:
INSTALLED_APPS = [
...
"clearplaintext",
]
🚀 Usage
Input
Load the template tag:
{% load clearplaintext_filters %}
Use it as a filter block:
{% filter clean_plaintext %}
Hello {{ user.get_full_name }},\n
Your order has been confirmed.\n
{% for item in order.items %}
\t- {{ item.name }} ({{ item.quantity }}x)\n
{% if not forloop.last %}\n{% endif %}
{% endfor %}
Best regards,\n
{{ company.name }}
{% endfilter %}
Output
Hello John Smith,
Your order has been confirmed.
Items:
- Widget (2x)
- Gadget (1x)
Best regards,
Acme Inc.
🎯 Why Use This?
Django templates often produce messy whitespace because of:
- Template indentation
- Conditionals and loops
- Readability formatting
- Multi-line template blocks
This filter lets you:
- Keep templates readable
- Avoid ugly output formatting
- Still control exact whitespace when needed
It is especially useful for plain-text emails where formatting matters.
🧠 Design Philosophy
- Minimal
- No external dependencies
- Focused on plain text formatting
- Safe and predictable behavior
- Suitable for reusable Django apps
🔍 Escaped Sequences Supported
| Sequence | Result |
|---|---|
\n |
Newline |
\t |
Tab |
\s |
Single space |
Escaped sequences are protected during normalization and restored afterward.
🧪 Testing
The package is tested against multiple Django versions using tox.
To run tests locally:
tox
📄 License
MIT License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_clearplaintext-1.0.2.tar.gz.
File metadata
- Download URL: django_clearplaintext-1.0.2.tar.gz
- Upload date:
- Size: 3.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
47462fefdf31b296cadcce2d9c0f457c9a3c445fa2c886213d1e0cd608c198c2
|
|
| MD5 |
e787ce02df684a03bd06fa8a86318e56
|
|
| BLAKE2b-256 |
0f10017bc34341dce187cb0884993a96abc8c100d6be835f4ef229b51039d699
|
File details
Details for the file django_clearplaintext-1.0.2-py3-none-any.whl.
File metadata
- Download URL: django_clearplaintext-1.0.2-py3-none-any.whl
- Upload date:
- Size: 3.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4dac45f123c22d29774ea2caa18de7f553cd44c0f8a0f722790abead69ca8b52
|
|
| MD5 |
bfc7b2a9400ddbfd770a5dee927935a3
|
|
| BLAKE2b-256 |
af2a960e7f8e6571c77c7597dc64bb730bf281c921cb0f7432180f17413859e9
|