5 Written by Broytman. Copyright (C) 1997-2008 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)
41 _part = unichr(_part).encode(charset)
43 title = ''.join(output)
46 for part in num_entity_re.split(title):
47 if num_entity_re.match(part):
49 part = unichr(int(part[2:-1])).encode(charset)
50 except UnicodeEncodeError:
51 pass # Leave the entity as is
54 return ''.join(output)
57 def parse_html(filename, charset=None, log=None):
60 codecs.lookup(charset) # In case of unknown charset...
61 except (ValueError, LookupError):
62 charset = None # ...try charset from HTML
64 charsets = [universal_charset, DEFAULT_CHARSET]
66 charset = charset.lower().replace("windows-", "cp")
67 if charset not in charsets:
68 charsets.insert(0, charset)
74 parser = p(filename, c, log)
76 except UnicodeEncodeError:
81 if log: log("Parser %s.%s failed, trying next one." % (p.__module__, p.__name__))
83 converted_title = title = parser.title
84 if title and (not parser.charset):
86 unicode(title, "ascii")
87 except UnicodeDecodeError:
88 parser.charset = DEFAULT_CHARSET
91 parser.charset = parser.charset.lower().replace("windows-", "cp")
93 if title and parser.charset and (
94 (parser.charset <> universal_charset) or
95 ((not charset) or (charset <> parser.charset))):
97 if parser.meta_charset:
98 if log: log(" META charset : %s" % parser.charset)
99 elif (not charset) or (charset <> parser.charset):
100 if log: log(" guessed charset: %s" % parser.charset)
101 if log: log(" current charset: %s" % universal_charset)
102 if log: log(" title : %s" % title)
103 if parser.charset <> universal_charset:
105 converted_title = unicode(title, parser.charset).encode(universal_charset)
107 if log: log(" incorrect conversion from %s, converting from %s" % (parser.charset, DEFAULT_CHARSET))
108 converted_title = unicode(title, DEFAULT_CHARSET, "replace").encode(universal_charset, "replace")
109 parser.charset = DEFAULT_CHARSET
110 if log and (converted_title <> title): log(" converted title: %s" % converted_title)
112 if log: log(" unknown charset: '%s'" % parser.charset)
114 if log: log(" title : %s" % title)
117 final_title = recode_entities(converted_title, universal_charset)
118 parts = [s.strip() for s in final_title.replace('\r', '').split('\n')]
119 final_title = ' '.join([s for s in parts if s])
120 if log and (final_title <> converted_title): log(" final title : %s" % final_title)
121 parser.title = final_title
125 if __name__ == '__main__':
130 filename = sys.argv[1]
131 charset = sys.argv[2]
133 filename = sys.argv[1]
134 charset = universal_charset
136 sys.exit("Usage: %s filename [charset]" % sys.argv[0])
138 parser = parse_html(filename, charset, log=lambda s: sys.stdout.write(s + '\n'))
139 print " refresh:", parser.refresh
140 print " icon :", parser.icon