Merge pull request #120 from hrjakobsen/require_pragma

Add require_pragma option
This commit is contained in:
sur.la.route 2021-10-13 18:10:07 +03:00 committed by GitHub
commit ed899525cb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 195 additions and 0 deletions

View file

@ -104,3 +104,26 @@ Usage:
.. code:: ini
profile="django"
require_pragma
--------------
Only format files that starts with a comment with only the word 'format'. The comment can be a HTML comment or a comment in the template language defined by the profile setting. If no profile is specified, a comment in any of the template languages is accepted.
Usage:
.. code:: ini
require_pragma = true
.. code:: html
<!-- format -->
or
{# format #}
or
{% comment %} format {% endcomment %}
or
{{ /* format */ }}
or
{{!-- format --}}

View file

@ -200,6 +200,11 @@ def build_quantity_tense(size: int) -> str:
type=str,
help="Enable defaults by template language. ops: django, jinja, nunjucks, handlebars, golang",
)
@click.option(
"--require-pragma",
is_flag=True,
help="Only formats files that starts with a comment with only the word 'format'",
)
def main(
src: List[str],
extension: str,
@ -209,6 +214,7 @@ def main(
check: bool,
quiet: bool,
profile: str,
require_pragma: str,
) -> None:
"""djLint · lint and reformat HTML templates."""
config = Config(
@ -218,6 +224,7 @@ def main(
indent=indent,
quiet=quiet,
profile=profile,
require_pragma=require_pragma,
)
temp_file = None

View file

@ -5,17 +5,56 @@ Much code is borrowed from https://github.com/rareyman/HTMLBeautify, many thanks
import difflib
from pathlib import Path
import regex as re
from .formatter.compress_html import compress_html
from .formatter.expand_html import expand_html
from .formatter.indent_html import indent_html
from .settings import Config
html_patterns = [re.compile(r"<!--\s*format\s*-->")]
django_jinja_patterns = [
re.compile(r"\{#\s*format\s*#\}"),
re.compile(r"\{%\s*comment\s*%\}\s*format\s*\{%\s*endcomment\s*%\}"),
]
nunjucks_patterns = [re.compile(r"\{#\s*format\s*#\}")]
handlebars_patterns = [re.compile(r"\{\{!--\s*format\s*--\}\}")]
golang_patterns = [
re.compile(r"\{\{\s*/\*\s*format\s*\*/\s*\}\}"),
re.compile(r"\{\{-\s*/\*\s*format\s*/\*\s*-\}\}"),
]
def has_pragma(config: Config, html_str: str):
first_line = html_str.partition("\n")[0]
pragma_patterns = {
"django": django_jinja_patterns + html_patterns,
"jinja": django_jinja_patterns + html_patterns,
"nunjucks": nunjucks_patterns + html_patterns,
"handlebars": handlebars_patterns + html_patterns,
"golang": golang_patterns + html_patterns,
"all": django_jinja_patterns
+ nunjucks_patterns
+ handlebars_patterns
+ golang_patterns
+ html_patterns,
}
for pattern in pragma_patterns[config.profile]:
if re.match(pattern, first_line):
return True
return False
def reformat_file(config: Config, check: bool, this_file: Path) -> dict:
"""Reformat html file."""
rawcode = this_file.read_text(encoding="utf8")
if config.require_pragma and not has_pragma(config, rawcode):
# The file should not be reformatted
return {this_file: []}
expanded = expand_html(rawcode, config)
compressed = compress_html(expanded, config)
indented = indent_html(compressed, config)

View file

@ -117,6 +117,7 @@ class Config:
indent: Optional[int] = None,
quiet: Optional[bool] = False,
profile: Optional[str] = None,
require_pragma: Optional[bool] = False,
):
djlint_settings = load_pyproject_settings(Path(src))
@ -124,6 +125,10 @@ class Config:
# custom configuration options
self.extension: str = str(extension or djlint_settings.get("extension", "html"))
self.quiet: str = str(quiet or djlint_settings.get("quiet", ""))
self.require_pragma: bool = (
require_pragma
or str(djlint_settings.get("require_pragma", "false")).lower() == "true"
)
self.custom_blocks: str = str(
build_custom_blocks(djlint_settings.get("custom_blocks")) or ""
)

View file

@ -0,0 +1,2 @@
<!-- format -->
{% extends "nothing.html" %}{% load stuff %}{% load stuff 2 %}{% include "html_two.html" %}<div></div>

View file

@ -0,0 +1,5 @@
{{ /* format */ }}
<h1>Test</h1><p>{{ .Variable }}</p>
{{ range .Items }} <p>{{ . }}
</p>{{ end }}

View file

@ -0,0 +1 @@
{% extends "nothing.html" %}{% load stuff %}{% load stuff 2 %}{% include "html_two.html" %}<div></div>

View file

@ -0,0 +1,2 @@
{% comment %} format {% endcomment %}
{% extends "nothing.html" %}{% load stuff %}{% load stuff 2 %}{% include "html_two.html" %}<div></div>

View file

@ -0,0 +1,4 @@
{{!-- format --}}
<p>
{{firstname}} </p><p>{{lastname}}</p>

View file

@ -0,0 +1,2 @@
{# format #}
{% extends "nothing.html" %}{% load stuff %}{% load stuff 2 %}{% include "html_two.html" %}<div></div>

View file

@ -0,0 +1,3 @@
[tool]
[tool.djlint]
require_pragma=true

View file

@ -183,3 +183,105 @@ def test_profile(runner: CliRunner) -> None:
+{{ test }}"""
in result.output
)
def test_require_pragma(runner: CliRunner) -> None:
result = runner.invoke(
djlint, ["tests/config_pragmas/html_one.html", "--check", "--profile", "django"]
)
assert """0 files would be updated.""" in result.output
assert result.exit_code == 0
result = runner.invoke(
djlint, ["tests/config_pragmas/html_two.html", "--check", "--profile", "django"]
)
assert (
""" {# format #}
-{% extends "nothing.html" %}{% load stuff %}{% load stuff 2 %}{% include "html_two.html" %}<div></div>
+{% extends "nothing.html" %}
+{% load stuff %}
+{% load stuff 2 %}
+{% include "html_two.html" %}
+<div></div>"""
in result.output
)
assert """1 file would be updated.""" in result.output
assert result.exit_code == 1
result = runner.invoke(
djlint,
["tests/config_pragmas/html_three.html", "--check", "--profile", "handlebars"],
)
assert (
""" {{!-- format --}}
<p>
-
-{{firstname}} </p><p>{{lastname}}</p>
+ {{firstname}}
+</p>
+<p>
+ {{lastname}}
+</p>"""
in result.output
)
assert """1 file would be updated.""" in result.output
assert result.exit_code == 1
result = runner.invoke(
djlint,
["tests/config_pragmas/html_four.html", "--check", "--profile", "golang"],
)
assert (
""" {{ /* format */ }}
-<h1>Test</h1><p>{{ .Variable }}</p>
-{{ range .Items }} <p>{{ . }}
-
-</p>{{ end }}
+<h1>Test</h1>
+<p>
+ {{ .Variable }}
+</p>
+{{ range .Items }}
+<p>
+ {{ . }}
+</p>
+{{ end }}
1 file would be updated."""
in result.output
)
assert """1 file would be updated.""" in result.output
assert result.exit_code == 1
result = runner.invoke(djlint, ["tests/config_pragmas/html_five.html", "--check"])
assert (
""" <!-- format -->
-{% extends "nothing.html" %}{% load stuff %}{% load stuff 2 %}{% include "html_two.html" %}<div></div>
+{% extends "nothing.html" %}
+{% load stuff %}
+{% load stuff 2 %}
+{% include "html_two.html" %}
+<div></div>"""
in result.output
)
assert """1 file would be updated.""" in result.output
assert result.exit_code == 1
result = runner.invoke(
djlint, ["tests/config_pragmas/html_six.html", "--check", "--profile", "django"]
)
assert (
""" {% comment %} format {% endcomment %}
-{% extends "nothing.html" %}{% load stuff %}{% load stuff 2 %}{% include "html_two.html" %}<div></div>
+{% extends "nothing.html" %}
+{% load stuff %}
+{% load stuff 2 %}
+{% include "html_two.html" %}
+<div></div>"""
in result.output
)
assert """1 file would be updated.""" in result.output
assert result.exit_code == 1