Merge pull request #163 from terminalkitten/feature/allow-custom-html-tags

[Feature] - allow custom html tags
This commit is contained in:
sur.la.route 2022-01-03 08:46:31 -06:00 committed by GitHub
commit c6efcea7ca
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 172 additions and 126 deletions

View file

@ -49,6 +49,17 @@ Usage:
custom_blocks = "toc,example"
custom_html
-----------
Use to indent custom HTML tags. For example ``<mjml>`` or ``<simple-greeting>`` or ``<mj-\w+>``
Usage:
.. code:: ini
custom_html = "mjml,simple-greeting,mj-\\w+"
indent
------

View file

@ -137,6 +137,13 @@ def build_custom_blocks(custom_blocks: Union[str, None]) -> Optional[str]:
return None
def build_custom_html(custom_html: Union[str, None]) -> Optional[str]:
"""Build regex string for custom HTML blocks."""
if custom_html:
return "|" + "|".join(x.strip() for x in custom_html.split(","))
return None
class Config:
"""Djlint Config."""
@ -176,10 +183,15 @@ class Config:
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 ""
)
self.custom_html: str = str(
build_custom_html(djlint_settings.get("custom_html")) or ""
)
self.format_attribute_template_tags: bool = djlint_settings.get(
"format_attribute_template_tags", False
)
@ -312,131 +324,133 @@ class Config:
"""
# all html tags possible
self.indent_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
| textarea
| tfoot
| th
| thead
| time
| title
| tr
| track
| tt
| u
| ul
| var
| video
| wbr
"""
self.indent_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
| textarea
| tfoot
| th
| thead
| time
| title
| tr
| track
| tt
| u
| ul
| var
| video
| wbr
"""
+ self.custom_html
)
self.indent_template_tags: str = (
r""" if
@ -763,6 +777,7 @@ class Config:
| summary
| """
+ self.always_self_closing_html_tags
+ self.custom_html
+ """
"""
)

View file

@ -0,0 +1 @@
<mjml><mj-body>this is a email text</mj-body></mjml>

View file

@ -0,0 +1,3 @@
[tool]
[tool.djlint]
custom_html = "mjml,mj-body"

View file

@ -7,7 +7,7 @@ run::
for a single test, run::
pytest tests/test_config.py::test_indent --cov=src/djlint \
pytest tests/test_config.py::test_custom_html --cov=src/djlint \
--cov-branch --cov-report xml:coverage.xml --cov-report term-missing
"""
@ -34,6 +34,22 @@ def test_custom_tags(runner: CliRunner) -> None:
assert result.exit_code == 1
def test_custom_html(runner: CliRunner) -> None:
result = runner.invoke(djlint, ["tests/config_custom_html/html.html", "--check"])
print(result.output)
assert (
"""-<mjml><mj-body>this is a email text</mj-body></mjml>
+<mjml>
+ <mj-body>
+ this is a email text
+ </mj-body>
+</mjml>
"""
in result.output
)
assert result.exit_code == 1
def test_extension(runner: CliRunner) -> None:
result = runner.invoke(djlint, ["tests/config_extension", "--check"])
assert """Checking""" in result.output