Arthur de Jong

Open Source / Free Software developer

summaryrefslogtreecommitdiffstats
path: root/webcheck/parsers/html/htmlparser.py
blob: 55fe552044e721865857a0a0563e7dd49c9ab789 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306

# html.py - parser functions for html content
#
# Copyright (C) 2005, 2006, 2007, 2009, 2011 Arthur de Jong
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
#
# The files produced as output from the software do not automatically fall
# under the copyright of the software, unless explicitly stated otherwise.

"""Parser functions for processing HTML content. This module uses
the legacy HTMLParser module. It will only be used if BeatifulSoup
is not available and can be considered depricated. This parser
will only handle properly formatted HTML."""

import HTMLParser
import logging
import re
import urlparse

from webcheck.myurllib import normalizeurl
from webcheck.parsers.html import htmlunescape
import webcheck.parsers.css


logger = logging.getLogger(__name__)


# pattern for matching numeric html entities
_charentitypattern = re.compile('&#([0-9]{1,3});')

# pattern for matching spaces
_spacepattern = re.compile(' ')

# pattern for matching charset declaration for http-equiv tag
_charsetpattern = re.compile('charset=([^ ]*)', re.I)

# pattern for matching the encoding part of an xml declaration
_encodingpattern = re.compile('^xml .*encoding="([^"]*)"', re.I)


class _MyHTMLParser(HTMLParser.HTMLParser):
    """A simple subclass of HTMLParser.HTMLParser continuing after errors
    and gathering some information from the parsed content."""

    def __init__(self, link):
        """Inialize the menbers in which we collect data from parsing the
        document."""
        self.link = link
        self.collect = None
        self.base = None
        self.title = None
        self.author = None
        self.embedded = []
        self.children = []
        self.anchors = []
        self.errmsg = None
        self.errcount = 0
        HTMLParser.HTMLParser.__init__(self)

    def _location(self):
        """Return the current parser location as a string."""
        (lineno, offset) = self.getpos()
        if lineno is not None:
            msg = 'at line %d' % lineno
        else:
            msg = 'at unknown line'
        if offset is not None:
            msg += ', column %d' % (offset + 1)
        return msg

    def _cleanurl(self, url, what='link'):
        """Do some translations of url."""
        # check for spaces in urls
        # (characters are escaped in normalizeurl())
        if _spacepattern.search(url):
            self.link.add_pageproblem(
              what + ' contains unescaped spaces: ' + url + ', ' + self._location())
        # replace &#nnn; entity refs with proper characters
        url = _charentitypattern.sub(lambda x: chr(int(x.group(1))), url)
        return normalizeurl(url)

    def error(self, message):
        """Override superclass' error() method to ignore errors."""
        # construct error message
        message += ', ' + self._location()
        # store error message
        logger.debug('problem parsing html: %s', message)
        if self.errmsg is None:
            self.errmsg = message
        # increment error count
        self.errcount += 1
        if self.errcount > 10:
            raise HTMLParser.HTMLParseError(message, self.getpos())

    def check_for_whole_start_tag(self, i):
        """Override to catch assertion exception."""
        try:
            return HTMLParser.HTMLParser.check_for_whole_start_tag(self, i)
        except AssertionError:
            logger.exception('caught assertion error')
            return None

    def handle_starttag(self, tag, attrs):
        """Handle start tags in html."""
        # turn attrs into hash
        attrs = dict(attrs)
        # <title>TITLE</title>
        if tag == 'title':
            self.collect = ''
        # <base href="URL">
        elif tag == 'base' and 'href' in attrs:
            self.base = self._cleanurl(attrs['href'])
        # <link rel="type" href="URL">
        elif tag == 'link' and 'rel' in attrs and 'href' in attrs:
            if attrs['rel'].lower() in ('stylesheet', 'alternate stylesheet', 'icon', 'shortcut icon'):
                self.embedded.append(self._cleanurl(attrs['href']))
        # <meta name="author" content="AUTHOR">
        elif tag == 'meta' and 'name' in attrs and 'content' in attrs and attrs['name'].lower() == 'author':
            if self.author is None:
                self.author = attrs['content']
        # <meta http-equiv="refresh" content="0;url=URL">
        elif tag == 'meta' and 'http-equiv' in attrs and 'content' in attrs and attrs['http-equiv'].lower() == 'refresh':
            pass  # TODO: implement
        # <meta http-equiv="content-type" content="text/html; charset=utf-8" />
        elif tag == 'meta' and 'http-equiv' in attrs and 'content' in attrs and attrs['http-equiv'].lower() == 'content-type':
            try:
                self.link.set_encoding(_charsetpattern.search(attrs['content']).group(1))
            except AttributeError:
                # ignore cases where encoding is not set in header
                pass
        # <img src="url">
        elif tag == 'img' and 'src' in attrs:
            self.embedded.append(self._cleanurl(attrs['src']))
        # <a href="url" name="anchor" id="anchor">
        elif tag == 'a':
            # <a href="url">
            if 'href' in attrs:
                self.children.append(self._cleanurl(attrs['href']))
            # <a name="anchor">
            a_name = None
            if 'name' in attrs:
                a_name = self._cleanurl(attrs['name'], 'anchor')
            # <a id="anchor">
            a_id = None
            if 'id' in attrs:
                a_id = self._cleanurl(attrs['id'], 'anchor')
            # if both id and name are used they should be the same
            if a_id and a_name and a_id != a_name:
                # add problem
                self.link.add_pageproblem(
                  'anchors defined in name and id attributes do not match %(location)s'
                  % {'location': self._location()})
            elif a_id == a_name:
                # ignore id if it's the same as name
                a_id = None
            # <a name="anchor">
            if a_name:
                if a_name in self.anchors:
                    self.link.add_pageproblem(
                      'anchor "%(anchor)s" defined again %(location)s'
                      % {'anchor':   a_name,
                         'location': self._location()})
                else:
                    self.anchors.append(a_name)
            # <a id="anchor">
            if a_id:
                if a_id in self.anchors:
                    self.link.add_pageproblem(
                      'anchor "%(anchor)s" defined again %(location)s'
                      % {'anchor':   a_id,
                         'location': self._location()})
                else:
                    self.anchors.append(a_id)
        # <frameset><frame src="url"...>...</frameset>
        elif tag == 'frame' and 'src' in attrs:
            self.embedded.append(self._cleanurl(attrs['src']))
        # <map><area href="url"...>...</map>
        elif tag == 'area' and 'href' in attrs:
            self.children.append(self._cleanurl(attrs['href']))
        # <applet archive="URL"...>
        elif tag == 'applet' and 'archive' in attrs:
            self.embedded.append(self._cleanurl(attrs['archive']))
        # <applet code="URL"...>
        elif tag == 'applet' and 'code' in attrs:
            self.embedded.append(self._cleanurl(attrs['code']))
        # <embed src="url"...>
        elif tag == 'embed' and 'src' in attrs:
            self.embedded.append(self._cleanurl(attrs['src']))
        # <embed><param name="movie" value="url"></embed>
        elif tag == 'param' and 'name' in attrs and 'value' in attrs:
            if attrs['name'].lower() == 'movie':
                self.embedded.append(self._cleanurl(attrs['value']))
        # <style>content</style>
        elif tag == 'style':
            self.collect = ''
        # <script src="url">
        elif tag == 'script' and 'src' in attrs:
            self.embedded.append(self._cleanurl(attrs['src']))
        # <body|table|td background="url">
        elif tag in ('body', 'table', 'td') and 'background' in attrs:
            self.embedded.append(self._cleanurl(attrs['background']))
        # pick up any tags with a style attribute
        if 'style' in attrs:
            # delegate handling of inline css to css module
            webcheck.parsers.css.parse(attrs['style'], self.link, self.base)

    def handle_endtag(self, tag):
        """Handle end tags in html."""
        if tag == 'title' and self.title is None:
            self.title = self.collect
            self.collect = None
        elif tag == 'style' and self.collect is not None:
            # delegate handling of inline css to css module
            webcheck.parsers.css.parse(self.collect, self.link, self.base)

    def handle_data(self, data):
        """Collect data if we were collecting data."""
        if self.collect is not None:
            self.collect += data

    def handle_charref(self, name):
        """Handle character references (e.g. &#65;) by passing the data to
        handle_data()."""
        self.handle_data('&#' + name + ';')
        # TODO: do not pass ; if plain text does not contain it?

    def handle_entityref(self, name):
        """Handle entity references (e.g. &eacute;) by passing the data to
        handle_data()."""
        self.handle_data('&' + name + ';')
        # TODO: do not pass ; if plain text does not contain it?

    def handle_pi(self, data):
        """Handle xml declaration."""
        # find character encoding from declaration
        try:
            self.link.set_encoding(_encodingpattern.search(data).group(1))
        except AttributeError:
            pass


