From c539b8f72b9d47453a09febe91ec551e11252f80 Mon Sep 17 00:00:00 2001 From: Asko Nõmm Date: Fri, 27 Dec 2024 22:55:13 +0200 Subject: bump --- htmtl/htmtl.py | 151 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 151 insertions(+) create mode 100644 htmtl/htmtl.py (limited to 'htmtl/htmtl.py') diff --git a/htmtl/htmtl.py b/htmtl/htmtl.py new file mode 100644 index 0000000..4a50043 --- /dev/null +++ b/htmtl/htmtl.py @@ -0,0 +1,151 @@ +from __future__ import annotations +from typing import Dict, Any, Tuple, Callable, Optional + + +class Node: + name: str + attributes: Dict[str, str] + children: list[Node] + + def __init__(self, name: str, attributes: Dict[str, str], children: list[Node]): + self.name = name + self.attributes = attributes + self.children = [] + + +class IRBlockPosNode: + name: str + coords: Tuple[int, int] + moved: bool + children: list[IRBlockPosNode] + + def __init__(self, name: str, coords: Tuple[int, int]): + self.name = name + self.coords = coords + + +class IRBlockNode: + name: str + children: list[IRBlockNode] + + def __init__(self, name: str, children: list[IRBlockNode]): + self.name = name + self.moved = False + self.children = children + + +class Htmtl: + __template: str = "" + __ir_block_pos_nodes: list[IRBlockPosNode] = [] + __ir_block_nodes: list[IRBlockNode] = [] + __block_elements = [ + "div", "span", "a" + ] + __inline_elements = [ + "img" + ] + + def __init__(self, template: str): + self.__template = template + self.__ir_block_pos_nodes = [] + self.__ir_block_nodes = [] + self.__parse_ir_block_pos_nodes() + self.__join_ir_block_pos_nodes() + self.__parse_ir_block_nodes() + + def __parse_ir_block_pos_nodes(self): + start = None + end = None + + for idx, part in enumerate(self.__template): + if part == "<": + start = idx + + if part == ">": + end = idx + 1 + + if start is not None and end is not None: + tag = self.__template[start:end] + + if tag.startswith(" list[Tuple[int, IRBlockPosNode]]: + found_block_position_nodes = [] + [start, end] = coords + + for idx, ir_block_position_node in enumerate(self.__ir_block_pos_nodes): + [iter_start, iter_end] = ir_block_position_node.coords + + if iter_start > start and iter_end < end: + found_block_position_nodes.append((idx, ir_block_position_node)) + + return found_block_position_nodes + + def __parse_ir_block_nodes(self): + pass + + @staticmethod + def __find_last_match(arr: list[Any], condition: Callable[[Any], bool]) -> Optional[Tuple[int, Any]]: + idx = len(arr) + + for item in reversed(arr): + idx -= 1 + + if condition(item): + return idx, item + + return None + + def toHtml(self): + pass -- cgit v1.2.3