A while back I wrote a brief post in which I shared some simple code to automatically highlight the syntax of code blocks on a blog, Code Syntax Highlighting in Tumblr. I want to revisit that post for two reasons:
- I want to more fully explain how all this works
- I have created a small project called prettify-wrapper to handle most of the housekeeping, making for a packaged and streamlined code highlighting solution.
I created a repository on github to house the wrapper code, support code, and accompanying themes. You can link directly to it to use it on your site. Please check it out:
Why this method? and why google-code-prettify?
There are tons posts on this subject but most fall short of ideal. Here is why this method is better:
It involves one cut and paste and virtually no thought.
Nothing changes on the server.
You don’t have to modify html in your posts.
This solution does not require any special tags, CSS, or configuration when you post some code. Just wrap your code in
<pre><code> tags like you see below. Blogging platforms do that anyway so for most people they do not need to do anything other than copy and paste to get the job done.
<pre><code> printf("Hello World!"); </code></pre>
If you just want this thing to work, copy and paste this code right before your
What? You say that’s more than two lines? Well, it’s two statements that call upon many more lines of actual code. But it’s basically two lines of code that you have to worry about. The rest is all behind the scenes.
The originial google-code-prettify
You can check out more about google-code-prettify here.
Feedback and Pull Requests are welcome. You can comment here or interact with the project via github.