Changeset 3040 for trac


Ignore:
Timestamp:
10/28/05 01:19:43 (8 years ago)
Author:
moschny
Message:

Porting the redirect macro to trac 0.9 - first version.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trac/trunk/wiki-macros/redirect.py

    r3026 r3040  
    11# vim: expandtab tabstop=4 
    2 from StringIO import StringIO 
    32import re 
    43import string 
    5 from trac import WikiFormatter 
    6 from trac.WikiFormatter import CommonFormatter 
     4from StringIO import StringIO 
     5from trac.wiki.formatter import Formatter 
     6from trac.wiki.api import WikiSystem 
    77 
    8 NL = "\n" 
     8class MyFormatter(Formatter): 
     9 
     10    def format(self, text): 
     11        self.formatted_link = None 
     12        self.missing = False 
     13        class NullOut(object): 
     14            def write(self, data): pass 
     15        Formatter.format(self, text, NullOut()) 
     16        return self.formatted_link, self.missing 
     17 
     18    def _shref_formatter(self, match, fullmatch): 
     19        if not self.formatted_link: 
     20            namespace = fullmatch.group('sns') 
     21            target    = fullmatch.group('stgt') 
     22            if target[0] in "'\"": 
     23                target = target[1:-1] 
     24            if namespace == 'wiki': 
     25                wiki = WikiSystem(self.env) 
     26                if not wiki.has_page(target): 
     27                    self.missing = True 
     28            self.formatted_link = self._make_link(namespace, target, match, match) 
     29        return 
    930 
    1031def execute(hdf, args, env): 
    11     curpage = hdf.getValue('args.page', "") 
     32    curpage = hdf.getValue('wiki.page_name', "") 
    1233    if not curpage: 
    1334        curpage = 'WikiStart' 
     
    1536    preview = hdf.getValue('args.preview', "") 
    1637    shouldRedirect = (hdf.getValue('args.redirect', "yes") == "yes") and (not preview) 
     38 
    1739    db = env.get_db_cnx() 
    1840    out = StringIO() 
    19     fmt = CommonFormatter(hdf, env, db) 
    20  
    21     if args: 
    22         link, _, missing, _ = fmt._expand_module_link(args) 
    23  
    24     if not link and not missing: 
    25         # This is an external non-trac link 
    26         link = args 
     41    link, missing = MyFormatter(env, True, db).format(args) 
    2742 
    2843    if link and not missing: 
    2944        out.write('<div class="system-message">') 
    3045        out.write('<strong>Redirect: </strong>') 
    31         out.write("This page is an alias for the real page located at ") 
    32         out.write('<a href="') 
    33         out.write(link) 
    34         out.write('">') 
    35         out.write(args) 
    36         out.write('</a>') 
     46        out.write('This page redirects to %s ' % link) 
    3747        out.write('</div>') 
    3848 
    3949        if shouldRedirect: 
    40             out.write('<form id="redirect" method="POST" action="' + link + '">') 
    41             out.write('<input type="hidden" name="redirectedfrom" value="' + curpage + '">') 
     50            link_target = re.sub(r'^.*href="([^"]*)".*$', r'\1', link) 
     51 
     52            out.write('<form id="redirect" method="POST" action="%s">' % link_target) 
     53            out.write('<input type="hidden" name="redirectedfrom" value="%s">' % curpage) 
    4254            out.write('</form>') 
    4355 
    44             out.write(NL) 
    45             out.write('<script language="JavaScript">' + NL) 
    46             out.write('document.forms["redirect"].submit();' + NL) 
    47             out.write('</script>' + NL) 
     56            out.write('\n') 
     57            out.write('<script language="JavaScript">\n') 
     58            out.write('document.forms["redirect"].submit();\n') 
     59            out.write('</script>\n') 
    4860 
    4961    elif link and missing: 
     
    5163        out.write('<div class="system-message">') 
    5264        out.write('<strong>Redirection Error: </strong>') 
    53         out.write('Redirection target not found: ') 
    54         out.write('<a href="') 
    55         out.write(link) 
    56         out.write('">') 
    57         out.write(args) 
    58         out.write('</a>') 
     65        out.write('Redirection target not found: %s' % link) 
    5966        out.write('</div>') 
    6067 
Note: See TracChangeset for help on using the changeset viewer.