1 | n/a | import xml.dom.minidom |
---|
2 | n/a | |
---|
3 | n/a | document = """\ |
---|
4 | n/a | <slideshow> |
---|
5 | n/a | <title>Demo slideshow</title> |
---|
6 | n/a | <slide><title>Slide title</title> |
---|
7 | n/a | <point>This is a demo</point> |
---|
8 | n/a | <point>Of a program for processing slides</point> |
---|
9 | n/a | </slide> |
---|
10 | n/a | |
---|
11 | n/a | <slide><title>Another demo slide</title> |
---|
12 | n/a | <point>It is important</point> |
---|
13 | n/a | <point>To have more than</point> |
---|
14 | n/a | <point>one slide</point> |
---|
15 | n/a | </slide> |
---|
16 | n/a | </slideshow> |
---|
17 | n/a | """ |
---|
18 | n/a | |
---|
19 | n/a | dom = xml.dom.minidom.parseString(document) |
---|
20 | n/a | |
---|
21 | n/a | def getText(nodelist): |
---|
22 | n/a | rc = [] |
---|
23 | n/a | for node in nodelist: |
---|
24 | n/a | if node.nodeType == node.TEXT_NODE: |
---|
25 | n/a | rc.append(node.data) |
---|
26 | n/a | return ''.join(rc) |
---|
27 | n/a | |
---|
28 | n/a | def handleSlideshow(slideshow): |
---|
29 | n/a | print("<html>") |
---|
30 | n/a | handleSlideshowTitle(slideshow.getElementsByTagName("title")[0]) |
---|
31 | n/a | slides = slideshow.getElementsByTagName("slide") |
---|
32 | n/a | handleToc(slides) |
---|
33 | n/a | handleSlides(slides) |
---|
34 | n/a | print("</html>") |
---|
35 | n/a | |
---|
36 | n/a | def handleSlides(slides): |
---|
37 | n/a | for slide in slides: |
---|
38 | n/a | handleSlide(slide) |
---|
39 | n/a | |
---|
40 | n/a | def handleSlide(slide): |
---|
41 | n/a | handleSlideTitle(slide.getElementsByTagName("title")[0]) |
---|
42 | n/a | handlePoints(slide.getElementsByTagName("point")) |
---|
43 | n/a | |
---|
44 | n/a | def handleSlideshowTitle(title): |
---|
45 | n/a | print("<title>%s</title>" % getText(title.childNodes)) |
---|
46 | n/a | |
---|
47 | n/a | def handleSlideTitle(title): |
---|
48 | n/a | print("<h2>%s</h2>" % getText(title.childNodes)) |
---|
49 | n/a | |
---|
50 | n/a | def handlePoints(points): |
---|
51 | n/a | print("<ul>") |
---|
52 | n/a | for point in points: |
---|
53 | n/a | handlePoint(point) |
---|
54 | n/a | print("</ul>") |
---|
55 | n/a | |
---|
56 | n/a | def handlePoint(point): |
---|
57 | n/a | print("<li>%s</li>" % getText(point.childNodes)) |
---|
58 | n/a | |
---|
59 | n/a | def handleToc(slides): |
---|
60 | n/a | for slide in slides: |
---|
61 | n/a | title = slide.getElementsByTagName("title")[0] |
---|
62 | n/a | print("<p>%s</p>" % getText(title.childNodes)) |
---|
63 | n/a | |
---|
64 | n/a | handleSlideshow(dom) |
---|