Add Syntax Highlighter to Blogger

There are quite few good Syntax Highlighters out there, one of them is “Prettify” which is simple and lightweight. Prettify states that it can automatically guess and highlight code snippets for many languages (best on C-like and HTML-like) and also has many language extensions, if you need one. So I setup a simple demo to test it and the result was quite good.
Syntax Highlighter

Simple setup Adding Prettify Syntax Highlighter to any CMS or webpage is very easy. For Blogger you just need to go to your blog’s Template tab, click “Edit HTML” button and add the below script before tag.

As you can see that the project is hosted on Google Code. This is an auto-loader which will automatically load the required JavaScript API and the CSS file from the subfolders of SVN. So the last thing that you are left is to mention where it should highlight the codes by adding class="prettyprint" to pre and/or code tags within Blogger Post Editor, HTML view. Here is an example:

$(function() {
alert('I am using Prettify as Syntax Highlighter');
There are also some styles available that you can call and load the stylesheet in one URL, here is an example:

Own hosting Well, the above tutorial was the simple yet effective setup but what if you want to host Prettify on your own server? Blogger don’t allow us to host files directly, so if you have got a hosting service then it’s good, otherwise you can use Google Code to upload the syntax highlighter files. Download the file form this page, extract it using archive software like WinRAR or 7-Zip and upload “prettify.css” and “prettify.js” files by creating a project. Now same as above add the CSS file before and the script before tag respectively. To execute it you need to add prettyPrint(); before closing body tag as shown below:

{All head contents}

{Inline CSS}

{Other JavaScript}

{Header, Footer, DIVs}

Đăng bình luận

Mới hơn Cũ hơn
Hãy chia sẻ nếu thấy bài viết có ích!