pygments

How can I change background of the specific lines in Pygments?


I developed a code to show the coverage for specific C, C++ and Python files (Coverage is computed b using GCov). Now I can show the source code using Pygments. I have an array of covered lines and missed lines, I want to change the background of covered lines to green and background of missed lines to red. Is it possible to do that using Pygments?


Solution

  • You can achieve that by subclassing the HTMLFormatter. Out of the box the HTMLFormatter only supports line highlighting, but not in different colors as you need it. See https://pygments.org/docs/formatters/#HtmlFormatter for more details (scroll down to the subclassing part.)