def _maketxt(txt, encoding):
    """Return an unicode text of the specified string do correct character
    conversions and replacing html entities with normal characters."""
    # try to decode with the given encoding
    if encoding:
        try:
            return htmlunescape(txt.decode(encoding))
        except (LookupError, TypeError, ValueError), e:
            logger.warn('page has unknown encoding: %s', str(encoding))
    # fall back to locale's encoding
    return htmlunescape(txt.decode('ascii', 'replace'))

def parse(content, link):
    """Parse the specified content and extract an url list, a list of images a
    title and an author. The content is assumed to contain HMTL."""
    # create parser and feed it the content
    parser = _MyHTMLParser(link)
    try:
        parser.feed(content.decode('ascii', 'ignore').encode())
        parser.close()
    except Exception, e:
        # ignore (but log) all errors
        logger.exception('caught exception: %s', str(e))
    # check for parser errors
    if parser.errmsg is not None:
        logger.debug('problem parsing html: %s', parser.errmsg)
        link.add_pageproblem('problem parsing html: %s' % parser.errmsg)
    # dump encoding
    logger.debug('html encoding: %s', str(link.encoding))
    # flag that the link contains a valid page
    link.is_page = True
    # save the title
    if parser.title is not None:
        link.title = _maketxt(parser.title, link.encoding).strip()
    # save the author
    if parser.author is not None:
        link.author = _maketxt(parser.author, link.encoding).strip()
    # figure out the base of the document (for building the other urls)
    base = link.url
    if parser.base is not None:
        base = parser.base
    # list embedded and children
    for embed in parser.embedded:
        if embed:
            link.add_embed(urlparse.urljoin(base, embed))
    for child in parser.children:
        if child:
            link.add_child(urlparse.urljoin(base, child))
    # list anchors
    for anchor in parser.anchors:
        if anchor:
            link.add_anchor(anchor)