added doc comments to attr_formatter and block_formatter
This commit is contained in:
1 parent
936b752c3b
commit
5fc200a75b
2 files changed
+29
-2
No files matched your search
@@ -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 <div dj-attr="class;my-class~id;my-id"></div>,
|
||||
after processing, it becomes <div class="{{ my-class }}" id="{{ my-id }}"></div>.
|
||||
"""
|
||||
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"]
|
||||
Reference in new issue
Block a user