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