blob: df5d1c7c07341711619cb12c20b74ba768af7c8f [file] [log] [blame]
Fred Drakeac5f7482000-10-16 15:27:05 +00001import sys
2
3from xml.sax import make_parser, handler
4
5# --- Templates
6
7top = \
8"""
9<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
10<HTML>
11<HEAD>
12 <TITLE>%s</TITLE>
13</HEAD>
14
15<BODY>
16<H1>%s</H1>
17"""
18
19bottom = \
20"""
21</ul>
22
23<HR>
24<ADDRESS>
25Converted to HTML by sax_rss2html.py.
26</ADDRESS>
27
28</BODY>
29</HTML>
30"""
31
32# --- The ContentHandler
33
34class RSSHandler(handler.ContentHandler):
35
36 def __init__(self, out = sys.stdout):
37 handler.ContentHandler.__init__(self)
38 self._out = out
39
40 self._text = ""
41 self._parent = None
42 self._list_started = 0
43 self._title = None
44 self._link = None
45 self._descr = ""
46
47 # ContentHandler methods
48
49 def startElement(self, name, attrs):
50 if name == "channel" or name == "image" or name == "item":
51 self._parent = name
52
53 self._text = ""
54
55 def endElement(self, name):
56 if self._parent == "channel":
57 if name == "title":
58 self._out.write(top % (self._text, self._text))
59 elif name == "description":
60 self._out.write("<p>%s</p>\n" % self._text)
61
62 elif self._parent == "item":
63 if name == "title":
64 self._title = self._text
65 elif name == "link":
66 self._link = self._text
67 elif name == "description":
68 self._descr = self._text
69 elif name == "item":
70 if not self._list_started:
71 self._out.write("<ul>\n")
72 self._list_started = 1
73
74 self._out.write(' <li><a href="%s">%s</a> %s\n' %
75 (self._link, self._title, self._descr))
76
77 self._title = None
78 self._link = None
79 self._descr = ""
80
81 if name == "rss":
82 self._out.write(bottom)
83
84 def characters(self, content):
85 self._text = self._text + content
86
87# --- Main program
88
89parser = make_parser()
90parser.setContentHandler(RSSHandler())
91parser.parse(sys.argv[1])