Edgewall Software

Ticket #780: patch_rst_coderole.diff

File patch_rst_coderole.diff, 8.2 KB (added by tonib, 8 years ago)

patch for the ticket

  • trac/wikimacros/rst.py

     
    4343    raise EnvironmentError, 'Docutils version >= %s required, %s found' % (docutils_required, __version__) 
    4444 
    4545from trac.Href import Href 
     46from trac.WikiFormatter import Formatter 
    4647 
    4748__docformat__ = 'reStructuredText' 
    4849 
    49 WIKI_LINK = re.compile(r'(?:wiki:)?(?P<w>[A-Za-z][\w\#\?]*[^\w\#\?]*)') # Links must begin with Letters, \# ? so we can link inside pages. 
    50 #WIKI_LINK = re.compile(r'(?:wiki:)?(?P<w>(^|(?<=[^A-Za-z]))[!]?[A-Z][a-z/]+(?:[A-Z][a-z/]+)+)') 
     50WIKI_LINK = re.compile(r'(?:wiki:)?(.+)') 
     51#WIKI_LINK = re.compile(r'(?:wiki:)?(?P<wikilink>[A-Za-z][\w\-]*[^\w\#\?]*)') 
    5152TICKET_LINK = re.compile(r'(?:#(\d+))|(?:ticket:(\d+))') 
    5253REPORT_LINK = re.compile(r'(?:{(\d+)})|(?:report:(\d+))') 
    5354CHANGESET_LINK = re.compile(r'(?:\[(\d+)\])|(?:changeset:(\d+))') 
    5455FILE_LINK = re.compile(r'(?:browser|repos|source):([^#]+)#?(.*)') 
    5556 
     57#import trac.Logging 
     58#log = trac.Logging.logger_factory(logtype='file', logfile="/tmp/debug_rst.txt", level='ALL') 
     59 
    5660def _wikipage(href, args): 
    5761    return href.wiki(args[0]) 
    5862 
     
    7175    return href.browser(path, rev) 
    7276 
    7377# TracLink REs and callback functions 
    74 LINKS = [(WIKI_LINK, _wikipage), 
    75          (TICKET_LINK, _ticket), 
     78LINKS = [(TICKET_LINK, _ticket), 
    7679         (REPORT_LINK, _report), 
    7780         (CHANGESET_LINK, _changeset), 
    78          (FILE_LINK, _browser)] 
     81         (FILE_LINK, _browser), 
     82         (WIKI_LINK, _wikipage)] 
    7983 
    8084 
    81 def trac_get_reference(env, rawtext, text): 
     85def trac_get_reference(env, rawtext, link, text): 
     86 
    8287    for (pattern, function) in LINKS: 
    83         m = pattern.match(text) 
     88        m = pattern.match(link) 
    8489        if m: 
    8590            g = filter(None, m.groups()) 
    8691            missing = False 
     92            if not text: 
     93                text = g[0] 
    8794            if pattern == WIKI_LINK: 
    88                 if not (env._wiki_pages.has_key(g[0])): 
     95                pagename = re.search(r'^[^\#]+',g[0]) 
     96                if not (env._wiki_pages.has_key(pagename.group())): 
    8997                        missing = True 
    90                         text = text + "?" 
     98                        text = text + "?"             
    9199            uri = function(env.href, g) 
    92100            reference = nodes.reference(rawtext, text) 
    93101            reference['refuri']= uri 
    94102            if missing: 
    95103                reference.set_class('missing') 
    96104            return reference 
     105         
    97106    return None 
    98107 
    99108def trac(env, name, arguments, options, content, lineno, 
     
    119128 
    120129    .. _TracLink: http://projects.edgewall.com/trac/wiki/TracLinks 
    121130    """ 
    122     text = arguments[int(len(arguments) == 2)] 
    123     reference = trac_get_reference(env, block_text, text) 
     131    link = arguments[0] 
     132    if len(arguments) == 2: 
     133        text = arguments[1] 
     134    else: 
     135        text = None 
     136    reference = trac_get_reference(env, block_text, link, text) 
    124137    if reference: 
    125138        return reference 
    126139    # didn't find a match (invalid TracLink), 
     
    133146 
    134147 
    135148def trac_role(env, name, rawtext, text, lineno, inliner, options={}, content=[]): 
    136     reference = trac_get_reference(env, rawtext, text) 
     149    args  = text.split(" ",1) 
     150    link = args[0] 
     151    if len(args)==2: 
     152        text = args[1] 
     153    else: 
     154        text = None 
     155    reference = trac_get_reference(env, rawtext, link, text) 
    137156    if reference: 
    138157        return [reference], [] 
    139158    warning = nodes.warning(None, 
     
    159178    rst.roles.register_local_role('trac', do_trac_role) 
    160179 
    161180    # The code_block could is taken from the leo plugin rst2 
     181    def code_formatter(language, text): 
     182        #log.debug("language '%s' args '%s'", % (language, arguments)) 
     183        Format = Formatter(hdf, env, None) 
     184        Format.set_code_processor(language) 
     185 
     186        html = Format.code_processor(hdf, text, env)         
     187        #log.debug("language '%s' htmltext '%s'" % (language, html)) 
     188        raw = nodes.raw('',html, format='html') #(self, rawsource='', text='', *children, **attributes): 
     189        return raw 
     190         
     191    def code_role(name, rawtext, text, lineno, inliner, options={}, content=[]): 
     192        args  = text.split(":",1) 
     193        language = args[0] 
     194        if len(args)==2: 
     195            text = args[1] 
     196        else: 
     197            text = "" 
     198        #log.debug("coderole '%s' text '%s'" % (language, text)) 
     199        reference = code_formatter(language, text) 
     200        return [reference], [] 
     201         
     202 
     203       
    162204    def code_block(name,arguments,options,content,lineno,content_offset,block_text,state,state_machine): 
    163205 
    164206        """Create a code-block directive for docutils. 
     
    166208        Usage: .. code-block:: language 
    167209 
    168210        If the language can be syntax highlighted it will be.""" 
    169  
    170  
    171          
    172         from trac.WikiFormatter import Formatter 
    173          
    174211        language = arguments[0] 
     212        text = '\n'.join(content)         
     213        reference = code_formatter(language, text) 
     214        return [reference] 
    175215 
    176         code_processor = None 
    177         if  Formatter.builtin_processors.has_key(language): 
    178             code_processor = Formatter.builtin_processors[language] 
    179         else: 
    180             code_processor = Formatter.builtin_processors['default'] 
    181  
    182  
    183         html = code_processor(hdf, '\n'.join(content), env)         
    184         raw = nodes.raw('',html, format='html') #(self, rawsource='', text='', *children, **attributes): 
    185         return [raw] 
    186  
    187216    # These are documented at http://docutils.sourceforge.net/spec/howto/rst-directives.html. 
    188217    code_block.arguments = ( 
    189218        1, # Number of required arguments. 
     
    199228    code_block.content = 1 # True if content is allowed. 
    200229    # Register the directive with docutils. 
    201230    rst.directives.register_directive('code-block',code_block) 
     231    rst.roles.register_local_role('code-block', code_role) 
    202232     
    203233     
    204234 
  • trac/WikiFormatter.py

     
    487487            match = Formatter._processor_re.search(line) 
    488488            if match: 
    489489                name = match.group(1) 
    490                 if  Formatter.builtin_processors.has_key(name): 
    491                     self.code_processor = Formatter.builtin_processors[name] 
    492                 else: 
    493                     try: 
    494                         self.code_processor = self.load_macro(name) 
    495                     except Exception, e: 
    496                         mimeviewer, exists = self.env.mimeview.get_viewer(name) 
    497                         if exists != -1: 
    498                             self.mime_type = name 
    499                             self.code_processor = self.mime_processor 
    500                         else: 
    501                             self.code_text += line + os.linesep 
    502                             self.code_processor = Formatter.builtin_processors['default'] 
    503                             self.out.write('<div class="system-message">Failed to load processor macro %s: %s t %s</div>' % (name, line, e)) 
     490                self.set_code_processor(name, line) 
    504491            else: 
    505492                self.code_text += line + os.linesep  
    506493                self.code_processor = Formatter.builtin_processors['default'] 
    507494        else: 
    508495            self.code_text += line + os.linesep 
    509496 
     497    def set_code_processor(self, name, line=""): 
     498        if  Formatter.builtin_processors.has_key(name): 
     499            self.code_processor = Formatter.builtin_processors[name] 
     500        else: 
     501            try: 
     502                self.code_processor = self.load_macro(name) 
     503            except Exception, e: 
     504                mimeviewer, exists = self.env.mimeview.get_viewer(name) 
     505                if exists != -1: 
     506                    self.mime_type = name 
     507                    self.code_processor = self.mime_processor 
     508                else: 
     509                    self.code_processor = Formatter.builtin_processors['default'] 
     510                    self.out.write("<div class='system-message'>Failed to load processor macro '%s': '%s' '%s'</div>" % (name, line, e)) 
     511                    self.code_text += line + os.linesep 
     512 
     513 
     514 
    510515    def format(self, text, out): 
    511516        self.out = out 
    512517        self._open_tags = []