File: linkparse.py

package info (click to toggle)
linkchecker 4.9-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 3,308 kB
  • ctags: 3,736
  • sloc: python: 21,328; lex: 1,114; yacc: 781; ansic: 551; makefile: 244; sh: 100; sql: 19; awk: 4
file content (254 lines) | stat: -rw-r--r-- 8,798 bytes parent folder | download
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
# -*- coding: iso-8859-1 -*-
# Copyright (C) 2001-2008 Bastian Kleineidam
#
# 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., 675 Mass Ave, Cambridge, MA 02139, USA.
"""
Find link tags in HTML text.
"""

import re
import linkcheck.strformat
import linkcheck.linkname
import linkcheck.log
import linkcheck.url

MAX_NAMELEN = 256
unquote = linkcheck.strformat.unquote

# ripped mainly from HTML::Tagset.pm
LinkTags = {
    'a':        [u'href'],
    'applet':   [u'archive', u'src'],
    'area':     [u'href'],
    'bgsound':  [u'src'],
    'blockquote': [u'cite'],
    'body':     [u'background'],
    'del':      [u'cite'],
    'embed':    [u'pluginspage', u'src'],
    'form':     [u'action'],
    'frame':    [u'src', u'longdesc'],
    'head':     [u'profile'],
    'iframe':   [u'src', u'longdesc'],
    'ilayer':   [u'background'],
    'img':      [u'src', u'lowsrc', u'longdesc', u'usemap'],
    'input':    [u'src', u'usemap'],
    'ins':      [u'cite'],
    'isindex':  [u'action'],
    'layer':    [u'background', u'src'],
    'link':     [u'href'],
    'meta':     [u'content', u'href'],
    'object':   [u'classid', u'data', u'archive', u'usemap'],
    'q':        [u'cite'],
    'script':   [u'src'],
    'table':    [u'background'],
    'td':       [u'background'],
    'th':       [u'background'],
    'tr':       [u'background'],
    'xmp':      [u'href'],
    None:       [u'style'],
}

# matcher for <meta http-equiv=refresh> tags
refresh_re = re.compile(ur"(?i)^\d+;\s*url=(?P<url>.+)$")
_quoted_pat = ur"('[^']+'|\"[^\"]+\"|[^\)\s]+)"
css_url_re = re.compile(ur"url\(\s*(?P<url>%s)\s*\)" % _quoted_pat)
swf_url_re = re.compile("(?i)%s" % linkcheck.url.safe_url_pattern)
c_comment_re = re.compile(ur"/\*.*?\*/", re.DOTALL)

def strip_c_comments (text):
    """Remove C/CSS-style comments from text. Note that this method also
    deliberately removes comments inside of strings."""
    return c_comment_re.sub('', text)


class TagFinder (object):
    """
    Base class storing HTML parse messages in a list.
    TagFinder instances are to be used as HtmlParser handlers.
    """

    def __init__ (self):
        """
        Initialize local variables.
        """
        super(TagFinder, self).__init__()
        # parser object will be initialized when it is used as
        # a handler object
        self.parser = None

    def start_element (self, tag, attrs):
        """
        Does nothing, override in a subclass.
        """
        pass

    def start_end_element (self, tag, attrs):
        """
        Delegate a combined start/end element (eg. <br/>) to
        the start_element method. Ignore the end element part.
        """
        self.start_element(tag, attrs)


class MetaRobotsFinder (TagFinder):
    """
    Class for finding robots.txt meta values in HTML.
    """

    def __init__ (self):
        """
        Initialize flags.
        """
        super(MetaRobotsFinder, self).__init__()
        self.follow = True
        self.index = True
        assert None == linkcheck.log.debug(linkcheck.LOG_CHECK,
            "meta robots finder")

    def start_element (self, tag, attrs):
        """
        Search for meta robots.txt "nofollow" and "noindex" flags.
        """
        if tag == 'meta':
            if attrs.get('name') == 'robots':
                val = attrs.get_true('content', u'').lower().split(u',')
                self.follow = u'nofollow' not in val
                self.index = u'noindex' not in val


