I'm using pygments to highlight lines from a file, but I want to highlight different lines with different colors.
note While I was writing this question I tried different things until I found what looks like a decent solution that solves my problem. I'll post it in the answers.
My first attempt at altering the default yellow (which is very pale) was:
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)
Now I am fully aware of the perils of using a regular expression to parse HTML but I thought the only alternative was to use the noclasses=True
option of highlight()
which does not use CSS classes inline CSS and then iterate through the entire file and replace the background colour of the lines I want.
So my question is: how can I highlight different set of lines using pygments with different colors?