blob: 49cd154ecdbba574a531e29c079579a338ce0336 [file] [log] [blame]
Georg Brandlbc470d52009-10-11 15:56:06 +00001"""
2A demo that reads in an RSS XML document and emits an HTML file containing
3a list of the individual items in the feed.
4"""
5
Fred Drakeac5f7482000-10-16 15:27:05 +00006import sys
Georg Brandlbc470d52009-10-11 15:56:06 +00007import codecs
Fred Drakeac5f7482000-10-16 15:27:05 +00008
9from xml.sax import make_parser, handler
10
11# --- Templates
12
Georg Brandlbc470d52009-10-11 15:56:06 +000013top = """\
Fred Drakeac5f7482000-10-16 15:27:05 +000014<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
Georg Brandlbc470d52009-10-11 15:56:06 +000015<html>
16<head>
17 <title>%s</title>
18 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
19</head>
Fred Drakeac5f7482000-10-16 15:27:05 +000020
Georg Brandlbc470d52009-10-11 15:56:06 +000021<body>
22<h1>%s</h1>
Fred Drakeac5f7482000-10-16 15:27:05 +000023"""
24
Georg Brandlbc470d52009-10-11 15:56:06 +000025bottom = """
Fred Drakeac5f7482000-10-16 15:27:05 +000026</ul>
27
Georg Brandlbc470d52009-10-11 15:56:06 +000028<hr>
29<address>
30Converted to HTML by rss2html.py.
31</address>
Fred Drakeac5f7482000-10-16 15:27:05 +000032
Georg Brandlbc470d52009-10-11 15:56:06 +000033</body>
34</html>
Fred Drakeac5f7482000-10-16 15:27:05 +000035"""
36
37# --- The ContentHandler
38
39class RSSHandler(handler.ContentHandler):
40
Georg Brandlbc470d52009-10-11 15:56:06 +000041 def __init__(self, out=sys.stdout):
Fred Drakeac5f7482000-10-16 15:27:05 +000042 handler.ContentHandler.__init__(self)
43 self._out = out
44
45 self._text = ""
46 self._parent = None
Georg Brandlbc470d52009-10-11 15:56:06 +000047 self._list_started = False
Fred Drakeac5f7482000-10-16 15:27:05 +000048 self._title = None
49 self._link = None
50 self._descr = ""
51
52 # ContentHandler methods
53
54 def startElement(self, name, attrs):
55 if name == "channel" or name == "image" or name == "item":
56 self._parent = name
57
58 self._text = ""
59
60 def endElement(self, name):
61 if self._parent == "channel":
62 if name == "title":
63 self._out.write(top % (self._text, self._text))
64 elif name == "description":
65 self._out.write("<p>%s</p>\n" % self._text)
66
67 elif self._parent == "item":
68 if name == "title":
69 self._title = self._text
70 elif name == "link":
71 self._link = self._text
72 elif name == "description":
73 self._descr = self._text
74 elif name == "item":
75 if not self._list_started:
76 self._out.write("<ul>\n")
Georg Brandlbc470d52009-10-11 15:56:06 +000077 self._list_started = True
Fred Drakeac5f7482000-10-16 15:27:05 +000078
79 self._out.write(' <li><a href="%s">%s</a> %s\n' %
80 (self._link, self._title, self._descr))
81
82 self._title = None
83 self._link = None
84 self._descr = ""
85
86 if name == "rss":
87 self._out.write(bottom)
Tim Peters182b5ac2004-07-18 06:16:08 +000088
Fred Drakeac5f7482000-10-16 15:27:05 +000089 def characters(self, content):
90 self._text = self._text + content
91
92# --- Main program
93
Georg Brandlbc470d52009-10-11 15:56:06 +000094if __name__ == '__main__':
95 parser = make_parser()
96 parser.setContentHandler(RSSHandler())
97 parser.parse(sys.argv[1])