3 This file is a part of Bookmarks database and Internet robot.
6 __author__ = "Oleg Broytman <phd@phdru.name>"
7 __copyright__ = "Copyright (C) 1997-2012 PhiloSoft Design"
8 __license__ = "GNU GPL"
10 __all__ = ['parse_html', 'universal_charset']
15 universal_charset = "utf-8"
16 DEFAULT_CHARSET = "cp1251" # Stupid default for Russian Cyrillic
21 from . import bkmk_ph_beautifulsoup
25 bkmk_ph_beautifulsoup.DEFAULT_CHARSET = DEFAULT_CHARSET
26 parsers.append(bkmk_ph_beautifulsoup.parse_html)
29 from . import bkmk_ph_html5
33 parsers.append(bkmk_ph_html5.parse_html)
36 from . import bkmk_ph_lxml
40 parsers.append(bkmk_ph_lxml.parse_html)
43 from . import bkmk_ph_htmlparser
47 parsers.append(bkmk_ph_htmlparser.parse_html)
49 # ElementTidy often segfaults
51 # from . import bkmk_ph_etreetidy
55 # parsers.append(bkmk_ph_etreetidy.parse_html)
58 from htmlentitydefs import name2codepoint
60 entity_re = re.compile("(&\w+;)")
61 num_entity_re = re.compile("(&#[0-9]+;)")
63 def recode_entities(title, charset):
65 for part in entity_re.split(title):
66 if part not in ("&", "<", ">", """) and \
67 entity_re.match(part):
68 _part = name2codepoint.get(part[1:-1], None)
70 part = unichr(_part).encode(charset)
72 title = ''.join(output)
75 for part in num_entity_re.split(title):
76 if num_entity_re.match(part):
78 part = unichr(int(part[2:-1])).encode(charset)
79 except UnicodeEncodeError:
80 pass # Leave the entity as is
83 return ''.join(output)
87 BKMK_DEBUG_HTML_PARSERS = os.environ.get("BKMK_DEBUG_HTML_PARSERS")
89 def parse_html(filename, charset=None, log=None):
95 codecs.lookup(charset) # In case of unknown charset...
96 except (ValueError, LookupError):
97 charset = None # ...try charset from HTML
99 charsets = [universal_charset, DEFAULT_CHARSET]
101 charset = charset.lower().replace("windows-", "cp")
102 if charset in charsets:
103 charsets.remove(charset)
104 charsets.insert(0, charset)
106 if BKMK_DEBUG_HTML_PARSERS:
112 parser = p(filename, c, log)
117 if BKMK_DEBUG_HTML_PARSERS:
118 if log: log(" Parser %s: ok" % p.__module__)
119 _parsers.append((p, parser))
122 if log: log(" Parser %s: fail" % p.__module__)
123 if not BKMK_DEBUG_HTML_PARSERS and parser:
126 if BKMK_DEBUG_HTML_PARSERS:
128 if log: log(" All parsers have failed")
131 if log: log(" All parsers have failed")
134 if BKMK_DEBUG_HTML_PARSERS:
135 p, parser = _parsers[0]
136 if log: log(" Using %s" % p.__module__)
138 converted_title = title = parser.title
139 if title and (not parser.charset):
141 unicode(title, "ascii")
142 except UnicodeDecodeError:
143 parser.charset = DEFAULT_CHARSET
146 parser.charset = parser.charset.lower().replace("windows-", "cp")
148 if title and parser.charset and (
149 (parser.charset <> universal_charset) or
150 ((not charset) or (charset <> parser.charset))):
152 if parser.meta_charset:
153 if log: log(" META charset : %s" % parser.charset)
154 elif (not charset) or (charset <> parser.charset):
155 if log: log(" guessed charset: %s" % parser.charset)
156 #if log: log(" current charset: %s" % universal_charset)
157 if log: log(" title : %s" % title)
158 if parser.charset <> universal_charset:
160 converted_title = unicode(title, parser.charset).encode(universal_charset)
162 if log: log(" incorrect conversion from %s, converting from %s" % (parser.charset, DEFAULT_CHARSET))
163 converted_title = unicode(title, DEFAULT_CHARSET, "replace").encode(universal_charset, "replace")
164 parser.charset = DEFAULT_CHARSET
165 if log and (converted_title <> title): log(" converted title: %s" % converted_title)
167 if log: log(" unknown charset: '%s'" % parser.charset)
169 if log: log(" title : %s" % title)
172 final_title = recode_entities(converted_title, universal_charset)
173 parts = [s.strip() for s in final_title.replace('\r', '').split('\n')]
174 final_title = ' '.join([s for s in parts if s])
175 if log and (final_title <> converted_title): log(" final title : %s" % final_title)
176 parser.title = final_title
179 if isinstance(icon, unicode):
181 parser.icon = icon.encode('ascii')
182 except UnicodeEncodeError:
184 parser.icon = icon.encode(parser.charset)