Skip to content

Commit

Permalink
Neato burrito!
Browse files Browse the repository at this point in the history
Closes #22
  • Loading branch information
JaapJoris committed May 25, 2021
1 parent 6a5f39c commit a774c94
Showing 1 changed file with 29 additions and 14 deletions.
43 changes: 29 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
# DjHTML

***A pure-Python Django template indenter without dependencies.***
***A pure-Python Django/Jinja2 template indenter without dependencies.***

DjHTML is a Django template indenter that works with mixed
HTML/CSS/Javascript templates. It works similar to other
code-formatting tools such as [Black](https://github.com/psf/black).
The goal is to correctly indent already well-structured templates but
not to fix broken ones.
DjHTML is a fully automatic template indenter that works with mixed
HTML/CSS/Javascript templates that contain
[Django](https://docs.djangoproject.com/en/stable/ref/templates/language/)
or [Jinja2](https://jinja.palletsprojects.com/templates/) template
tags. It works similar to other code-formatting tools such as
[Black](https://github.com/psf/black) and interoperates nicely with
[pre-commit](https://pre-commit.com/).

For example, DjHTML converts the following badly indented template:
DjHTML is an _indenter_ and not a _formatter_: it will only add/remove
whitespace at the beginning of lines. It will not insert newlines or
other characters. The goal is to correctly indent already
well-structured templates, not to fix broken ones.

For example, consider the following incorrectly indented template:

```jinja
<!doctype html>
Expand All @@ -26,7 +33,7 @@ For example, DjHTML converts the following badly indented template:
</html>
```

To the following:
This is what it will look like after processing by DjHTML:

```jinja
<!doctype html>
Expand All @@ -44,22 +51,25 @@ To the following:
</html>
```


## Installation

You can install DjHTML with the following command:
Install DjHTML with the following command:

$ pip install djhtml


## Usage

After installation you can indent Django templates using the `djhtml`
After installation you can indent templates using the `djhtml`
command. The default is to write the indented output to standard out.
To modify the source file in-place, use the `-i` / `--in-place`
option:

$ djhtml -i template.html
Successfully reformatted template.html
reindented template.html
All done! \o/
1 template reindented.

The other available options are:

Expand All @@ -68,11 +78,14 @@ The other available options are:
- `-t` / `--tabwidth`: set tabwidth (default is 4)
- `-o` / `--output-file`: write output to specified file

The installer also installs the `djtxt`, `djcss`, and `djjs` commands
for indenting plain text, CSS and Javascript source files,
respectively.

## pre-commit configuration

You can use DjHTML as a [pre-commit](https://pre-commit.com/) hook to
automatically indent your Django templates upon each commit.
automatically indent your templates upon each commit.

First, install pre-commit:

Expand Down Expand Up @@ -102,7 +115,9 @@ following output:
- hook id: djhtml
- files were modified by this hook

Successfully reformatted template.html
reindented template.html
All done! \o/
1 template reindented.

To inspect the changes that were made, use `git diff`. If you are
happy with the changes, you can commit them normally. If you are not
Expand All @@ -112,7 +127,7 @@ happy, please do the following:
`djhtml` hook.

2. Consider [opening an issue](https://github.com/rtts/djhtml/issues)
with relevant part of the input file that was incorrectly
with the relevant part of the input file that was incorrectly
formatted, and an example of how it should have been formatted.

Your feedback for improving DjHTML is very welcome!

0 comments on commit a774c94

Please sign in to comment.