Jerry-rigging pygments to support new PHP keywords
I use rst2pdf to create my presentations and noticed that the syntax highlighter wasn’t highlighting instanceof.
rst2pdf uses pygments for syntax highlighting, so I wondered what was going on. A short investigation led to me realise that the current stable version of pigments is 1.6 and they are working on 2.0. It seems that 2.0 has a number of changes to the PHP lexer, which aren’t in 1.6.
While I’m waiting, I modified my local copy of pigments 1.6 directly!
On my Mac, the file I’m interested in is in the egg file at /Library/Python/2.7/site-packages/Pygments-1.6-py2.7.egg. The .egg file is simply a directory, so within there, I edited pygments/lexer/web.py which is where the PHP lexer is.
Open web.py and look for the PhpLexer class (it’s around line 759 at the moment!). Scrolling further down, you come to the tokens section and then with the 'php' array, I found:
This is the list of words that will be highlighted as keywords. Simply add the new ones that you need.
I then deleted the web.pyc file that was in the lexer directory (though I’m unsure if I needed to as it may auto-recreate itself) and then ran rst2pdf again to create my pdf with instanceof, yield & finally now correctly highlighted!
Obviously, when the next version of pygments is released, hopefully it’ll be up to date, so jerry-rigging won’t be required. Until then, this is working for me, at least.