domingo, maio 04, 2008

How to add syntax highlight to Blogger

Adding syntax highlight to blogger is very simple, first of all you need to download SyntaxHighlighter.

Now extract the contents of the package and upload the Scripts and Styles folder to any host or website which can be linked from your blog.

To make it work, you will need to edit your blog's template and add the following code after the <!-- end outer-wrapper --> tag:


<link href='http://[YOUR HOST URL]/SyntaxHighlighter.css' rel='stylesheet' type='text/css'/>
<script language='javascript' src='http://[YOUR HOST URL]/shCore.js'/>


<script language='javascript'>
dp.SyntaxHighlighter.BloggerMode();
dp.SyntaxHighlighter.HighlightAll('code');
</script>


Now you just need to add support for the languages you want, I could just say, just put this bunch of code on your blog's template and it will work, but most people don't post code in several languages, also it's very important to understand what the hell you are doing.

To add syntax highlight support for python, after the shCore.js line you added before, add this line:

<script language='javascript' src='http://[YOUR HOST URL]/shBrushPython.js'/>


If you browse the Scripts folder of the syntaxhighlighter package, you will notice several shBrush*.js files, like shBrushPhp.js, these files are the highlight rules for especific languages, to support other languages, just change the shBrushPython in the above code to your language's specific file and add it to your template.

Syntaxhighlighter supports pre and textarea tags, but it will not work automagically, you need to specify that a pre or textarea tag is code and which language the code is.

To add some python code to your blog use:

<pre name="code" class="python">

The list of supported languages and their aliases can be found at the syntaxhighlighter wiki

35 comentários:

LKRaider disse...

Awesome! Thanks :)

El Guille disse...

justo lo que buscaba :-P ..

charas_override disse...

Muy bueno, gracias por la información, pero no es necesario colgarlo en un servidor propio, pues ya está colgado por los autores del mismo:

<link href='http://syntaxhighlighter.googlecode.com/svn-history/r57/trunk/Styles/SyntaxHighlighter.css' rel='stylesheet' type='text/css'/>
<script language='javascript' src='http://syntaxhighlighter.googlecode.com/svn-history/r57/trunk/Scripts/shCore.js'/>

javascript:void(0)
<script language='javascript'>
dp.SyntaxHighlighter.BloggerMode();
dp.SyntaxHighlighter.HighlightAll('code');
</script>
<!--Support for python : -->
<script language='javascript' src='http://syntaxhighlighter.googlecode.com/svn-history/r57/trunk/Scripts/shBrushPython.js'/>

charas_override disse...

Nice one!, thanx for the tip, just a remark: Its not really needed to upload the code as its already hosted by google.code. This would make it:

<link href='http://syntaxhighlighter.googlecode.com/svn-history/r57/trunk/Styles/SyntaxHighlighter.css' rel='stylesheet' type='text/css'/>
<script language='javascript' src='http://syntaxhighlighter.googlecode.com/svn-history/r57/trunk/Scripts/shCore.js'/>

javascript:void(0)
<script language='javascript'>
dp.SyntaxHighlighter.BloggerMode();
dp.SyntaxHighlighter.HighlightAll('code');
</script>
<!--Support for python : -->
<script language='javascript' src='http://syntaxhighlighter.googlecode.com/svn-history/r57/trunk/Scripts/shBrushPython.js'/>

Deepak Sakpal disse...

I followed all the steps but when I see my blog in Firefox it is not working as expected, but its working in Internet Explorer. Here is the link to my blog: http://deepaksakpal.blogspot.com/2008/09/narrowing-widening-conversion-gotcha.html

mariuz disse...

thanks , i always had problems with posting pretty code on blogger

Fhernd disse...

Hi! I received this error message from Blogger service when I try to save the template: bX-m0ky5r.... So long!

චමිල disse...

Thanx for the tip man....
I hosted my files on mediafire. but with those links syntax highlighter doesn't work. then i pasted your links to the code and it worked. seems like mediafire is not serving the files. can you please host the other language files and post the links?

Anônimo disse...

There is not <--- outer-wrapper---> tag on my blogger templates...what do i do?

reyt disse...

we provide a power leveling and free wow gold wow power leveling

Peter Strøiman disse...

Thanks, I just applied this to my own blog ;)

andi disse...

Thanks, After browsing to everywhere, your guide is crystal clear. thanks

Geshan disse...

it is not working in my blog :(

Rudy disse...

Procurei bastante mas o seu post foi o mais simples de implementar. Parabéns pelo trabalho.

Rudy

Anônimo disse...

Who knows where to download XRumer 5.0 Palladium?
Help, please. All recommend this program to effectively advertise on the Internet, this is the best program!

AccesInterzis disse...

It dosen't work. I followed all steps but something is wrong.

Anônimo disse...

here is a tool wich does it for you!
click

Anônimo disse...

hi..thanks..
my blog is about VHDL coding.I want to use syntax highlighter for VHDL in my site.Is it available in google?
Here is a link to my site:
http://vhdlguru.blogspot.com/

ekozul disse...

nice post, thanks

Anônimo disse...

Here's a guide on how to use syntax highlighter in your sie:

http://practician.blogspot.com/2010/07/color-my-world-syntax-highlighter.html#links

Muhammad Taqi disse...

wow, nice trick :)

Anônimo disse...

cool thanks,
http://youtrickz.blogspot.com

Andhika Budihardjo disse...

Thanks man :)

Generic Viagra disse...

Thanks for giving us those tips to be able to add syntax highlight to Blogger.

Generic Viagra disse...

I hate those codes because it is so difficult to me manage then when I'm working.

Generic Viagra disse...

I'm having many problems with my computer and I try to fix it by myself following those steps and I save a lot of money because I don't have to pay for a technician.

YellowRose disse...

Excellent..thanks

YellowRose disse...

There is also a non javascript version of syntax highlighter for Blogger, like the one provided..

fadehelix disse...

great simple tip, thanks!

benjamin disse...

no on in my template nor in 'classic' template. Which is your base template?
Are there any specific tags it need to fit in?
Thanks.

Anônimo disse...

Can I add syntax for android?Here is the link of my blog http://www.androidcookers.co.cc/

sd disse...

thanks!

Android developer disse...

These blog is adorable able and it can calmly to affect the visitors , because its accepting able brawl . So you to beforehand the blog by appliance some Internet business activity and its calmly to adeptness the complete exchange place.

Bits Randômicos disse...

Funciona direitinho, eu hospedei os arquivos no dropbox, free.

O css voce coloca antes de fechar a tag head. Eu estava esquecendo de colocar rel = 'stylesheet', e os estilos
nao estavam sendo aplicados.

Os scripts voce pode colocar antes da tag body.

vlew!

Levitra disse...

Thanks for sharing information.wonderfull peice of code thanks.Keep posting.