Skip to main content

Django template filter for cleaning up whitespace in plain text

Project description

django-clearplaintext

Provides Django template filters that normalize 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

clean_plaintext

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

keep_whitespace

Escapes real whitespace characters in a value so they survive clean_plaintext normalization:

  • \n\n (literal)
  • \t\t (literal)
  • \s (literal)

Use this when passing database values or dynamic content that contains meaningful whitespace into a clean_plaintext block.

📦 Installation

pip install django-clearplaintext

🔧 Setup

Add it to your Django project:

INSTALLED_APPS = [
    ...
    "clearplaintext",
]

🚀 Usage

Load the template tag:

{% load clearplaintext_filters %}

clean_plaintext — template block formatting

Use it as a filter block to normalize whitespace in your template while keeping explicit escape sequences:

{% 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 forloop.last %}\n{% endif %}
{% endfor %}

Best regards,\n
{{ company.name }}
{% endfilter %}

Output:

Hello John Smith,

Your order has been confirmed.
    - Widget (2x)
    - Gadget (1x)

Best regards,
Acme Inc.

keep_whitespace — preserving whitespace from dynamic values

When a variable comes from the database and already contains meaningful whitespace, pipe it through keep_whitespace before passing it into a clean_plaintext block:

{% filter clean_plaintext %}
{{ post.content|keep_whitespace }}
{% endfilter %}

This ensures that real newlines, tabs, and spaces in the value are escaped before normalization runs, so they are restored correctly in the output rather than being collapsed.

🎯 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. The keep_whitespace filter converts real whitespace into these sequences so that dynamic values receive the same treatment.

🧪 Testing

The package is tested against multiple Django versions using tox.

To run tests locally:

tox

📄 License

MIT License

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_clearplaintext-1.1.0.tar.gz (3.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_clearplaintext-1.1.0-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

Details for the file django_clearplaintext-1.1.0.tar.gz.

File metadata

  • Download URL: django_clearplaintext-1.1.0.tar.gz
  • Upload date:
  • Size: 3.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for django_clearplaintext-1.1.0.tar.gz
Algorithm Hash digest
SHA256 93ab78b6d2fa7045d5d46c38e48c0c11545e45cf7a8527e80a0e3dd5c8f3cce5
MD5 71654871d6b38803c19a6e4a9e2dea92
BLAKE2b-256 27571db623d007c56e13caa895858ac19030b0bad82fbe7d1166aa5a289580ae

See more details on using hashes here.

File details

Details for the file django_clearplaintext-1.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_clearplaintext-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0199170a4ae3469fe8e2eedb613fa13cc38e31c7dee3b2b4878599a121785e95
MD5 5b4a1c0575ac31b338bcf565ae60e880
BLAKE2b-256 2e4a2efb4c839825affff99f301444eae247d6c124e51d2c3ebfc941f3ff6f8a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page