Keep track of how deeply nested our document is.
| 177 | |
| 178 | |
| 179 | class ElementStack: |
| 180 | """ |
| 181 | Keep track of how deeply nested our document is. |
| 182 | """ |
| 183 | |
| 184 | def __init__(self): |
| 185 | self.open_tags = [] |
| 186 | self.indent = "" |
| 187 | |
| 188 | def push_tag(self, tag: str): |
| 189 | if len(self.open_tags) > 16: |
| 190 | return |
| 191 | self.open_tags.append(tag) |
| 192 | if tag not in NO_INDENT_TAGS: |
| 193 | self.indent += " " * INDENT |
| 194 | |
| 195 | def pop_tag(self, tag: str): |
| 196 | if tag in self.open_tags: |
| 197 | remove_indent = 0 |
| 198 | while True: |
| 199 | t = self.open_tags.pop() |
| 200 | if t not in NO_INDENT_TAGS: |
| 201 | remove_indent += INDENT |
| 202 | if t == tag: |
| 203 | break |
| 204 | self.indent = self.indent[:-remove_indent] |
| 205 | else: |
| 206 | pass # this closing tag has no start tag. let's keep indentation as-is. |
| 207 | |
| 208 | |
| 209 | def format_xml(tokens: Iterable[Token]) -> str: |
no outgoing calls
no test coverage detected
searching dependent graphs…