changeset 18727:778f4fc8cbcc

Parse HTML and show bold, italic etc. in the textview.
author Sadrul Habib Chowdhury <imadil@gmail.com>
date Mon, 30 Jul 2007 23:57:47 +0000
parents 68fec9f954dd
children 3db31505e42f
files finch/libgnt/pygnt/example/rss/gnthtml.py finch/libgnt/pygnt/example/rss/gntrss-ui.py finch/libgnt/pygnt/example/rss/gntrss.py
diffstat 3 files changed, 92 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/finch/libgnt/pygnt/example/rss/gnthtml.py	Mon Jul 30 23:57:47 2007 +0000
@@ -0,0 +1,89 @@
+#!/usr/bin/env python
+
+"""
+gr - An RSS-reader built using libgnt and feedparser.
+
+Copyright (C) 2007 Sadrul Habib Chowdhury <sadrul@pidgin.im>
+
+This application is free software; you can redistribute it and/or
+modify it under the terms of the GNU Lesser General Public
+License as published by the Free Software Foundation; either
+version 2.1 of the License, or (at your option) any later version.
+
+This application is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+Lesser General Public License for more details.
+
+You should have received a copy of the GNU Lesser General Public
+License along with this application; if not, write to the Free Software
+Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+USA
+"""
+
+"""
+This file defines GParser, which is a simple HTML parser to display HTML
+in a GntTextView nicely.
+"""
+
+import sgmllib
+import gnt
+
+class GParser(sgmllib.SGMLParser):
+    def __init__(self, view):
+        sgmllib.SGMLParser.__init__(self, False)
+        self.link = None
+        self.view = view
+        self.flag = gnt.TEXT_FLAG_NORMAL
+
+    def parse(self, s):
+        self.feed(s)
+        self.close()
+
+    def unknown_starttag(self, tag, attrs):
+        if tag in ["b", "i", "blockquote"]:
+            self.flag = self.flag | gnt.TEXT_FLAG_BOLD
+        else:
+            print tag
+
+    def unknown_endtag(self, tag):
+        if tag in ["b", "i", "blockquote"]:
+            self.flag = self.flag & ~gnt.TEXT_FLAG_BOLD
+        else:
+            print tag
+
+    def start_u(self, attrs):
+        self.flag = self.flag | gnt.TEXT_FLAG_UNDERLINE
+
+    def end_u(self):
+        self.flag = self.flag & ~gnt.TEXT_FLAG_UNDERLINE
+
+    def do_p(self, attr):
+        self.view.append_text_with_flags("\n", self.flag)
+
+    def end_p(self):
+        self.view.append_text_with_flags("\n", self.flag)
+
+    def start_a(self, attributes):
+        for name, value in attributes:
+            if name == "href":
+                self.link = value
+
+    def do_img(self, attrs):
+        for name, value in attrs:
+            if name == 'src':
+                self.view.append_text_with_flags("[img:" + value + "]", self.flag)
+
+    def end_a(self):
+        if not self.link:
+            return
+        self.view.append_text_with_flags(" (", self.flag)
+        self.view.append_text_with_flags(self.link, self.flag | gnt.TEXT_FLAG_UNDERLINE)
+        self.view.append_text_with_flags(")", self.flag)
+        self.link = None
+
+    def handle_data(self, data):
+        if len(data.strip()) == 0:
+            return
+        self.view.append_text_with_flags(data, self.flag)
+
--- a/finch/libgnt/pygnt/example/rss/gntrss-ui.py	Mon Jul 30 20:46:07 2007 +0000
+++ b/finch/libgnt/pygnt/example/rss/gntrss-ui.py	Mon Jul 30 23:57:47 2007 +0000
@@ -36,6 +36,7 @@
 """
 
 import gntrss
+import gnthtml
 import gnt
 import gobject
 import sys
@@ -246,8 +247,8 @@
     details.append_text_with_flags(str(item.link) + "\n", gnt.TEXT_FLAG_UNDERLINE)
     details.append_text_with_flags("Date: ", gnt.TEXT_FLAG_BOLD)
     details.append_text_with_flags(str(item.date) + "\n", gnt.TEXT_FLAG_NORMAL)
-
-    details.append_text_with_flags("\n" + str(item.summary), gnt.TEXT_FLAG_NORMAL)
+    parser = gnthtml.GParser(details)
+    parser.parse(str(item.summary))
     item.mark_unread(False)
 
     if old and old.unread:   # If the last selected item is marked 'unread', then make sure it's bold
--- a/finch/libgnt/pygnt/example/rss/gntrss.py	Mon Jul 30 20:46:07 2007 +0000
+++ b/finch/libgnt/pygnt/example/rss/gntrss.py	Mon Jul 30 23:57:47 2007 +0000
@@ -199,7 +199,6 @@
 feeds = []
 urls = ("http://rss.slashdot.org/Slashdot/slashdot",
         "http://www.python.org/channews.rdf",
-        "http://pidgin.im/rss.php",
         "http://kerneltrap.org/node/feed"
         )