RexOps / rexify-website

Website content and code for rexify.org
https://www.rexify.org
14 stars 31 forks source link

Highlight syntax during site build (fix #106) #123

Closed ferki closed 5 years ago

ferki commented 5 years ago

This PR introduces syntax highlighting of fenced code blocks via a Statocles plugin.

There are 3 steps which build on top of each other:

  1. Remove highlight.js and related references

    Except the CSS theme for the highlighted fragments, so the exact same design can be reused, or even replaceed easily later.

  2. Add a site-specific Statocles plugin for highlighting

    It is now living in ./lib under project root, so has to be included on recent Perl versions with PERL5LIB="./lib:${PERL5LIB}" or similar.

    It basically hooks into the page collection step of the Statocles build process, and preprocesses each document by replacing fenced code blocks with HTML fragments including <span class="...">...</span> tags around parts that needs to be highlighted.

    It also adds leading invisible and non-copyable (? fixme!) WORD JOINER characters to the code blocks, so the subsequent markdown rendering will leave these fragments intact, while also not interfering with copy-pasting from the browser.

    It expects code fences with hints about the language of the code blocks, and supports any language that is supported by Syntax::Highlight::Engine::Kate.

  3. Add fences around code blocks which need highlighting

    This is the last step needed to actually enable the preprocessing and highlighting of the code blocks.

    As a side effect, it makes it relatively easy to identify perl code blocks and process them in various ways, e.g. run perltidy on them (stay tuned for a followup PR, work in progress :).