Skip to content

Commit

Permalink
feat(void tags): added option to auto close void tags (linter rule H017)
Browse files Browse the repository at this point in the history
closes #252
  • Loading branch information
christopherpickering committed May 8, 2023
1 parent 249c8e8 commit 9050d30
Show file tree
Hide file tree
Showing 8 changed files with 228 additions and 81 deletions.
23 changes: 23 additions & 0 deletions docs/src/_data/configuration.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,27 @@
[
{
"name": "close_void_tags",
"description": {
"en": "Close void tags.",
"ru": "Закрыть недействительные теги.",
"fr": "Fermer les étiquettes vides."
},
"usage": [
{
"name": "pyproject.toml",
"value": "close_void_tags=true"
},
{
"name": ".djlintrc",
"value": "\"close_void_tags\": true"
},
{
"name": "cli",
"value": "--close-void-tags"
}
],
"tags": ["formatter"]
},
{
"name": "ignore",
"description": {
Expand Down
77 changes: 51 additions & 26 deletions docs/src/docs/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,40 +24,65 @@ npm i djlint

djLint is a command line application. See `configuration` for advanced configuration.

{% raw %}

```bash
Usage: djlint [OPTIONS] SRC ...

djLint · lint and reformat HTML templates.

Options:
--version Show the version and exit.
-e, --extension TEXT File extension to check [default: html]
-i, --ignore TEXT Codes to ignore. ex: "H014,H017"
--include TEXT Codes to include. ex: "H014,H017"
--reformat Reformat the file(s).
--check Check formatting on the file(s).
--indent INTEGER Indent spacing. [default: 4]
--quiet Do not print diff when reformatting.
--profile TEXT Enable defaults by template language. ops: django,
jinja, nunjucks, handlebars, golang, angular,
html [default: html]
--require-pragma Only format or lint files that starts with a comment
with the text 'djlint:on'
--lint Lint for common issues. [default option]
--use-gitignore Use .gitignore file to extend excludes.
--warn Return errors as warnings.
--preserve-leading-space Attempt to preserve leading space on text.
--preserve-blank-lines Attempt to preserve blank lines.
--format-css Also format contents of <style> tags.
--format-js Also format contents of <script> tags.
--configuration PATH Path to global configuration file in .djlintrc format
--statistics Count the number of occurrences of each
error/warning code.
--ignore-case Do not fix case on known html tags.
--ignore-blocks Comma list of template blocks to not indent.
-h, --help Show this message and exit.
--version Show the version and exit.
-e, --extension TEXT File extension to check [default: html]
-i, --ignore TEXT Codes to ignore. ex: "H014,H017"
--reformat Reformat the file(s).
--check Check formatting on the file(s).
--indent INTEGER Indent spacing. [default: 4]
--quiet Do not print diff when reformatting.
--profile TEXT Enable defaults by template language. ops:
django, jinja, nunjucks, handlebars, golang,
angular, html [default: html]
--require-pragma Only format or lint files that starts with a
comment with the text 'djlint:on'
--lint Lint for common issues. [default option]
--use-gitignore Use .gitignore file to extend excludes.
--warn Return errors as warnings.
--preserve-leading-space Attempt to preserve leading space on text.
--preserve-blank-lines Attempt to preserve blank lines.
--format-css Also format contents of <style> tags.
--format-js Also format contents of <script> tags.
--configuration PATH Path to global configuration file in
.djlintrc format
--statistics Count the number of occurrences of each
error/warning code.
--include TEXT Codes to include. ex: "H014,H017"
--ignore-case Do not fix case on known html tags.
--ignore-blocks TEXT Comma list of template blocks to not indent.
--blank-line-after-tag TEXT Add an additional blank line after {% <tag>
... %} tag groups.
--blank-line-before-tag TEXT Add an additional blank line before {% <tag>
... %} tag groups.
--custom-blocks TEXT Indent custom template blocks. For example
{% toc %}...{% endtoc %}
--custom-html TEXT Indent custom HTML tags. For example <mjml>
--exclude TEXT Override the default exclude paths.
--extend-exclude TEXT Add additional paths to the default exclude.
--linter-output-format TEXT Customize order of linter output message.
--max-line-length INTEGER Max line length. [default: 120]
--max-attribute-length INTEGER Max attribute length. [default: 70]
--format-attribute-template-tags
Attempt to format template syntax inside of
tag attributes.
--per-file-ignores <TEXT TEXT>...
Ignore linter rules on a per-file basis.
--indent-css INTEGER Set CSS indent level.
--indent-js INTEGER Set JS indent level.
--close-void-tags Add closing mark on known void tags. Ex:
<img> becomse <img />
-h, --help Show this message and exit.
```
{% endraw %}
{% admonition
"note",
"Note",
Expand Down
78 changes: 52 additions & 26 deletions docs/src/fr/docs/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,40 +24,66 @@ npm i djlint

djLint est une application en ligne de commande. Voir `configuration` pour une configuration avancée.

{% raw %}

```bash
Usage: djlint [OPTIONS] SRC ...

djLint · lint and reformat HTML templates.

Options:
--version Show the version and exit.
-e, --extension TEXT File extension to check [default: html]
-i, --ignore TEXT Codes to ignore. ex: "H014,H017"
--include TEXT Codes to include. ex: "H014,H017"
--reformat Reformat the file(s).
--check Check formatting on the file(s).
--indent INTEGER Indent spacing. [default: 4]
--quiet Do not print diff when reformatting.
--profile TEXT Enable defaults by template language. ops: django,
jinja, nunjucks, handlebars, golang, angular,
html [default: html]
--require-pragma Only format or lint files that starts with a comment
with the text 'djlint:on'
--lint Lint for common issues. [default option]
--use-gitignore Use .gitignore file to extend excludes.
--warn Return errors as warnings.
--preserve-leading-space Attempt to preserve leading space on text.
--preserve-blank-lines Attempt to preserve blank lines.
--format-css Also format contents of <style> tags.
--format-js Also format contents of <script> tags.
--configuration PATH Path to global configuration file in .djlintrc format
--statistics Count the number of occurrences of each
error/warning code.
--ignore-case Do not fix case on known html tags.
--ignore-blocks Comma list of template blocks to not indent.
-h, --help Show this message and exit.
--version Show the version and exit.
-e, --extension TEXT File extension to check [default: html]
-i, --ignore TEXT Codes to ignore. ex: "H014,H017"
--reformat Reformat the file(s).
--check Check formatting on the file(s).
--indent INTEGER Indent spacing. [default: 4]
--quiet Do not print diff when reformatting.
--profile TEXT Enable defaults by template language. ops:
django, jinja, nunjucks, handlebars, golang,
angular, html [default: html]
--require-pragma Only format or lint files that starts with a
comment with the text 'djlint:on'
--lint Lint for common issues. [default option]
--use-gitignore Use .gitignore file to extend excludes.
--warn Return errors as warnings.
--preserve-leading-space Attempt to preserve leading space on text.
--preserve-blank-lines Attempt to preserve blank lines.
--format-css Also format contents of <style> tags.
--format-js Also format contents of <script> tags.
--configuration PATH Path to global configuration file in
.djlintrc format
--statistics Count the number of occurrences of each
error/warning code.
--include TEXT Codes to include. ex: "H014,H017"
--ignore-case Do not fix case on known html tags.
--ignore-blocks TEXT Comma list of template blocks to not indent.
--blank-line-after-tag TEXT Add an additional blank line after {% <tag>
... %} tag groups.
--blank-line-before-tag TEXT Add an additional blank line before {% <tag>
... %} tag groups.
--custom-blocks TEXT Indent custom template blocks. For example
{% toc %}...{% endtoc %}
--custom-html TEXT Indent custom HTML tags. For example <mjml>
--exclude TEXT Override the default exclude paths.
--extend-exclude TEXT Add additional paths to the default exclude.
--linter-output-format TEXT Customize order of linter output message.
--max-line-length INTEGER Max line length. [default: 120]
--max-attribute-length INTEGER Max attribute length. [default: 70]
--format-attribute-template-tags
Attempt to format template syntax inside of
tag attributes.
--per-file-ignores <TEXT TEXT>...
Ignore linter rules on a per-file basis.
--indent-css INTEGER Set CSS indent level.
--indent-js INTEGER Set JS indent level.
--close-void-tags Add closing mark on known void tags. Ex:
<img> becomse <img />
-h, --help Show this message and exit.
```
{% endraw %}
{% admonition
"note",
"Note",
Expand Down
78 changes: 52 additions & 26 deletions docs/src/ru/docs/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,40 +24,66 @@ npm i djlint

djLint - это приложение командной строки. Для расширенной настройки смотрите `конфигурация`.

{% raw %}

```bash
Usage: djlint [OPTIONS] SRC ...

djLint · lint and reformat HTML templates.

Options:
--version Show the version and exit.
-e, --extension TEXT File extension to check [default: html]
-i, --ignore TEXT Codes to ignore. ex: "H014,H017"
--include TEXT Codes to include. ex: "H014,H017"
--reformat Reformat the file(s).
--check Check formatting on the file(s).
--indent INTEGER Indent spacing. [default: 4]
--quiet Do not print diff when reformatting.
--profile TEXT Enable defaults by template language. ops: django,
jinja, nunjucks, handlebars, golang, angular,
html [default: html]
--require-pragma Only format or lint files that starts with a comment
with the text 'djlint:on'
--lint Lint for common issues. [default option]
--use-gitignore Use .gitignore file to extend excludes.
--warn Return errors as warnings.
--preserve-leading-space Attempt to preserve leading space on text.
--preserve-blank-lines Attempt to preserve blank lines.
--format-css Also format contents of <style> tags.
--format-js Also format contents of <script> tags.
--configuration PATH Path to global configuration file in .djlintrc format
--statistics Count the number of occurrences of each
error/warning code.
--ignore-case Do not fix case on known html tags.
--ignore-blocks Comma list of template blocks to not indent.
-h, --help Show this message and exit.
--version Show the version and exit.
-e, --extension TEXT File extension to check [default: html]
-i, --ignore TEXT Codes to ignore. ex: "H014,H017"
--reformat Reformat the file(s).
--check Check formatting on the file(s).
--indent INTEGER Indent spacing. [default: 4]
--quiet Do not print diff when reformatting.
--profile TEXT Enable defaults by template language. ops:
django, jinja, nunjucks, handlebars, golang,
angular, html [default: html]
--require-pragma Only format or lint files that starts with a
comment with the text 'djlint:on'
--lint Lint for common issues. [default option]
--use-gitignore Use .gitignore file to extend excludes.
--warn Return errors as warnings.
--preserve-leading-space Attempt to preserve leading space on text.
--preserve-blank-lines Attempt to preserve blank lines.
--format-css Also format contents of <style> tags.
--format-js Also format contents of <script> tags.
--configuration PATH Path to global configuration file in
.djlintrc format
--statistics Count the number of occurrences of each
error/warning code.
--include TEXT Codes to include. ex: "H014,H017"
--ignore-case Do not fix case on known html tags.
--ignore-blocks TEXT Comma list of template blocks to not indent.
--blank-line-after-tag TEXT Add an additional blank line after {% <tag>
... %} tag groups.
--blank-line-before-tag TEXT Add an additional blank line before {% <tag>
... %} tag groups.
--custom-blocks TEXT Indent custom template blocks. For example
{% toc %}...{% endtoc %}
--custom-html TEXT Indent custom HTML tags. For example <mjml>
--exclude TEXT Override the default exclude paths.
--extend-exclude TEXT Add additional paths to the default exclude.
--linter-output-format TEXT Customize order of linter output message.
--max-line-length INTEGER Max line length. [default: 120]
--max-attribute-length INTEGER Max attribute length. [default: 70]
--format-attribute-template-tags
Attempt to format template syntax inside of
tag attributes.
--per-file-ignores <TEXT TEXT>...
Ignore linter rules on a per-file basis.
--indent-css INTEGER Set CSS indent level.
--indent-js INTEGER Set JS indent level.
--close-void-tags Add closing mark on known void tags. Ex:
<img> becomse <img />
-h, --help Show this message and exit.
```
{% endraw %}
{% admonition
"note",
"Note",
Expand Down
7 changes: 7 additions & 0 deletions src/djlint/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,11 @@
help="Set JS indent level.",
show_default=False,
)
@click.option(
"--close-void-tags",
is_flag=True,
help="Add closing mark on known void tags. Ex: <img> becomse <img />",
)
@colorama_text(autoreset=True)
def main(
src: List[str],
Expand Down Expand Up @@ -258,6 +263,7 @@ def main(
per_file_ignores: Optional[List[Tuple[str, str]]],
indent_css: Optional[int],
indent_js: Optional[int],
close_void_tags: bool,
) -> None:
"""djLint · HTML template linter and formatter."""
config = Config(
Expand Down Expand Up @@ -295,6 +301,7 @@ def main(
per_file_ignores=per_file_ignores,
indent_css=indent_css,
indent_js=indent_js,
close_void_tags=close_void_tags,
)

temp_file = None
Expand Down
10 changes: 7 additions & 3 deletions src/djlint/formatter/compress.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import regex as re
from HtmlTagNames import html_tag_names
from HtmlVoidElements import html_void_elements

from ..helpers import child_of_unformatted_block
from ..settings import Config
Expand Down Expand Up @@ -42,9 +43,12 @@ def _clean_tag(match: re.Match) -> str:
if match.group(3)
else ""
)
close_braket = (
match.group(4) if "/" not in match.group(4) else f" {match.group(4)}"
)
if tag.lower() in html_void_elements and config.close_void_tags:
close_braket = " />"
else:
close_braket = (
match.group(4) if "/" not in match.group(4) else f" {match.group(4)}"
)

return f"{open_braket}{tag}{attributes}{close_braket}"

Expand Down
5 changes: 5 additions & 0 deletions src/djlint/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ def __init__(
per_file_ignores: Optional[List[Tuple[str, str]]] = None,
indent_css: Optional[int] = None,
indent_js: Optional[int] = None,
close_void_tags: bool = False,
):
self.reformat = reformat
self.check = check
Expand Down Expand Up @@ -315,6 +316,10 @@ def __init__(
"ignore_case", False
)

self.close_void_tags: bool = close_void_tags or djlint_settings.get(
"close_void_tags", False
)

# ignore is based on input and also profile
self.ignore: str = str(ignore or djlint_settings.get("ignore", ""))
self.include: str = str(include or djlint_settings.get("include", ""))
Expand Down
Loading

0 comments on commit 9050d30

Please sign in to comment.