【问题标题】:using more than one highlight color in pygments在 pygments 中使用不止一种高亮颜色
【发布时间】:2016-09-20 20:42:10
【问题描述】:

我正在使用 pygments 突出显示文件中的行,但我想用不同的颜色突出显示不同的行。

note 在我写这个问题时,我尝试了不同的方法,直到我找到了一个看起来不错的解决方案来解决我的问题。我会在答案中发布。

我第一次尝试更改默认黄色(非常苍白)是:

HIGHLIGHT_COLOR = '#F4E004'

formatter = HtmlFormatter(linenos='inline', hl_lines=hl_lines, lineanchors='foo')
style = formatter.get_style_defs()
with open(the_xml_fullpath) as f:
    highlighted = highlight(f.read(), XmlLexer(), formatter)
# make the yellow more ...yellow
_style = re.sub(r'background-color: \#.+ ', 'background-color: {} '.format(HIGHLIGHT_COLOR), style)

现在我完全了解the perils of using a regular expression to parse HTML,但我认为唯一的选择是使用highlight()noclasses=True 选项,它不使用CSS 类内联CSS,然后遍历整个文件并替换背景颜色我想要的线条。

所以我的问题是:如何使用不同颜色的 pygments 突出显示不同的行集?

【问题讨论】:

    标签: python css pygments


    【解决方案1】:

    我的解决方案按照文档的建议将HtmlFormatter 类子类化,如下所示:

    class MyFormatter(HtmlFormatter):
        """Overriding formatter to highlight more than one kind of lines"""
        def __init__(self, **kwargs):
            super(MyFormatter, self).__init__(**kwargs)
            # a list of [ (highlight_colour, [lines]) ]
            self.highlight_groups = kwargs.get('highlight_groups', [])
    
        def wrap(self, source, outfile):
            return self._wrap_code(source)
    
        # generator: returns 0, html if it's not a source line; 1, line if it is
        def _wrap_code(self, source):
            _prefix = ''
            if self.cssclass is not None:
                _prefix += '<div class="highlight">'
            if self.filename is not None:
                _prefix += '<span class="filename">{}</span>'.format(self.filename)
            yield 0, _prefix + '<pre>'
    
            for count, _t in enumerate(source):
                i, t = _t
                if i == 1:
                    # it's a line of formatted code
                    for highlight_group in self.highlight_groups:
                        col, lines = highlight_group
                        # count starts from 0...
                        if (count + 1) in lines:
                            # it's a highlighted line - set the colour
                            _row = '<span style="background-color:{}">{}</span>'.format(col, t)
                            t = _row
                yield i, t
    
            # close open things
            _postfix = ''
            if self.cssclass is not None:
                _postfix += '</div>'
            yield 0, '</pre>' + _postfix
    

    使用它:

    # dark yellow
    HIGHLIGHT_COLOUR = '#F4E004'
    # pinkish
    DEPRECATED_COLOUR = '#FF4ED1'
    
    formatter = MyFormatter(
        linenos='inline',
        # no need to highlight lines - we take care of it in the formatter
        # hl_lines=hl_lines,
        filename="sourcefile",
        # a list of tuples (color, [lines]) indicating which colur to use
        highlight_groups=[
            (HIGHLIGHT_COLOR, hl_lines),
            (DEPRECATED_COLOR, deprecated_lines),
        ]
    )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-24
      • 1970-01-01
      • 2011-03-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多