X-Git-Url: https://git.phdru.name/?a=blobdiff_plain;f=Robots%2Fparse_html.py;h=a83585df318afb1602a916a1a17768485a099138;hb=8ce74f839238093d7278aa041ff55dbcb7abd3a0;hp=fd901a8857883a7bc1c5e02d764f76be7f411406;hpb=8ae42a33f8948c056f5acff3f9748a28700c55f0;p=bookmarks_db.git diff --git a/Robots/parse_html.py b/Robots/parse_html.py index fd901a8..a83585d 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,7 @@ universal_charset = "utf-8" DEFAULT_CHARSET = "cp1251" # Stupid default for Russian Cyrillic parsers = [] + try: import parse_html_beautifulsoup parse_html_beautifulsoup.DEFAULT_CHARSET = DEFAULT_CHARSET @@ -19,8 +20,26 @@ except ImportError: else: parsers.append(parse_html_beautifulsoup.parse_html) -from parse_html_htmlparser import parse_html -parsers.append(parse_html) +try: + from parse_html_lxml import parse_html +except ImportError: + pass +else: + parsers.append(parse_html) + +try: + from parse_html_htmlparser import parse_html +except ImportError: + pass +else: + parsers.append(parse_html) + +try: + import parse_html_html5 +except ImportError: + pass +else: + parsers.append(parse_html_html5.parse_html) import re @@ -34,7 +53,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) @@ -51,6 +72,9 @@ def recode_entities(title, charset): def parse_html(filename, charset=None, log=None): + if not parsers: + return None + if charset: try: codecs.lookup(charset) # In case of unknown charset... @@ -60,8 +84,9 @@ def parse_html(filename, charset=None, log=None): charsets = [universal_charset, DEFAULT_CHARSET] if charset: charset = charset.lower().replace("windows-", "cp") - if charset not in charsets: - charsets.insert(0, charset) + if charset in charsets: + charsets.remove(charset) + charsets.insert(0, charset) for p in parsers: parser = None @@ -76,6 +101,9 @@ def parse_html(filename, charset=None, log=None): else: if log: log("Parser %s.%s failed, trying next one." % (p.__module__, p.__name__)) + if not parser: + return None + converted_title = title = parser.title if title and (not parser.charset): try: @@ -115,6 +143,14 @@ def parse_html(filename, charset=None, log=None): 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 + + icon = parser.icon + if isinstance(icon, unicode): + try: + parser.icon = icon.encode('ascii') + except UnicodeEncodeError: + if parser.charset: + parser.icon = icon.encode(parser.charset) return parser