Thursday, August 5, 2010

Using SyntaxHighlighter 3.0+ on your blog

You can guess from the title that this is a Techie blog and that means I will be posting some code snippets from time to time and so I was looking into some options of code syntax highlighting and I narrowed it down to the awesome SyntaxHighlighter. I use a custom domain name but host my blog on blogger. I also use the awesome Windows Live Writer for posting.

Setting SyntaxHighlighter up was fairly easy but I did find a ton of blog posts on the web about how to do that with varying information. A lot of these posts were so old that they just misdirect you and deprive you from using the all the great features that the latest version offers so I thought I might take a moment to post the best solution that I know so far. Here goes,

Things you should know
SyntaxHighligher is a completely javscript and css based solution to syntax highlighting and it works on the client side and does not require you to embed inline styles and css. Now that is great with one drawback. Since the scripts are hosted (either on your site or somewhere on the web) and are linked on your website pages they will not make it to your RSS feed so most readers will not be able to show the highlighting. Personally, I think it is alright but if you don’t agree stop right here, SyntaxHighligher is not for you.

So How to Set it up then
As I said most of the information on the web is old so those posts tell you to link all the the brush JS on your page but from version 3.0 onwards SyntaxHighlighter has a nifty little feature for auto loading just the required brushes rather than linking all of them. Now if you don’t understand what I am talking about don’t worry, it’s even better since you don’t have to “unlearn” anything :-). Just follow the steps below to set it up for your blogger blog!

Step 1: Add the css and JavaScript links in the <head> section of your blog

<link href='http://alexgorbatchev.com/pub/sh/current/styles/shCore.css' rel='stylesheet' type='text/css'/> 
<link href='http://alexgorbatchev.com/pub/sh/current/styles/shThemeDefault.css' rel='stylesheet' type='text/css'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shCore.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shAutoloader.js' type='text/javascript'/>



Now notice that Alex Gorbatchev is actually providing free hosting for the required JavaScripts and CSS so you don’t have to worry about getting a place to host them. Also you will always point to the latest version if you just point to the “Current” directory.



Step 2: Setup Autoloader for brushes by adding the following javascript function again in the <head> section of your blog



<script language='javascript'>

