Package: djhtml
DjHTMLA pure-Python Django/Jinja template indenter without dependencies.DjHTML indents mixed HTML/CSS/JavaScript templates that containDjangoorJinjatemplate
tags. It works similar to other code-formatting tools such asBlackand interoperates nicely withpre-commit.DjHTML is anindenterand not aformatter: 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.New! Multi-line HTML elementsAs of version 3, DjHTML indents multi-line HTML elements and
multi-line attribute values like this:<blockquote cite="Guido Van Rossum"style="font-style: italic;{%ifdark_mode%}background: black;{%endif%}">Don't you hate code that's not properly indented?</blockquote>New! Multi-line CSS indentationMulti-line CSS values are now continued at the same indentation level:<style>@font-face {font-family: Helvetica;src:{%forformat,filenameinlicensed_fonts%}url('{%staticfilename%}') format('{{format}}'),{%endfor%}url('Arial.woff2') format('woff2'),url('Arial.woff') format('woff');}</style>New! Improved JavaScript indentationMany new JavaScript indention rules have been added, such as the
indentation of method chaining:<script>window.fetch('/test.html').then((html) => {document.body.innerHTML = html;{%blockextra_statements%}{%endblock%}});</script>New! Tabwidth guessingWithout the-t/--tabwidthargument, DjHTML no longer defaults to
a tabwidth of 4 but instead guesses the correct tabwidth.InstallationDjHTML requires Python 3.8 or higher and is compatible with all
operating systems supported by Python. Install DjHTML with the
following command:$ pip install djhtmlNote thatWindows still uses legacy code pagesinstead of UTF-8. It is highly advised to set the environment variablePYTHONUTF8to1with thesetxcommand:C:\> setx /m PYTHONUTF8 1UsageAfter installation you can indent templates using thedjhtmlcommand:$ djhtml template.html
reindented template.html
1 template has been reindented.You can also rundjhtml .to indent all HTML files beneath the
current directory.An exit status of 0 means that everything went well, regardless of
whether any files were changed. When the option-c/--checkis
used, the exit status is 1 when one or more files would have changed,
but no changes are actually made. All available options are given bydjthml -h/djthml --help.fmt:offandfmt:onYou can exclude specific lines from being processed with the{# fmt:off #}and{# fmt:on #}operators:{# fmt:off #},-._|\/     .\\_,--._/{# fmt:on #}Contents inside<pre> ... </pre>,<!-- ... --->,/* ... */, and{% comment %} ... {% endcomment %}tags are also ignored (depending
on the current mode).ModesThe indenter operates in one of three different modes:DjHTML mode: the default mode. Invoked by using thedjhtmlcommand
or the pre-commit hook.DjCSS mode. Will be entered when a<style>tag is encountered in
DjHTML mode. It can also be invoked directly with the commanddjcss.DjJS mode. Will be entered when a<script>tag is encountered in
DjHTML mode. It can also be invoked directly with the commanddjjs.pre-commit configurationA great way to use DjHTML is as apre-commithook, so all your HTML, CSS and JavaScript files will automatically be
indented upon every commit.First, install pre-commit:$ pip install pre-commit
$ pre-commit installThen, add the following to your.pre-commit-config.yaml:repos:
  - repo: https://github.com/rtts/djhtml
    rev: 'main'  # replace with the latest tag on GitHub
    hooks:
      - id: djhtml
      - id: djcss
      - id: djjsNow runpre-commit autoupdateto automatically replacemainwith
the latest tag on GitHub,as recommended by pre-commit.If you want to override a command-line option, for example to change
the default tabwidth, you change theentrypoint of these hooks:hooks:
      - id: djhtml
        # Use a tabwidth of 2 for HTML files
        entry: djhtml --tabwidth 2
      - id: djcss
      - id: djjsIf you want to limit the files these hooks operate on, you can usepre-commit mechanisms for filtering.
For example:hooks:
      - id: djhtml
        # Indent only HTML files in template directories
        files: .*/templates/.*\.html$
      - id: djcss
        # Run this hook only on SCSS files (CSS and SCSS is the default)
        types: [scss]
      - id: djjs
        # Exclude JavaScript files in vendor directories
        exclude: .*/vendor/.*Now when you rungit commityou will see something like the
following output:$ git commit

DjHTML...................................................................Failed
- hook id: djhtml
- files were modified by this hook

reindented template.html
1 template has been reindented.To inspect the changes that were made, usegit diff. If you are
happy with the changes, you can commit them normally. If you are not
happy, please do the following:RunSKIP=djhtml git committo commit anyway, skipping thedjhtmlhook.Consideropening an issuewith 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!DevelopmentFirst of all, clone this repository:$ git clone https://github.com/rtts/djhtml
$ cd djhtmlThen, create a Python virtualenv and activate it:$ python -m venv ~/.virtualenvs/djhtml
$ . ~/.virtualenvs/djhtml/bin/activateThen, install the package indevelopment
modeincluding thedevdependencies, and install the pre-commit hooks:$ python -m pip install -e '.[dev]'
$ pre-commit install --install-hooksYou can run the unittests with:$ python -m unittestOr usenoxto test all supported Python
interpreters:$ noxFinally, to get a little insight into the tokenization step of the
indenting algorithm, you can run DjHTML with the-d/--debugargument. You will see a Python representation of the tokens that are
created.