more formatting

This commit is contained in:
truePhilipp committed 2024-05-05 20:48:28 +02:00
1 parent 42715de295
commit b042a297b5
4 files changed
+54 -4

No files matched your search

+4 -2
View File
@@ -18,7 +18,9 @@ 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")
file.write(soup.prettify(formatter=bs4.formatter.HTMLFormatter(indent=4))) file.write(soup
.prettify(formatter=bs4.formatter.HTMLFormatter(indent=4))
.replace(""", '"')) # Could this cause any issues? (maybe in attributes)
def convert_dir(path: str, overwrite: bool = False) -> None: def convert_dir(path: str, overwrite: bool = False) -> None:
@@ -29,6 +31,6 @@ def convert_dir(path: str, overwrite: bool = False) -> None:
def command_entry() -> None: def command_entry() -> None:
path = os.path.abspath(sys.argv[1]) path = os.path.abspath(sys.argv[1])
if os.path.isdir(path): if os.path.isdir(path):
convert_dir(path) convert_dir(path, True)
elif os.path.isfile(path): elif os.path.isfile(path):
convert_file(path) convert_file(path)
+4 -2
View File
@@ -1,6 +1,8 @@
from . import (for_formatter, if_formatter, var_formatter, block_formatter, static_formatter, attr_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) for_wrap_formatter, unwrap_formatter, include_formatter, remove_default_formatter, command_formatter,
style_formatter, style_raw_formatter)
formatters = [for_formatter, if_formatter, var_formatter, block_formatter, static_formatter, attr_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] for_wrap_formatter, unwrap_formatter, include_formatter, remove_default_formatter, command_formatter,
style_formatter, style_raw_formatter]
@@ -0,0 +1,34 @@
from bs4 import BeautifulSoup
def parse_style(style: str) -> dict:
result = {}
key_buffer = ""
buffer = ""
for char in style:
if char == ":" and not key_buffer:
key_buffer = buffer
buffer = ""
elif char == ";":
result[key_buffer] = buffer
key_buffer = ""
buffer = ""
elif char != " ":
buffer += char
if key_buffer and buffer:
result[key_buffer] = buffer
return result
def style_to_string(style: dict) -> str:
return ";".join([f"{name}:{value}" for name, value in style.items()])
def format(soup: BeautifulSoup) -> None:
for element in soup.find_all(attrs={"dj-style": True}):
style = parse_style(element["style"])
dj_style = element.get("dj-style").replace(""", '"')
for name, value, value_type in [declaration.split(";") for declaration in dj_style.split("~") if declaration]:
style[name] = f"{{{{ {value} }}}}{value_type}"
element["style"] = style_to_string(style)
del element.attrs["dj-style"]
@@ -0,0 +1,12 @@
from bs4 import BeautifulSoup
from .style_formatter import parse_style, style_to_string
def format(soup: BeautifulSoup) -> None:
for element in soup.find_all(attrs={"dj-style-raw": True}):
style = parse_style(element["style"])
dj_style = element.get("dj-style-raw")
for name, value in [declaration.split(";") for declaration in dj_style.split("~") if declaration]:
style[name] = value
element["style"] = style_to_string(style)
del element.attrs["dj-style-raw"]