]> git.phdru.name Git - bookmarks_db.git/blob - parse_html/bkmk_ph_html5.py
a4906288af13fe5da0cf3f24f39e30a010f7cbe7
[bookmarks_db.git] / parse_html / bkmk_ph_html5.py
1 """HTML Parser using html5
2
3 This file is a part of Bookmarks database and Internet robot.
4 """
5
6 __author__ = "Oleg Broytman <phd@phdru.name>"
7 __copyright__ = "Copyright (C) 2010-2012 PhiloSoft Design"
8 __license__ = "GNU GPL"
9
10 __all__ = ['parse_html']
11
12
13 from html5lib import HTMLParser as HTML5Parser
14 from .bkmk_ph_util import HTMLParser
15
16
17 def parse_html(filename, charset=None, log=None):
18     parser = HTML5Parser()
19     fp = open(filename)
20     parser._parse(fp, encoding=charset, parseMeta=bool(charset))
21     fp.close()
22     html_tree = parser.tree.getDocument()
23
24     for node in html_tree.childNodes:
25         if (node.name == 'html') and (node.type != 3): # Skip DocType element
26             html = node
27             break
28     else:
29         html = None
30
31     if not html:
32         return None
33
34     for node in html.childNodes:
35         if node.name == 'head':
36             head = node
37             break
38     else:
39         head = None
40
41     meta_charset = False
42     title = None
43     refresh = None
44     icon = None
45
46     if head:
47         for node in head.childNodes:
48             if node.name == 'title':
49                 if node.childNodes:
50                     title = node.childNodes[0].value
51                     break
52                 else:
53                     title = ''
54
55         for node in head.childNodes:
56             if node.name == 'meta' and \
57                     ('http-equiv' in node.attributes) and \
58                     (node.attributes['http-equiv'] == 'content-type'):
59                 meta_content = node.attributes['content']
60                 if meta_content:
61                     try:
62                         meta_charset = \
63                             meta_content.lower().split('charset=')[1].split(';')[0]
64                     except IndexError:
65                         meta_charset = False
66                     else:
67                         break
68
69         if not charset:
70             charset = parser.tokenizer.stream.charEncoding[0]
71
72         if title and (charset or meta_charset):
73             title = title.encode(charset or meta_charset)
74
75         for node in head.childNodes:
76             if node.name == 'meta' and \
77                     ('http-equiv' in node.attributes) and \
78                     (node.attributes['http-equiv'] == 'refresh'):
79                 refresh = node.attributes['content']
80                 break
81
82         for node in head.childNodes:
83             if node.name == 'link' and \
84                     ('rel' in node.attributes) and \
85                     (node.attributes['rel'] in ('icon', 'shortcut icon')):
86                 icon = node.attributes['href']
87                 break
88
89     else:
90         for node in html.childNodes:
91             if node.name == 'title':
92                 if node.childNodes:
93                     title = node.childNodes[0].value
94                     break
95                 else:
96                     title = ''
97
98     if (title is None) and (refresh is None) and (icon is None):
99         return None
100     return HTMLParser(charset, meta_charset, title, refresh, icon)