Ticket #308: changeset_2164.diff
| File changeset_2164.diff, 5.4 KB (added by rlrj60, 23 months ago) |
|---|
-
trac/TracRedirect/redirect/redirect.py
45 45 46 46 import re 47 47 from trac.core import Component, implements 48 48 from trac.wiki.api import WikiSystem, IWikiMacroProvider 49 49 from trac.web.api import IRequestFilter 50 50 from trac.web.chrome import ITemplateProvider, INavigationContributor, add_stylesheet 51 51 from trac.wiki.formatter import Formatter 52 52 from trac.util import Markup, arity 53 53 from StringIO import StringIO 54 54 55 HREF_RE = re.compile(r'^.*href="([^"]*)".*$') 55 HREF_RE = re.compile(r'^.*href="([^"]*)".*$') #"rlrj60 56 56 57 57 58 58 class RedirectFormatter(Formatter): 59 59 60 60 def __init__(self, *args, **kwargs): 61 61 Formatter.__init__(self, *args, **kwargs) 62 62 self.formatted_link = None 63 63 self.missing = False 64 64 65 65 def format(self, text): … … 71 71 72 72 def _shref_formatter(self, match, fullmatch): 73 73 if not self.formatted_link: 74 74 namespace = fullmatch.group('sns') 75 75 target = self._unquote(fullmatch.group('stgt')) 76 76 if namespace == 'wiki': 77 77 pagename = self.split_link(target)[0].split('@')[0] 78 78 if not WikiSystem(self.env).has_page(pagename): 79 79 self.missing = True 80 80 # compat: the signature changed between 0.11 and 0.11.1 81 if 6 == arity(self._make_link): 81 # FIXME:rlrj60:6/29/2011: port to 0.12.2 82 # if 6 == arity(self._make_link): 83 if True: 82 84 self.formatted_link = self._make_link(namespace, target, match, match, fullmatch) 83 85 else: 84 86 self.formatted_link = self._make_link(namespace, target, match, match) 85 87 return self.formatted_link 86 88 87 89 88 90 class TracRedirect(Component): 89 91 90 92 implements(IWikiMacroProvider, ITemplateProvider, INavigationContributor, IRequestFilter) 91 93 92 94 # IWikiMacroProvider methods 93 95 def expand_macro(self, formatter, name, args): 94 96 """ 95 97 Main routine of the wiki macro. 96 98 """ 97 99 context = formatter.context 98 100 req = context.req 99 101 100 102 preview = req.args.get('preview', '') 101 103 should_redirect = req.args.get('redirect', 'yes') == 'yes' and (not preview) 102 curpage = context.resource.id or 'WikiStart' 104 #FIXME:rlrj60:6/29/2011: covert unicode id to string 105 curpage = str(context.resource.id) or 'WikiStart' 103 106 104 107 out = StringIO() 105 108 link, missing = RedirectFormatter(self.env, context).format(args) 106 109 107 110 if link and not missing: 108 111 out.write('<div class="system-message">') 109 112 out.write('<strong>Redirect: </strong>') 110 113 out.write('This page redirects to %s ' % link) 111 114 out.write('</div>') 112 115 113 self.log.debug('Redirect target=>>%s<<' % link)114 116 self.log.debug('Redirect target=>>%s<<' % link) 117 115 118 if should_redirect: 116 link_target = HREF_RE.match(str(link)).group(1) 117 118 out.write('<form id="redirect" method="GET" action="%s">' % link_target) 119 out.write('<input type="hidden" name="redirectedfrom" value="%s">' % curpage) 120 out.write('</form>') 121 122 out.write('\n') 123 out.write('<script language="JavaScript">\n') 124 out.write('document.forms["redirect"].submit();\n') 125 out.write('</script>\n') 126 119 try: 120 link_target = HREF_RE.match(str(link)).group(1) 121 122 out.write('<form id="redirect" method="GET" action="%s">' % link_target) 123 out.write('<input type="hidden" name="redirectedfrom" value="%s">' % curpage) 124 out.write('</form>') 125 126 out.write('\n') 127 out.write('<script language="JavaScript">\n') 128 out.write('document.forms["redirect"].submit();\n') 129 out.write('</script>\n') 130 131 # FIXME:rlrj60:6/29/2011: handle missing ticket/page 132 except AttributeError: 133 # Internal link to a non-existing page 134 out.write('<div class="system-message">') 135 out.write('<strong>Redirection Error: </strong>') 136 out.write('Redirection target not found: %s' % link) 137 out.write('</div>') 138 127 139 elif link and missing: 128 140 # Internal link to a non-existing page 129 141 out.write('<div class="system-message">') 130 142 out.write('<strong>Redirection Error: </strong>') 131 143 out.write('Redirection target not found: %s' % link) 132 144 out.write('</div>') 133 145 134 146 else: 135 147 # No arguments given to the redirect macro 136 148 out.write('<div class="system-message">') 137 149 out.write('<strong>Redirection Error: </strong>') 138 150 out.write('No redirection target given') 139 151 out.write('</div>') 140 152 141 153 return out.getvalue() 142 154 143 155 def get_macros(self): 144 156 yield 'redirect' 145 157 146 158 def get_macro_description(self, name): 147 159 from inspect import getdoc, getmodule 148 160 return getdoc(getmodule(self)) 149 161 150 162
