Artificial truth

archives | latest | homepage | atom/rss/twitter

The more you see, the less you believe.

Highlighting inline PHP code in Pelican
Mon 10 May 2021 — download

When writing my blogpost on Wordpress' latest XXE, I was surprised when I saw that my inline PHP code wasn't properly syntactically colourised.

Internally, Pelican is using python-markdown, which comes with an extension named codehilite for syntax highlighting via Pygments. There is an interesting small note at the end of its documentation about its options:

All other options are accepted and passed on to Pygments’ lexer and formatter. Therefore, valid options include any options which are accepted by the html formatter or whichever lexer the code’s language uses. Invalid options are ignored without error.

A quick look at pygments.lexers.php.PhpLexer's code tells use that the option we're looking for is startinline, or _startinline if you prefer.

Long story short, this is what you have to put in your pelican.conf if you want your inline php code to be highlighted:

    'extension_configs': {
        'markdown.extensions.codehilite': {'startinline': True},

An other, albeit über-gross and not future-proof solution, is to monkey patch the PHP lexer from within your