-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathformatting.py
More file actions
34 lines (25 loc) · 879 Bytes
/
formatting.py
File metadata and controls
34 lines (25 loc) · 879 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# Copyright (C) 2013, Thomas Leonard
# See the README file for details, or visit http://0install.net.
from xml.dom import Node
def format_node(node, indent):
"""Ensure that every element is indented by the string 'indent'."""
doc = node.ownerDocument
elems = []
for child in node.childNodes:
if child.nodeType == Node.TEXT_NODE:
data = child.data
if data.count('\n') < 2:
child.data = child.data.strip()
elif not data.strip():
# Preserve blank lines between elements
child.data = '\n' * (data.count('\n') - 1)
elif child.nodeType == Node.ELEMENT_NODE:
elems.append(child)
for elem in elems:
node.insertBefore(doc.createTextNode(indent + ' '), elem)
format_node(elem, indent + ' ')
if elems:
node.appendChild(doc.createTextNode(indent))
def format_doc(doc):
"""Note: modifies 'doc'."""
format_node(doc.documentElement, "\n")