function path()
{
var args = arguments,
result = [];

for(var i = 0; i != (args.length-1); i++)
result.push(
args[i].replace(&#39;@&#39;,&#39;http://alexgorbatchev.com/pub/sh/current/scripts/&#39;));


return result
};
</script>


Now blogger actually changes the single quotes to &#39; for you or you can post it encoded like above. Either ways it should be fine.



Step 3: Call to action – Use the JS to highlight all the sections on your blog, insert the script below just above </body> tag.



<script type='text/javascript'>
SyntaxHighlighter.config.bloggerMode = true;
SyntaxHighlighter.config.clipboardSwf = &#39;http://alexgorbatchev.com/pub/sh/current/scripts/clipboard.swf&#39;

SyntaxHighlighter.autoloader.apply(null, path(
&#39;applescript @shBrushAppleScript.js&#39;,
&#39;actionscript3 as3 @shBrushAS3.js&#39;,
&#39;bash shell @shBrushBash.js&#39;,
&#39;coldfusion cf @shBrushColdFusion.js&#39;,
&#39;cpp c @shBrushCpp.js&#39;,
&#39;c# c-sharp csharp @shBrushCSharp.js&#39;,
&#39;css @shBrushCss.js&#39;,
&#39;delphi pascal @shBrushDelphi.js&#39;,
&#39;diff patch pas @shBrushDiff.js&#39;,
&#39;erl erlang @shBrushErlang.js&#39;,
&#39;groovy @shBrushGroovy.js&#39;,
&#39;java @shBrushJava.js&#39;,
&#39;jfx javafx @shBrushJavaFX.js&#39;,
&#39;js jscript javascript @shBrushJScript.js&#39;,
&#39;perl pl @shBrushPerl.js&#39;,
&#39;php @shBrushPhp.js&#39;,
&#39;text plain @shBrushPlain.js&#39;,
&#39;py python @shBrushPython.js&#39;,
&#39;ruby rails ror rb @shBrushRuby.js&#39;,
&#39;sass scss @shBrushSass.js&#39;,
&#39;scala @shBrushScala.js&#39;,
&#39;sql @shBrushSql.js&#39;,
&#39;vb vbnet @shBrushVb.js&#39;,
&#39;xml xhtml xslt html @shBrushXml.js&#39;,
&#39;xml @shBrushXml.js&#39;
));
SyntaxHighlighter.all()
</script>


Notice I am using the new AutoLoader functionality and also since my blog is hosted on Blogger I am setting the bloggerMode to true. If your blog/website is different you should not set this flag and the rest of the setup should be the same for you. Also notice I have added XML brush twice one as a part of the xml xhtml.. group and one as a stand alone item. This was done to work around an issue with the WLW plug-in that I use so if you are not using it then you can remove that. Keeping it the way it is doesn’t hurt too. 



Step 4: Add the sections that you want to highlight and set the correct CSS class



<pre class="brush: css; toolbar: false;">

public static void main()


{


    Console.WriteLine(&quot;Highlight me!&quot;);


}


</pre>



And if everything is setup correctly it should look like below,



public static void main()
{
Console.WriteLine("Highlight me!");
}


A complete list of brushes can be found here. And the complete list of Settings can be found here. But wait, why remember these brushes and settings, why not install the awesome PreCode Snippet plug-in for the Live Writer and let the plug-in handle that for you. Once you install the plug-in you should be able to fire up the plug-in window like below and go crazy..



image



Wow, this post got long! But hopefully it will save people some time. And hey, this post will be a good test to see if I set it up correctly or not.

119 comments:

  1. Thanks for sharing use interview questions on .Net technology. While preparing for my job interview, your article helped me a lot to sharpen my skills and do well in my interview. One of trainer from leading dot net training in Chennai suggests me about your site.

    ReplyDelete
  2. Thanks Admin for sharing such a useful post, I hope it’s useful to many individuals for developing their skill to get good career. SAS Training in Chennai

    ReplyDelete
  3. Thank you for this brief explanation and ver nice information. well, got a good knowledge. Have a nice idea, this is more helpful to me. This can gives the better performance.
    Dotnet Training in Chennai

    ReplyDelete
  4. this is really awesome and informative blog today i came to know about Iseialize concept clearly it is really good thanks for sharing.

    sas training in chennai

    ReplyDelete
  5. First is to thank you for all this informative posts you give us for free; i bet all of us are happy.
    Such a great idea of yours! You have been a big help for me. Thanks a lot. more post for interesting topic. Great!

    SAP training in Chennai

    ReplyDelete
  6. Excellent post!!!. The strategy you have posted on this technology helped me to get into the next level and had lot of information in it.
    Hadoop Training Chennai | PHP Training in Chennai

    ReplyDelete
  7. Well Said, you have furnished the right information that will be useful to anyone at all time. Thanks for sharing your Ideas.
    SAS Training in Chennai | SAS Course in Chennai

    ReplyDelete
  8. Much obliged to you for requiring significant investment to give us a portion of the valuable and restrictive data with us.
    Regards,
    SAS Training in Chennai | SAS Course in Chennai | SAS Training Institute in Chennai

    ReplyDelete
  9. It is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful to me...
    CCNA Training Center in Chennai | CCNA Training in Chennai | Best CCNA Training in Velachery | Online CCNA Training in Chennai

    ReplyDelete
  10. http://www.hparikh.com/2010/08/using-syntaxhighlighter-30-on-your-blog.html

    Java Training Institute in Chennai

    ReplyDelete
  11. Impressive blog with lovely information. really very useful article for us thanks for sharing such a wonderful blog... No.1 Software Testing Training Institute in Chennai | Best Selenium Training Institute in Chennai | ISTQB Certification Center in Velachery

    ReplyDelete
  12. Thanks a lot very much for the high quality and results-oriented help. I won’t think twice to endorse your blog post to anybody who wants and needs support about this area.
    MCA Project Center in Chennai | MCA Project Center in Velachery

    ReplyDelete
  13. Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging…
    No.1 IOS Training Institute in Chennai | Best IOS Training Institute in Velachery

    ReplyDelete
  14. I really love reading and following your post as I find them extremely informative and interesting. This post is equally informative as well as interesting.No.1 IOS Training Institute in Velachery | Best Android Training Institute in Velachery | Core Java Training Institute in Chennai

    ReplyDelete
  15. I really enjoyed your blog..what a amazing post to sharing you..I gathered more information from your post..
    Software Testing Training in Chennai |No.1 Selenium Training Institute in Chennai | Web Designing Training Institute in Chennai

    ReplyDelete
  16. It is most knowledgeable information like this.I will read this article it is very easy to learn this blog.
    No.1 IOS Training Institute in Velachery | Best Android Training Institute in Velachery | Core Java Training Institute in Chennai

    ReplyDelete
  17. You made some good points there. I did a search on the topic and found most people will agree with your blog.Ethical Hacking Training Institute in Chennai | Ethical Hacking Training in Velachery

    ReplyDelete
  18. The strategy you have posted on this technology helped me to get into the next level and had lot of information in it…
    Best Microsoft Azure Training Institute in Chennai | Best Microsoft Azure Training Institute in Velachery

    ReplyDelete
  19. The strategy you have updated here will make me to get trained in future technologies. By the way you are running a great blog. Thanks for sharing this
    Best Advanced Excel Training Institute in Chennai | Best Advanced Excel Training Institute in Velachery

    ReplyDelete
  20. Nice post... Really you are done a wonderful job. Thanks for sharing such wonderful information with us. Please keep on updating...

    Best Java Training Academy in Kanchipuram

    ReplyDelete
  21. I have read your blog. It’s very informative and useful blog. You have done really great job. Keep update your blog.

    Best Tally Courses Training Academy in Kanchipuram

    ReplyDelete
  22. Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging…

    Best Web Designing Training Academy in Kanchipuram

    ReplyDelete
  23. Thank you so much for sharing. Keep updating your blog. It will very useful to the many users.
    Best Web Designing Training Academy in Kanchipuram

    ReplyDelete
  24. I have read your blog its very attractive and impressive. I like it your blog.
    Best Software Testing Training Academy in Kanchipuram

    ReplyDelete
  25. Your Blog is nice and informative..Thanks for sharing this valuable information with us..I am visiting this website regularly, this web site sharing nice thoughts

    Best MATLAB Training Academy in Kanchipuram

    ReplyDelete
  26. Thanks for this grateful information. all this information is very important to all the users and can be used good at all this process.


    Best Web Designing Training Academy in Kanchipuram

    ReplyDelete
  27. really you have posted an informative blog. it will be really helpful to many peoples. thank you for sharing this blog. so keep on sharing such kind of useful blogs.

    Best TALLY ERP 9.0 Training Academy In Kanchipuram

    ReplyDelete
  28. Excellent post!!!. The strategy you have posted on this technology helped me to get into the next level and had lot of information in it.
    Best web Designing Training Training Institute in Kanchipuram

    ReplyDelete
  29. Quite Interesting post!!! Thanks for posting such a useful post. I wish to read your upcoming post to enhance my skill set, keep blogging.
    Best Web Technologies Course Training Institute in kanchipuram|

    ReplyDelete
  30. Quite Interesting post!!! Thanks for posting such a useful post. I wish to read your upcoming post to enhance my skill set, keep blogging.
    Best Web Technologies Course Training Institute in kanchipuram|

    ReplyDelete
  31. Nice Post! It is really interesting to read from the beginning & I would like to share your blog to my circles, keep your blog as updated.
    Best Tally ERP 9 Course Training Institute in kanchipuram|

    ReplyDelete
  32. Interesting post! This is really helpful for me. I like it! Thanks for sharing!
    Best Graphic Designing Course Training Institute in kanchipuram|

    ReplyDelete
  33. Informative post indeed, I’ve being in and out reading posts regularly and I see alot of engaging people sharing things and majority of the shared information is very valuable and so, here’s my fine read.
    click here now
    click here for ecs status of pensioners
    click here to know centre location
    click here png
    click here to download (link 1)

    ReplyDelete
  34. your blog contain very useful information. Really hereafter I am very big follower of your blog..
    Best MATLAB Course Training Institute in kanchipuram|

    ReplyDelete
  35. Very good and informative information. Thanks for sharing such nice blog, keep on updating such good post.
    Best Software Testing Course Training Institute in kanchipuram|

    ReplyDelete
  36. Nice blog. Thank you for sharing. The information you shared is very effective for

    learners I have got some important suggestions from it.
    No:1 Hardware and Networking

    Training Academy in Kanchipuram


    ReplyDelete
  37. I love reading and following your post as I find them extremely informative and interesting.
    Best AWS (Advanced Amazon Web Services) Course Training Institute in kanchipuram|

    ReplyDelete
  38. This is excellent information. It is amazing and wonderful to visit your site.Thanks for sharing this information, this is useful to me…
    No:1 python Training Center in Kanchipuram







    ReplyDelete
  39. The provided information’s are very useful to me.Thanks for sharing.Keep updating your blog.

    No:1 Networking project Centre in kanchipuram

    ReplyDelete
  40. Thank you for sharing in this webpage, I can learn a lot and could also be a reference, I hope to read the next your article updates.
    No:1
    JAVA Project Center in kanchipuram

    ReplyDelete
  41. Great post! I am actually getting ready to across this information, It's very helpful for this blog.Also great with all of the valuable information you have Keep up the good work you are doing well.
    No:1
    VLSI Project Center in kanchipuram

    ReplyDelete
  42. You post explain everything in detail and it was very interesting to read.Thank you for Sharing.
    No:1 Best Tally Training Center in kanchipuram|

    ReplyDelete

  43. These provided information was really so nice,thanks for giving that post and the more skills to develop after refer that post. Your blog really impressed for me,because of all information so nice.No: 1 VLSI Project Center in Chennai|

    ReplyDelete
  44. This blog is informative.It helps me to gain good knowledge.It helps to understand the concept easily. please update this kind of information…No: 1 IT Project Center in Chennai|

    ReplyDelete
  45. I am reading your post from the beginning, it was so interesting to read & I feel thanks to you for posting such a good blog, keep updates regularly.No: 1 PMP Exam Center in Chennai|

    ReplyDelete
  46. Good one, it is very useful for me to learn and understand, thanks for sharing your information and ideas.. keep rocks..No: 1 AWS Exam Center in Chennai|

    ReplyDelete
  47. Thank you so much as you have been willing to sharing your information with us. And not only that we will spend a lot of time other blog but your blog is so informative and compare to that your blog is unique.No: 1 EEE Project Center in Chennai|

    ReplyDelete
  48. I am really happy with your blog because your blog is very unique and powerful for new reader.No: 1 Microsoft Exam Center in Chennai|

    ReplyDelete
  49. Thanks for sharing such a nice blog. Its so informative concept and easy to understand also, kindly keep updating such a nice blog..No: 1 Engineering Project Center in Chennai|

    ReplyDelete
  50. I have completely read your post and the content iscrisp and clear.Thank you for

    posting such aninformative article, I have decided to follow your blog so that I can

    myself updated.

    No:1 AWS Exam Center in Chennai

    ReplyDelete
  51. I have read your blog its very attractive and impressive. I like it your blog.No: 1 Python Certification in Chennai|

    ReplyDelete
  52. Hello , Really awesome blog. Your blog is really useful for me. Thanks for sharing this informative blog. Keep updating your blog.No: 1 JAVA Application Center in Chennai|

    ReplyDelete
  53. Great and useful article.Thanks for taking time to share

    this post
    Android Project Center in

    Chennai

    ReplyDelete
  54. Nice blog. Thank you for sharing. The information you shared is very effective for learners I have got some important suggestions from it.No: 1 JAVA Application Center in Chennai|

    ReplyDelete
  55. Nice Post! It is really interesting to read from the

    beginning & I would like to share your blog to my

    circles, keep your blog as updated.
    No.1 CCNA Training Center In Kanchipuram

    ReplyDelete
  56. Very nice blog. It is very useful for me. I read your blog completely. I gather lot of information in this post. Thanks for sharing. No: 1 BBA Project Center in Chennai |

    ReplyDelete
  57. Thanks for giving great kind of information. Thanks for your excellent blog, nice work keep it up thanks for sharing the knowledge. No: 1 Graphic Designing Course in Kanchipuram|

    ReplyDelete
  58. I am really enjoying reading your well written information. I think you spend numerous effort and time updating your blog. No: 1 Graphic Designing Training Institute in Kanchipuram|

    ReplyDelete
  59. Very happy to see this blog. Gives a wonderful information with coded explanation. Thank you for this blog. very useful to me. No: 1 JAVA Training Institute in Kanchipuram|

    ReplyDelete
  60. Wonderful!! this is really one of the most beneficial blogs I’ve ever browsed on this subject. I am very glad to read such a great blog and thank you for sharing this good info with us.Keep posting stuff like this.. No: 1 MS Office Training Center in Chennai|

    ReplyDelete
  61. Excellent blog. Very interesting to read. I really love to read such a nice a information. Thanks! keep rocking. No: 1 Final Year Project in Chennai|

    ReplyDelete
  62. You have done really great job. Your blog is very unique and informative. Thanks. No: 1 Automation Anywhere Training Institute in Chennai|

    ReplyDelete
  63. You have done really great job. Your blog is very unique and informative. Thanks. No: 1 Final Year Project in Chennai|

    ReplyDelete
  64. I have read all the articles in your blog; was really impressed after reading it.Thanks for giving nice information from your blog.. No: 1 Advanced MATLAB Course in Kanchipuram|

    ReplyDelete
  65. I really enjoyed while reading your blog, the information you have delivered in this post was damn good. Keep sharing your post with efficient news.Regards, No: 1 Adobe Illustrator Training Institute in Kanchipuram|

    ReplyDelete
  66. great blog, you have provided the right information that will be beneficial to somebody at all time. Thanks for sharing such valuable Ideas to our vision. No: 1 Adobe Illustrator Training Institute in Kanchipuram|

    ReplyDelete
  67. very nice blogs!!! I have to learning for lot of information for this sites...Sharing for wonderful information. Thanks for sharing this valuable
    information to our vision. You have posted a trust worthy blog keep sharing....
    No.1 PHP project center In kanchipuram









    ReplyDelete
  68. information to our vision. You have posted a trust worthy blog keep sharing....
    No.1 PHP project center In kanchipuram

    ReplyDelete
  69. information to our vision. You have posted a trust worthy blog keep sharing....
    No.1 MBA project center In kanchipuram

    ReplyDelete
  70. Wow it is really wonderful and awesome thus it is very much useful for me to understand many information and helped me a lot. it is really explainable very well and i got more information from your blog.
    Best Tally Training Institute in Kanchipuram

    ReplyDelete
  71. Nice blog…. with lovely information. Really very useful article for us thanks for sharing such a wonderful blog. Keep updating…..
    Best HTML Training institute in kanchipuram

    ReplyDelete
  72. I really enjoy simply reading all of your weblogs. Simply wanted to inform you that you have people like me who appreciate your work. Best Embedded Training Course in Kanchipuram|

    ReplyDelete
  73. very nice blogs!!! I have to learning for lot of information for this sites...Sharing for wonderful information. Thanks for sharing this valuable information to our vision. You have posted a trust worthy blog keep sharing.
    Best Tally institute in chennai

    ReplyDelete
  74. Very interesting topic. Helps to gain knowledge about lot of information. Thanks for posting information in this blog.
    Machine Learning Training in Chennai | Machine Learning Training in OMR

    ReplyDelete
  75. Wow, interesting and informative topic. Helps to gain a lot of knowledge . Thanks for posting such an informative post in this blog.
    Coreldraw training course in Chennai | Coreldraw training Course in Guindy

    ReplyDelete
  76. Thanks for giving great kind of information. So useful and practical for me. Thanks for your excellent blog, nice work keep it up thanks for sharing the knowledge. N0.1 Advanced Illustrator Certification in kanchipuram |

    ReplyDelete
  77. The provided information’s are very useful to me.Thanks for sharing.Keep updating

    your blog.No:1 MBA project center

    in kanchipuram
    .

    ReplyDelete
  78. Thank you for taking time to provide us some of the useful and exclusive information with us. N0.1 JAVA Project Center in kanchipuram |

    ReplyDelete
  79. Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging… Best RPA Training Course in Chennai |

    ReplyDelete
  80. very nice blogs!!! i have to learning for lot of information for this sites...Sharing for wonderful information. Thanks for sharing this valuable information to our vision. You have posted a trust worthy blog keep sharing…. Best Advanced Photoshop Certification Course in Chennai |

    ReplyDelete
  81. Thanks for splitting your comprehension with us. It’s really useful to me & I hope it helps the people who in need of this vital information. Best Selenium Certification Center in Chennai |

    ReplyDelete
  82. hanks for your informative blog. Your post helped me to understand the future and career prospects. Keep on updating your blog with such awesome information. Best Tally Training Course in Kanchipuram|

    ReplyDelete
  83. Awesome Blog with informative concept. Really I feel happy to see this useful blog, Thanks for sharing such a nice blog. Best Matlab Training in Kanchipuram|

    ReplyDelete
  84. Thanks for sharing this valuable information to our vision. You have posted a trust worthy blog keep sharing.
    No:1 MATLAP Training institute in kanchipuram.

    ReplyDelete
  85. This is really nice post. Thanks for sharing such an informative post and keep for sharing.. Best Tally Training in Kanchipuram|

    ReplyDelete
  86. Thanks for one marvelous posting! I enjoyed reading it;Great post.The information was very useful.Keep the good work goin on!! Best Adobe InDesign Training Course in Chennai|

    ReplyDelete
  87. This is excellent information. It is amazing and wonderful to visit your site.Thanks for

    sharing this information, this is useful to me…

    Best PYTHONE certification in

    kanchipuram

    ReplyDelete
  88. This is excellent information. It is amazing and wonderful to visit your site.Thanks for

    sharing this information, this is useful to me…
    Best Advanced .Net training

    Institute in chennai

    ReplyDelete
  89. Nice Blog...looking amazing...thank you so much for sharing such a wonderful blog...keep sharing... Best CCNA Certification In Chennai|

    ReplyDelete
  90. Excellant post!!!. The strategy you have posted on this technology helped me to get into the next level and had lot of information in it. Best Tally Training Center in chennai |

    ReplyDelete
  91. Nice Post! It is really interesting to read from the beginning & I would like to share your blog to my circles, keep your blog as updated. Best CCNP Training institute in chennai |

    ReplyDelete
  92. I like viewing web sites which comprehend the price of delivering the excellent useful resource free of charge. I truly adored reading your posting. Thank you! Best CEH Training institute in chennai |

    ReplyDelete
  93. Good one, it is very useful for me to learn and understand, thanks for sharing your information and ideas.. keep rocks.. Best Agile Scrum Master Exam center in chennai|

    ReplyDelete
  94. We are really grateful for your blog post. You will find a lot of approaches after visiting your post. Great work. Best Matlab Training Center in Chennai |

    ReplyDelete
  95. Very nice blogs!!! I have to learning for lot of information for this sites...Sharing for wonderful information. Thanks for sharing this valuable information to our vision. You have posted a trust worthy blog keep sharing.… ISTQB Certification in Chennai |

    ReplyDelete
  96. Very happy to see this blog. Gives a wonderful information with coded explanaion. Thank you for this blog. very useful to me. Prince2 Certification Center in Chennai |

    ReplyDelete
  97. Well Said, you have furnished the right information that will be useful to anyone at all time. Thanks for sharing your Ideas. Selenium Automation Training center in chennai |

    ReplyDelete
  98. Brilliant article. The information I have been searching precisely. It helped me a lot, thanks. Keep coming with more such informative article. Would love to follow them. Graphic design Training Institute in Chennai|

    ReplyDelete
  99. Really an awesome post. I wondered by reading this blog post. Thanks a lot for posting this unique post which you have shared with us. Keep on posting like this exclusive post with us.Python Training institute in Chennai |

    ReplyDelete