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