]> git.phdru.name Git - bookmarks_db.git/blob - Robots/parse_html.py
3250a0de5980c43a00c6cce8ff5e91a9e5b5471c
[bookmarks_db.git] / Robots / parse_html.py
1 #! /usr/bin/env python
2 """
3    HTML Parsers wrapper
4
5    Written by BroytMann. Copyright (C) 1997-2007 PhiloSoft Design
6 """
7
8 import codecs
9
10 from m_lib.defenc import default_encoding
11 current_charset = default_encoding.replace("windows-", "cp")
12 DEFAULT_CHARSET = "cp1251" # Stupid default for Russian Cyrillic
13
14 try:
15    from parse_html_beautifulsoup import parse_html as _parse_html
16 except ImportError:
17    from parse_html_htmlparser import parse_html as _parse_html
18
19
20 import re
21 entity_re = re.compile("(&#[0-9]+;)")
22
23 def recode_entities(title, charset):
24    output = []
25    for part in entity_re.split(title):
26       if entity_re.match(part):
27          part = unichr(int(part[2:-1])).encode(charset, "replace")
28       output.append(part)
29
30    return ''.join(output)
31
32
33 def parse_html(filename, charset=None, log=None):
34    if charset:
35       try:
36          codecs.lookup(charset) # In case of unknown charset...
37       except (ValueError, LookupError):
38          charset = None         # ...try charset from HTML
39
40    parser = _parse_html(filename, charset)
41    title = parser.title
42
43    if not parser.charset:
44       try:
45          unicode(title, "ascii")
46       except UnicodeDecodeError:
47          parser.charset = DEFAULT_CHARSET
48
49    if parser.charset:
50       parser.charset = parser.charset.replace("windows-", "cp").lower()
51
52    if parser.charset and (parser.charset <> current_charset):
53       try:
54          if parser.meta_charset:
55             if log: log("   META charset   : %s" % parser.charset)
56          else:
57             if log: log("   HTTP charset   : %s" % parser.charset)
58          if log: log("   current charset: %s" % current_charset)
59          if log: log("   title          : %s" % title)
60          save_title = title
61          try:
62             title = unicode(title, parser.charset).encode(current_charset)
63          except UnicodeError:
64             if parser.meta_charset and parser.charset.endswith("1252") and \
65                   not DEFAULT_CHARSET.endswith("1252") and (DEFAULT_CHARSET <> current_charset):
66                parser.charset = DEFAULT_CHARSET
67                if log: log("   incorrect conversion from cp1252, converting from %s" % DEFAULT_CHARSET)
68                title = unicode(save_title, DEFAULT_CHARSET, "replace").encode(current_charset, "replace")
69             else:
70                title = unicode(title, parser.charset, "replace").encode(current_charset, "replace")
71          if log: log("   converted title: %s" % title)
72       except LookupError:
73          if log: log("   unknown charset: `%s' or `%s'" % (parser.charset, current_charset))
74
75    parser.title = recode_entities(title, current_charset)
76    return parser
77
78
79 if __name__ == '__main__':
80    import sys
81    parser = parse_html(sys.argv[1], current_charset)
82    print parser.charset
83    print parser.title
84    print parser.refresh
85    print parser.icon