]> git.sommitrealweird.co.uk Git - rss2maildir.git/blobdiff - rss2maildir.py
Fix bug in character reference handling code
[rss2maildir.git] / rss2maildir.py
index 9473dd009e3d5ebead31933d7ba110abd4202162..84a9f0964408cd9f216d45be9b1921302f3312df 100755 (executable)
@@ -44,6 +44,8 @@ import md5
 import cgi
 import dbm
 
+import re
+
 from HTMLParser import HTMLParser
 
 class HTML2Text(HTMLParser):
@@ -557,7 +559,16 @@ class HTML2Text(HTMLParser):
         self.curdata = self.curdata + data.decode("utf-8")
 
     def handle_charref(self, name):
-        entity = unichr(int(name))
+        try:
+            entity = unichr(int(name))
+        except:
+            if entity[0] == 'x':
+                try:
+                    entity = unichr(int('0%s' %(name,), 16))
+                except:
+                    entity = u'#%s' %(name,)
+            else:
+                entity = u'#%s' %(name,)
         self.curdata = self.curdata + unicode(entity.encode('utf-8'), \
             "utf-8")
 
@@ -725,7 +736,10 @@ def parse_and_deliver(maildir, url, statedir):
             pass
         msg.add_header("Date", createddate)
         subj_gen = HTML2Text()
-        subj_gen.feed(item["title"].encode("utf-8"))
+        title = item["title"].encode("utf-8")
+        title = re.sub(u'<', u'&lt;', title)
+        title = re.sub(u'>', u'&gt;', title)
+        subj_gen.feed(title)
         msg.add_header("Subject", subj_gen.gettext())
         msg.set_default_type("text/plain")