adamchainz / djade

A Django template formatter.
MIT License
283 stars 4 forks source link

Format HTML #91

Closed christophehenry closed 1 month ago

christophehenry commented 1 month ago

Description

Unless I'm wring, Djade currently only enforces Django template rules. I think it'd be nice if it proposed an HTML formatter too. I'd propose the following rules:

  1. max line length sourced from .editorconfig, defaulting to pyproject.toml, defaulting to 120,
  2. indentation sourced from .editorconfig, defaulting to pyproject.toml, defaulting to 4 spaces,
  3. all HTML attributes on one line if line is less that max line length,
  4. if line lenght is more than max line length, each HTML attribute on a new line, tag on its own line, closing chevron on it own line:
    <div
        id="some-id"
        class="some-class"
        data-some-attribute="some data"
        data-some-attribute2="some data 2"
    >
        …
    </div>
  5. tag content collapses first then attributes, following the previous rules if line is still too long,
  6. opening and closing tag on their own line if multiline except when tag is empty:
    <p
        id="…"
        class="…"
        data-some-attr="…"
    ></p>
    <p
        id="…"
        class="…"
        data-some-attr="…"
    >
        Some very long text
    </p>
    <p>
        Some very long text
    </p>
  7. auto-closing tags always closed with a space before slash: <img src="…" />, not <img src="…"/> or <img src="…">
  8. tag attributes ordered like:
    1. some tag-specific attributes: href, method, action (maybe configurable?)
    2. id
    3. class
    4. other attributes alphabetically ordered
    5. data-* attributes, alphabetically ordered (maybe configurable?)
adamchainz commented 1 month ago

Deliberately out of scope, per README. Parsing HTML correctly is very complicated - for example, lots of things are optional. Combining this with parsing the meaning and potential output of template tags is near impossible. Other template formatters like djlint and DjHTML can introduce issues that break the meaning of templates, which is why I no longer recommend them.

I'm not saying never, but I am saying I have zero energy to look into this right now and having a large issue tracking ideas is not inspiring. You're free to experiment with extending Djade in this direction, though.