added new formatters

This commit is contained in:
truePhilipp committed 2024-05-05 14:31:34 +02:00
1 parent 043b1e918f
commit 42715de295
8 files changed
+60 -4

No files matched your search

+1 -2
View File
@@ -18,8 +18,7 @@ def convert_file(path: str, overwrite: bool = False) -> None:
out_path = path.replace(".html", ".n.html") out_path = path.replace(".html", ".n.html")
with open(out_path, "w") as file: with open(out_path, "w") as file:
file.write("{% load static %}\n") file.write("{% load static %}\n")
# indent=4 will be re added when https://github.com/python/typeshed/pull/11855 is accepted file.write(soup.prettify(formatter=bs4.formatter.HTMLFormatter(indent=4)))
file.write(soup.prettify(formatter=bs4.formatter.HTMLFormatter()))
def convert_dir(path: str, overwrite: bool = False) -> None: def convert_dir(path: str, overwrite: bool = False) -> None:
+4 -2
View File
@@ -1,4 +1,6 @@
from . import for_formatter, if_formatter, var_formatter, block_formatter, static_formatter from . import (for_formatter, if_formatter, var_formatter, block_formatter, static_formatter, attr_formatter,
for_wrap_formatter, unwrap_formatter, include_formatter, remove_default_formatter, command_formatter)
formatters = [for_formatter, if_formatter, var_formatter, block_formatter, static_formatter] formatters = [for_formatter, if_formatter, var_formatter, block_formatter, static_formatter, attr_formatter,
for_wrap_formatter, unwrap_formatter, include_formatter, remove_default_formatter, command_formatter]
@@ -0,0 +1,8 @@
from bs4 import BeautifulSoup
def format(soup: BeautifulSoup) -> None:
for element in soup.find_all(attrs={"dj-attr": True}):
tag, value = element.get("dj-attr").split(";")
element[tag] = f"{{{{ {value} }}}}"
del element.attrs["dj-attr"]
@@ -0,0 +1,7 @@
from bs4 import BeautifulSoup
def format(soup: BeautifulSoup) -> None:
for element in soup.find_all(attrs={"dj-command": True}):
element.string = f"{{% {element.get("dj-command")} %}}"
element.unwrap()
@@ -0,0 +1,8 @@
from bs4 import BeautifulSoup
def format(soup: BeautifulSoup) -> None:
for element in soup.find_all(attrs={"dj-for-wrap": True}):
element.insert_before(f"{{% for {element.get("dj-for-wrap")} %}}")
element.insert_after("{% endfor %}")
del element["dj-for-wrap"]
@@ -0,0 +1,7 @@
from bs4 import BeautifulSoup
def format(soup: BeautifulSoup) -> None:
for element in soup.find_all(attrs={"dj-include": True}):
element.string = f'{{% include "{element.get("dj-include")}" %}}'
del element.attrs["dj-include"]
@@ -0,0 +1,19 @@
from bs4 import BeautifulSoup, Doctype, Tag
def format(soup: BeautifulSoup) -> None:
if html := soup.find("html"):
if isinstance(html, Tag):
if html.has_attr("dj-remove-default"):
if isinstance(soup.contents[0], Doctype):
soup.contents[0].extract()
if head := html.find("head"):
if isinstance(head, Tag):
head.decompose()
if body := html.find("body"):
if isinstance(body, Tag):
if script := body.find("script"):
if isinstance(script, Tag):
script.decompose()
body.unwrap()
html.unwrap()
@@ -0,0 +1,6 @@
from bs4 import BeautifulSoup
def format(soup: BeautifulSoup) -> None:
for element in soup.find_all(attrs={"dj-unwrap": True}):
element.unwrap()