3 This file is a part of Bookmarks database and Internet robot.
6 __version__ = "$Revision$"[11:-2]
7 __revision__ = "$Id$"[5:-2]
8 __date__ = "$Date$"[7:-2]
9 __author__ = "Oleg Broytman <phd@phdru.name>"
10 __copyright__ = "Copyright (C) 1997-2011 PhiloSoft Design"
11 __license__ = "GNU GPL"
13 __all__ = ['parse_html', 'universal_charset']
18 universal_charset = "utf-8"
19 DEFAULT_CHARSET = "cp1251" # Stupid default for Russian Cyrillic
23 # Statistics by parser - successfully parsed HTML pages:
30 from . import bkmk_ph_beautifulsoup
34 bkmk_ph_beautifulsoup.DEFAULT_CHARSET = DEFAULT_CHARSET
35 parsers.append(bkmk_ph_beautifulsoup.parse_html)
38 from . import bkmk_ph_htmlparser
42 parsers.append(bkmk_ph_htmlparser.parse_html)
45 from . import bkmk_ph_html5
49 parsers.append(bkmk_ph_html5.parse_html)
52 from . import bkmk_ph_lxml
56 parsers.append(bkmk_ph_lxml.parse_html)
58 # ElementTidy often segfaults
60 # from . import bkmk_ph_etreetidy
64 # parsers.append(bkmk_ph_etreetidy.parse_html)
67 from htmlentitydefs import name2codepoint
69 entity_re = re.compile("(&\w+;)")
70 num_entity_re = re.compile("(&#[0-9]+;)")
72 def recode_entities(title, charset):
74 for part in entity_re.split(title):
75 if part not in ("&", "<", ">", """) and \
76 entity_re.match(part):
77 _part = name2codepoint.get(part[1:-1], None)
79 part = unichr(_part).encode(charset)
81 title = ''.join(output)
84 for part in num_entity_re.split(title):
85 if num_entity_re.match(part):
87 part = unichr(int(part[2:-1])).encode(charset)
88 except UnicodeEncodeError:
89 pass # Leave the entity as is
92 return ''.join(output)
95 def parse_html(filename, charset=None, log=None):
101 codecs.lookup(charset) # In case of unknown charset...
102 except (ValueError, LookupError):
103 charset = None # ...try charset from HTML
105 charsets = [universal_charset, DEFAULT_CHARSET]
107 charset = charset.lower().replace("windows-", "cp")
108 if charset in charsets:
109 charsets.remove(charset)
110 charsets.insert(0, charset)
117 parser = p(filename, c, log)
122 if log: log(" Parser %s: ok" % p.__module__)
123 #_parsers.append(parser)
126 if log: log(" Parser %s: fail" % p.__module__)
132 if log: log(" All parser has failed")
135 #parser = _parsers[0]
136 if log: log(" Using %s" % parser.__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)