-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpagemaker.py
29 lines (27 loc) · 1 KB
/
pagemaker.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
from xml.sax.handler import ContentHandler
from xml.sax import parse
class PageMaker(ContentHandler):
passthrough = False
def startElement(self, name, attrs):
if name == "page":
self.passthrough = True
self.out = open(attrs['name'] + '.html', 'w')
self.out.write('<html><head>\n')
self.out.write('<title>%s</title>\n' % attrs['title'])
self.out.write('</head><body>\n')
elif self.passthrough:
self.out.write('<' + name)
for key, val in attrs.items():
self.out.write(' %s = %s' % (key, val))
self.out.write('>')
def endElement(self, name):
if name == "page":
self.passthrough = False
self.out.write('</body></html>\n')
self.out.close()
elif self.passthrough:
self.out.write('</%s>\n' % name)
def characters(self, chars):
if self.passthrough:
self.out.write(chars)
parse('website.xml', PageMaker())