diff --git a/html_to_django/formatters/__init__.py b/html_to_django/formatters/__init__.py index 2c991ed..1250135 100644 --- a/html_to_django/formatters/__init__.py +++ b/html_to_django/formatters/__init__.py @@ -1,3 +1,12 @@ +""" +formatters + +This package contains modules that define various formatters for modifying a BeautifulSoup object in place. + +Each formatter module contains a `format` function that performs a specific modification on the BeautifulSoup object. + +The `formatters` list in this package contains all the formatter modules for easy access and usage. +""" 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, style_formatter, style_raw_formatter) diff --git a/html_to_django/formatters/command_formatter.py b/html_to_django/formatters/command_formatter.py index 3ce8193..b8f97d4 100644 --- a/html_to_django/formatters/command_formatter.py +++ b/html_to_django/formatters/command_formatter.py @@ -1,3 +1,16 @@ +""" +command_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-command". The value of "dj-command" is expected to be a +string representing a Django template command. For each found element, it replaces the element's content with the Django +template command, using the value of "dj-command" as the command. Then it removes the element's original tag, leaving +only the Django template command. + +Example: + If an element is
, + after processing, it becomes {% url 'index' %}. +""" from bs4 import BeautifulSoup diff --git a/html_to_django/formatters/for_formatter.py b/html_to_django/formatters/for_formatter.py index b7bc490..6a039de 100644 --- a/html_to_django/formatters/for_formatter.py +++ b/html_to_django/formatters/for_formatter.py @@ -1,3 +1,16 @@ +""" +for_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-for". The value of "dj-for" is expected to be a +string representing a Django template for loop. For each found element, it inserts a Django template for loop +before the element and an endfor tag after the element, using the value of "dj-for" as the loop. Then it removes +the element's original tag, leaving only the Django template for loop. + +Example: + If an element is , + after processing, it becomes {% for item in list %}{% endfor %}. +""" from bs4 import BeautifulSoup diff --git a/html_to_django/formatters/for_wrap_formatter.py b/html_to_django/formatters/for_wrap_formatter.py index 37c9b28..3b1a29a 100644 --- a/html_to_django/formatters/for_wrap_formatter.py +++ b/html_to_django/formatters/for_wrap_formatter.py @@ -1,3 +1,16 @@ +""" +for_wrap_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-for-wrap". The value of "dj-for-wrap" is expected to be a +string representing a Django template for loop. For each found element, it inserts a Django template for loop +before the element and an endfor tag after the element, using the value of "dj-for-wrap" as the loop. Then it removes +the "dj-for-wrap" attribute from the element, leaving the element wrapped in the Django template for loop. + +Example: + If an element is
.
+"""
from bs4 import BeautifulSoup
diff --git a/html_to_django/formatters/style_formatter.py b/html_to_django/formatters/style_formatter.py
index 9f04db6..b0b1a4c 100644
--- a/html_to_django/formatters/style_formatter.py
+++ b/html_to_django/formatters/style_formatter.py
@@ -1,7 +1,48 @@
+"""
+style_formatter.py
+
+This module contains a function `format` that modifies a BeautifulSoup object in place. It also contains two helper
+functions, `parse_style` and `style_to_string`.
+
+The `parse_style` function takes a string of CSS styles and parses it into a dictionary where keys are
+CSS property names and values are the corresponding CSS values.
+
+The `style_to_string` function takes a dictionary of CSS styles and converts it into a string representation.
+
+The `format` function searches for all elements with the attribute "dj-style". For each found element, it parses the
+existing style attributes (if any), merges them with the styles specified in "dj-style", and replaces the
+style attribute with the merged styles. The values of the "dj-style" attribute are expected to be strings with pairs of
+CSS properties, values, and value types separated by semicolons (;), and each triplet separated by tildes (~).
+The value is inserted into the style attribute in Django template variable format (i.e., "{{ value }}").
+If a value type is specified, it is appended to the value in the style attribute.
+The value type can be omitted if not needed. In that case make sure to keep the semicolon at the end of the value.
+Then it removes the "dj-style" attribute from the element.
+
+Example:
+ If an element is Hello, world!
Hello, world!
. +""" from bs4 import BeautifulSoup diff --git a/html_to_django/formatters/var_formatter.py b/html_to_django/formatters/var_formatter.py index 3b2440a..84c131d 100644 --- a/html_to_django/formatters/var_formatter.py +++ b/html_to_django/formatters/var_formatter.py @@ -1,9 +1,16 @@ """ -Find tags with a "dj-var" attribute and replace its content with the -value enclosed in {{ }}. +var_formatter.py + +This module contains a function `format` that modifies a BeautifulSoup object in place. + +The `format` function searches for all elements with the attribute "dj-var". For each found element, it replaces the +element's content with a Django template variable that has the same name as the value of the "dj-var" attribute. +Then it removes the "dj-var" attribute from the element. + +Example: + If an element isHello, world!
, + after processing, it becomes{{ greeting }}
. """ - - from bs4 import BeautifulSoup