Metadata-Version: 2.4
Name: django-prettify-html
Version: 0.1.1
Classifier: Development Status :: 3 - Alpha
Classifier: Framework :: Django
Classifier: Framework :: Django :: 4.2
Classifier: Framework :: Django :: 5.0
Classifier: Framework :: Django :: 5.1
Classifier: Framework :: Django :: 5.2
Classifier: Framework :: Django :: 6.0
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Programming Language :: Rust
Classifier: Topic :: Internet :: WWW/HTTP
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: Topic :: Text Processing :: Markup :: HTML
Requires-Dist: pytest>=8.0 ; extra == 'test'
Provides-Extra: test
License-File: LICENSE
Summary: Use markup_fmt, the extremely fast HTML formatter, with Django.
Keywords: django,html,formatter,prettify,markup_fmt,rust
Author: Nick Petrovic
License-Expression: MIT
Requires-Python: >=3.10
Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
Project-URL: Homepage, https://github.com/nickpetrovic/django-prettify-html
Project-URL: Repository, https://github.com/nickpetrovic/django-prettify-html

# django-prettify-html

Use [markup_fmt](https://github.com/g-plane/markup_fmt), the extremely fast HTML formatter, with Django.

Provides a Django middleware that automatically prettifies HTML responses for
readable "View Source" output during development — the counterpart to
[django-minify-html](https://github.com/adamchainz/django-minify-html) for production.

Powered by Rust via [PyO3](https://github.com/PyO3/pyo3) — no subprocess overhead, no Node.js dependency.

## Requirements

- Python 3.10 to 3.14
- Django 4.2 to 6.0

## Installation

```bash
pip install django-prettify-html
```

## Setup

1. Add to your `INSTALLED_APPS`:

```python
INSTALLED_APPS = [
    ...,
    "django_prettify_html",
    ...,
]
```

2. Add the middleware (typically last, or in place of a minification middleware):

```python
MIDDLEWARE = [
    ...,
    "django_prettify_html.middleware.PrettifyHtmlMiddleware",
]
```

## Usage

The middleware prettifies all non-streaming, non-encoded HTML responses.

### Customizing options

Subclass the middleware and override `format_args`:

```python
from django_prettify_html.middleware import PrettifyHtmlMiddleware

class ProjectPrettifyHtmlMiddleware(PrettifyHtmlMiddleware):
    format_args = PrettifyHtmlMiddleware.format_args | {
        "indent_width": 4,
        "print_width": 100,
    }
```

### Skipping specific views

```python
from django_prettify_html.decorators import no_html_prettification

@no_html_prettification
def raw_view(request):
    return HttpResponse("<pre>unformatted</pre>")
```

### Using the formatter directly

```python
from django_prettify_html import format

html = "<div><p>Hello</p></div>"
pretty = format(html, indent_width=2, print_width=80)
```

### Available options

| Option | Type | Default | Description |
|--------|------|---------|-------------|
| `language` | str | `"html"` | Language: html, jinja, vue, svelte, astro, angular, vento, mustache, xml |
| `print_width` | int | `80` | Maximum line width before wrapping |
| `indent_width` | int | `2` | Number of spaces per indent level |
| `use_tabs` | bool | `False` | Use tabs instead of spaces |
| `line_break` | str | `"lf"` | Line break style: "lf" or "crlf" |
| `quotes` | str | `"double"` | Attribute quote style: "double" or "single" |
| `format_comments` | bool | `False` | Format HTML comments |
| `closing_bracket_same_line` | bool | `False` | Keep closing bracket on same line as last attribute |
| `max_attrs_per_line` | int\|None | `None` | Maximum attributes per line (None = unlimited) |
| `prefer_attrs_single_line` | bool | `False` | Prefer all attributes on a single line when possible |
| `html_normal_self_closing` | bool\|None | `None` | Self-close normal HTML elements |
| `html_void_self_closing` | bool\|None | `None` | Self-close void elements (br, img, etc.) |
| `whitespace_sensitivity` | str | `"css"` | Whitespace handling: "css", "strict", or "ignore" |
| `doctype_keyword_case` | str | `"ignore"` | DOCTYPE keyword case: "ignore", "upper", or "lower" |

## Recommended setup: minify in production, prettify in development

```python
# settings/base.py
MIDDLEWARE = [
    ...,
    "myapp.middleware.MinifyMiddleware",  # production minification
]

# settings/development.py
MIDDLEWARE = [
    "django_prettify_html.middleware.PrettifyHtmlMiddleware" if m == "myapp.middleware.MinifyMiddleware" else m
    for m in MIDDLEWARE
]
```

## License

MIT

