| |
1 #!/usr/bin/env python |
| |
2 |
| |
3 """ |
| |
4 gr - An RSS-reader built using libgnt and feedparser. |
| |
5 |
| |
6 Copyright (C) 2007 Sadrul Habib Chowdhury <sadrul@pidgin.im> |
| |
7 |
| |
8 This application is free software; you can redistribute it and/or |
| |
9 modify it under the terms of the GNU Lesser General Public |
| |
10 License as published by the Free Software Foundation; either |
| |
11 version 2.1 of the License, or (at your option) any later version. |
| |
12 |
| |
13 This application is distributed in the hope that it will be useful, |
| |
14 but WITHOUT ANY WARRANTY; without even the implied warranty of |
| |
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| |
16 Lesser General Public License for more details. |
| |
17 |
| |
18 You should have received a copy of the GNU Lesser General Public |
| |
19 License along with this application; if not, write to the Free Software |
| |
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
| |
21 USA |
| |
22 """ |
| |
23 |
| |
24 """ |
| |
25 This file defines GParser, which is a simple HTML parser to display HTML |
| |
26 in a GntTextView nicely. |
| |
27 """ |
| |
28 |
| |
29 import sgmllib |
| |
30 import gnt |
| |
31 |
| |
32 class GParser(sgmllib.SGMLParser): |
| |
33 def __init__(self, view): |
| |
34 sgmllib.SGMLParser.__init__(self, False) |
| |
35 self.link = None |
| |
36 self.view = view |
| |
37 self.flag = gnt.TEXT_FLAG_NORMAL |
| |
38 |
| |
39 def parse(self, s): |
| |
40 self.feed(s) |
| |
41 self.close() |
| |
42 |
| |
43 def unknown_starttag(self, tag, attrs): |
| |
44 if tag in ["b", "i", "blockquote"]: |
| |
45 self.flag = self.flag | gnt.TEXT_FLAG_BOLD |
| |
46 else: |
| |
47 print tag |
| |
48 |
| |
49 def unknown_endtag(self, tag): |
| |
50 if tag in ["b", "i", "blockquote"]: |
| |
51 self.flag = self.flag & ~gnt.TEXT_FLAG_BOLD |
| |
52 else: |
| |
53 print tag |
| |
54 |
| |
55 def start_u(self, attrs): |
| |
56 self.flag = self.flag | gnt.TEXT_FLAG_UNDERLINE |
| |
57 |
| |
58 def end_u(self): |
| |
59 self.flag = self.flag & ~gnt.TEXT_FLAG_UNDERLINE |
| |
60 |
| |
61 def do_p(self, attr): |
| |
62 self.view.append_text_with_flags("\n", self.flag) |
| |
63 |
| |
64 def end_p(self): |
| |
65 self.view.append_text_with_flags("\n", self.flag) |
| |
66 |
| |
67 def start_a(self, attributes): |
| |
68 for name, value in attributes: |
| |
69 if name == "href": |
| |
70 self.link = value |
| |
71 |
| |
72 def do_img(self, attrs): |
| |
73 for name, value in attrs: |
| |
74 if name == 'src': |
| |
75 self.view.append_text_with_flags("[img:" + value + "]", self.flag) |
| |
76 |
| |
77 def end_a(self): |
| |
78 if not self.link: |
| |
79 return |
| |
80 self.view.append_text_with_flags(" (", self.flag) |
| |
81 self.view.append_text_with_flags(self.link, self.flag | gnt.TEXT_FLAG_UNDERLINE) |
| |
82 self.view.append_text_with_flags(")", self.flag) |
| |
83 self.link = None |
| |
84 |
| |
85 def handle_data(self, data): |
| |
86 if len(data.strip()) == 0: |
| |
87 return |
| |
88 self.view.append_text_with_flags(data, self.flag) |
| |
89 |