Edgewall Software

Ticket #5654: tracpygments-options.diff

File tracpygments-options.diff, 1.7 KB (added by thatch, 5 years ago)

First version, against r2367 for tracpygments

  • __init__.py

     
    6060        to override the default quality ratio used by the 
    6161        Pygments render.""") 
    6262 
     63    pygments_options = ListOption('mimeviewer', 'pygments_options', 
     64        '', doc= 
     65        """List of options to be passed to lexers. 
     66 
     67        For each, a tuple `mode:option` has to be specified, 
     68        for example `php:startinline=1`.  To specify more than 
     69        one option, simply repeat the mode in the style, 
     70        `php:a=1,php:b=2` 
     71        """) 
     72 
    6373    expand_tabs = True 
    6474    returns_source = True 
    6575 
     
    218228                    Mimeview(self.env).configured_modes_mapping('pygments') 
    219229                ) 
    220230 
     231        self._options = {} 
     232        for t, opt in [x.split(':', 1) for x in self.pygments_options]: 
     233            opts = self._options.get(t) 
     234            if not opts: 
     235                opts = {} 
     236                self._options[t] = opts 
     237            key, value = opt.split('=', 1) 
     238            opts[str(key)] = value 
     239 
     240 
    221241    def _highlight(self, language, content, annotate): 
     242        self.log.debug("Pygments renderering for language=%s" % language) 
    222243        formatter = HtmlFormatter(cssclass=not annotate and 'code' or '') 
    223         html = pygments.highlight(content, get_lexer_by_name(language), 
     244        opts = self._options.get(language, {}) 
     245        opts['stripnl'] = False 
     246        lexer = get_lexer_by_name(language, **opts) 
     247        html = pygments.highlight(content, lexer, 
    224248                                  formatter).rstrip('\n') 
    225249        if annotate: 
    226250            return html[len('<div><pre>'):-len('</pre></div>')].splitlines()