]> oss.titaniummirror.com Git - webber.git/blobdiff - plugins/link.py
used Emacs' "tabify" command on some python file.
[webber.git] / plugins / link.py
index ae86ca0a0b94ef649ebd7be0df6ddfdc5c530807..6b57e256b560f5fe5a220b94dc8f892e7451d345 100644 (file)
@@ -5,18 +5,18 @@ import os, re
 # 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):
@@ -26,9 +26,10 @@ def do_link(m):
        link = m.group(2).replace("\n", " ")
        anchor = m.group(3) or ""
        if link.find(".") == -1:
-               #link = link.tolower()
                for f in files:
                        file = files[f]
+                       if not file.has_key("linktitle"):
+                               continue
                        if file.title == link or \
                           file.linktitle == link or \
                           os.path.splitext(os.path.basename(file.path))[0] == link:
@@ -38,6 +39,8 @@ def do_link(m):
                                link = get_link_from(get_current_file().linktitle, file.linktitle)
                                #print "LINK: '%s' '%s'" % (text, link)
                                break
+       if not text:
+               text = link
        # TODO: validate link
        return '<a href="%s%s">%s</a>' % (link, anchor, text)
 
@@ -52,9 +55,9 @@ 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
 
@@ -69,12 +72,7 @@ def test_sub():
                res = reLink.sub(do_link, s)
                print "", res
 
-#test_link()
-#test_sub()
-
-
-
 
 @set_hook("linkify")
 def linkify(params):
-       return reLink.sub(do_link, params.file.contents)
+       params.file.contents = reLink.sub(do_link, params.file.contents)