|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from html.parser import HTMLParser |
| 4 | +from weakref import ref |
| 5 | + |
| 6 | + |
| 7 | +class TextElement: |
| 8 | + def __init__(self, content, location=None): |
| 9 | + self.content = content |
| 10 | + self.location = location |
| 11 | + |
| 12 | + |
| 13 | +class Comment: |
| 14 | + def __init__(self, content, location=None): |
| 15 | + self.content = content |
| 16 | + self.location = location |
| 17 | + |
| 18 | + |
| 19 | +class Element: |
| 20 | + """Node that represents an element from a CGX file.""" |
| 21 | + |
| 22 | + def __init__(self, tag: str, attrs: dict, location: tuple[int, int]): |
| 23 | + self.tag = tag |
| 24 | + self.attrs = attrs or {} |
| 25 | + self.location = location |
| 26 | + self.end: tuple[int, int] | None = None |
| 27 | + self.data: str | None = None |
| 28 | + self.children: list[Element | Comment | TextElement] = [] |
| 29 | + self.parent: ref | None = None |
| 30 | + |
| 31 | + def child_with_tag(self, tag): |
| 32 | + for child in self.children: |
| 33 | + if getattr(child, "tag", None) == tag: |
| 34 | + return child |
| 35 | + |
| 36 | + |
| 37 | +class CGXParser(HTMLParser): |
| 38 | + """Parser for CGX files. |
| 39 | +
|
| 40 | + Creates a tree of Nodes with all encountered attributes and data. |
| 41 | + """ |
| 42 | + |
| 43 | + def __init__(self, *args, **kwargs): |
| 44 | + super().__init__(*args, **kwargs) |
| 45 | + self.root = Element("root", attrs={}, location=(-1, -1)) |
| 46 | + self.stack = [self.root] |
| 47 | + |
| 48 | + def handle_starttag(self, tag: str, attrs: list[tuple[str, str | None]]): |
| 49 | + # The tag parameter is lower-cased by the HTMLParser. |
| 50 | + # In order to figure out whether the tag indicates |
| 51 | + # an imported class, we need the original casing for |
| 52 | + # the tag. |
| 53 | + # Using the original start tag, we can figure out where |
| 54 | + # the tag is located using a lower-cased version. And then |
| 55 | + # use the index to extract the original casing for the tag. |
| 56 | + complete_tag = self.get_starttag_text() |
| 57 | + index = complete_tag.lower().index(tag) |
| 58 | + original_tag = complete_tag[index : index + len(tag)] |
| 59 | + node = Element(original_tag, attrs=dict(attrs), location=self.getpos()) |
| 60 | + |
| 61 | + # Cast attributes that have no value to boolean (True) |
| 62 | + # so that they function like flags |
| 63 | + for key, value in node.attrs.items(): |
| 64 | + if value is None: |
| 65 | + node.attrs[key] = True |
| 66 | + |
| 67 | + # Add item as child to the last on the stack |
| 68 | + parent = self.stack[-1] |
| 69 | + parent.children.append(node) |
| 70 | + node.parent = ref(parent) |
| 71 | + # Make the new node the last on the stack |
| 72 | + self.stack.append(node) |
| 73 | + |
| 74 | + def handle_endtag(self, tag: str): |
| 75 | + # pop it till popping the same tag in order to |
| 76 | + # work around unclosed tags? |
| 77 | + # Pop the stack until (but not the root!) |
| 78 | + while len(self.stack) > 1: |
| 79 | + node = self.stack.pop() |
| 80 | + node.end = self.getpos() |
| 81 | + if node.tag.lower() == tag: |
| 82 | + break |
| 83 | + |
| 84 | + def handle_data(self, data: str): |
| 85 | + if data.strip(): |
| 86 | + # Add item as child to the last on the stack |
| 87 | + self.stack[-1].children.append( |
| 88 | + TextElement(content=data, location=self.getpos()) |
| 89 | + ) |
| 90 | + |
| 91 | + def handle_comment(self, data: str): |
| 92 | + if data.strip(): |
| 93 | + # Add item as child to the last on the stack |
| 94 | + self.stack[-1].children.append( |
| 95 | + Comment(content=data, location=self.getpos()) |
| 96 | + ) |
0 commit comments