Publish syntax highlighted code

In Markdown and in several CMS sufficient code beautifiers work out of the box. But what if you dislike the color pattern or need more control? Manually apply HTML styles to every key word? And if you need the styles inline because your CMS ignores <styles>, but let's you insert inline format commands? You would not do that.

You need pandoc, and if you need inline styles, you need premailer. If you are content with the first results, you are done in a few seconds. If you want to author your own style patterns, that will take you some hours the first time. But never as long as a manual edit would take.

This has been tested on Linux Mint Ulyana. Since Python and pandoc exist for almost every real system around (not for the little pocket play machines), you should be able to do this on every serious machine.

Preparation (once):

Download pandoc. Play with my sample command and the several styles you can use. Create your own styles, if you want.

Only if you need inline styles: Ensure Python and PIP in your System. Install premailer.

Beautify a peace of code works this way:

1. pandoc works easiest with Markdown input. Embrace the code part by at least three ~~~ and define the language (cf. list of languages pandoc supports and highlights, in its man pages / command line help or on its website.)

  ~~~~~~ {.powershell}

Tip: To show markdown code on a markdown site, nest two ~ row pairs, use another count of ~ for start and end row of the inner pair than for the other.

2. Have pandoc convert your .md to .html. Fenced code blocks (those embraced by ~~~) are automatically syntax highlighted. With -s, pandoc writes a standalone html that you can open in your browser and immediately check the result. If you don't like the color pattern, set another highlighter on the command line. If you want, you can create your own highlighter for repeated use.

$ pandoc "/path/" -s -o "/path/file.ps1.html" --metadata pagetitle="file.ps1"

If you have full control over your website's code, you can use the <style>s and the lines of code directly in your html. If your target is an edit control in a CMS or a mail body editor you will need the next step.

3. Have premailer convert <style>s into inline styles.

$ python3 -m premailer -f "/path/file.ps1.html" > "/path/file.ps1.inline.html"