5 Written by BroytMann. Copyright (C) 1997-2007 PhiloSoft Design
10 from m_lib.defenc import default_encoding
11 current_charset = default_encoding.replace("windows-", "cp")
12 DEFAULT_CHARSET = "cp1251" # Stupid default for Russian Cyrillic
14 from parse_html_htmlparser import parse_html as _parse_html
17 class HTMLParser(object):
18 def __init__(self, charset=None):
19 _HTMLParser.__init__(self)
20 self.charset = charset
28 entity_re = re.compile("(&#[0-9]+;)")
30 def recode_entities(title, charset):
32 for part in entity_re.split(title):
33 if entity_re.match(part):
34 part = unichr(int(part[2:-1])).encode(charset, "replace")
37 return ''.join(output)
40 def parse_html(filename, charset=None, log=None):
43 codecs.lookup(charset) # In case of unknown charset...
44 except (ValueError, LookupError):
45 charset = None # ...try charset from HTML
47 parser = _parse_html(filename, charset)
50 if not parser.charset:
52 unicode(title, "ascii")
53 except UnicodeDecodeError:
54 parser.charset = DEFAULT_CHARSET
57 parser.charset = parser.charset.replace("windows-", "cp").lower()
59 if parser.charset and (parser.charset <> current_charset):
61 if parser.meta_charset:
62 if log: log(" META charset : %s" % parser.charset)
64 if log: log(" charset : %s" % parser.charset)
65 if log: log(" title : %s" % title)
66 title = unicode(title, parser.charset, "replace").encode(current_charset, "replace")
67 if log: log(" current charset: %s" % current_charset)
68 if log: log(" converted title: %s" % title)
70 if log: log(" unknown charset: `%s' or `%s'" % (parser.charset, current_charset))
72 parser.title = recode_entities(title, current_charset)
76 if __name__ == '__main__':
78 parser = parse_html(sys.argv[1])