I am using Pygments as part of MarkDown, Python, and LaTeX (through minted
). For these reasons, I cannot easily change to another tool and would like to find a solution in Pygments.
What I want to achieve is to have "special highlighting" beyond plain syntax highlighting. This includes, for example, keeping specific terminal output in its original colour. Or adding extra styles to more easily highlight arguments for the user to fill in. An example is definitely going to make this easier to understand.
In the Swift manual written by Apple, the arguments that are there for you to fill in are highlighted in balloons. This makes them a lot easier to identify, and a balloon can cover a range of tokens:
I want to achieve something similar with Pygments. Merely changing the styling doesn't work. You want these things to be normally styled for their type of token when they are not inside a balloon, and you don't want any of the type's specific highlighting when it is inside a balloon. I don't think these types of balloons are possible in current vanilla Pygments. I think I need a new type of token to achieve what I want. I would want to use this for a variety of languages, so ideally I want to find the way to do this with the least amount of modifications. Sadly, I don't think I can achieve what I want with Pygments filters. My gut feeling here is that I need to rewrite the lexers for all the languages I want to use, but I think I can do this by subclassing them.
To summarise, what I want is that I feed code such as
§label name§: while §condition§ {
and that, in for example HTML output, something like
<pre><span class="balloon">label name</span>: <span class="k">while</span><span class="balloon">condition</span> { </pre>
comes out. I'm not pinning myself down on the use of the symbol §
for this new environment or anything like that, it is merely an example of the intended behaviour.
Since modifying the lexer for each language I use is quite the endeavour, and since Pygments' documentation is quite spartan, I would like to ask how this can be solved and whether I am overlooking something. Am I right in assuming I need a new token and need to rewrite each and every lexer? Or is there a smarter way?
balloon
as a second class on the span (<span class="k balloon">
). Why not use an HTML filter to pass Pygments output through? The filter can identify the special elements and modify them accordingly. I suppose you could identify the tokens to modify with some extra characters, but why not use the same method Pygments uses for highlighting lines; pass a list in:filter(html, ['label name', 'condition'])
. – Waylan