Ticket #2423: macros.diff

File macros.diff, 3.1 kB (added by lei@mms-dresden.de, 5 months ago)

Headlines hierachy PATCH - I accidently added the macros.py file before

  • macros.py

    old new  
    11# TracIncludeMacro macros 
    22import urllib2 
     3import re 
    34from StringIO import StringIO 
    45 
    56from trac.core import * 
     
    89from trac.wiki.model import WikiPage 
    910from trac.mimeview.api import Mimeview, get_mimetype, Context 
    1011from trac.perm import IPermissionRequestor 
     12from trac.util.html import html 
    1113from genshi.core import escape 
    1214from genshi.input import HTMLParser, ParseError 
    1315from genshi.filters.html import HTMLSanitizer 
     
    2628        'wiki': 'text/x-trac-wiki', 
    2729    } 
    2830     
     31    # Increase of the hierarchy level 
     32    hierarchy_offset = None 
     33     
    2934    # IWikiMacroProvider methods 
    3035    def render_macro(self, req, name, content): 
    3136        args = [x.strip() for x in content.split(',')] 
    3237        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: 
    3341            args.append(None) 
    34         elif len(args) != 2
     42        elif len(args) != 3
    3543            return system_message('Invalid arguments "%s"'%content) 
    3644             
    3745        # Pull out the arguments 
    38         source, dest_format = args 
     46        source, self.hierarchy_offset, dest_format = args 
     47         
    3948        try: 
    4049            source_format, source_obj = source.split(':', 1) 
    4150        except ValueError: # If no : is present, assume its a wiki page 
     
    7180            page = WikiPage(self.env, source_obj) 
    7281            if not page.exists: 
    7382                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             
    7591            ctxt = Context.from_request(req, 'wiki', source_obj) 
    7692        elif source_format == 'source': 
    7793            if not req.perm.has_permission('FILE_VIEW'): 
     
    99115                out = escape(out) 
    100116         
    101117        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     
    102145             
    103146    # IPermissionRequestor methods 
    104147    def get_permission_actions(self):