New Nikola Plugin: rst code blocks using prism.js
I have been writing some of my posts using Markdown lately. And not even plain old Python Markdown, but misaka which is an alternative implementation based on Hoedown
So, I noticed that those markdown files had no syntax highlighting! I looked and Misaka does offer something to "highlight things". Here is what it does:
<pre> <code class="language-python"> def f(): pass </code> </pre>
No classes in the elements, no nothing, just a class in the code element! It turns out that is the recommended way to indicate you are showing code:
There is no formal way to indicate the language of computer code being marked up. Authors who wish to mark code elements with the language used, e.g. so that syntax highlighting scripts can use the right rules, can use the class attribute, e.g. by adding a class prefixed with "language-" to the element.
So, I looked for how to actually display those things nicely, and found prismjs.com which looked very nice!
So nice, indeed, that now I hated how my reStructured Text Pages looked!
Therefore, I wrote a new Nikola plugin that replaces docutils' code directive and replaces it with a much simpler one that just tags things as code of a given language and lets prism do the rest.
AFAICS it's functionally equivalent, it supports the
:linenos: option, including
shifted line numbers.
I doubt anyone ever will want to use it but hey, there it is.