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)
24 from parse_html_htmlparser import parse_html
28 parsers.append(parse_html)
31 import parse_html_html5
35 parsers.append(parse_html_html5.parse_html)
39 from htmlentitydefs import name2codepoint
41 entity_re = re.compile("(&\w+;)")
42 num_entity_re = re.compile("(&#[0-9]+;)")
44 def recode_entities(title, charset):
46 for part in entity_re.split(title):
47 if part not in ("&", "<", ">", """) and \
48 entity_re.match(part):
49 _part = name2codepoint.get(part[1:-1], None)
51 part = unichr(_part).encode(charset)
53 title = ''.join(output)
56 for part in num_entity_re.split(title):
57 if num_entity_re.match(part):
59 part = unichr(int(part[2:-1])).encode(charset)
60 except UnicodeEncodeError:
61 pass # Leave the entity as is
64 return ''.join(output)
67 def parse_html(filename, charset=None, log=None):
73 codecs.lookup(charset) # In case of unknown charset...
74 except (ValueError, LookupError):
75 charset = None # ...try charset from HTML
77 charsets = [universal_charset, DEFAULT_CHARSET]
79 charset = charset.lower().replace("windows-", "cp")
80 if charset in charsets:
81 charsets.remove(charset)
82 charsets.insert(0, charset)
88 parser = p(filename, c, log)
90 except UnicodeEncodeError:
95 if log: log("Parser %s.%s failed, trying next one." % (p.__module__, p.__name__))
100 converted_title = title = parser.title
101 if title and (not parser.charset):
103 unicode(title, "ascii")
104 except UnicodeDecodeError:
105 parser.charset = DEFAULT_CHARSET
108 parser.charset = parser.charset.lower().replace("windows-", "cp")
110 if title and parser.charset and (
111 (parser.charset <> universal_charset) or
112 ((not charset) or (charset <> parser.charset))):
114 if parser.meta_charset:
115 if log: log(" META charset : %s" % parser.charset)
116 elif (not charset) or (charset <> parser.charset):
117 if log: log(" guessed charset: %s" % parser.charset)
118 if log: log(" current charset: %s" % universal_charset)
119 if log: log(" title : %s" % title)
120 if parser.charset <> universal_charset:
122 converted_title = unicode(title, parser.charset).encode(universal_charset)
124 if log: log(" incorrect conversion from %s, converting from %s" % (parser.charset, DEFAULT_CHARSET))
125 converted_title = unicode(title, DEFAULT_CHARSET, "replace").encode(universal_charset, "replace")
126 parser.charset = DEFAULT_CHARSET
127 if log and (converted_title <> title): log(" converted title: %s" % converted_title)
129 if log: log(" unknown charset: '%s'" % parser.charset)
131 if log: log(" title : %s" % title)
134 final_title = recode_entities(converted_title, universal_charset)
135 parts = [s.strip() for s in final_title.replace('\r', '').split('\n')]
136 final_title = ' '.join([s for s in parts if s])
137 if log and (final_title <> converted_title): log(" final title : %s" % final_title)
138 parser.title = final_title
142 if __name__ == '__main__':
147 filename = sys.argv[1]
148 charset = sys.argv[2]
150 filename = sys.argv[1]
151 charset = universal_charset
153 sys.exit("Usage: %s filename [charset]" % sys.argv[0])
155 parser = parse_html(filename, charset, log=lambda s: sys.stdout.write(s + '\n'))
156 print " refresh:", parser.refresh
157 print " icon :", parser.icon