rtts / djhtml

Django/Jinja template indenter
GNU General Public License v3.0
582 stars 32 forks source link

[Feature Request] `--exclude` parameter #48

Closed Archmonger closed 2 years ago

Archmonger commented 2 years ago

Currently running into some issues where djhtml is formatting my mkdocs documentation directory, causing unnecessary html code modifications.

Ideally I'd like to be able to exclude my ./docs/* to prevent that.

JaapJoris commented 2 years ago

Hi there, and thanks for your suggestion!

I think the following pre-commit config will accomplish what you want:

repos:
- repo: https://github.com/rtts/djhtml
  rev: 'main'  # replace with the latest tag on GitHub
  hooks:
    - id: djhtml
      exclude: ^docs/

Could you try this and see if it works?

JaapJoris commented 2 years ago

Alternatively, if you're not using pre-commit, you can combine find and xargs to accomplish the desired behavior, with the added benefit that you can specify more advanced inclusion/exclusion rules as needed:

find -name *.html -not -path "./docs/*" | xargs djhtml -i
raLaaaa commented 2 years ago

What about Windows if we don´t have find or xargs?

For anyone who also stumbles across this one this is how I solved it.

Create an extra python script which invokes djhtml (requires .venv)`:

import subprocess
import sys

def main():
    filepath = sys.argv[1]

    if "templates" in filepath and filepath.endswith(".html"):
        args = [".\\.venv\\scripts\\djhtml.exe", "-i", filepath]
        ret = subprocess.Popen(args).wait()

        exit(ret)

if __name__ == "__main__":
    main()

Afterwards I created an external tool in Pycharm so that I can use djhtml safely as on save-hook in my IDE.

image

Unfertunately I did not find another way under Windows without using pre-commit. Always running djhtml on save is not working since it breaks (it itendends incorreclty) for instance .toml files.