annotate finch/libgnt/pygnt/example/rss/gnthtml.py @ 22135:3550c5d7c493

Plug a leak.
author John Bailey <rekkanoryo@rekkanoryo.org>
date Thu, 17 Jan 2008 06:17:02 +0000
parents 44b4e8bd759b
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
18727
778f4fc8cbcc Parse HTML and show bold, italic etc. in the textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff changeset
1 #!/usr/bin/env python
778f4fc8cbcc Parse HTML and show bold, italic etc. in the textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff changeset
2
778f4fc8cbcc Parse HTML and show bold, italic etc. in the textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff changeset
3 """
778f4fc8cbcc Parse HTML and show bold, italic etc. in the textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff changeset
4 gr - An RSS-reader built using libgnt and feedparser.
778f4fc8cbcc Parse HTML and show bold, italic etc. in the textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff changeset
5
778f4fc8cbcc Parse HTML and show bold, italic etc. in the textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff changeset
6 Copyright (C) 2007 Sadrul Habib Chowdhury <sadrul@pidgin.im>
778f4fc8cbcc Parse HTML and show bold, italic etc. in the textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff changeset
7
778f4fc8cbcc Parse HTML and show bold, italic etc. in the textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff changeset
8 This application is free software; you can redistribute it and/or
778f4fc8cbcc Parse HTML and show bold, italic etc. in the textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff changeset
9 modify it under the terms of the GNU Lesser General Public
778f4fc8cbcc Parse HTML and show bold, italic etc. in the textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff changeset
10 License as published by the Free Software Foundation; either
778f4fc8cbcc Parse HTML and show bold, italic etc. in the textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff changeset
11 version 2.1 of the License, or (at your option) any later version.
778f4fc8cbcc Parse HTML and show bold, italic etc. in the textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff changeset
12
778f4fc8cbcc Parse HTML and show bold, italic etc. in the textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff changeset
13 This application is distributed in the hope that it will be useful,
778f4fc8cbcc Parse HTML and show bold, italic etc. in the textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff changeset
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
778f4fc8cbcc Parse HTML and show bold, italic etc. in the textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff changeset
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
778f4fc8cbcc Parse HTML and show bold, italic etc. in the textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff changeset
16 Lesser General Public License for more details.
778f4fc8cbcc Parse HTML and show bold, italic etc. in the textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff changeset
17
778f4fc8cbcc Parse HTML and show bold, italic etc. in the textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff changeset
18 You should have received a copy of the GNU Lesser General Public
778f4fc8cbcc Parse HTML and show bold, italic etc. in the textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff changeset
19 License along with this application; if not, write to the Free Software
19680
44b4e8bd759b The FSF changed its address a while ago; our files were out of date.
John Bailey <rekkanoryo@rekkanoryo.org>
parents: 18731
diff changeset
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301
18727
778f4fc8cbcc Parse HTML and show bold, italic etc. in the textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff changeset
21 USA
778f4fc8cbcc Parse HTML and show bold, italic etc. in the textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff changeset
22 """
778f4fc8cbcc Parse HTML and show bold, italic etc. in the textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff changeset
23
778f4fc8cbcc Parse HTML and show bold, italic etc. in the textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff changeset
24 """
778f4fc8cbcc Parse HTML and show bold, italic etc. in the textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff changeset
25 This file defines GParser, which is a simple HTML parser to display HTML
778f4fc8cbcc Parse HTML and show bold, italic etc. in the textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff changeset
26 in a GntTextView nicely.
778f4fc8cbcc Parse HTML and show bold, italic etc. in the textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff changeset
27 """
778f4fc8cbcc Parse HTML and show bold, italic etc. in the textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff changeset
28
778f4fc8cbcc Parse HTML and show bold, italic etc. in the textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff changeset
29 import sgmllib
778f4fc8cbcc Parse HTML and show bold, italic etc. in the textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff changeset
30 import gnt
778f4fc8cbcc Parse HTML and show bold, italic etc. in the textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff changeset
31
778f4fc8cbcc Parse HTML and show bold, italic etc. in the textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff changeset
32 class GParser(sgmllib.SGMLParser):
778f4fc8cbcc Parse HTML and show bold, italic etc. in the textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff changeset
33 def __init__(self, view):
778f4fc8cbcc Parse HTML and show bold, italic etc. in the textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff changeset
34 sgmllib.SGMLParser.__init__(self, False)
778f4fc8cbcc Parse HTML and show bold, italic etc. in the textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff changeset
35 self.link = None
778f4fc8cbcc Parse HTML and show bold, italic etc. in the textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff changeset
36 self.view = view
778f4fc8cbcc Parse HTML and show bold, italic etc. in the textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff changeset
37 self.flag = gnt.TEXT_FLAG_NORMAL
778f4fc8cbcc Parse HTML and show bold, italic etc. in the textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff changeset
38
778f4fc8cbcc Parse HTML and show bold, italic etc. in the textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff changeset
39 def parse(self, s):
778f4fc8cbcc Parse HTML and show bold, italic etc. in the textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff changeset
40 self.feed(s)
778f4fc8cbcc Parse HTML and show bold, italic etc. in the textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff changeset
41 self.close()
778f4fc8cbcc Parse HTML and show bold, italic etc. in the textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff changeset
42
778f4fc8cbcc Parse HTML and show bold, italic etc. in the textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff changeset
43 def unknown_starttag(self, tag, attrs):
18731
6976a6dae441 Add support for auto-refreshing. There should be some way to change the
Sadrul Habib Chowdhury <imadil@gmail.com>
parents: 18728
diff changeset
44 if tag in ["b", "i", "blockquote", "strong"]:
18727
778f4fc8cbcc Parse HTML and show bold, italic etc. in the textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff changeset
45 self.flag = self.flag | gnt.TEXT_FLAG_BOLD
18728
3db31505e42f Use pseudo-nonblocking stuff to update the feeds.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents: 18727
diff changeset
46 elif tag in ["p", "hr", "br"]:
3db31505e42f Use pseudo-nonblocking stuff to update the feeds.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents: 18727
diff changeset
47 self.view.append_text_with_flags("\n", self.flag)
18727
778f4fc8cbcc Parse HTML and show bold, italic etc. in the textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff changeset
48 else:
778f4fc8cbcc Parse HTML and show bold, italic etc. in the textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff changeset
49 print tag
778f4fc8cbcc Parse HTML and show bold, italic etc. in the textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff changeset
50
778f4fc8cbcc Parse HTML and show bold, italic etc. in the textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff changeset
51 def unknown_endtag(self, tag):
18731
6976a6dae441 Add support for auto-refreshing. There should be some way to change the
Sadrul Habib Chowdhury <imadil@gmail.com>
parents: 18728
diff changeset
52 if tag in ["b", "i", "blockquote", "strong"]:
18727
778f4fc8cbcc Parse HTML and show bold, italic etc. in the textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff changeset
53 self.flag = self.flag & ~gnt.TEXT_FLAG_BOLD
18728
3db31505e42f Use pseudo-nonblocking stuff to update the feeds.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents: 18727
diff changeset
54 elif tag in ["p", "hr", "br"]:
3db31505e42f Use pseudo-nonblocking stuff to update the feeds.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents: 18727
diff changeset
55 self.view.append_text_with_flags("\n", self.flag)
18727
778f4fc8cbcc Parse HTML and show bold, italic etc. in the textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff changeset
56 else:
778f4fc8cbcc Parse HTML and show bold, italic etc. in the textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff changeset
57 print tag
778f4fc8cbcc Parse HTML and show bold, italic etc. in the textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff changeset
58
778f4fc8cbcc Parse HTML and show bold, italic etc. in the textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff changeset
59 def start_u(self, attrs):
778f4fc8cbcc Parse HTML and show bold, italic etc. in the textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff changeset
60 self.flag = self.flag | gnt.TEXT_FLAG_UNDERLINE
778f4fc8cbcc Parse HTML and show bold, italic etc. in the textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff changeset
61
778f4fc8cbcc Parse HTML and show bold, italic etc. in the textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff changeset
62 def end_u(self):
778f4fc8cbcc Parse HTML and show bold, italic etc. in the textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff changeset
63 self.flag = self.flag & ~gnt.TEXT_FLAG_UNDERLINE
778f4fc8cbcc Parse HTML and show bold, italic etc. in the textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff changeset
64
778f4fc8cbcc Parse HTML and show bold, italic etc. in the textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff changeset
65 def start_a(self, attributes):
778f4fc8cbcc Parse HTML and show bold, italic etc. in the textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff changeset
66 for name, value in attributes:
778f4fc8cbcc Parse HTML and show bold, italic etc. in the textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff changeset
67 if name == "href":
778f4fc8cbcc Parse HTML and show bold, italic etc. in the textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff changeset
68 self.link = value
778f4fc8cbcc Parse HTML and show bold, italic etc. in the textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff changeset
69
778f4fc8cbcc Parse HTML and show bold, italic etc. in the textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff changeset
70 def do_img(self, attrs):
778f4fc8cbcc Parse HTML and show bold, italic etc. in the textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff changeset
71 for name, value in attrs:
778f4fc8cbcc Parse HTML and show bold, italic etc. in the textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff changeset
72 if name == 'src':
778f4fc8cbcc Parse HTML and show bold, italic etc. in the textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff changeset
73 self.view.append_text_with_flags("[img:" + value + "]", self.flag)
778f4fc8cbcc Parse HTML and show bold, italic etc. in the textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff changeset
74
778f4fc8cbcc Parse HTML and show bold, italic etc. in the textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff changeset
75 def end_a(self):
778f4fc8cbcc Parse HTML and show bold, italic etc. in the textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff changeset
76 if not self.link:
778f4fc8cbcc Parse HTML and show bold, italic etc. in the textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff changeset
77 return
778f4fc8cbcc Parse HTML and show bold, italic etc. in the textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff changeset
78 self.view.append_text_with_flags(" (", self.flag)
778f4fc8cbcc Parse HTML and show bold, italic etc. in the textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff changeset
79 self.view.append_text_with_flags(self.link, self.flag | gnt.TEXT_FLAG_UNDERLINE)
778f4fc8cbcc Parse HTML and show bold, italic etc. in the textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff changeset
80 self.view.append_text_with_flags(")", self.flag)
778f4fc8cbcc Parse HTML and show bold, italic etc. in the textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff changeset
81 self.link = None
778f4fc8cbcc Parse HTML and show bold, italic etc. in the textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff changeset
82
778f4fc8cbcc Parse HTML and show bold, italic etc. in the textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff changeset
83 def handle_data(self, data):
778f4fc8cbcc Parse HTML and show bold, italic etc. in the textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff changeset
84 if len(data.strip()) == 0:
778f4fc8cbcc Parse HTML and show bold, italic etc. in the textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff changeset
85 return
778f4fc8cbcc Parse HTML and show bold, italic etc. in the textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff changeset
86 self.view.append_text_with_flags(data, self.flag)
778f4fc8cbcc Parse HTML and show bold, italic etc. in the textview.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff changeset
87