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_lxml import parse_html
28 parsers.append(parse_html)
31 from parse_html_htmlparser import parse_html
35 parsers.append(parse_html)
38 import parse_html_html5
42 parsers.append(parse_html_html5.parse_html)
46 from htmlentitydefs import name2codepoint
48 entity_re = re.compile("(&\w+;)")
49 num_entity_re = re.compile("(&#[0-9]+;)")
51 def recode_entities(title, charset):
53 for part in entity_re.split(title):
54 if part not in ("&", "<", ">", """) and \
55 entity_re.match(part):
56 _part = name2codepoint.get(part[1:-1], None)
58 part = unichr(_part).encode(charset)
60 title = ''.join(output)
63 for part in num_entity_re.split(title):
64 if num_entity_re.match(part):
66 part = unichr(int(part[2:-1])).encode(charset)
67 except UnicodeEncodeError:
68 pass # Leave the entity as is
71 return ''.join(output)
74 def parse_html(filename, charset=None, log=None):
80 codecs.lookup(charset) # In case of unknown charset...
81 except (ValueError, LookupError):
82 charset = None # ...try charset from HTML
84 charsets = [universal_charset, DEFAULT_CHARSET]
86 charset = charset.lower().replace("windows-", "cp")
87 if charset in charsets:
88 charsets.remove(charset)
89 charsets.insert(0, charset)
95 parser = p(filename, c, log)
97 except UnicodeEncodeError:
102 if log: log("Parser %s.%s failed, trying next one." % (p.__module__, p.__name__))
107 converted_title = title = parser.title
108 if title and (not parser.charset):
110 unicode(title, "ascii")
111 except UnicodeDecodeError:
112 parser.charset = DEFAULT_CHARSET
115 parser.charset = parser.charset.lower().replace("windows-", "cp")
117 if title and parser.charset and (
118 (parser.charset <> universal_charset) or
119 ((not charset) or (charset <> parser.charset))):
121 if parser.meta_charset:
122 if log: log(" META charset : %s" % parser.charset)
123 elif (not charset) or (charset <> parser.charset):
124 if log: log(" guessed charset: %s" % parser.charset)
125 if log: log(" current charset: %s" % universal_charset)
126 if log: log(" title : %s" % title)
127 if parser.charset <> universal_charset:
129 converted_title = unicode(title, parser.charset).encode(universal_charset)
131 if log: log(" incorrect conversion from %s, converting from %s" % (parser.charset, DEFAULT_CHARSET))
132 converted_title = unicode(title, DEFAULT_CHARSET, "replace").encode(universal_charset, "replace")
133 parser.charset = DEFAULT_CHARSET
134 if log and (converted_title <> title): log(" converted title: %s" % converted_title)
136 if log: log(" unknown charset: '%s'" % parser.charset)
138 if log: log(" title : %s" % title)
141 final_title = recode_entities(converted_title, universal_charset)
142 parts = [s.strip() for s in final_title.replace('\r', '').split('\n')]
143 final_title = ' '.join([s for s in parts if s])
144 if log and (final_title <> converted_title): log(" final title : %s" % final_title)
145 parser.title = final_title
149 if __name__ == '__main__':
154 filename = sys.argv[1]
155 charset = sys.argv[2]
157 filename = sys.argv[1]
158 charset = universal_charset
160 sys.exit("Usage: %s filename [charset]" % sys.argv[0])
162 parser = parse_html(filename, charset, log=lambda s: sys.stdout.write(s + '\n'))
163 print " refresh:", parser.refresh
164 print " icon :", parser.icon