Render the given :class:`~xml.dom.minidom.Document` `document` into a prettified string.
(document: 'Document',
declaration: Optional[str] = None,
encoding: Optional[str] = UTF8,
indent: int = 2)
| 27 | |
| 28 | |
| 29 | def pretty_xml(document: 'Document', |
| 30 | declaration: Optional[str] = None, |
| 31 | encoding: Optional[str] = UTF8, |
| 32 | indent: int = 2) -> str: |
| 33 | """Render the given :class:`~xml.dom.minidom.Document` `document` into a prettified string.""" |
| 34 | kwargs = { |
| 35 | 'encoding': encoding or UTF8, |
| 36 | 'indent': ' ' * indent, |
| 37 | } |
| 38 | body = document.toprettyxml(**kwargs).decode(kwargs['encoding']) |
| 39 | |
| 40 | # Remove blank lines automatically added by `toprettyxml()`. |
| 41 | lines = [line for line in body.splitlines() if line.strip()] |
| 42 | |
| 43 | # xml.dom automatically adds the declaration, even if |
| 44 | # it is not present in the actual body. Remove it. |
| 45 | if len(lines) >= 1 and parse_declaration(lines[0]): |
| 46 | lines.pop(0) |
| 47 | if declaration: |
| 48 | lines.insert(0, declaration) |
| 49 | |
| 50 | return '\n'.join(lines) |
| 51 | |
| 52 | |
| 53 | class XMLFormatter(FormatterPlugin): |
no test coverage detected