mirror of
https://github.com/Hopiu/djLint.git
synced 2026-05-02 02:44:43 +00:00
Merge pull request #273 from Riverside-Healthcare/dev
This commit is contained in:
commit
e1303f3bad
13 changed files with 182 additions and 5 deletions
2
.github/pull_request_template.md
vendored
2
.github/pull_request_template.md
vendored
|
|
@ -1,3 +1,5 @@
|
|||
|
||||
|
||||
# Pull Request Check List
|
||||
|
||||
Resolves: #issue-number-here
|
||||
|
|
|
|||
|
|
@ -393,3 +393,28 @@ preserve_leading_space=true
|
|||
}
|
||||
```
|
||||
|
||||
## preserve_blank_lines
|
||||
|
||||
Preserve blank where possible. Ideal for non-html template files where blank lines are intentional.
|
||||
|
||||
Usage:
|
||||
|
||||
**flag**
|
||||
|
||||
```bash
|
||||
--preserve-blank-lines
|
||||
```
|
||||
|
||||
**pyproject.toml**
|
||||
|
||||
```ini
|
||||
preserve_blank_lines=true
|
||||
```
|
||||
|
||||
**.djlintrc**
|
||||
|
||||
```json
|
||||
{
|
||||
"preserve_blank_lines": true
|
||||
}
|
||||
```
|
||||
|
|
|
|||
|
|
@ -392,3 +392,30 @@ preserve_leading_space=true
|
|||
"preserve_leading_space": true
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
## preserve_blank_lines
|
||||
|
||||
Préserve les blancs lorsque cela est possible. Idéal pour les fichiers de modèles non-html où les lignes vides sont intentionnelles.
|
||||
|
||||
Utilisation:
|
||||
|
||||
**flag**
|
||||
|
||||
```bash
|
||||
--preserve-blank-lines
|
||||
```
|
||||
|
||||
**pyproject.toml**
|
||||
|
||||
```ini
|
||||
preserve_blank_lines=true
|
||||
```
|
||||
|
||||
**.djlintrc**
|
||||
|
||||
```json
|
||||
{
|
||||
"preserve_blank_lines": true
|
||||
}
|
||||
```
|
||||
|
|
|
|||
|
|
@ -388,3 +388,30 @@ preserve_leading_space=true
|
|||
"preserve_leading_space": true
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
## preserve_blank_lines
|
||||
|
||||
Сохраняйте пробелы там, где это возможно. Идеально подходит для не-html файлов шаблонов, где пустые строки являются намеренными.
|
||||
|
||||
Использование:
|
||||
|
||||
**flag**
|
||||
|
||||
```bash
|
||||
--preserve-blank-lines
|
||||
```
|
||||
|
||||
**pyproject.toml**
|
||||
|
||||
```ini
|
||||
preserve_blank_lines=true
|
||||
```
|
||||
|
||||
**.djlintrc**
|
||||
|
||||
```json
|
||||
{
|
||||
"preserve_blank_lines": true
|
||||
}
|
||||
```
|
||||
|
|
|
|||
|
|
@ -99,6 +99,11 @@ from .src import get_src
|
|||
is_flag=True,
|
||||
help="Attempt to preserve leading space on text.",
|
||||
)
|
||||
@click.option(
|
||||
"--preserve-blank-lines",
|
||||
is_flag=True,
|
||||
help="Attempt to preserve blank lines.",
|
||||
)
|
||||
@colorama_text(autoreset=True)
|
||||
def main(
|
||||
src: List[str],
|
||||
|
|
@ -114,6 +119,7 @@ def main(
|
|||
use_gitignore: bool,
|
||||
warn: bool,
|
||||
preserve_leading_space: bool,
|
||||
preserve_blank_lines: bool,
|
||||
) -> None:
|
||||
"""djLint · HTML template linter and formatter."""
|
||||
config = Config(
|
||||
|
|
@ -130,6 +136,7 @@ def main(
|
|||
use_gitignore=use_gitignore,
|
||||
warn=warn,
|
||||
preserve_leading_space=preserve_leading_space,
|
||||
preserve_blank_lines=preserve_blank_lines,
|
||||
)
|
||||
|
||||
temp_file = None
|
||||
|
|
|
|||
|
|
@ -25,16 +25,35 @@ def condense_html(html: str, config: Config) -> str:
|
|||
|
||||
func = partial(strip_space, config, html)
|
||||
|
||||
line_contents = r"(.*?)"
|
||||
trailing_contents = r"\n \t"
|
||||
|
||||
if config.preserve_blank_lines:
|
||||
line_contents = r"([^\n]+?)"
|
||||
trailing_contents = r" \t"
|
||||
|
||||
if not config.preserve_leading_space:
|
||||
# remove any leading/trailing space
|
||||
html = re.sub(re.compile(r"^[ \t]*(.*?)[\n \t]*$", re.M), func, html)
|
||||
|
||||
html = re.sub(
|
||||
re.compile(rf"^[ \t]*{line_contents}[{trailing_contents}]*$", re.M),
|
||||
func,
|
||||
html,
|
||||
)
|
||||
|
||||
else:
|
||||
# only remove leading space in front of tags
|
||||
# <, {%, {#, {{
|
||||
html = re.sub(
|
||||
re.compile(r"^[ \t]*((?:<|{%|{#|{{).*?)[\n \t]*$", re.M), func, html
|
||||
re.compile(
|
||||
rf"^[ \t]*((?:<|{{%|{{\#|{{{{).*?)[{trailing_contents}]*$", re.M
|
||||
),
|
||||
func,
|
||||
html,
|
||||
)
|
||||
html = re.sub(
|
||||
re.compile(rf"^{line_contents}[{trailing_contents}]*$", re.M), func, html
|
||||
)
|
||||
html = re.sub(re.compile(r"^(.*?)[\n \t]*$", re.M), func, html)
|
||||
|
||||
def if_blank_line_after_match(config: Config, html: str) -> bool:
|
||||
"""Check if there should be a blank line after."""
|
||||
|
|
|
|||
|
|
@ -248,4 +248,7 @@ def indent_html(rawcode: str, config: Config) -> str:
|
|||
# handlebars templates
|
||||
beautified_code = re.sub(r"({{#(?:each|if).+?[^ ])(}})", func, beautified_code)
|
||||
|
||||
return beautified_code.strip() + "\n"
|
||||
if not config.preserve_blank_lines:
|
||||
beautified_code = beautified_code.lstrip()
|
||||
|
||||
return beautified_code.rstrip() + "\n"
|
||||
|
|
|
|||
|
|
@ -184,7 +184,7 @@
|
|||
message: Tag seems to be an orphan.
|
||||
flags: re.I|re.DOTALL
|
||||
patterns:
|
||||
- <((/?(\w+))\b(\"[^\"]*\"|'[^']*'|{{[^}]*}}|{%[^%]*%}|[^'\">{}])*)(?<!/)>
|
||||
- <((/?(\w+))\b(\"[^\"]*\"|'[^']*'|{{[^}]*}}|{%[^%]*%}|{#[^%]*#}|[^'\">{}])*)(?<!/)>
|
||||
- rule:
|
||||
name: H026
|
||||
message: Empty id and class tags can be removed.
|
||||
|
|
|
|||
|
|
@ -192,6 +192,7 @@ class Config:
|
|||
use_gitignore: bool = False,
|
||||
warn: bool = False,
|
||||
preserve_leading_space: bool = False,
|
||||
preserve_blank_lines: bool = False,
|
||||
):
|
||||
|
||||
self.reformat = reformat
|
||||
|
|
@ -234,6 +235,10 @@ class Config:
|
|||
or djlint_settings.get("preserve_leading_space", False)
|
||||
)
|
||||
|
||||
self.preserve_blank_lines: bool = preserve_blank_lines or djlint_settings.get(
|
||||
"preserve_blank_lines", False
|
||||
)
|
||||
|
||||
# ignore is based on input and also profile
|
||||
self.ignore: str = str(ignore or djlint_settings.get("ignore", ""))
|
||||
|
||||
|
|
|
|||
0
tests/test_config/test_preserve_blank_lines/__init__.py
Normal file
0
tests/test_config/test_preserve_blank_lines/__init__.py
Normal file
28
tests/test_config/test_preserve_blank_lines/html.html
Normal file
28
tests/test_config/test_preserve_blank_lines/html.html
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
|
||||
{% if abc == 101 %}
|
||||
interface Ethernet1/2
|
||||
description // Connected to leaf-2
|
||||
no switchport
|
||||
ip address 10.1.2.1/30
|
||||
ip router ospf 1 area 0.0.0.0
|
||||
no shutdown
|
||||
{% endif %}
|
||||
|
||||
|
||||
{% if abc == 102 %}
|
||||
interface Ethernet1/2
|
||||
description // Connected to leaf-2
|
||||
no switchport
|
||||
ip address 10.1.2.1/30
|
||||
ip router ospf 1 area 0.0.0.0
|
||||
no shutdown
|
||||
{% endif %}
|
||||
|
||||
{% if abc == 103 %}
|
||||
interface Ethernet1/2
|
||||
description // Connected to leaf-2
|
||||
no switchport
|
||||
ip address 10.1.2.1/30
|
||||
ip router ospf 1 area 0.0.0.0
|
||||
no shutdown
|
||||
{% endif %}
|
||||
29
tests/test_config/test_preserve_blank_lines/test_config.py
Normal file
29
tests/test_config/test_preserve_blank_lines/test_config.py
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
"""Djlint tests specific to --preserve-leading-space option.
|
||||
|
||||
run::
|
||||
|
||||
pytest tests/test_config/test_preserve_blank_lines/test_config.py --cov=src/djlint --cov-branch \
|
||||
--cov-report xml:coverage.xml --cov-report term-missing
|
||||
|
||||
pytest tests/test_config/test_preserve_blank_lines/test_config.py::test_config
|
||||
|
||||
"""
|
||||
# pylint: disable=C0116
|
||||
from click.testing import CliRunner
|
||||
|
||||
from src.djlint import main as djlint
|
||||
|
||||
|
||||
def test_config(runner: CliRunner) -> None:
|
||||
|
||||
result = runner.invoke(
|
||||
djlint,
|
||||
[
|
||||
"tests/test_config/test_preserve_blank_lines/html.html",
|
||||
"--check",
|
||||
"--preserve-leading-space",
|
||||
"--preserve-blank-lines",
|
||||
],
|
||||
)
|
||||
|
||||
assert result.exit_code == 0
|
||||
|
|
@ -522,6 +522,11 @@ def test_H025(runner: CliRunner, tmp_file: TextIO) -> None:
|
|||
result = runner.invoke(djlint, [tmp_file.name])
|
||||
assert "H025" not in result.output
|
||||
|
||||
# test {# #} inside tag
|
||||
write_to_file(tmp_file.name, b'<div id="example" {# for #}></div>')
|
||||
result = runner.invoke(djlint, [tmp_file.name])
|
||||
assert "H025" not in result.output
|
||||
|
||||
|
||||
def test_H026(runner: CliRunner, tmp_file: TextIO) -> None:
|
||||
write_to_file(tmp_file.name, b'<asdf id="" >')
|
||||
|
|
|
|||
Loading…
Reference in a new issue