From: Oleg Broytman Date: Mon, 11 Feb 2008 16:15:49 +0000 (+0000) Subject: Recode HTML entities. X-Git-Tag: v4.5.3~208 X-Git-Url: https://git.phdru.name/?a=commitdiff_plain;h=7d1dbd530c688dc71f98139efaf6890c84b12f94;p=bookmarks_db.git Recode HTML entities. git-svn-id: file:///home/phd/archive/SVN/bookmarks_db/trunk@168 fdd5c36f-1aea-0310-aeeb-c58d7e2b6c23 --- diff --git a/Robots/parse_html.py b/Robots/parse_html.py index 9f924c4..1125dcc 100755 --- a/Robots/parse_html.py +++ b/Robots/parse_html.py @@ -24,19 +24,29 @@ parsers.append(parse_html) import re -entity_re = re.compile("(&#[0-9]+;)") +from htmlentitydefs import entitydefs + +entity_re = re.compile("(&\w+;)") +num_entity_re = re.compile("(&#[0-9]+;)") def recode_entities(title, charset): output = [] - for part in entity_re.split(title): - if entity_re.match(part): + + for part in num_entity_re.split(title): + if num_entity_re.match(part): try: part = unichr(int(part[2:-1])).encode(charset) except UnicodeEncodeError: pass # Leave the entity as is output.append(part) - return ''.join(output) + output2 = [] + for part in entity_re.split(''.join(output)): + if entity_re.match(part): + part = entitydefs.get(part[1:-1], part) + output2.append(part) + + return ''.join(output2) def parse_html(filename, charset=None, log=None):