3 This file is a part of Bookmarks database and Internet robot.
7 __author__ = "Oleg Broytman <phd@phdru.name>"
8 __copyright__ = "Copyright (C) 1997-2024 PhiloSoft Design"
9 __license__ = "GNU GPL"
11 __all__ = ['parse_html', 'parse_filename', 'universal_charset']
17 from html.entities import name2codepoint
19 DEFAULT_CHARSET = "cp1251" # Stupid default for Russian Cyrillic
23 from . import bkmk_ph_beautifulsoup
27 bkmk_ph_beautifulsoup.DEFAULT_CHARSET = DEFAULT_CHARSET
28 parsers.append(bkmk_ph_beautifulsoup.parse_html)
31 from . import bkmk_ph_beautifulsoup4
35 bkmk_ph_beautifulsoup4.DEFAULT_CHARSET = DEFAULT_CHARSET
36 parsers.append(bkmk_ph_beautifulsoup4.parse_html)
39 from . import bkmk_ph_htmlparser
43 parsers.append(bkmk_ph_htmlparser.parse_html)
46 from . import bkmk_ph_lxml
50 parsers.append(bkmk_ph_lxml.parse_html)
52 universal_charset = "utf-8"
53 entity_re = re.compile("(&\\w+;)")
54 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)
66 title = ''.join(output)
69 for part in num_entity_re.split(title):
70 if num_entity_re.match(part):
72 part = chr(int(part[2:-1]))
73 except UnicodeEncodeError:
74 pass # Leave the entity as is
77 return ''.join(output)
80 BKMK_DEBUG_HTML_PARSERS = os.environ.get("BKMK_DEBUG_HTML_PARSERS")
83 def parse_html(html_text, charset=None, log=None):
84 if not html_text or not parsers:
89 codecs.lookup(charset) # In case of unknown charset...
90 except (ValueError, LookupError):
91 charset = None # ...try charset from HTML
93 charsets = [universal_charset, DEFAULT_CHARSET]
95 charset = charset.lower().replace("windows-", "cp")
96 if charset in charsets:
97 charsets.remove(charset)
98 charsets.insert(0, charset)
100 if BKMK_DEBUG_HTML_PARSERS:
106 parser = p(html_text, c, log)
111 if BKMK_DEBUG_HTML_PARSERS:
112 if log: log(" Parser %s: ok" % p.__module__)
113 _parsers.append((p, parser))
116 if log: log(" Parser %s: fail" % p.__module__)
117 if not BKMK_DEBUG_HTML_PARSERS and parser:
120 if BKMK_DEBUG_HTML_PARSERS:
122 if log: log(" All parsers have failed")
125 if log: log(" All parsers have failed")
128 if BKMK_DEBUG_HTML_PARSERS:
129 p, parser = _parsers[0]
130 if log: log(" Using %s" % p.__module__)
132 converted_title = title = parser.title
133 if title and isinstance(title, bytes) and (not parser.charset):
135 title.decode("ascii")
136 except UnicodeDecodeError:
137 parser.charset = DEFAULT_CHARSET
140 parser.charset = parser.charset.lower().replace("windows-", "cp")
142 if title and parser.charset and (
143 (parser.charset != universal_charset) or
144 ((not charset) or (charset != parser.charset))):
146 if parser.meta_charset:
147 if log: log(" META charset : %s" % parser.charset)
148 elif (not charset) or (charset != parser.charset):
149 if log: log(" guessed charset: %s" % parser.charset)
150 if log: log(" title : %s" % title)
152 if log: log(" unknown charset: '%s'" % parser.charset)
154 if log: log(" title : %s" % title)
157 final_title = recode_entities(converted_title, universal_charset)
158 parts = [s.strip() for s in final_title.replace('\r', '').split('\n')]
159 final_title = ' '.join([s for s in parts if s])
160 if log and (final_title != converted_title):
161 log(" final title : %s" % final_title)
162 parser.title = final_title
167 def parse_filename(filename, charset=None, log=None):
168 fp = open(filename, 'rt', encoding=charset)
170 parser = parse_html(fp.read(), charset=charset, log=log)