Skip to content

Commit

Permalink
feat(formatter): added option to disable newlines after yaml front ma…
Browse files Browse the repository at this point in the history
…tter

closes #662
  • Loading branch information
christopherpickering committed May 22, 2023
1 parent faba4f4 commit 9bf64ac
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 14 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": "no_line_after_yaml",
"description": {
"en": "Do not add a blank line after yaml front matter.",
"ru": "Не добавляйте пустую строку после лицевой части yaml.",
"fr": "N'ajoutez pas de ligne vierge après la matière première yaml."
},
"usage": [
{
"name": "pyproject.toml",
"value": "no_line_after_yaml=true"
},
{
"name": ".djlintrc",
"value": "\"no_line_after_yaml\": true"
},
{
"name": "cli",
"value": "--no-line-after-yaml"
}
],
"tags": ["formatter"]
},
{
"name": "close_void_tags",
"description": {
Expand Down
2 changes: 2 additions & 0 deletions docs/src/docs/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ Options:
--indent-js INTEGER Set JS indent level.
--close-void-tags Add closing mark on known void tags. Ex:
<img> becomse <img />
--no-line-after-yaml Do not add a blank line after yaml front
matter.
-h, --help Show this message and exit.
```
Expand Down
2 changes: 2 additions & 0 deletions docs/src/fr/docs/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ Options:
--indent-js INTEGER Set JS indent level.
--close-void-tags Add closing mark on known void tags. Ex:
<img> becomse <img />
--no-line-after-yaml Do not add a blank line after yaml front
matter.
-h, --help Show this message and exit.
```
Expand Down
2 changes: 2 additions & 0 deletions docs/src/ru/docs/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ Options:
--indent-js INTEGER Set JS indent level.
--close-void-tags Add closing mark on known void tags. Ex:
<img> becomse <img />
--no-line-after-yaml Do not add a blank line after yaml front
matter.
-h, --help Show this message and exit.
```
Expand Down
7 changes: 7 additions & 0 deletions src/djlint/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,11 @@
is_flag=True,
help="Add closing mark on known void tags. Ex: <img> becomse <img />",
)
@click.option(
"--no-line-after-yaml",
is_flag=True,
help="Do not add a blank line after yaml front matter.",
)
@colorama_text(autoreset=True)
def main(
src: List[str],
Expand Down Expand Up @@ -270,6 +275,7 @@ def main(
indent_css: Optional[int],
indent_js: Optional[int],
close_void_tags: bool,
no_line_after_yaml: bool,
) -> None:
"""djLint · HTML template linter and formatter."""
config = Config(
Expand Down Expand Up @@ -309,6 +315,7 @@ def main(
indent_css=indent_css,
indent_js=indent_js,
close_void_tags=close_void_tags,
no_line_after_yaml=no_line_after_yaml,
)

temp_file = None
Expand Down
19 changes: 10 additions & 9 deletions src/djlint/formatter/condense.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,16 @@ def yaml_add_blank_line_after(html: str, match: re.Match) -> str:

return match.group()

func = partial(yaml_add_blank_line_after, html)
html = re.sub(
re.compile(
r"(^---.+?---)$",
re.MULTILINE | re.DOTALL,
),
func,
html,
)
if config.no_line_after_yaml is False:
func = partial(yaml_add_blank_line_after, html)
html = re.sub(
re.compile(
r"(^---.+?---)$",
re.MULTILINE | re.DOTALL,
),
func,
html,
)

return html

Expand Down
6 changes: 5 additions & 1 deletion src/djlint/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ def build_custom_blocks(custom_blocks: Union[str, None]) -> Optional[str]:
open_tags = [x.strip() for x in custom_blocks.split(",")]
close_tags = ["end" + x.strip() for x in custom_blocks.split(",")]
# Group all tags together with a negative lookahead.
tags = set([tag + r"\b" for tag in open_tags + close_tags])
tags = {tag + r"\b" for tag in open_tags + close_tags}
return "|" + "|".join(sorted(tags))
return None

Expand Down Expand Up @@ -251,6 +251,7 @@ def __init__(
indent_css: Optional[int] = None,
indent_js: Optional[int] = None,
close_void_tags: bool = False,
no_line_after_yaml: bool = False,
):
self.reformat = reformat
self.check = check
Expand Down Expand Up @@ -322,6 +323,9 @@ def __init__(
self.close_void_tags: bool = close_void_tags or djlint_settings.get(
"close_void_tags", False
)
self.no_line_after_yaml: bool = no_line_after_yaml or djlint_settings.get(
"no_line_after_yaml", False
)

# ignore is based on input and also profile
self.ignore: str = str(ignore or djlint_settings.get("ignore", ""))
Expand Down
24 changes: 20 additions & 4 deletions tests/test_html/test_yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import pytest

from src.djlint.reformat import formatter
from tests.conftest import printer
from tests.conftest import config_builder, printer

test_data = [
pytest.param(
Expand All @@ -30,6 +30,7 @@
" <body></body>\n"
"</html>\n"
),
({}),
id="invalid",
),
pytest.param(
Expand All @@ -49,11 +50,13 @@
" <body></body>\n"
"</html>\n"
),
({}),
id="valid",
),
pytest.param(
("---\n" "layout: <div><div></div></div>\n" "---\n" "<div></div>\n"),
("---\n" "layout: <div><div></div></div>\n" "---\n" "\n" "<div></div>\n"),
({}),
id="more",
),
pytest.param(
Expand All @@ -73,26 +76,31 @@
"\n"
"<h1>Hello world!</h1>\n"
),
({}),
id="custom_parser",
),
pytest.param(
("---\n" "---\n" "<h1>\n" " Hello world!</h1>\n"),
("---\n" "---\n" "\n" "<h1>Hello world!</h1>\n"),
({}),
id="empty",
),
pytest.param(
("---\n" "---\n" "<div>\n" "---\n" "</div>\n"),
("---\n" "---\n" "\n" "<div>---</div>\n"),
({}),
id="empty_2",
),
pytest.param(
("---\n" "---\n\n\n" "<div>\n" "---\n" "</div>\n"),
("---\n" "---\n" "\n" "<div>---</div>\n"),
({}),
id="blank_lines",
),
pytest.param(
("---\n" "---\n\n\n\n" "{{ this }}\n"),
("---\n" "---\n" "\n" "{{ this }}\n"),
({}),
id="blank_lines_2",
),
pytest.param(
Expand All @@ -110,6 +118,7 @@
"\n"
'Test <a href="https://djlint.com">abc</a>.\n'
),
({}),
id="issue_9042_no_empty_line",
),
pytest.param(
Expand All @@ -127,14 +136,21 @@
"\n"
'Test <a href="https://djlint.com">abc</a>.\n'
),
({}),
id="issue_9042",
),
pytest.param(
("---\n" "---\n\n\n\n" "{{ this }}\n"),
("---\n" "---\n" "{{ this }}\n"),
({"no_line_after_yaml": True}),
id="blank_lines_2",
),
]


@pytest.mark.parametrize(("source", "expected"), test_data)
def test_base(source, expected, basic_config):
output = formatter(basic_config, source)
@pytest.mark.parametrize(("source", "expected", "args"), test_data)
def test_base(source, expected, args):
output = formatter(config_builder(args), source)

printer(expected, source, output)
assert expected == output

0 comments on commit 9bf64ac

Please sign in to comment.