X-Git-Url: https://git.sommitrealweird.co.uk/rss2maildir.git/blobdiff_plain/2d1b58f010d4f239f66222f28a7869348112c2b4..88b4ead7d0ce70bb35bc27a49225bfca23354185:/rss2maildir.py diff --git a/rss2maildir.py b/rss2maildir.py index dce6c3c..b77c6d7 100755 --- a/rss2maildir.py +++ b/rss2maildir.py @@ -44,6 +44,8 @@ import md5 import cgi import dbm +import re + from HTMLParser import HTMLParser class HTML2Text(HTMLParser): @@ -137,6 +139,39 @@ class HTML2Text(HTMLParser): u'THORN': u'Þ', u'eth': u'ð', u'ETH': u'Ð', + u'mdash': u'—', + u'ndash': u'–', + u'sect': u'§', + u'para': u'¶', + u'uarr': u'↑', + u'darr': u'↓', + u'larr': u'←', + u'rarr': u'→', + u'dagger': u'†', + u'Dagger': u'‡', + u'permil': u'‰', + u'prod': u'∏', + u'infin': u'∞', + u'radic': u'√', + u'there4': u'∴', + u'int': u'∫', + u'asymp': u'≈', + u'ne': u'≠', + u'equiv': '≡', + u'le': u'≤', + u'ge': u'≥', + u'loz': u'⋄', + u'sum': u'∑', + u'part': u'∂', + u'prime': u'′', + u'Prime': u'″', + u'harr': u'↔', + u'micro': u'µ', + u'not': u'¬', + u'plusmn': u'±', + u'divide': u'÷', + u'cent': u'¢', + u'euro': u'€', } blockleveltags = [ @@ -155,7 +190,7 @@ class HTML2Text(HTMLParser): u'dt', u'dd', u'div', - #u'blockquote', + u'blockquote', ] liststarttags = [ @@ -280,12 +315,11 @@ class HTML2Text(HTMLParser): else: while self.images.has_key(alt): alt = alt + "_" - self.images[alt]["url"] = url + self.images[alt] = {"url": url} self.curdata = self.curdata \ + u'|%s|' %(alt,) else: - self.images[alt] = {} - self.images[alt]["url"] = url + self.images[alt] = {"url": url} self.curdata = self.curdata \ + u'|%s|' %(alt,) else: @@ -375,11 +409,11 @@ class HTML2Text(HTMLParser): quote = unicode( \ " ".join(self.curdata.encode("utf-8").strip().split()), \ "utf-8") - seperator = u'\n' + u' ' * self.indentlevel + u'> ' + seperator = u'\n' + u' ' * self.indentlevel + u' ' if len(self.text) > 0 and self.text[-1] != u'\n': self.text = self.text + u'\n' self.text = self.text \ - + u'> ' \ + + u' ' \ + seperator.join( \ textwrap.wrap( \ quote, \ @@ -524,12 +558,24 @@ class HTML2Text(HTMLParser): self.opentags.append(u'p') self.curdata = self.curdata + data.decode("utf-8") + def handle_charref(self, name): + try: + entity = unichr(int(name)) + except: + if name[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") + def handle_entityref(self, name): entity = name if HTML2Text.entities.has_key(name): entity = HTML2Text.entities[name] - elif name[0] == "#": - entity = unichr(int(name[1:])) else: entity = "&" + name + ";" @@ -637,7 +683,10 @@ def parse_and_deliver(maildir, url, statedir): if item.has_key("content"): content = item["content"][0]["value"] else: - content = item["summary"] + if item.has_key("description"): + content = item["description"] + else: + content = u'' md5sum = md5.md5(content.encode("utf-8")).hexdigest() @@ -690,7 +739,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'<', title) + title = re.sub(u'>', u'>', title) + subj_gen.feed(title) msg.add_header("Subject", subj_gen.gettext()) msg.set_default_type("text/plain")