From 7b0fdb70fab014eb5bc58131ed3c31ecbf646145 Mon Sep 17 00:00:00 2001 From: Oleg Broytman Date: Thu, 24 Jul 2003 14:20:42 +0000 Subject: [PATCH] Parse and recode unicode entities. git-svn-id: file:///home/phd/archive/SVN/bookmarks_db/trunk@8 fdd5c36f-1aea-0310-aeeb-c58d7e2b6c23 --- Robots/parse_html.py | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/Robots/parse_html.py b/Robots/parse_html.py index e95dcef..a534e21 100755 --- a/Robots/parse_html.py +++ b/Robots/parse_html.py @@ -2,13 +2,13 @@ """ HTML Parser - Written by BroytMann, Jun 2002 - Aug 2002. Copyright (C) 1997-2002 PhiloSoft Design + Written by BroytMann, Jun 2002 - May 2003. Copyright (C) 1997-2003 PhiloSoft Design """ import sys current_charset = sys.getdefaultencoding() -DEFAULT_CHARSET = "windows-1251" +DEFAULT_CHARSET = "windows-1251" # Stupid default for Russian Cyrillic from HTMLParser import HTMLParseError @@ -46,7 +46,8 @@ class HTMLParser(_HTMLParser): try: # extract charset from "text/html; foo; charset=UTF-8; bar;" self.charset = content.lower().split('charset=')[1].split(';')[0] - self.meta_charset = 1 + self.meta_charset = 1 # Remember that the charset was retrieved from + # META tag, not from the Content-Type header except IndexError: pass @@ -56,11 +57,25 @@ class HTMLParser(_HTMLParser): def start_title(self, attrs): self.accumulator = '' + def end_title(self): if not self.title: # use only the first title self.title = self.accumulator +import re +entity_re = re.compile("(&#[0-9]+;)") + +def recode_entities(title, charset): + output = [] + for part in entity_re.split(title): + if entity_re.match(part): + part = unichr(int(part[2:-1])).encode(charset, "replace") + output.append(part) + + return ''.join(output) + + def parse_html(filename, charset=None, log=None): infile = open(filename, 'r') parser = HTMLParser(charset) @@ -78,8 +93,9 @@ def parse_html(filename, charset=None, log=None): except (HTMLParseError, HTMLHeadDone): pass + title = parser.title + if not parser.charset: - title = parser.title ascii = 1 for c in title: if not (32 <= ord(c) <= 127): # non-ASCII character @@ -87,19 +103,21 @@ def parse_html(filename, charset=None, log=None): break if not ascii: parser.charset = DEFAULT_CHARSET + if parser.charset and (parser.charset <> current_charset): try: if parser.meta_charset: if log: log(" META charset : %s" % parser.charset) else: if log: log(" charset : %s" % parser.charset) - if log: log(" title : %s" % parser.title) - parser.title = unicode(parser.title, parser.charset, "replace").encode(current_charset, "replace") + if log: log(" title : %s" % title) + title = unicode(title, parser.charset, "replace").encode(current_charset, "replace") if log: log(" current charset: %s" % current_charset) - if log: log(" converted title: %s" % parser.title) + if log: log(" converted title: %s" % title) except LookupError: if log: log(" unknown charset: `%s' or `%s'" % (parser.charset, current_charset)) + parser.title = recode_entities(title, current_charset) return parser -- 2.39.5