Fred Drake | b866770 | 2001-09-02 06:07:36 +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 | space = " " |
| 22 | def getText(nodelist): |
| 23 | rc = "" |
| 24 | for node in nodelist: |
| 25 | if node.nodeType == node.TEXT_NODE: |
| 26 | rc = rc + node.data |
| 27 | return rc |
| 28 | |
| 29 | def handleSlideshow(slideshow): |
| 30 | print "<html>" |
| 31 | handleSlideshowTitle(slideshow.getElementsByTagName("title")[0]) |
| 32 | slides = slideshow.getElementsByTagName("slide") |
| 33 | handleToc(slides) |
| 34 | handleSlides(slides) |
| 35 | print "</html>" |
| 36 | |
| 37 | def handleSlides(slides): |
| 38 | for slide in slides: |
| 39 | handleSlide(slide) |
| 40 | |
| 41 | def handleSlide(slide): |
| 42 | handleSlideTitle(slide.getElementsByTagName("title")[0]) |
| 43 | handlePoints(slide.getElementsByTagName("point")) |
| 44 | |
| 45 | def handleSlideshowTitle(title): |
| 46 | print "<title>%s</title>" % getText(title.childNodes) |
| 47 | |
| 48 | def handleSlideTitle(title): |
| 49 | print "<h2>%s</h2>" % getText(title.childNodes) |
| 50 | |
| 51 | def handlePoints(points): |
| 52 | print "<ul>" |
| 53 | for point in points: |
| 54 | handlePoint(point) |
| 55 | print "</ul>" |
| 56 | |
| 57 | def handlePoint(point): |
| 58 | print "<li>%s</li>" % getText(point.childNodes) |
| 59 | |
| 60 | def handleToc(slides): |
| 61 | for slide in slides: |
| 62 | title = slide.getElementsByTagName("title")[0] |
| 63 | print "<p>%s</p>" % getText(title.childNodes) |
| 64 | |
| 65 | handleSlideshow(dom) |