2021-07-23 20:58:24 +00:00
|
|
|
"""Settings for reformater."""
|
2021-07-26 14:05:55 +00:00
|
|
|
# pylint: disable=C0301,C0103
|
2021-07-23 20:58:24 +00:00
|
|
|
# flake8: noqa
|
2021-07-26 14:05:55 +00:00
|
|
|
|
2021-09-08 08:46:18 +00:00
|
|
|
|
|
|
|
|
import logging
|
2021-09-21 11:02:30 +00:00
|
|
|
import re
|
2021-09-08 08:46:18 +00:00
|
|
|
|
|
|
|
|
## get pyproject.toml settings
|
|
|
|
|
from pathlib import Path
|
|
|
|
|
from typing import Dict, Optional, Union
|
|
|
|
|
|
|
|
|
|
import tomlkit
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def find_pyproject(src: Path) -> Optional[Path]:
|
|
|
|
|
"""Search upstream for a pyprojec.toml file."""
|
|
|
|
|
|
|
|
|
|
for directory in [src, *src.resolve().parents]:
|
|
|
|
|
|
|
|
|
|
candidate = directory / "pyproject.toml"
|
|
|
|
|
|
|
|
|
|
if candidate.is_file():
|
|
|
|
|
return candidate
|
|
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def load_pyproject_settings(src: Path) -> Dict:
|
|
|
|
|
"""Load djlint config from pyproject.toml."""
|
|
|
|
|
|
2021-09-08 09:52:16 +00:00
|
|
|
djlint_content: Dict = {}
|
2021-09-08 08:46:18 +00:00
|
|
|
pyproject_file = find_pyproject(src)
|
|
|
|
|
|
|
|
|
|
if pyproject_file:
|
|
|
|
|
content = tomlkit.parse(pyproject_file.read_text())
|
|
|
|
|
try:
|
2021-09-08 09:52:16 +00:00
|
|
|
djlint_content = content["tool"]["djlint"] # type: ignore
|
2021-09-08 08:46:18 +00:00
|
|
|
except KeyError:
|
|
|
|
|
logger.info("No pyproject.toml found.")
|
|
|
|
|
|
|
|
|
|
return djlint_content
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def build_custom_blocks(custom_blocks: Union[str, None]) -> Optional[str]:
|
|
|
|
|
"""Build regex string for custom template blocks."""
|
|
|
|
|
if custom_blocks:
|
|
|
|
|
return "|" + "|".join(x.strip() for x in custom_blocks.split(","))
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Config:
|
|
|
|
|
"""Djling Config."""
|
|
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
|
self,
|
|
|
|
|
src: str,
|
|
|
|
|
ignore: Optional[str] = None,
|
|
|
|
|
extension: Optional[str] = None,
|
2021-09-28 11:42:13 +00:00
|
|
|
indent: Optional[int] = None,
|
2021-09-08 08:46:18 +00:00
|
|
|
quiet: Optional[bool] = False,
|
|
|
|
|
):
|
|
|
|
|
|
2021-09-21 11:02:30 +00:00
|
|
|
djlint_settings = load_pyproject_settings(Path(src))
|
|
|
|
|
|
|
|
|
|
# custom configuration options
|
|
|
|
|
self.extension: str = str(extension or djlint_settings.get("extension", "html"))
|
|
|
|
|
self.ignore: str = str(ignore or djlint_settings.get("ignore", ""))
|
|
|
|
|
self.quiet: str = str(quiet or djlint_settings.get("quiet", ""))
|
|
|
|
|
self.custom_blocks: str = str(
|
|
|
|
|
build_custom_blocks(djlint_settings.get("custom_blocks")) or ""
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# base options
|
2021-09-28 11:42:13 +00:00
|
|
|
self.indent: str = (indent or int(djlint_settings.get("indent", 4))) * " "
|
2021-09-28 11:04:04 +00:00
|
|
|
print(len(self.indent))
|
2021-09-21 11:02:30 +00:00
|
|
|
|
|
|
|
|
default_exclude: str = r"""
|
2021-09-08 08:46:18 +00:00
|
|
|
\.venv
|
|
|
|
|
| venv
|
|
|
|
|
| \.tox
|
|
|
|
|
| \.eggs
|
|
|
|
|
| \.git
|
|
|
|
|
| \.hg
|
|
|
|
|
| \.mypy_cache
|
|
|
|
|
| \.nox
|
|
|
|
|
| \.svn
|
|
|
|
|
| \.bzr
|
|
|
|
|
| _build
|
|
|
|
|
| buck-out
|
|
|
|
|
| build
|
|
|
|
|
| dist
|
|
|
|
|
| \.pants\.d
|
|
|
|
|
| \.direnv
|
|
|
|
|
| node_modules
|
|
|
|
|
| __pypackages__
|
|
|
|
|
"""
|
2021-09-21 11:02:30 +00:00
|
|
|
self.exclude: str = djlint_settings.get("exclude", default_exclude)
|
2021-09-21 11:05:49 +00:00
|
|
|
extend_exclude: str = djlint_settings.get("extend_exclude", "")
|
2021-09-21 11:05:16 +00:00
|
|
|
if extend_exclude:
|
2021-09-21 11:02:30 +00:00
|
|
|
self.exclude += r" | " + r" | ".join(
|
|
|
|
|
re.escape(x.strip()) for x in extend_exclude.split(",")
|
|
|
|
|
)
|
2021-09-08 08:46:18 +00:00
|
|
|
|
2021-09-23 08:19:11 +00:00
|
|
|
# add blank line after load tags
|
|
|
|
|
self.blank_line_after_tag: Optional[str] = djlint_settings.get(
|
|
|
|
|
"blank_line_after_tag", None
|
|
|
|
|
)
|
|
|
|
|
|
2021-09-08 08:46:18 +00:00
|
|
|
# contents of tags will not be formatted, but tags will be formatted
|
|
|
|
|
self.ignored_block_opening: str = r"""
|
|
|
|
|
<style
|
|
|
|
|
| {\*
|
|
|
|
|
| <\?php
|
|
|
|
|
| <script
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
self.ignored_block_closing: str = r"""
|
|
|
|
|
</style
|
|
|
|
|
| \*}
|
|
|
|
|
| \?>
|
|
|
|
|
| </script
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
# contents of tags will not be formated and tags will not be formatted
|
|
|
|
|
self.ignored_group_opening: str = r"""
|
|
|
|
|
<!--
|
|
|
|
|
| [^\{]{\#
|
|
|
|
|
| <pre
|
|
|
|
|
| <textarea
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
self.ignored_group_closing: str = r"""
|
|
|
|
|
-->
|
|
|
|
|
| \#}
|
|
|
|
|
| </pre
|
|
|
|
|
| </textarea
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
# the contents of these tag blocks will be indented, then unindented
|
|
|
|
|
self.tag_indent: str = (
|
|
|
|
|
r"""
|
2021-09-08 09:52:16 +00:00
|
|
|
(?:\{\{\#|\{%-?)[ ]*?
|
2021-09-08 08:46:18 +00:00
|
|
|
(
|
|
|
|
|
if
|
|
|
|
|
| for
|
|
|
|
|
| block
|
|
|
|
|
| else
|
|
|
|
|
| spaceless
|
|
|
|
|
| compress
|
|
|
|
|
| addto
|
|
|
|
|
| language
|
|
|
|
|
| with
|
|
|
|
|
| assets
|
|
|
|
|
| verbatim
|
|
|
|
|
| autoescape
|
|
|
|
|
| comment
|
|
|
|
|
| filter
|
|
|
|
|
| each
|
|
|
|
|
"""
|
|
|
|
|
+ self.custom_blocks
|
|
|
|
|
+ r"""
|
|
|
|
|
)
|
|
|
|
|
| (?:<
|
|
|
|
|
(?:
|
|
|
|
|
html
|
|
|
|
|
| head
|
|
|
|
|
| body
|
|
|
|
|
| div
|
|
|
|
|
| a
|
|
|
|
|
| nav
|
|
|
|
|
| ul
|
|
|
|
|
| ol
|
|
|
|
|
| dl
|
2021-09-16 07:50:13 +00:00
|
|
|
| dd
|
|
|
|
|
| dt
|
2021-09-08 08:46:18 +00:00
|
|
|
| li
|
|
|
|
|
| table
|
|
|
|
|
| thead
|
|
|
|
|
| tbody
|
|
|
|
|
| tr
|
|
|
|
|
| th
|
|
|
|
|
| td
|
|
|
|
|
| blockquote
|
|
|
|
|
| select
|
|
|
|
|
| form
|
|
|
|
|
| option
|
|
|
|
|
| cache
|
|
|
|
|
| optgroup
|
|
|
|
|
| fieldset
|
|
|
|
|
| legend
|
|
|
|
|
| label
|
|
|
|
|
| header
|
|
|
|
|
| main
|
|
|
|
|
| section
|
|
|
|
|
| aside
|
|
|
|
|
| footer
|
|
|
|
|
| figure
|
2021-09-21 09:55:54 +00:00
|
|
|
| figcaption
|
2021-09-08 08:46:18 +00:00
|
|
|
| video
|
|
|
|
|
| span
|
|
|
|
|
| p
|
|
|
|
|
| g
|
|
|
|
|
| svg
|
|
|
|
|
| h\d
|
|
|
|
|
| button
|
|
|
|
|
| img
|
|
|
|
|
| path
|
|
|
|
|
| script
|
|
|
|
|
| style
|
|
|
|
|
| source
|
2021-09-21 09:55:54 +00:00
|
|
|
| details
|
|
|
|
|
| summary
|
2021-09-08 08:46:18 +00:00
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
"""
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
self.tag_unindent: str = r"""^
|
|
|
|
|
(?:
|
|
|
|
|
(?:\{\{\/)
|
2021-09-08 09:52:16 +00:00
|
|
|
| (?:\{%-?[ ]*?end)
|
2021-09-08 08:46:18 +00:00
|
|
|
)
|
|
|
|
|
| (?:</
|
|
|
|
|
(?:
|
|
|
|
|
html
|
|
|
|
|
| head
|
|
|
|
|
| body
|
|
|
|
|
| div
|
|
|
|
|
| a
|
|
|
|
|
| nav
|
|
|
|
|
| ul
|
|
|
|
|
| ol
|
|
|
|
|
| dl
|
2021-09-16 07:50:13 +00:00
|
|
|
| dd
|
|
|
|
|
| dt
|
2021-09-08 08:46:18 +00:00
|
|
|
| li
|
|
|
|
|
| table
|
|
|
|
|
| thead
|
|
|
|
|
| tbody
|
|
|
|
|
| tr
|
|
|
|
|
| th
|
|
|
|
|
| td
|
|
|
|
|
| blockquote
|
|
|
|
|
| select
|
|
|
|
|
| form
|
|
|
|
|
| option
|
|
|
|
|
| optgroup
|
|
|
|
|
| fieldset
|
|
|
|
|
| legend
|
|
|
|
|
| label
|
|
|
|
|
| header
|
|
|
|
|
| cache
|
|
|
|
|
| main
|
|
|
|
|
| section
|
|
|
|
|
| aside
|
|
|
|
|
| footer
|
|
|
|
|
| figure
|
2021-09-21 09:55:54 +00:00
|
|
|
| figcaption
|
2021-09-08 08:46:18 +00:00
|
|
|
| video
|
|
|
|
|
| span
|
|
|
|
|
| p
|
|
|
|
|
| g
|
|
|
|
|
| svg
|
|
|
|
|
| h\d
|
|
|
|
|
| button
|
|
|
|
|
| img
|
|
|
|
|
| path
|
|
|
|
|
| script
|
|
|
|
|
| style
|
|
|
|
|
| source
|
2021-09-21 09:55:54 +00:00
|
|
|
| details
|
|
|
|
|
| summary
|
2021-09-08 08:46:18 +00:00
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
# these tags should be unindented and next line will be indented
|
|
|
|
|
self.tag_unindent_line: str = r"""
|
2021-09-08 09:52:16 +00:00
|
|
|
(?:\{%-?[ ]*?(?:elif|else|empty))
|
2021-09-08 08:46:18 +00:00
|
|
|
| (?:
|
|
|
|
|
\{\{[ ]*?
|
|
|
|
|
(
|
|
|
|
|
(?:else|\^)
|
|
|
|
|
[ ]*?\}\}
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
# reduce empty lines greater than x to 1 line
|
|
|
|
|
self.reduce_extralines_gt = 2
|
|
|
|
|
|
|
|
|
|
# if lines are longer than x
|
|
|
|
|
self.max_line_length = 120
|
|
|
|
|
self.format_long_attributes = True
|
|
|
|
|
|
|
|
|
|
# pattern used to find attributes in a tag
|
2021-09-20 09:33:23 +00:00
|
|
|
# order is important.
|
|
|
|
|
# 1. attributes="{% if %}with if or for statement{% endif %}"
|
|
|
|
|
# 2. attributes="{{ stuff in here }}"
|
|
|
|
|
# 3. {% if %}with if or for statement{% endif %}
|
|
|
|
|
# 4. attributes="normal html"
|
|
|
|
|
# 5. require | checked | otherword | other-word
|
|
|
|
|
# 6. {{ stuff }}
|
|
|
|
|
template_if_for_pattern = (
|
|
|
|
|
r"(?:{%-?\s?(?:if|for)[^}]*?%}(?:.*?{%\s?end(?:if|for)[^}]*?-?%})+?)"
|
|
|
|
|
)
|
|
|
|
|
self.attribute_pattern: str = (
|
|
|
|
|
r"""
|
|
|
|
|
(?:[^\s]+?=(?:\"[^\"]*?"""
|
|
|
|
|
+ template_if_for_pattern
|
|
|
|
|
+ r"""[^\"]*?\"|\'[^\']*?"""
|
|
|
|
|
+ template_if_for_pattern
|
|
|
|
|
+ r"""[^\']*?\'))
|
|
|
|
|
| (?:[^\s]+?=(?:\"[^\"]*?{{.*?}}[^\"]*?\"|\'[^\']*?{{.*?}}[^\']*?\'))
|
|
|
|
|
| """
|
|
|
|
|
+ template_if_for_pattern
|
|
|
|
|
+ r"""
|
2021-09-08 08:46:18 +00:00
|
|
|
| (?:[^\s]+?=(?:\".*?\"|\'.*?\'))
|
|
|
|
|
| required
|
|
|
|
|
| checked
|
|
|
|
|
| [\w|-]+
|
|
|
|
|
| [\w|-]+=[\w|-]+
|
|
|
|
|
| {{.*?}}
|
|
|
|
|
"""
|
2021-09-20 09:33:23 +00:00
|
|
|
)
|
2021-09-08 08:46:18 +00:00
|
|
|
|
|
|
|
|
self.tag_pattern: str = r"""
|
|
|
|
|
(<\w+?[^>]*?)((?:\n[^>]+?)+?)(/?\>)
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
self.start_template_tags: str = (
|
|
|
|
|
r"""
|
|
|
|
|
if
|
|
|
|
|
| for
|
|
|
|
|
| block
|
|
|
|
|
| spaceless
|
|
|
|
|
| compress
|
|
|
|
|
| load
|
|
|
|
|
| assets
|
|
|
|
|
| addto
|
|
|
|
|
| language
|
|
|
|
|
| with
|
|
|
|
|
| assets
|
|
|
|
|
| autoescape
|
|
|
|
|
| comment
|
|
|
|
|
| filter
|
|
|
|
|
| verbatim
|
|
|
|
|
| each
|
|
|
|
|
"""
|
|
|
|
|
+ self.custom_blocks
|
|
|
|
|
+ r"""
|
|
|
|
|
"""
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
self.break_template_tags: str = (
|
|
|
|
|
r"""
|
|
|
|
|
if
|
|
|
|
|
| end
|
|
|
|
|
| for
|
|
|
|
|
| block
|
|
|
|
|
| endblock
|
|
|
|
|
| else
|
|
|
|
|
| spaceless
|
|
|
|
|
| compress
|
|
|
|
|
| load
|
|
|
|
|
| include
|
|
|
|
|
| assets
|
|
|
|
|
| addto
|
|
|
|
|
| language
|
|
|
|
|
| with
|
|
|
|
|
| assets
|
|
|
|
|
| autoescape
|
|
|
|
|
| comment
|
|
|
|
|
| filter
|
|
|
|
|
| elif
|
|
|
|
|
| resetcycle
|
|
|
|
|
| verbatim
|
|
|
|
|
| each
|
|
|
|
|
"""
|
|
|
|
|
+ self.custom_blocks
|
|
|
|
|
+ r"""
|
|
|
|
|
"""
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
self.ignored_blocks: list = [
|
|
|
|
|
r"<(script|style|pre|textarea).*?(?:%s).*?</(\1)>",
|
|
|
|
|
r"<!--.*?(?:%s).*?-->",
|
|
|
|
|
r"{\*.*?(?:%s).*?\*}",
|
2021-09-21 10:50:35 +00:00
|
|
|
r"{\#.*?(?:%s).*?\#}",
|
2021-09-08 08:46:18 +00:00
|
|
|
r"<\?php.*?(?:%s).*?\?>",
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
self.ignored_inline_blocks: str = r"""
|
|
|
|
|
<!--.*?-->
|
|
|
|
|
| {\*.*?\*}
|
|
|
|
|
| {\#.*?\#}
|
|
|
|
|
| <\?php.*?\?>
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
self.single_line_html_tags: str = r"""
|
|
|
|
|
button
|
|
|
|
|
| a
|
|
|
|
|
| h1
|
|
|
|
|
| h2
|
|
|
|
|
| h3
|
|
|
|
|
| h4
|
|
|
|
|
| h5
|
|
|
|
|
| h6
|
|
|
|
|
| td
|
|
|
|
|
| th
|
|
|
|
|
| strong
|
2021-09-16 07:48:41 +00:00
|
|
|
| small
|
2021-09-08 08:46:18 +00:00
|
|
|
| em
|
|
|
|
|
| icon
|
|
|
|
|
| span
|
|
|
|
|
| title
|
|
|
|
|
| link
|
|
|
|
|
| path
|
|
|
|
|
| label
|
|
|
|
|
| div
|
|
|
|
|
| li
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
self.always_single_line_html_tags: str = r"""
|
|
|
|
|
link
|
|
|
|
|
| img
|
|
|
|
|
| meta
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
self.single_line_template_tags: str = r"""
|
|
|
|
|
if
|
|
|
|
|
| for
|
|
|
|
|
| block
|
|
|
|
|
| with
|
|
|
|
|
| comment
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
self.break_html_tags: str = r"""
|
|
|
|
|
a
|
|
|
|
|
| abbr
|
|
|
|
|
| acronym
|
|
|
|
|
| address
|
|
|
|
|
| applet
|
|
|
|
|
| area
|
|
|
|
|
| article
|
|
|
|
|
| aside
|
|
|
|
|
| audio
|
|
|
|
|
| b
|
|
|
|
|
| base
|
|
|
|
|
| basefont
|
|
|
|
|
| bdi
|
|
|
|
|
| bdo
|
|
|
|
|
| big
|
|
|
|
|
| blockquote
|
|
|
|
|
| body
|
|
|
|
|
| br
|
|
|
|
|
| button
|
|
|
|
|
| canvas
|
|
|
|
|
| caption
|
|
|
|
|
| center
|
|
|
|
|
| cite
|
|
|
|
|
| code
|
|
|
|
|
| col
|
|
|
|
|
| colgroup
|
|
|
|
|
| data
|
|
|
|
|
| datalist
|
|
|
|
|
| dd
|
|
|
|
|
| del
|
|
|
|
|
| details
|
|
|
|
|
| dfn
|
|
|
|
|
| dialog
|
|
|
|
|
| dir
|
|
|
|
|
| div
|
|
|
|
|
| dl
|
|
|
|
|
| dt
|
|
|
|
|
| em
|
|
|
|
|
| embed
|
|
|
|
|
| fieldset
|
|
|
|
|
| figcaption
|
|
|
|
|
| figure
|
|
|
|
|
| font
|
|
|
|
|
| footer
|
|
|
|
|
| form
|
|
|
|
|
| frame
|
|
|
|
|
| frameset
|
|
|
|
|
| h1
|
|
|
|
|
| h2
|
|
|
|
|
| h3
|
|
|
|
|
| h4
|
|
|
|
|
| h5
|
|
|
|
|
| h6
|
|
|
|
|
| head
|
|
|
|
|
| header
|
|
|
|
|
| hr
|
|
|
|
|
| html
|
|
|
|
|
| i
|
|
|
|
|
| iframe
|
|
|
|
|
| icon
|
|
|
|
|
| img
|
|
|
|
|
| input
|
|
|
|
|
| ins
|
|
|
|
|
| kbd
|
|
|
|
|
| label
|
|
|
|
|
| legend
|
|
|
|
|
| li
|
|
|
|
|
| link
|
|
|
|
|
| main
|
|
|
|
|
| map
|
|
|
|
|
| mark
|
|
|
|
|
| meta
|
|
|
|
|
| meter
|
|
|
|
|
| nav
|
|
|
|
|
| noframes
|
|
|
|
|
| noscript
|
|
|
|
|
| object
|
|
|
|
|
| ol
|
|
|
|
|
| optgroup
|
|
|
|
|
| option
|
|
|
|
|
| output
|
|
|
|
|
| p
|
|
|
|
|
| path
|
|
|
|
|
| param
|
|
|
|
|
| picture
|
|
|
|
|
| progress
|
|
|
|
|
| q
|
|
|
|
|
| rp
|
|
|
|
|
| rt
|
|
|
|
|
| ruby
|
|
|
|
|
| s
|
|
|
|
|
| samp
|
|
|
|
|
| script
|
|
|
|
|
| section
|
|
|
|
|
| select
|
|
|
|
|
| small
|
|
|
|
|
| source
|
|
|
|
|
| span
|
|
|
|
|
| strike
|
|
|
|
|
| strong
|
|
|
|
|
| style
|
|
|
|
|
| sub
|
|
|
|
|
| summary
|
|
|
|
|
| sup
|
|
|
|
|
| svg
|
|
|
|
|
| table
|
|
|
|
|
| tbody
|
|
|
|
|
| td
|
|
|
|
|
| template
|
|
|
|
|
| tfoot
|
|
|
|
|
| th
|
|
|
|
|
| thead
|
|
|
|
|
| time
|
|
|
|
|
| title
|
|
|
|
|
| tr
|
|
|
|
|
| track
|
|
|
|
|
| tt
|
|
|
|
|
| u
|
|
|
|
|
| ul
|
|
|
|
|
| var
|
|
|
|
|
| video
|
|
|
|
|
| wbr
|
|
|
|
|
"""
|