(tokens: Iterable[Token])
| 207 | |
| 208 | |
| 209 | def format_xml(tokens: Iterable[Token]) -> str: |
| 210 | out = io.StringIO() |
| 211 | |
| 212 | context = ElementStack() |
| 213 | |
| 214 | for prev2, prev1, token, next1, next2 in sliding_window.window(tokens, 2, 2): |
| 215 | if isinstance(token, Tag): |
| 216 | if token.is_opening: |
| 217 | out.write(indent_text(token.data, context.indent)) |
| 218 | |
| 219 | if not is_inline(prev2, prev1, token, next1, next2): |
| 220 | out.write("\n") |
| 221 | |
| 222 | context.push_tag(token.tag) |
| 223 | elif token.is_closing: |
| 224 | context.pop_tag(token.tag) |
| 225 | |
| 226 | if is_inline(prev2, prev1, token, next1, next2): |
| 227 | out.write(token.data) |
| 228 | else: |
| 229 | out.write(indent_text(token.data, context.indent)) |
| 230 | out.write("\n") |
| 231 | |
| 232 | else: # self-closing |
| 233 | out.write(indent_text(token.data, context.indent)) |
| 234 | out.write("\n") |
| 235 | elif isinstance(token, Text): |
| 236 | if is_inline(prev2, prev1, token, next1, next2): |
| 237 | out.write(token.text) |
| 238 | else: |
| 239 | out.write(indent_text(token.data, context.indent)) |
| 240 | out.write("\n") |
| 241 | else: # pragma: no cover |
| 242 | raise RuntimeError() |
| 243 | |
| 244 | return out.getvalue() |
| 245 | |
| 246 | |
| 247 | class XmlHtmlContentview(Contentview): |
no test coverage detected
searching dependent graphs…