5 Written by Broytman. Copyright (C) 1997-2010 PhiloSoft Design
10 universal_charset = "utf-8"
11 DEFAULT_CHARSET = "cp1251" # Stupid default for Russian Cyrillic
15 import parse_html_beautifulsoup
16 parse_html_beautifulsoup.DEFAULT_CHARSET = DEFAULT_CHARSET
20 parsers.append(parse_html_beautifulsoup.parse_html)
22 from parse_html_htmlparser import parse_html
23 parsers.append(parse_html)
27 from htmlentitydefs import name2codepoint
29 entity_re = re.compile("(&\w+;)")
30 num_entity_re = re.compile("(&#[0-9]+;)")
32 def recode_entities(title, charset):
34 for part in entity_re.split(title):
35 if part not in ("&", "<", ">", """) and \
36 entity_re.match(part):
37 _part = name2codepoint.get(part[1:-1], None)
39 part = unichr(_part).encode(charset)
41 title = ''.join(output)
44 for part in num_entity_re.split(title):
45 if num_entity_re.match(part):
47 part = unichr(int(part[2:-1])).encode(charset)
48 except UnicodeEncodeError:
49 pass # Leave the entity as is
52 return ''.join(output)
55 def parse_html(filename, charset=None, log=None):
58 codecs.lookup(charset) # In case of unknown charset...
59 except (ValueError, LookupError):
60 charset = None # ...try charset from HTML
62 charsets = [universal_charset, DEFAULT_CHARSET]
64 charset = charset.lower().replace("windows-", "cp")
65 if charset not in charsets:
66 charsets.insert(0, charset)
72 parser = p(filename, c, log)
74 except UnicodeEncodeError:
79 if log: log("Parser %s.%s failed, trying next one." % (p.__module__, p.__name__))
81 converted_title = title = parser.title
82 if title and (not parser.charset):
84 unicode(title, "ascii")
85 except UnicodeDecodeError:
86 parser.charset = DEFAULT_CHARSET
89 parser.charset = parser.charset.lower().replace("windows-", "cp")
91 if title and parser.charset and (
92 (parser.charset <> universal_charset) or
93 ((not charset) or (charset <> parser.charset))):
95 if parser.meta_charset:
96 if log: log(" META charset : %s" % parser.charset)
97 elif (not charset) or (charset <> parser.charset):
98 if log: log(" guessed charset: %s" % parser.charset)
99 if log: log(" current charset: %s" % universal_charset)
100 if log: log(" title : %s" % title)
101 if parser.charset <> universal_charset:
103 converted_title = unicode(title, parser.charset).encode(universal_charset)
105 if log: log(" incorrect conversion from %s, converting from %s" % (parser.charset, DEFAULT_CHARSET))
106 converted_title = unicode(title, DEFAULT_CHARSET, "replace").encode(universal_charset, "replace")
107 parser.charset = DEFAULT_CHARSET
108 if log and (converted_title <> title): log(" converted title: %s" % converted_title)
110 if log: log(" unknown charset: '%s'" % parser.charset)
112 if log: log(" title : %s" % title)
115 final_title = recode_entities(converted_title, universal_charset)
116 parts = [s.strip() for s in final_title.replace('\r', '').split('\n')]
117 final_title = ' '.join([s for s in parts if s])
118 if log and (final_title <> converted_title): log(" final title : %s" % final_title)
119 parser.title = final_title
123 if __name__ == '__main__':
128 filename = sys.argv[1]
129 charset = sys.argv[2]
131 filename = sys.argv[1]
132 charset = universal_charset
134 sys.exit("Usage: %s filename [charset]" % sys.argv[0])
136 parser = parse_html(filename, charset, log=lambda s: sys.stdout.write(s + '\n'))
137 print " refresh:", parser.refresh
138 print " icon :", parser.icon