Here’s the situation: on my other website, I’ve been working on posting information for my course on CSS. As part of that, I have CSS & HTML code on various webpages. To create the pages, I compose in Markdown (using Mou, a fabulous Markdown editor for Mac OS X) & then paste the Markdown into a textarea of the CMS my company uses (Concrete5, which is simply the best CMS available today). The CMS takes the Markdown, runs it through PHP Markdown Extra, & the resulting code is there for the world to see. So, for instance, if I put this in my Markdown (yes, I know this is simple, but this is an illustration):
… when it’s finally rendered by the CMS, this is the code that’s produced:
Since I’m displaying code to my students, I’d sure like it if the syntax was highlighted for better readability & understanding.
I looked at over 20 different syntax highlighting tools, & they all required that you add special classes to your code, to either the
<pre> or the
<code> elements. Since I use Markdown, this is impossible. I’m pasting Markdown into the textarea, & the HTML is rendered for me, so I never actually touch the HTML. Oh, I could always export HTML from Mou & paste that in, but then I’d have to edit HTML when I wanted to change the page. I’m quite capable of that, but I wanted to stick to Markdown.
</code></pre>, recognizing the language of the code & coloring accordingly1. Perfect.
After downloading it, copy
sunburst.css. Once those files are in place, you call them on your webpage by putting this at the end of
<head>. Here’s what I have:
If you’re going to be presenting code on every page, you could just put that in your website’s template. However, I only have code on a few pages on my website, so I don’t want it in my templates. Fortunately, a nice feature of Concrete5 is that you can add code to the
<head> of arbitrary pages, so I can add the above lines only when I need them.
The end result is this:
You can see more of highlight.js in action at http://www.granneman.com/webdev/coding/css/css-reset/. So far, I’m very happy with it. If you need an automated syntax highlighter, I’d recommend giving it a try.
If your snippet of code is too short or if highlight.js can’t figure out which language is being used, it can get confused (based on my experience, it would have to be a really short & vague chunk o’ code to confuse highlight.js.). In those cases, you can specify the language to highlight.js via a special class, with a value that specifies the language, like either
<code class="html">. Both would work to let highlight.js know which syntax to use. ↩