finch/libgnt/pygnt/example/rss/gnthtml.py

Wed, 12 Sep 2007 11:04:26 +0000

author
Sadrul Habib Chowdhury <sadrul@pidgin.im>
date
Wed, 12 Sep 2007 11:04:26 +0000
changeset 19965
e4de5a1d84a5
parent 19859
71d37b57eff2
permissions
-rw-r--r--

Fix a memory corruption.
Change default binding for 'jump next unread' from 'J' to 'n'.
Treat 'None' in pygnt as NULL in gnt.

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

mercurial