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_html5
20 parsers.append(parse_html_html5.parse_html)
23 import parse_html_beautifulsoup
24 parse_html_beautifulsoup.DEFAULT_CHARSET = DEFAULT_CHARSET
28 parsers.append(parse_html_beautifulsoup.parse_html)
30 from parse_html_htmlparser import parse_html
31 parsers.append(parse_html)
35 from htmlentitydefs import name2codepoint
37 entity_re = re.compile("(&\w+;)")
38 num_entity_re = re.compile("(&#[0-9]+;)")
40 def recode_entities(title, charset):
42 for part in entity_re.split(title):
43 if part not in ("&", "<", ">", """) and \
44 entity_re.match(part):
45 _part = name2codepoint.get(part[1:-1], None)
47 part = unichr(_part).encode(charset)
49 title = ''.join(output)
52 for part in num_entity_re.split(title):
53 if num_entity_re.match(part):
55 part = unichr(int(part[2:-1])).encode(charset)
56 except UnicodeEncodeError:
57 pass # Leave the entity as is
60 return ''.join(output)
63 def parse_html(filename, charset=None, log=None):
66 codecs.lookup(charset) # In case of unknown charset...
67 except (ValueError, LookupError):
68 charset = None # ...try charset from HTML
70 charsets = [universal_charset, DEFAULT_CHARSET]
72 charset = charset.lower().replace("windows-", "cp")
73 if charset not in charsets:
74 charsets.insert(0, charset)
80 parser = p(filename, c, log)
82 except UnicodeEncodeError:
87 if log: log("Parser %s.%s failed, trying next one." % (p.__module__, p.__name__))
89 converted_title = title = parser.title
90 if title and (not parser.charset):
92 unicode(title, "ascii")
93 except UnicodeDecodeError:
94 parser.charset = DEFAULT_CHARSET
97 parser.charset = parser.charset.lower().replace("windows-", "cp")
99 if title and parser.charset and (
100 (parser.charset <> universal_charset) or
101 ((not charset) or (charset <> parser.charset))):
103 if parser.meta_charset:
104 if log: log(" META charset : %s" % parser.charset)
105 elif (not charset) or (charset <> parser.charset):
106 if log: log(" guessed charset: %s" % parser.charset)
107 if log: log(" current charset: %s" % universal_charset)
108 if log: log(" title : %s" % title)
109 if parser.charset <> universal_charset:
111 converted_title = unicode(title, parser.charset).encode(universal_charset)
113 if log: log(" incorrect conversion from %s, converting from %s" % (parser.charset, DEFAULT_CHARSET))
114 converted_title = unicode(title, DEFAULT_CHARSET, "replace").encode(universal_charset, "replace")
115 parser.charset = DEFAULT_CHARSET
116 if log and (converted_title <> title): log(" converted title: %s" % converted_title)
118 if log: log(" unknown charset: '%s'" % parser.charset)
120 if log: log(" title : %s" % title)
123 final_title = recode_entities(converted_title, universal_charset)
124 parts = [s.strip() for s in final_title.replace('\r', '').split('\n')]
125 final_title = ' '.join([s for s in parts if s])
126 if log and (final_title <> converted_title): log(" final title : %s" % final_title)
127 parser.title = final_title
131 if __name__ == '__main__':
136 filename = sys.argv[1]
137 charset = sys.argv[2]
139 filename = sys.argv[1]
140 charset = universal_charset
142 sys.exit("Usage: %s filename [charset]" % sys.argv[0])
144 parser = parse_html(filename, charset, log=lambda s: sys.stdout.write(s + '\n'))
145 print " refresh:", parser.refresh
146 print " icon :", parser.icon