"""
__author__ = "Oleg Broytman <phd@phdru.name>"
-__copyright__ = "Copyright (C) 1997-2017 PhiloSoft Design"
+__copyright__ = "Copyright (C) 1997-2023 PhiloSoft Design"
__license__ = "GNU GPL"
__all__ = ['parse_html', 'parse_filename', 'universal_charset']
import codecs
universal_charset = "utf-8"
-DEFAULT_CHARSET = "cp1251" # Stupid default for Russian Cyrillic
+DEFAULT_CHARSET = "cp1251" # Stupid default for Russian Cyrillic
parsers = []
parsers.append(bkmk_ph_htmlparser.parse_html)
# ElementTidy often segfaults
-#try:
-# from . import bkmk_ph_etreetidy
-#except ImportError:
-# pass
-#else:
-# parsers.append(bkmk_ph_etreetidy.parse_html)
+# try:
+# from . import bkmk_ph_etreetidy
+# except ImportError:
+# pass
+# else:
+# parsers.append(bkmk_ph_etreetidy.parse_html)
import re
from htmlentitydefs import name2codepoint
entity_re = re.compile("(&\w+;)")
num_entity_re = re.compile("(&#[0-9]+;)")
+
def recode_entities(title, charset):
output = []
for part in entity_re.split(title):
try:
part = unichr(int(part[2:-1])).encode(charset)
except UnicodeEncodeError:
- pass # Leave the entity as is
+ pass # Leave the entity as is
output.append(part)
return ''.join(output)
import os
BKMK_DEBUG_HTML_PARSERS = os.environ.get("BKMK_DEBUG_HTML_PARSERS")
+
def parse_html(html_text, charset=None, log=None):
if not parsers:
return None
if charset:
try:
- codecs.lookup(charset) # In case of unknown charset...
+ codecs.lookup(charset) # In case of unknown charset...
except (ValueError, LookupError):
charset = None # ...try charset from HTML
if log: log(" META charset : %s" % parser.charset)
elif (not charset) or (charset != parser.charset):
if log: log(" guessed charset: %s" % parser.charset)
- #if log: log(" current charset: %s" % universal_charset)
+ # if log: log(" current charset: %s" % universal_charset)
if log: log(" title : %s" % title)
if parser.charset != universal_charset:
try:
parser.icon = icon.encode(parser.charset)
return parser
+
def parse_filename(filename, charset=None, log=None):
fp = open(filename, 'r')
try: