Ticket #2423: macros.diff
| File macros.diff, 3.1 kB (added by lei@mms-dresden.de, 5 months ago) |
|---|
-
macros.py
old new 1 1 # TracIncludeMacro macros 2 2 import urllib2 3 import re 3 4 from StringIO import StringIO 4 5 5 6 from trac.core import * … … 8 9 from trac.wiki.model import WikiPage 9 10 from trac.mimeview.api import Mimeview, get_mimetype, Context 10 11 from trac.perm import IPermissionRequestor 12 from trac.util.html import html 11 13 from genshi.core import escape 12 14 from genshi.input import HTMLParser, ParseError 13 15 from genshi.filters.html import HTMLSanitizer … … 26 28 'wiki': 'text/x-trac-wiki', 27 29 } 28 30 31 # Increase of the hierarchy level 32 hierarchy_offset = None 33 29 34 # IWikiMacroProvider methods 30 35 def render_macro(self, req, name, content): 31 36 args = [x.strip() for x in content.split(',')] 32 37 if len(args) == 1: 38 args.append(1) # hierarchy_offset: if not given, it defaults to 1 39 args.append(None) 40 elif len(args) == 2: 33 41 args.append(None) 34 elif len(args) != 2:42 elif len(args) != 3: 35 43 return system_message('Invalid arguments "%s"'%content) 36 44 37 45 # Pull out the arguments 38 source, dest_format = args 46 source, self.hierarchy_offset, dest_format = args 47 39 48 try: 40 49 source_format, source_obj = source.split(':', 1) 41 50 except ValueError: # If no : is present, assume its a wiki page … … 71 80 page = WikiPage(self.env, source_obj) 72 81 if not page.exists: 73 82 return system_message('Wiki page %s does not exist'%source_obj) 74 out = page.text 83 out = page.text 84 85 # modify headlines 86 out = re.sub('=+', self.add_equal_sign, out) 87 88 # modify hierarchy level parameters in any existing IncludeMacros 89 out = re.sub('\[\[Include\([\w/]+(,[\w-]+){0,2}\)\]\]', self.increase_incl_macro_param, out) 90 75 91 ctxt = Context.from_request(req, 'wiki', source_obj) 76 92 elif source_format == 'source': 77 93 if not req.perm.has_permission('FILE_VIEW'): … … 99 115 out = escape(out) 100 116 101 117 return out 118 119 # add '=' to increase hierarchy level 120 def add_equal_sign(self, matchObj): 121 return matchObj.group(0) + "="*int(self.hierarchy_offset) 122 123 # modify the hierarchy level parameter 124 def increase_incl_macro_param(self, matchObj): 125 # Lucas 2008-05-14 126 127 split = re.split('[\(\),]', matchObj.group(0)) 128 string = split[0] + "(" + split[1] 129 130 if re.match('[1-7]', split[2]): 131 string += ',' + str((int(split[2]) +1)) 132 i = 3 133 else: 134 string += ',2' 135 i = 2 136 137 while split[i] != ']]': 138 string += ',' + split[i] 139 i += 1 140 else: 141 string += ')' + split[i] 142 143 return string 144 102 145 103 146 # IPermissionRequestor methods 104 147 def get_permission_actions(self):
