5 Written by BroytMann. Copyright (C) 1997-2008 PhiloSoft Design
10 from m_lib.defenc import default_encoding
11 current_charset = default_encoding.replace("windows-", "cp")
13 universal_charset = "utf-8"
14 DEFAULT_CHARSET = "cp1251" # Stupid default for Russian Cyrillic
18 from parse_html_beautifulsoup import parse_html
22 parsers.append(parse_html)
24 from parse_html_htmlparser import parse_html
25 parsers.append(parse_html)
29 from htmlentitydefs import entitydefs
31 entity_re = re.compile("(&\w+;)")
32 num_entity_re = re.compile("(&#[0-9]+;)")
34 def recode_entities(title, charset):
36 for part in num_entity_re.split(title):
37 if num_entity_re.match(part):
39 part = unichr(int(part[2:-1])).encode(charset)
40 except UnicodeEncodeError:
41 pass # Leave the entity as is
45 for part in entity_re.split(''.join(output)):
46 if entity_re.match(part):
47 part = entitydefs.get(part[1:-1], part)
48 if num_entity_re.match(part):
50 part = unichr(int(part[2:-1])).encode(charset)
51 except UnicodeEncodeError:
52 pass # Leave the entity as is
55 return ''.join(output2)
58 def parse_html(filename, charset=None, log=None):
61 codecs.lookup(charset) # In case of unknown charset...
62 except (ValueError, LookupError):
63 charset = None # ...try charset from HTML
66 parser = p(filename, charset)
70 if log: log("Parser %s.%s failed, trying next one." % (p.__module__, p.__name__))
72 converted_title = title = parser.title
73 if not parser.charset:
75 unicode(title, "ascii")
76 except UnicodeDecodeError:
77 parser.charset = DEFAULT_CHARSET
80 parser.charset = parser.charset.replace("windows-", "cp").lower()
82 if parser.charset and (parser.charset <> universal_charset):
84 if parser.meta_charset:
85 if log: log(" META charset : %s" % parser.charset)
87 if log: log(" HTTP charset : %s" % parser.charset)
88 if log: log(" current charset: %s" % universal_charset)
89 if log: log(" title : %s" % title)
92 converted_title = unicode(title, parser.charset).encode(universal_charset)
94 if parser.meta_charset and parser.charset.endswith("1252") and \
95 not DEFAULT_CHARSET.endswith("1252") and (DEFAULT_CHARSET <> universal_charset):
96 parser.charset = DEFAULT_CHARSET
97 if log: log(" incorrect conversion from cp1252, converting from %s" % DEFAULT_CHARSET)
98 converted_title = unicode(save_title, DEFAULT_CHARSET, "replace").encode(universal_charset, "replace")
100 converted_title = unicode(title, parser.charset, "replace").encode(universal_charset, "replace")
101 if log and (converted_title <> title): log(" converted title: %s" % converted_title)
103 if log: log(" unknown charset: `%s' or `%s'" % (parser.charset, universal_charset))
105 if log: log(" title : %s" % title)
107 final_title = recode_entities(converted_title, universal_charset)
108 parts = [s.strip() for s in final_title.replace('\r', '').split('\n')]
109 final_title = ' '.join([s for s in parts if s])
110 if log and (final_title <> converted_title): log(" final title : %s" % final_title)
111 parser.title = final_title
115 if __name__ == '__main__':
117 parser = parse_html(sys.argv[1], universal_charset)
120 print parser.title.decode(universal_charset).encode(current_charset, "replace")