]> oss.titaniummirror.com Git - webber.git/blobdiff - plugins/link.py
Merge branch 'upstream'
[webber.git] / plugins / link.py
index d8c42149434ed60f5c1bb71c026a01307ce35755..8a3404a4befc43d4751f84160db07a840b66cd8b 100644 (file)
@@ -1,24 +1,27 @@
 # -*- 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 :-)
 
 reLink = re.compile(r'''
-       \[\[                # Begin of link
-       (?=[^!])            # Don't fire for macros
+       \[\[                            # Begin of link
+       (?=[^!])                        # Don't fire for macros
        (?:
-               ([^\]\|]+)      # 1: link text
-               \|              # followed by '|'
-       )?                  # optional
-       ([^\n\r\]#]+)       # 2: page to link to
+               ([^\]\|]+)              # 1: link text
+               \|                              # followed by '|'
+       )?                                      # optional
+       ([^\n\r\]#]+)           # 2: page to link to
        (
-               \#              # '#', beginning of anchor
-               [^\s\]]+        # 3: anchor text, doesn't contain spaces or ']'
-       )?                  # optional
-       \]\]                # end of link
+               \#                              # '#', beginning of anchor
+               [^\s\]]+                # 3: anchor text, doesn't contain spaces or ']'
+       )?                                      # optional
+       \]\]                            # end of link
        ''', re.VERBOSE)
 
+
 def do_link(m):
        """Used in re.sub() to substitute link with HTML"""
        text = m.group(1) or ""
@@ -41,7 +44,13 @@ def do_link(m):
                                break
        if not text:
                text = link
-       # TODO: validate link
+       # validate local files
+       components = urlparse.urlparse(link)
+       if components.scheme in ("", "file"):
+               file = get_current_file()
+               fname = os.path.join(file.direc, components.path)
+               fname = os.path.normpath(fname)
+               _file_links[fname] = file.rel_path
        return '<a href="%s%s">%s</a>' % (link, anchor, text)
 
 
@@ -55,12 +64,13 @@ def test_link():
                m = reLink.search(s)
                if m:
                        print "link:", s
-                       print "  name:", m.group(1)
-                       print "  link:", m.group(2)
-                       print "  anchor:", m.group(3)
+                       print "  name:", m.group(1)
+                       print "  link:", m.group(2)
+                       print "  anchor:", m.group(3)
                else:
                        print "No link:", s
 
+
 def test_sub():
        for s in (
                        'Before [[!macro]] after',
@@ -72,12 +82,19 @@ def test_sub():
                res = reLink.sub(do_link, s)
                print "", res
 
-#test_link()
-#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"""
 
-@set_hook("linkify")
-def linkify(params):
-       return reLink.sub(do_link, params.file.contents)
+       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))