5 Written by Broytman. Copyright (C) 1997-2010 PhiloSoft Design
10 universal_charset = "utf-8"
11 DEFAULT_CHARSET = "cp1251" # Stupid default for Russian Cyrillic
16 import parse_html_beautifulsoup
17 parse_html_beautifulsoup.DEFAULT_CHARSET = DEFAULT_CHARSET
21 parsers.append(parse_html_beautifulsoup.parse_html)
23 from parse_html_htmlparser import parse_html
24 parsers.append(parse_html)
28 from htmlentitydefs import name2codepoint
30 entity_re = re.compile("(&\w+;)")
31 num_entity_re = re.compile("(&#[0-9]+;)")
33 def recode_entities(title, charset):
35 for part in entity_re.split(title):
36 if part not in ("&", "<", ">", """) and \
37 entity_re.match(part):
38 _part = name2codepoint.get(part[1:-1], None)
40 part = unichr(_part).encode(charset)
42 title = ''.join(output)
45 for part in num_entity_re.split(title):
46 if num_entity_re.match(part):
48 part = unichr(int(part[2:-1])).encode(charset)
49 except UnicodeEncodeError:
50 pass # Leave the entity as is
53 return ''.join(output)
56 def parse_html(filename, charset=None, log=None):
59 codecs.lookup(charset) # In case of unknown charset...
60 except (ValueError, LookupError):
61 charset = None # ...try charset from HTML
63 charsets = [universal_charset, DEFAULT_CHARSET]
65 charset = charset.lower().replace("windows-", "cp")
66 if charset not in charsets:
67 charsets.insert(0, charset)
73 parser = p(filename, c, log)
75 except UnicodeEncodeError:
80 if log: log("Parser %s.%s failed, trying next one." % (p.__module__, p.__name__))
82 converted_title = title = parser.title
83 if title and (not parser.charset):
85 unicode(title, "ascii")
86 except UnicodeDecodeError:
87 parser.charset = DEFAULT_CHARSET
90 parser.charset = parser.charset.lower().replace("windows-", "cp")
92 if title and parser.charset and (
93 (parser.charset <> universal_charset) or
94 ((not charset) or (charset <> parser.charset))):
96 if parser.meta_charset:
97 if log: log(" META charset : %s" % parser.charset)
98 elif (not charset) or (charset <> parser.charset):
99 if log: log(" guessed charset: %s" % parser.charset)
100 if log: log(" current charset: %s" % universal_charset)
101 if log: log(" title : %s" % title)
102 if parser.charset <> universal_charset:
104 converted_title = unicode(title, parser.charset).encode(universal_charset)
106 if log: log(" incorrect conversion from %s, converting from %s" % (parser.charset, DEFAULT_CHARSET))
107 converted_title = unicode(title, DEFAULT_CHARSET, "replace").encode(universal_charset, "replace")
108 parser.charset = DEFAULT_CHARSET
109 if log and (converted_title <> title): log(" converted title: %s" % converted_title)
111 if log: log(" unknown charset: '%s'" % parser.charset)
113 if log: log(" title : %s" % title)
116 final_title = recode_entities(converted_title, universal_charset)
117 parts = [s.strip() for s in final_title.replace('\r', '').split('\n')]
118 final_title = ' '.join([s for s in parts if s])
119 if log and (final_title <> converted_title): log(" final title : %s" % final_title)
120 parser.title = final_title
124 if __name__ == '__main__':
129 filename = sys.argv[1]
130 charset = sys.argv[2]
132 filename = sys.argv[1]
133 charset = universal_charset
135 sys.exit("Usage: %s filename [charset]" % sys.argv[0])
137 parser = parse_html(filename, charset, log=lambda s: sys.stdout.write(s + '\n'))
138 print " refresh:", parser.refresh
139 print " icon :", parser.icon