From a8af2856a38b19ccb78eb4525c74afb837b4e9ee Mon Sep 17 00:00:00 2001 From: Andre Borie Date: Fri, 12 Nov 2021 00:51:11 +0000 Subject: [PATCH] Make the repo a pre-commit hook. --- .pre-commit-hooks.yaml | 29 +++++++++++++++++++++ docs/djlint/usage.rst | 58 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 .pre-commit-hooks.yaml diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml new file mode 100644 index 0000000..10e3c8e --- /dev/null +++ b/.pre-commit-hooks.yaml @@ -0,0 +1,29 @@ +- id: djlint-django + name: djLint for Django + entry: djlint --profile=django + files: "templates/.*\\.html$" + language: python + +- id: djlint-jinja + name: djLint for Jinja + entry: djlint --profile=jinja + files: "\\.j2$" + language: python + +- id: djlint-nunjucks + name: djLint for Nunjucks + entry: djlint --profile=nunjucks + files: "\\.njk$" + language: python + +- id: djlint-handlebars + name: djLint for Handlebars/Mustache + entry: djlint --profile=handlebars + files: "\\.hbs" + language: python + +- id: djlint-golang + name: djLint for Golang templates + entry: djlint --profile=golang + files: "\\.tmpl" + language: python diff --git a/docs/djlint/usage.rst b/docs/djlint/usage.rst index ed660be..5cf373c 100644 --- a/docs/djlint/usage.rst +++ b/docs/djlint/usage.rst @@ -21,7 +21,7 @@ Linter Usage Formatter Usage --------------- -Foramtting is a beta tool. ``--check`` the output before applying changes. +Formatting is a beta tool. ``--check`` the output before applying changes. Reformatting does not work with long json/html embedded into attribute data. @@ -119,3 +119,59 @@ CLI Args --lint Lint for common issues. [default option] --use-gitignore Use .gitignore file to extend excludes. -h, --help Show this message and exit. + + +As a pre-commit hook +-------------------- + +djLint can also be used as a `pre-commit `_ hook. + +The repo provides multiple pre-configured hooks for specific djLint profiles (it just pre-sets the ``--profile`` argument and tells pre-commit which file extensions to look for): + +* ``djlint-django`` for Django templates: + +This will look for files matching ``templates/**.html`` and set ``--profile=django``. + +* ``djlint-jinja`` + +This will look for files matching ``*.j2`` and set ``--profile=jinja``. + +* ``djlint-nunjucks`` + +This will look for files matching ``*.njk`` and set ``--profile=nunjucks``. + +* ``djlint-handlebars`` + +This will look for files matching ``*.hbs`` and set ``--profile=handlebars``. + +* ``djlint-golang`` + +This will look for files matching ``*.tmpl`` and set ``--profile=golang``. + +Note that these predefined hooks are sometimes too conservative in the inputs they accept (your templates may be using a different extension) so pre-commit explicitly allows you to override any of these pre-defined options. + +Default Django example +^^^^^^^^^^^^^^^^^^^^^^ + +.. code:: yaml + + repos: + - repo: https://github.com/Riverside-Healthcare/djLint + rev: 0.5.10 # grab latest tag from GitHub + hooks: + - id: djlint-django + + +Handlebars with .html extension instead of .hbs +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +.. code:: yaml + + repos: + - repo: https://github.com/Riverside-Healthcare/djLint + rev: 0.5.10 # grab latest tag from GitHub + hooks: + - id: djlint-handlebars + files: "\\.html" + +You can use the ``files`` or ``exclude`` parameters to constrain each hook to its own directory, allowing you to support multiple templating languages within the same repo. \ No newline at end of file