def is_meta_url (attr, attrs):
    """
    Check if the meta attributes contain a URL.
    """
    res = False
    if attr == "content":
        equiv = attrs.get_true('http-equiv', u'').lower()
        scheme = attrs.get_true('scheme', u'').lower()
        res = equiv in (u'refresh',) or scheme in (u'dcterms.uri',)
    if attr == "href":
        rel = attrs.get_true('rel', u'').lower()
        res = rel in (u'shortcut icon', u'icon')
    return res


class LinkFinder (TagFinder):
    """
    Find a list of links. After parsing, self.urls
    will be a list of parsed links entries with the format
    (url, lineno, column, name, codebase).
    """

    def __init__ (self, content, tags=None):
        """
        Store content in buffer and initialize URL list.
        """
        super(LinkFinder, self).__init__()
        self.content = content
        if tags is None:
            self.tags = LinkTags
        else:
            self.tags = tags
        self.urls = []
        self.base_ref = u''
        assert None == linkcheck.log.debug(linkcheck.LOG_CHECK, "link finder")

    def start_element (self, tag, attrs):
        """
        Search for links and store found URLs in a list.
        """
        assert None == linkcheck.log.debug(linkcheck.LOG_CHECK,
            "LinkFinder tag %s attrs %s", tag, attrs)
        assert None == linkcheck.log.debug(linkcheck.LOG_CHECK,
            "line %d col %d old line %d old col %d",
            self.parser.lineno(), self.parser.column(),
            self.parser.last_lineno(), self.parser.last_column())
        if tag == "base" and not self.base_ref:
            self.base_ref = attrs.get_true("href", u'')
        tagattrs = self.tags.get(tag, [])
        tagattrs.extend(self.tags.get(None, []))
        # eliminate duplicate tag attrs
        tagattrs = set(tagattrs)
        for attr in tagattrs:
            if attr not in attrs:
                continue
            if tag == "meta" and not is_meta_url(attr, attrs):
                continue
            # name of this link
            name = self.get_link_name(tag, attrs, attr)
            # possible codebase
            if tag in ('applet', 'object'):
                codebase = unquote(attrs.get_true('codebase', u''))
            else:
                codebase = u''
            # note: value can be None
            value = unquote(attrs.get(attr))
            # add link to url list
            self.add_link(tag, attr, value, name, codebase)
        assert None == linkcheck.log.debug(linkcheck.LOG_CHECK,
            "LinkFinder finished tag %s", tag)

    def get_link_name (self, tag, attrs, attr):
        """
        Parse attrs for link name. Return name of link.
        """
        if tag == 'a' and attr == 'href':
            name = unquote(attrs.get_true('title', u''))
            if not name:
                pos = self.parser.pos()
                # Look for name only up to MAX_NAMELEN characters from current
                # position, to limit the amount of data to encode.
                data = self.content[pos:pos+MAX_NAMELEN]
                data = data.decode(self.parser.encoding, "ignore")
                name = linkcheck.linkname.href_name(data)
        elif tag == 'img':
            name = unquote(attrs.get_true('alt', u''))
            if not name:
                name = unquote(attrs.get_true('title', u''))
        else:
            name = u""
        return name

    def add_link (self, tag, attr, url, name, base):
        """
        Add given url data to url list.
        """
        assert isinstance(tag, unicode), repr(tag)
        assert isinstance(attr, unicode), repr(attr)
        assert isinstance(name, unicode), repr(name)
        assert isinstance(base, unicode), repr(base)
        assert isinstance(url, unicode) or url is None, repr(url)
        urls = []
        # look for meta refresh
        if tag == u'meta' and url:
            mo = refresh_re.match(url)
            if mo:
                urls.append(mo.group("url"))
            elif attr != 'content':
                urls.append(url)
        elif attr == u'style' and url:
            for mo in css_url_re.finditer(url):
                u = mo.group("url")
                urls.append(unquote(u, matching=True))
        else:
            urls.append(url)
        if not urls:
            # no url found
            return
        for u in urls:
            assert isinstance(u, unicode) or u is None, repr(u)
            assert None == linkcheck.log.debug(linkcheck.LOG_CHECK,
              u"LinkParser add link %r %r %r %r %r", tag, attr, u, name, base)
            self.urls.append((u, self.parser.last_lineno(),
                              self.parser.last_column(), name, base))