Make the repo a pre-commit hook.

This commit is contained in:
Andre Borie 2021-11-12 00:51:11 +00:00
parent 5e8107cbf2
commit a8af2856a3
2 changed files with 86 additions and 1 deletions

29
.pre-commit-hooks.yaml Normal file
View file

@ -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

View file

@ -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 <https://pre-commit.com>`_ 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.