source: trac/trunk/wiki-macros/redirect.py @ 3059

Revision 3050, 4.5 KB checked in by moschny, 8 years ago (diff)

Even more cosmetics.

Line 
1"""
2= Redirect: Redirection for Trac =
3
4== Purpose and Usage ==
5
6The redirect wiki macro provides redirect functionality for Trac with
7backlink support. It takes a qualified [TracLinks TracLink] (type:id)
8as argument, e.g. `wiki:SandBox`, `source:README` or
9`ticket:123`. Users with a JavaScript-enabled browser will get
10redirected to the page specified by this shortlink.
11
12== Installation ==
13
14Put the file `redirect.py` either in the global wiki-macros directory
15of your Trac installation (typically `/usr/share/trac/wiki-macros`) or
16into the `wiki-macros` subdirectory of your project.
17
18Then add these lines to the `templates/site_header.cs` file within
19your project:
20{{{
21<?cs if:args.redirectedfrom ?>
22<?cs set:chrome.nav.metanav.redirectedfrom =
23  '<div class="redirect">Redirected from <a href="' +
24  trac.href.wiki + '/' + args.redirectedfrom + '?redirect=no">' +
25  args.redirectedfrom + '</a>' +
26  ' (<a href="' + base_url + HTTP.PathInfo + '">hide</a>)</div>' ?>
27<?cs /if ?>
28}}}
29
30Additionally, you should add this line
31{{{
32.redirect { color: #cccccc; }
33}}}
34to the `templates/site_css.cs` of your project.
35
36== Author and license ==
37
38Copyright 2005 Bernhard Haumacher (haui at haumacher.de)
39and Thomas Moschny (moschny at ipd.uni-karlsruhe.de)
40
41{{{
42This program is free software; you can redistribute it and/or modify
43it under the terms of the GNU General Public License as published by
44the Free Software Foundation; either version 2 of the License, or
45(at your option) any later version.
46
47This program is distributed in the hope that it will be useful,
48but WITHOUT ANY WARRANTY; without even the implied warranty of
49MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
50GNU General Public License for more details.
51
52You should have received a copy of the GNU General Public License
53along with this program; if not, write to the Free Software
54Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
55}}}
56
57== Additional information ==
58
59Please visit: http://svn.ipd.uka.de/trac/javaparty/wiki/TracRedirect.
60"""
61
62import re
63import string
64from StringIO import StringIO
65from trac.wiki.formatter import Formatter
66from trac.wiki.api import WikiSystem
67
68class MyFormatter(Formatter):
69
70    def format(self, text):
71        self.formatted_link = None
72        self.missing = False
73        class NullOut(object):
74            def write(self, data): pass
75        Formatter.format(self, text, NullOut())
76        return self.formatted_link, self.missing
77
78    def _shref_formatter(self, match, fullmatch):
79        if not self.formatted_link:
80            namespace = fullmatch.group('sns')
81            target    = fullmatch.group('stgt')
82            if target[0] in "'\"":
83                target = target[1:-1]
84            if namespace == 'wiki':
85                wiki = WikiSystem(self.env)
86                if not wiki.has_page(target):
87                    self.missing = True
88            self.formatted_link = self._make_link(namespace, target, match, match)
89        return
90
91def execute(hdf, args, env):
92    curpage = hdf.getValue('wiki.page_name', "")
93    if not curpage:
94        curpage = 'WikiStart'
95
96    preview = hdf.getValue('args.preview', "")
97    shouldRedirect = (hdf.getValue('args.redirect', "yes") == "yes") and (not preview)
98
99    db = env.get_db_cnx()
100    out = StringIO()
101    link, missing = MyFormatter(env, True, db).format(args)
102
103    if link and not missing:
104        out.write('<div class="system-message">')
105        out.write('<strong>Redirect: </strong>')
106        out.write('This page redirects to %s ' % link)
107        out.write('</div>')
108
109        if shouldRedirect:
110            link_target = re.sub(r'^.*href="([^"]*)".*$', r'\1', link)
111
112            out.write('<form id="redirect" method="GET" action="%s">' % link_target)
113            out.write('<input type="hidden" name="redirectedfrom" value="%s">' % curpage)
114            out.write('</form>')
115
116            out.write('\n')
117            out.write('<script language="JavaScript">\n')
118            out.write('document.forms["redirect"].submit();\n')
119            out.write('</script>\n')
120
121    elif link and missing:
122        # Internal link to a non-existing page
123        out.write('<div class="system-message">')
124        out.write('<strong>Redirection Error: </strong>')
125        out.write('Redirection target not found: %s' % link)
126        out.write('</div>')
127
128    else:
129        # No arguments given to the redirect macro
130        out.write('<div class="system-message">')
131        out.write('<strong>Redirection Error: </strong>')
132        out.write('Missing redirection target.')
133        out.write('</div>')
134
135    return out.getvalue()
136
Note: See TracBrowser for help on using the repository browser.