-
Notifications
You must be signed in to change notification settings - Fork 205
/
xml2rfc-tidy.py
executable file
·144 lines (121 loc) · 3.64 KB
/
xml2rfc-tidy.py
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/env python3
# Tidy an xml2rfc file.
#
# This:
# * removes non-semantic content (comments, processing instructions, DOCTYPE
# declarations, broken entity references)
# * wraps BCP 14 language in <bcp14> elements
# * indents elements neatly
import sys
import xml.sax
import re
from xml.sax.saxutils import escape, quoteattr
class Tidy(xml.sax.handler.ContentHandler):
pattern = re.compile(
r"\b((?:(?:MUST|SHOULD|SHALL)(?:\s+NOT)?)|(?:(?:NOT\s+)?RECOMMENDED)|MAY|OPTIONAL|REQUIRED)\b"
)
def __init__(self):
self.tags = []
self.nesting = 0
self.c = ""
self.state = ""
def startDocument(self):
print('<?xml version="1.0" encoding="UTF-8"?>')
def preserve(tag):
return tag in ["artwork", "sourcecode"]
def textElement(tag):
return tag in [
"annotation",
"blockquote",
"dd",
"dt",
"em",
"li",
"preamble",
"refcontent",
"strong",
"sub",
"sup",
"t",
"td",
"th",
"tt",
]
def inline(tag):
return tag in [
"code",
"contact",
"cref",
"em",
"eref",
"iref",
"sub",
"sup",
"tt",
"xref",
]
def flush(self, tag, start=None):
if Tidy.preserve(tag):
c = f"<![CDATA[{self.c}]]>"
else:
c = escape(self.c)
if Tidy.textElement(tag):
if self.state == "open":
# The element is opening, so strip left is safe.
c = c.lstrip()
if start is None or not Tidy.inline(start):
# The element is closing, or the element that is starting
# isn't inline, so strip right is safe.
c = c.rstrip()
c = Tidy.pattern.sub(r"<bcp14>\1</bcp14>", c)
else:
c = c.strip()
if c != "":
if self.state == "open":
print(">", end="")
print(c, end="")
self.state = "text"
self.nl = False
self.c = ""
def currentTag(self):
return next(reversed(self.tags), False)
def startElement(self, tag, attributes):
parent = self.currentTag()
self.flush(parent, tag)
if self.state == "open":
print(">", end="")
if not Tidy.inline(tag):
print()
self.tags.append(tag)
if not Tidy.inline(tag):
print(" " * self.nesting, end="")
self.nesting = self.nesting + 1
print(f"<{tag}", end="")
for name, value in attributes.items():
print(f" {name}={quoteattr(value)}", end="")
self.state = "open"
self.nl = False
def endElement(self, tag):
self.flush(self.tags.pop())
if not Tidy.inline(tag):
self.nesting = self.nesting - 1
if self.nl and not Tidy.inline(self.currentTag()):
print(" " * self.nesting, end="")
if self.state == "open":
print("/>", end="")
else:
print(f"</{tag}>", end="")
self.nl = not Tidy.inline(tag)
if self.nl:
print()
self.state = "close"
def characters(self, content):
self.c = self.c + content
def processingInstruction(self, target, data):
pass
parser = xml.sax.make_parser()
parser.setContentHandler(Tidy())
if len(sys.argv) >= 2:
parser.parse(sys.argv[1])
else:
parser.parse(sys.stdin)