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