From 5982f089383299080c01a345ba251f5ea6af6bd9 Mon Sep 17 00:00:00 2001 From: Filip Engberg Date: Fri, 12 Dec 2014 09:22:02 +0100 Subject: [PATCH 1/2] Added plugin for finding links in content using Regular expression --- config/linkcheckerrc | 3 ++ linkcheck/plugins/regexlinkcheck.py | 71 +++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 linkcheck/plugins/regexlinkcheck.py diff --git a/config/linkcheckerrc b/config/linkcheckerrc index 2dc4e3606..e9bc2911c 100644 --- a/config/linkcheckerrc +++ b/config/linkcheckerrc @@ -269,3 +269,6 @@ # Parse and check links in Word files #[WordParser] +# Search for and check links in page contents using regular expression +[RegexLinkCheck] +linkregex=:"([^"]*\.html)" diff --git a/linkcheck/plugins/regexlinkcheck.py b/linkcheck/plugins/regexlinkcheck.py new file mode 100644 index 000000000..3bce023f0 --- /dev/null +++ b/linkcheck/plugins/regexlinkcheck.py @@ -0,0 +1,71 @@ +# -*- coding: iso-8859-1 -*- +# Copyright (C) 2000-2014 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., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +""" +Check page content with regular expression for links. +""" +import re +from . import _ContentPlugin +from .. import log, LOG_PLUGIN + + +class RegexLinkCheck(_ContentPlugin): + """Define a regular expression which matches any content. + It will treat matched content as a url and check it. + This applies only to valid pages, so we can get their content. + + Use this to check for pages that contain urls that cannot + be found using the built-in tag finding, + for example if there are urls defined in a json object. + + Note that multiple values can be combined in the regular expression".""" + + def __init__(self, config): + """Set link regex from config.""" + super(RegexLinkCheck, self).__init__(config) + self.linkregex = None + pattern = config["linkregex"] + if pattern: + try: + self.linkregex = re.compile(pattern) + except re.error as msg: + log.warn(LOG_PLUGIN, "Invalid regex pattern %r: %s" % (pattern, msg)) + + def applies_to(self, url_data): + """Check for linkregex, extern flag and parseability.""" + return self.linkregex and not url_data.extern[0] and url_data.is_parseable() + + def check(self, url_data): + """Check content.""" + log.debug(LOG_PLUGIN, "checking content for link regex") + content = url_data.get_content() + # add urls for found matches, up to the maximum allowed number + match = self.linkregex.search(content) + if match: + url_data.add_url(match.group(1)) + + @classmethod + def read_config(cls, configparser): + """Read configuration file options.""" + config = dict() + section = cls.__name__ + option = "linkregex" + if configparser.has_option(section, option): + value = configparser.get(section, option) + else: + value = None + config[option] = value + return config From 847b3a3a807940809fa1ca3566da5891121a2332 Mon Sep 17 00:00:00 2001 From: Filip Engberg Date: Mon, 15 Dec 2014 10:24:56 +0100 Subject: [PATCH 2/2] Comment out plugin by default --- config/linkcheckerrc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/config/linkcheckerrc b/config/linkcheckerrc index e9bc2911c..3bf72b2e6 100644 --- a/config/linkcheckerrc +++ b/config/linkcheckerrc @@ -270,5 +270,5 @@ #[WordParser] # Search for and check links in page contents using regular expression -[RegexLinkCheck] -linkregex=:"([^"]*\.html)" +#[RegexLinkCheck] +#linkregex=:"([^"]*\.html)"