X-Git-Url: https://git.phdru.name/?a=blobdiff_plain;f=Robots%2Fparse_html.py;h=cc7655bfa950770d3f092ad7c32d387f2f65bd8b;hb=ee556aa2ed24d28ad25cbbe31f9141e4e8fdc932;hp=47cbb190527d25c3a1bdb74bf0b694a0fe7df909;hpb=73f38dccb4a11e857ae3497a61089a4ea89cd654;p=bookmarks_db.git diff --git a/Robots/parse_html.py b/Robots/parse_html.py index 47cbb19..cc7655b 100755 --- a/Robots/parse_html.py +++ b/Robots/parse_html.py @@ -2,7 +2,7 @@ """ HTML Parsers wrapper - Written by BroytMann. Copyright (C) 1997-2008 PhiloSoft Design + Written by Broytman. Copyright (C) 1997-2010 PhiloSoft Design """ import codecs @@ -11,6 +11,14 @@ universal_charset = "utf-8" DEFAULT_CHARSET = "cp1251" # Stupid default for Russian Cyrillic parsers = [] + +try: + import parse_html_etreetidy +except ImportError: + pass +else: + parsers.append(parse_html_etreetidy.parse_html) + try: import parse_html_beautifulsoup parse_html_beautifulsoup.DEFAULT_CHARSET = DEFAULT_CHARSET @@ -34,7 +42,9 @@ def recode_entities(title, charset): for part in entity_re.split(title): if part not in ("&", "<", ">", """) and \ entity_re.match(part): - part = unichr(name2codepoint.get(part[1:-1], part)).encode(charset) + _part = name2codepoint.get(part[1:-1], None) + if _part is not None: + part = unichr(_part).encode(charset) output.append(part) title = ''.join(output) @@ -77,7 +87,7 @@ def parse_html(filename, charset=None, log=None): if log: log("Parser %s.%s failed, trying next one." % (p.__module__, p.__name__)) converted_title = title = parser.title - if not parser.charset: + if title and (not parser.charset): try: unicode(title, "ascii") except UnicodeDecodeError: @@ -86,7 +96,7 @@ def parse_html(filename, charset=None, log=None): if parser.charset: parser.charset = parser.charset.lower().replace("windows-", "cp") - if parser.charset and ( + if title and parser.charset and ( (parser.charset <> universal_charset) or ((not charset) or (charset <> parser.charset))): try: @@ -109,11 +119,12 @@ def parse_html(filename, charset=None, log=None): else: if log: log(" title : %s" % title) - final_title = recode_entities(converted_title, universal_charset) - parts = [s.strip() for s in final_title.replace('\r', '').split('\n')] - final_title = ' '.join([s for s in parts if s]) - if log and (final_title <> converted_title): log(" final title : %s" % final_title) - parser.title = final_title + if title: + final_title = recode_entities(converted_title, universal_charset) + parts = [s.strip() for s in final_title.replace('\r', '').split('\n')] + final_title = ' '.join([s for s in parts if s]) + if log and (final_title <> converted_title): log(" final title : %s" % final_title) + parser.title = final_title return parser