4 Written by Broytman. Copyright (C) 1997-2011 PhiloSoft Design
9 universal_charset = "utf-8"
10 DEFAULT_CHARSET = "cp1251" # Stupid default for Russian Cyrillic
15 from . import beautifulsoup
19 beautifulsoup.DEFAULT_CHARSET = DEFAULT_CHARSET
20 parsers.append(beautifulsoup.parse_html)
23 from .lxml import parse_html
27 parsers.append(parse_html)
30 from .htmlparser import parse_html
34 parsers.append(parse_html)
41 parsers.append(html5.parse_html)
43 # ElementTidy often segfaults
45 # from . import etreetidy
49 # parsers.append(etreetidy.parse_html)
52 from htmlentitydefs import name2codepoint
54 entity_re = re.compile("(&\w+;)")
55 num_entity_re = re.compile("(&#[0-9]+;)")
57 def recode_entities(title, charset):
59 for part in entity_re.split(title):
60 if part not in ("&", "<", ">", """) and \
61 entity_re.match(part):
62 _part = name2codepoint.get(part[1:-1], None)
64 part = unichr(_part).encode(charset)
66 title = ''.join(output)
69 for part in num_entity_re.split(title):
70 if num_entity_re.match(part):
72 part = unichr(int(part[2:-1])).encode(charset)
73 except UnicodeEncodeError:
74 pass # Leave the entity as is
77 return ''.join(output)
80 def parse_html(filename, charset=None, log=None):
86 codecs.lookup(charset) # In case of unknown charset...
87 except (ValueError, LookupError):
88 charset = None # ...try charset from HTML
90 charsets = [universal_charset, DEFAULT_CHARSET]
92 charset = charset.lower().replace("windows-", "cp")
93 if charset in charsets:
94 charsets.remove(charset)
95 charsets.insert(0, charset)
101 parser = p(filename, c, log)
102 except UnicodeEncodeError:
109 if log: log("Parser %s.%s failed, trying next one." % (p.__module__, p.__name__))
114 converted_title = title = parser.title
115 if title and (not parser.charset):
117 unicode(title, "ascii")
118 except UnicodeDecodeError:
119 parser.charset = DEFAULT_CHARSET
122 parser.charset = parser.charset.lower().replace("windows-", "cp")
124 if title and parser.charset and (
125 (parser.charset <> universal_charset) or
126 ((not charset) or (charset <> parser.charset))):
128 if parser.meta_charset:
129 if log: log(" META charset : %s" % parser.charset)
130 elif (not charset) or (charset <> parser.charset):
131 if log: log(" guessed charset: %s" % parser.charset)
132 if log: log(" current charset: %s" % universal_charset)
133 if log: log(" title : %s" % title)
134 if parser.charset <> universal_charset:
136 converted_title = unicode(title, parser.charset).encode(universal_charset)
138 if log: log(" incorrect conversion from %s, converting from %s" % (parser.charset, DEFAULT_CHARSET))
139 converted_title = unicode(title, DEFAULT_CHARSET, "replace").encode(universal_charset, "replace")
140 parser.charset = DEFAULT_CHARSET
141 if log and (converted_title <> title): log(" converted title: %s" % converted_title)
143 if log: log(" unknown charset: '%s'" % parser.charset)
145 if log: log(" title : %s" % title)
148 final_title = recode_entities(converted_title, universal_charset)
149 parts = [s.strip() for s in final_title.replace('\r', '').split('\n')]
150 final_title = ' '.join([s for s in parts if s])
151 if log and (final_title <> converted_title): log(" final title : %s" % final_title)
152 parser.title = final_title
155 if isinstance(icon, unicode):
157 parser.icon = icon.encode('ascii')
158 except UnicodeEncodeError:
160 parser.icon = icon.encode(parser.charset)
169 filename = sys.argv[1]
170 charset = sys.argv[2]
172 filename = sys.argv[1]
173 charset = universal_charset
175 sys.exit("Usage: %s filename [charset]" % sys.argv[0])
177 parser = parse_html(filename, charset, log=lambda s: sys.stdout.write(s + '\n'))
178 print " refresh:", parser.refresh
179 print " icon :", parser.icon