2016-09-20 2 views
1

Я использую pygments для выделения строк из файла, но хочу выделить разные строки с разными цветами.Использование более одного цвета выделения в пигментах

примечание Пока я писал этот вопрос, я пробовал разные вещи, пока не нашел, что похоже на достойное решение, которое решает мою проблему. Я отправлю его в ответах.

Моя первая попытка изменения желтый по умолчанию (что очень бледным) было:

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, но я думал, что единственной альтернативой было использовать noclasses=True вариант highlight(), который делает не использовать CSS-классы встроенного CSS, а затем перебирать весь файл и заменять цвет фона строк, которые я хочу.

Итак, мой вопрос: как я могу выделить различные строки, используя пигменты с разными цветами?

ответ

0

Мое решение подклассы в 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), 
    ] 
) 
Смежные вопросы