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"
15 universal_charset = "utf-8"
16 DEFAULT_CHARSET = "cp1251" # Stupid default for Russian Cyrillic
21 from . import beautifulsoup
25 beautifulsoup.DEFAULT_CHARSET = DEFAULT_CHARSET
26 parsers.append(beautifulsoup.parse_html)
29 from .lxml import parse_html
33 parsers.append(parse_html)
36 from .htmlparser import parse_html
40 parsers.append(parse_html)
47 parsers.append(html5.parse_html)
49 # ElementTidy often segfaults
51 # from . import etreetidy
55 # parsers.append(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)
86 def parse_html(filename, charset=None, log=None):
92 codecs.lookup(charset) # In case of unknown charset...
93 except (ValueError, LookupError):
94 charset = None # ...try charset from HTML
96 charsets = [universal_charset, DEFAULT_CHARSET]
98 charset = charset.lower().replace("windows-", "cp")
99 if charset in charsets:
100 charsets.remove(charset)
101 charsets.insert(0, charset)
107 parser = p(filename, c, log)
108 except UnicodeEncodeError:
115 if log: log("Parser %s.%s failed, trying next one." % (p.__module__, p.__name__))
120 converted_title = title = parser.title
121 if title and (not parser.charset):
123 unicode(title, "ascii")
124 except UnicodeDecodeError:
125 parser.charset = DEFAULT_CHARSET
128 parser.charset = parser.charset.lower().replace("windows-", "cp")
130 if title and parser.charset and (
131 (parser.charset <> universal_charset) or
132 ((not charset) or (charset <> parser.charset))):
134 if parser.meta_charset:
135 if log: log(" META charset : %s" % parser.charset)
136 elif (not charset) or (charset <> parser.charset):
137 if log: log(" guessed charset: %s" % parser.charset)
138 if log: log(" current charset: %s" % universal_charset)
139 if log: log(" title : %s" % title)
140 if parser.charset <> universal_charset:
142 converted_title = unicode(title, parser.charset).encode(universal_charset)
144 if log: log(" incorrect conversion from %s, converting from %s" % (parser.charset, DEFAULT_CHARSET))
145 converted_title = unicode(title, DEFAULT_CHARSET, "replace").encode(universal_charset, "replace")
146 parser.charset = DEFAULT_CHARSET
147 if log and (converted_title <> title): log(" converted title: %s" % converted_title)
149 if log: log(" unknown charset: '%s'" % parser.charset)
151 if log: log(" title : %s" % title)
154 final_title = recode_entities(converted_title, universal_charset)
155 parts = [s.strip() for s in final_title.replace('\r', '').split('\n')]
156 final_title = ' '.join([s for s in parts if s])
157 if log and (final_title <> converted_title): log(" final title : %s" % final_title)
158 parser.title = final_title
161 if isinstance(icon, unicode):
163 parser.icon = icon.encode('ascii')
164 except UnicodeEncodeError:
166 parser.icon = icon.encode(parser.charset)
175 filename = sys.argv[1]
176 charset = sys.argv[2]
178 filename = sys.argv[1]
179 charset = universal_charset
181 sys.exit("Usage: %s filename [charset]" % sys.argv[0])
183 parser = parse_html(filename, charset, log=lambda s: sys.stdout.write(s + '\n'))
184 print " refresh:", parser.refresh
185 print " icon :", parser.icon