From e50ce6bb58e08ee22cd96e1cc6f8d358c1f6a54b Mon Sep 17 00:00:00 2001 From: michalpokusa <72110769+michalpokusa@users.noreply.github.com> Date: Thu, 7 Mar 2024 23:11:39 +0000 Subject: [PATCH 01/18] Moved to offset based teplate processing logic, Token class, function for underlining token in template --- adafruit_templateengine.py | 257 ++++++++++++++++++++++++++++--------- 1 file changed, 195 insertions(+), 62 deletions(-) diff --git a/adafruit_templateengine.py b/adafruit_templateengine.py index 21234cf..1de3ef0 100644 --- a/adafruit_templateengine.py +++ b/adafruit_templateengine.py @@ -57,6 +57,17 @@ class Language: # pylint: disable=too-few-public-methods """Markdown language""" +class Token: # pylint: disable=too-few-public-methods + """Stores a token with its position in a template.""" + + def __init__(self, template: str, start_position: int, end_position: int): + self.template = template + self.start_position = start_position + self.end_position = end_position + + self.content = template[start_position:end_position] + + def safe_html(value: Any) -> str: """ Encodes unsafe symbols in ``value`` to HTML entities and returns the string that can be safely @@ -191,6 +202,64 @@ def _find_named_endblock(template: str, name: str): return re.search(r"{% endblock " + name + r" %}", template) +def _underline_token_in_template( + token: Token, *, lines_around: int = 5, symbol: str = "^" +) -> str: + """ + Return ``number_of_lines`` lines before and after the token, with the token content underlined + with ``symbol`` e.g.: + + ```html + [8 lines skipped] + Shopping list: +