As we all know, any self-respecting blogger needs some kind of syntax-highlighting plug-in before posting source code on a blog. I've been amiss in this regard, but decided it was high time to correct this particular deficiency.
From doing a little web searching, it looks like the favorite choice is a Java script tool called syntaxhighlighter. As of this writing, syntaxhighlighter hasn't been updated since version 1.5.1 was release in August 2007. It's been surprisingly robust all this time, except that it appears to have some trouble with scrollbars in the more recent Firefox versions, and Safari can be pegged at 100% CPU utilization. Also, the documentation is a little sparse.
That said, here are the steps I followed to make syntax highlighting work with Blogger (the host for this blog) using the syntaxhighlighter suite of javascript and css code.
In the blogger back end (reachable by logging into your blog and clicking "New Post" or "Customize", or other methods), click the"Layout" tab, then the "Edit HTML" sub-tab. Within the textbox within the "Edit Template" section, do the following:
1. Go to http://syntaxhighlighter.googlecode.com/svn/trunk/Styles/SyntaxHighlighter.css, then perform a "select all" and "copy". The css information is now in the clipboard.
2. Paste the css information at the end of the css section of your blogger html template (i.e., after
<b:skin><!--[CDATA[/* and before ]]--></b:skin>).3. [Updated March 25, 2009 to include closing script tags] Before the
</head> tag, paste the following:<!-- Add-in CSS for syntax highlighting --> <script src='http://syntaxhighlighter.googlecode.com/svn/trunk/Scripts/shCore.js' type='text/javascript'></script> <script src='http://syntaxhighlighter.googlecode.com/svn/trunk/Scripts/shBrushCpp.js' type='text/javascript'></script> <script src='http://syntaxhighlighter.googlecode.com/svn/trunk/Scripts/shBrushCSharp.js' type='text/javascript'></script> <script src='http://syntaxhighlighter.googlecode.com/svn/trunk/Scripts/shBrushCss.js' type='text/javascript'></script> <script src='http://syntaxhighlighter.googlecode.com/svn/trunk/Scripts/shBrushDelphi.js' type='text/javascript'></script> <script src='http://syntaxhighlighter.googlecode.com/svn/trunk/Scripts/shBrushJava.js' type='text/javascript'></script> <script src='http://syntaxhighlighter.googlecode.com/svn/trunk/Scripts/shBrushJScript.js' type='text/javascript'></script> <script src='http://syntaxhighlighter.googlecode.com/svn/trunk/Scripts/shBrushPhp.js' type='text/javascript'></script> <script src='http://syntaxhighlighter.googlecode.com/svn/trunk/Scripts/shBrushPython.js' type='text/javascript'></script> <script src='http://syntaxhighlighter.googlecode.com/svn/trunk/Scripts/shBrushRuby.js' type='text/javascript'></script> <script src='http://syntaxhighlighter.googlecode.com/svn/trunk/Scripts/shBrushSql.js' type='text/javascript'></script> <script src='http://syntaxhighlighter.googlecode.com/svn/trunk/Scripts/shBrushVb.js' type='text/javascript'></script> <script src='http://syntaxhighlighter.googlecode.com/svn/trunk/Scripts/shBrushXml.js' type='text/javascript'></script>Feel free to remove lines for languages you'll never use (for example, Delphi) -- it will save some loading time.
4. [Updated to add final /script] Before the
</body> tag, insert the following:<!-- Add-in Script for syntax highlighting -->
<script language='javascript'>
dp.SyntaxHighlighter.BloggerMode();
dp.SyntaxHighlighter.HighlightAll('code');
</script>
5. Use the "Preview" button to make sure your website is correct, then click "Save Template".
6. When composing a blog entry that contains source code, click the "Edit Html" tab and put your source code (with html-escaped characters) between these tags:
<pre name="code" class="cpp"> ...Your html-escaped code goes here... </pre>
Substitute "cpp" with whatever language you're using (full list). (Choices: cpp, c, c++, c#, c-sharp, csharp, css, delphi, pascal, java, js, jscript, javascript, php, py, python, rb, ruby, rails, ror, sql, vb, vb.net, xml, html, xhtml, xslt)
For performing the HTML escaping, you can get a good list of tools by searching for 'html esaper' or a similar term. Here's the one I used while writing this post.
Let me know how it works!
Notes:
- Step 2 is a work-around to accommodate Firefox browsers, which for some reason refuse to load the css when linked, because it thinks the type is mime/plain instead of mime/css (I don't personally understand this -- I'm just using a workaround recommended from elsewhere)
- See full documentation here for Syntaxhighlighter.
Sources:
54 comments:
Step 4. Is lacking a "</script>", but otherwise, it works great.
Thanks!
Brent: Good catch! I've updated the post with the final /script... :)
Syntaxhighlighter v2.0 is now available at http://alexgorbatchev.com/wiki/SyntaxHighlighter.
What is your settings for 'Convert line breaks' set to? In my case, if I set it to 'Yes', it's inserting <br /> into codes defined under <pre> tag.
My "Convert line breaks" setting is set to "yes". However, I typically use the raw html editor instead of the WYSIWYG editor when entering in code. I think this avoids the problem of inserting the "br" tags. Also, the Syntaxhighlighter script has some provision for removing the "br" tags within a "pre" section. That is basically what the dp.SyntaxHighlighter.BloggerMode() function accomplishes. Does that work for you?
Thanks a lot! It was previously so irritating that I came accross a blogger whose last post on his blog was "Thinking of getting my own blog since I am tired of formatting code in blogger" and he had a programming blog (like mine, so I understand him! Now neat and clean syntax highlighting has landed. Safe landing! Thanks again for the simple tips. Let me know you that there in the world I found a post on this technique which scared me because it was pointed out that I need to host the scripts myself! But coming to your post has eased me a lot and surely to thousand others also! Thanks again!!
Very very useful
Thanks
Great, It works well for me..
Thank you so much dude..
For anyone who is having trouble I wanted to note that this did not work for me until I made the script tags valid XML by having them not be self-closing, ie <script></script> instead of <script />.
Thanks for the instructions, getting this working was pretty painless, now it's time to see how many lines of code it takes to throttle the js engine ;)
Hi jonz: By doing a "view source" on this page, I've discovered that blogger converts the self-closing javascript tags (i.e., <script.../>) into tag pairs (i.e., <script...></script>). If you're not using blogger, then it won't automatically update the text for you. I'll update the main example to use the tag pairs for the non-blogger users. Thanks for noticing this!
I've got problem. when I add lines like
dp.SyntaxHighlighter.HighlightAll('code');
blogger automatically converts it after saving into:
dp.SyntaxHighlighter.HighlightAll('code');
Blogger does that to many other characters in JS code. what can I do?
Hi Pawel: Unfortunately, I fail to see the difference in the two lines you've given as an example. Maybe Blogger converted your comment? Could you explain in more detail what was changed?
Useful stuff
Great job man
Thank you
Thanks Matthew. I have successfully add the syntax highlighter using your tutorial. Great job..
Thank You Matthew... your tutorial was the better i found on this argument! Ciao
Thanks Matthew . It was very useful and i added Mxml and As3 from Shigeru Nakagaki blog
Great Job. I referred lot many blogs but found your helpful.
great tut. thanksss.
Neat Highlighter is a great highlighter and supports many languages
Thank you! Worked first time - you have brought to an end hours of frustration!
Hey Mathew,
Thanks for the good post, it helped me to set my blog with syntax highlighter in 10 minutes.
Hi,
Its now working sir
Man, I found your post is really useful. Thanks man
Thanks for the tip on syntax highlighting. I got it working on my blog.
However, I am finding that it chews up CPU on the Google chrome browser. I dont know if this is a known issue?
Feroze: I remember seeing that issue too, where the CPU pegs. I thought it might have been on Mac's Safari, but wouldn't be surprised if they both do it, since Chrome is based on the same browser code as Safari is.
The SyntaxHighlighter project has since been upgraded to version 2, so I suspect that bugs in this version won't get fixed. I should probably spend some time to upgrade and maybe write a newer blog post, but I'm a bit lazy... :)
Took me a while to figure out, but that is because I think I was having the same problem as Pawel.
I was following another blog that said to use the link tag for the css. Blogger is replacing the ' with &-#-3-9-; (added - in the sequence so it would not be rendered as a ' ). At first I thought this was the problem until I read your blog that says FF has problems with loading the css in this manner. I tried everything to stop those ' from turning into the code, and I got it to stop using a CDATA. It still didnt work so I tried copying the css into the template's css. No luck. Finally, I decided to remove the CDATA and lo and behold it works with the ' converted into the above code (&-#-3-9-;).
Thanks a lot...really helpful.
I will try it.
Almost works for me. I did a test post to see if it works, but it only shows the highlighter and any regular text I put into the pre tags, but not any code.
hi, its very useful forever to post program code to my blog/websites thank you
Worked Perfect! Thank you!
Thanks for your helpful post.
There is an updated version on http://alexgorbatchev.com/wiki/SyntaxHighlighter
easy way to install updated version on blogger this post seems to use the old version... ive made a post that describes how to use the updated one...
Thanks a lot !
Hi, Thanks a lot
but I have a problem with it because
when I want to preview my blog in step 4, it asked me username and password!
What's wrong?
Alex's Blog: I'm guessing that you somehow logged out of Google on a different window or through a timeout. For example, logging out of gmail may cause this behavior. I don't think that it's related to the instructions in this post.
Worked Perfect! Thank you!
great stuff man!
now I'm adding php syntax hightlight to all my blog!
http://yuppframework.blogspot.com/2010/01/testing-del-orm.html
great article.
i tried ur method. it worked. but i tried
link rel=stylesheet href="http://...syntaxhighlighter.css" type="text/css"
instead of copying css information in the clipboard. it doesnt work.
Thanks dude! it helps me a lot.
Great Article! This is the tutorial most complete that i've found about syntaxhighlighter!
Thank you! This is the only tutorial that I have found to work.
wonderful and great. anyone can do it with the help of your article. thanks a lot. fantastic.
this is the best article among others about syntaxhighlighter. i have added it to my blog....
Have a nice day. take care. bye.
Rajeshkumar Govindarajan.
oracle dba
http://oracleinstance.blogspot.com
thanks a lot
Wouldn't it be more clever to use a tagged version of syntaxhighlighter to have it stable e.g.
http://syntaxhighlighter.googlecode.com/svn/tags/1.5.1/Scripts/shBrushCss.js
instead of
http://syntaxhighlighter.googlecode.com/svn/trunk/Scripts/shBrushCss.js
This is the best tutorial for this on the web. I swear I looked forever and slogged through unclear, cryptic instructions.
I'm frustrated that it took so long to find, though.
Thanks man! I was finally able to make syntaxhighlighter work with my blog.
Thankx man.... i check lot of links to work syntaxhighlighter in ma blog. finally with your help i did it. this is better one i ever read abt
syntaxhighlighter
Thanks you very much.. this post very much help..
thanks, I finally found this tutorial..
it's work... I think this version is better than new version that I've tried..
thanks... it's work fine in my blog
I am having this problem
I need to highlight this php
"echo $data."
"; "
but I can't get the "
" to show :(
You don't need to host the css and js file at your site, have a look at here:
http://practician.blogspot.com/2010/07/color-my-world-syntax-highlighter.html#links
Thanks, works perfectly.
Post a Comment