5 Written by BroytMann. 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 = unichr(name2codepoint.get(part[1:-1], part)).encode(charset)
39 title = ''.join(output)
42 for part in num_entity_re.split(title):
43 if num_entity_re.match(part):
45 part = unichr(int(part[2:-1])).encode(charset)
46 except UnicodeEncodeError:
47 pass # Leave the entity as is
50 return ''.join(output)
53 def parse_html(filename, charset=None, log=None):
56 codecs.lookup(charset) # In case of unknown charset...
57 except (ValueError, LookupError):
58 charset = None # ...try charset from HTML
60 charsets = [universal_charset, DEFAULT_CHARSET]
62 charset = charset.lower().replace("windows-", "cp")
63 if charset not in charsets:
64 charsets.insert(0, charset)
70 parser = p(filename, c, log)
72 except UnicodeEncodeError:
77 if log: log("Parser %s.%s failed, trying next one." % (p.__module__, p.__name__))
79 converted_title = title = parser.title
80 if title and (not parser.charset):
82 unicode(title, "ascii")
83 except UnicodeDecodeError:
84 parser.charset = DEFAULT_CHARSET
87 parser.charset = parser.charset.lower().replace("windows-", "cp")
89 if title and parser.charset and (
90 (parser.charset <> universal_charset) or
91 ((not charset) or (charset <> parser.charset))):
93 if parser.meta_charset:
94 if log: log(" META charset : %s" % parser.charset)
95 elif (not charset) or (charset <> parser.charset):
96 if log: log(" guessed charset: %s" % parser.charset)
97 if log: log(" current charset: %s" % universal_charset)
98 if log: log(" title : %s" % title)
99 if parser.charset <> universal_charset:
101 converted_title = unicode(title, parser.charset).encode(universal_charset)
103 if log: log(" incorrect conversion from %s, converting from %s" % (parser.charset, DEFAULT_CHARSET))
104 converted_title = unicode(title, DEFAULT_CHARSET, "replace").encode(universal_charset, "replace")
105 parser.charset = DEFAULT_CHARSET
106 if log and (converted_title <> title): log(" converted title: %s" % converted_title)
108 if log: log(" unknown charset: '%s'" % parser.charset)
110 if log: log(" title : %s" % title)
113 final_title = recode_entities(converted_title, universal_charset)
114 parts = [s.strip() for s in final_title.replace('\r', '').split('\n')]
115 final_title = ' '.join([s for s in parts if s])
116 if log and (final_title <> converted_title): log(" final title : %s" % final_title)
117 parser.title = final_title
121 if __name__ == '__main__':
126 filename = sys.argv[1]
127 charset = sys.argv[2]
129 filename = sys.argv[1]
130 charset = universal_charset
132 sys.exit("Usage: %s filename [charset]" % sys.argv[0])
134 parser = parse_html(filename, charset, log=lambda s: sys.stdout.write(s + '\n'))
135 print " refresh:", parser.refresh
136 print " icon :", parser.icon