From dd0bf0a5ef3065ff978c52df899cabefa07624cc Mon Sep 17 00:00:00 2001 From: Holger Schurig Date: Thu, 1 Jul 2010 14:09:48 +0200 Subject: [PATCH] link.py: removed a TODO item: finally correcly search for wrong links --- in/plugins/read_html.md | 2 +- in/plugins/read_markdown.md | 2 +- in/plugins/read_rst.md | 2 +- plugins/link.py | 28 +++++++++++++++++++++++----- 4 files changed, 26 insertions(+), 8 deletions(-) diff --git a/in/plugins/read_html.md b/in/plugins/read_html.md index 9ae4540..d2e6345 100644 --- a/in/plugins/read_html.md +++ b/in/plugins/read_html.md @@ -21,4 +21,4 @@ A sample "`test.html`" document looks like this: You'll find more about "`title:`", "`parent:`" and "`ctime:`" in the -[[page format|pageformat.html]] description. +[[page format|pageformat]] description. diff --git a/in/plugins/read_markdown.md b/in/plugins/read_markdown.md index c9682a9..cc4206b 100644 --- a/in/plugins/read_markdown.md +++ b/in/plugins/read_markdown.md @@ -27,7 +27,7 @@ A sample "`test.md`" document looks like this: Don't send me spam, *ever*! You'll find more about "`title:`", "`parent:`" and "`ctime:`" in the -[[page format|pageformat.html]] description. +[[page format|pageformat]] description. = Modifications = diff --git a/in/plugins/read_rst.md b/in/plugins/read_rst.md index 45543d8..d229335 100644 --- a/in/plugins/read_rst.md +++ b/in/plugins/read_rst.md @@ -27,4 +27,4 @@ standard Python module "`docutils`" to convert RST into HTML. A sample Don't send me spam, *ever*! You'll find more about "`title:`", "`parent:`" and "`ctime:`" in the -[[page format|pageformat.html]] description. \ No newline at end of file +[[page format|pageformat]] description. diff --git a/plugins/link.py b/plugins/link.py index f7ecb2a..8a3404a 100644 --- a/plugins/link.py +++ b/plugins/link.py @@ -1,6 +1,8 @@ # -*- coding: iso-8859-1 -*- from webber import * -import os, re +import os, re, urlparse + +_file_links = {} # To understand this beast, read /usr/share/doc/python2.5-doc/html/lib/module-re.html :-) @@ -19,6 +21,7 @@ reLink = re.compile(r''' \]\] # end of link ''', re.VERBOSE) + def do_link(m): """Used in re.sub() to substitute link with HTML""" text = m.group(1) or "" @@ -41,11 +44,13 @@ def do_link(m): break if not text: text = link - # validate link - # TODO: validating local files still not working - if not link.startswith("http:") and not link.endswith(".html") and not link.endswith(".png"): + # validate local files + components = urlparse.urlparse(link) + if components.scheme in ("", "file"): file = get_current_file() - warning("%s: unknown link to '%s'" % (file.rel_path, link) ) + fname = os.path.join(file.direc, components.path) + fname = os.path.normpath(fname) + _file_links[fname] = file.rel_path return '%s' % (link, anchor, text) @@ -65,6 +70,7 @@ def test_link(): else: print "No link:", s + def test_sub(): for s in ( 'Before [[!macro]] after', @@ -80,3 +86,15 @@ def test_sub(): @set_hook("linkify") def linkify(params): params.file.contents = reLink.sub(do_link, params.file.contents) + + +@set_hook("finish") +def check_links(params): + """Checks all links that are stored in _file_links to warn if the + file doesn't exist""" + + for s in _file_links: + #print "check:", s, cfg.out_dir + out_file = os.path.join(cfg.out_dir, s) + if not os.path.exists(out_file): + warning("%s: invalid link to '%s'" % (_file_links[s], s)) -- 2.39.2