diff --git a/html_to_django/formatters/attr_formatter.py b/html_to_django/formatters/attr_formatter.py index cccde21..a4025c9 100644 --- a/html_to_django/formatters/attr_formatter.py +++ b/html_to_django/formatters/attr_formatter.py @@ -1,8 +1,23 @@ +""" +attr_formatter.py + +This module contains a function `format` that modifies a BeautifulSoup object in place. +The function searches for all elements with the attribute "dj-attr". +The value of "dj-attr" is expected to be a string with pairs of attributes and values separated by semicolons (;), +and each pair separated by tildes (~). For each found element, it splits the "dj-attr" value into attribute-value pairs, +formats the value into a Django template variable format (i.e., "{{ value }}"), and assigns it to the corresponding +attribute of the element. After processing all attribute-value pairs, it removes the "dj-attr" attribute from +the element. + +Example: + If an element is
, + after processing, it becomes
. +""" from bs4 import BeautifulSoup def format(soup: BeautifulSoup) -> None: for element in soup.find_all(attrs={"dj-attr": True}): - for tag, value in [attr.split(";") for attr in element["dj-attr"].split("~")]: - element[tag] = f"{{{{ {value} }}}}" + for attribute, value in [pair.split(";") for pair in element["dj-attr"].split("~")]: + element[attribute] = f"{{{{ {value} }}}}" del element.attrs["dj-attr"] diff --git a/html_to_django/formatters/block_formatter.py b/html_to_django/formatters/block_formatter.py index 1d9363c..1526c10 100644 --- a/html_to_django/formatters/block_formatter.py +++ b/html_to_django/formatters/block_formatter.py @@ -1,3 +1,15 @@ +""" +block_formatter.py + +This module contains a function `format` that modifies a BeautifulSoup object in place. +The function searches for all elements with the attribute "dj-block". The value of "dj-block" is expected to be a +string representing the name of a Django block. For each found element, it inserts Django block tags before and after +the element, using the value of "dj-block" as the block name. Then it removes the "dj-block" attribute from the element. + +Example: + If an element is
Content
, + after processing, it becomes {% block content %}Content{% endblock %}. +""" from bs4 import BeautifulSoup