If you have problems with Syntax Highlighter, post them here.
Syntax or code highlighting is important particularly when you need to show code examples on your blog.
It is always a big task to read codes on a blog or a website without syntax highlighting, though they are enclosed with
code
tags. By integrating the syntax highlighter on a blog or a website, readers can read the code examples easier.There are so many free syntax highlighting scripts available on many websites. Most of the scripts are written in JavaScript, though some of them are powered by other programming languages such as Phyton or Ruby.
I believe "SyntaxHighlighter by Alex Gorbatchev" is the most common syntax highlighting script used by most of us. It supports a lot of different languages and you can easily create new "brush" for your language if it is not supported by default.
Adding Syntax Highlighter to your blog
First you need to backup your blogger template. In your blogger dashboard, click on 'Design' > 'Edit HTML' and then click on 'download full template' and save your template.
1. Go to Blogger Dashboard > Design > Edit HTML.
2. Press CTRL+F to find the code
</head>
3. Copy the below code
<!--SYNTAX HIGHLIGHTER BEGINS-->
<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>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushCpp.js' type='text/javascript'></script>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushCSharp.js' type='text/javascript'></script>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushCss.js' type='text/javascript'></script>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushJava.js' type='text/javascript'></script>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushJScript.js' type='text/javascript'></script>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushPhp.js' type='text/javascript'></script>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushPython.js' type='text/javascript'></script>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushRuby.js' type='text/javascript'></script>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushSql.js' type='text/javascript'></script>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushVb.js' type='text/javascript'></script>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushXml.js' type='text/javascript'></script>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushPerl.js' type='text/javascript'></script>
<script language='javascript'>
SyntaxHighlighter.config.bloggerMode = true;
SyntaxHighlighter.config.clipboardSwf = 'http://alexgorbatchev.com/pub/sh/current/scripts/clipboard.swf';
SyntaxHighlighter.all();
</script>
<!--SYNTAX HIGHLIGHTER ENDS-->
4. Paste it on top of
</head>
this code.5. Preview your template and if everything works fine, then save it.
Make the script work
You have just added the script to your blog. If you want to highlight code in your post, then you need to use a tag in order to make your Syntax Highlighter work properly. There are two tags and you can use any one on your blog.
1.
<pre>
tag2.
<script>
tag<pre> tag
When writing a new post, you need to use
<pre>
tag in the post. The code of your post needs to go in between <pre class="brush:html">
and </pre>
tags, in order to highlight your code properly. See the screenshot below.If you are posting HTML code, then you have to use 'html' brush in the
<pre>
tag.<pre class="brush:html">
Your 'HTML' code goes here
</pre>
Similarly, for CSS code use 'css' brush in the
<pre>
tag. Here is a list of brushes you can use for your blog post.<pre class="brush:css">
Your 'CSS' code goes here
</pre>
Note for Bloggers
There is a small issue with
<pre>
tag. When you post HTML code in blogger post between <pre class="brush:html">
and </pre>
tags directly, you might get an error like this.Your HTML cannot be accepted
Stop showing HTML errors for the body of this post
Stop showing HTML errors for the body of this post
To get rid of this error, You have to convert raw HTML to escape characters.
- Go to Quick Escape.
- Paste your HTML code into the text box and click on "Convert to escaped characters →" button.
- After conversion, you can use the converted HTML code in between
<pre class="brush: html">
and</pre>
tags. - Now, HTML code will render correctly on your blog.
<script> tag
This is almost same as
<pre>
tag, but the code of your post needs to go in between <script type="syntaxhighlighter" class="brush:html"><![CDATA[
and ]]></script>
tags.<script type="syntaxhighlighter" class="brush:html"><![CDATA[
Your 'HTML' code goes here
]]></script>
If you are posting PHP code, then you have to use 'php' brush in the
<script>
tag.<script type="syntaxhighlighter" class="brush:php"><![CDATA[
Your 'PHP' code goes here
]]></script>
If you use
<script>
tag, then you don't have to convert HTML to escape characters. But I suggest you to use <pre>
tag.Themes for your Syntax Highlighter
Syntax Highlighter 2.0 introduced custom CSS themes. This means that by switching out just one CSS file you can completely change the look and feel of the highlighted syntax. A small number of popular color themes are included with Syntax Highlighter and you can easily make your own. Just click the css theme below to see how it looks...
<link href='http://alexgorbatchev.com/pub/sh/current/styles/shThemeDefault.css' rel='stylesheet' type='text/css'/>
<link href='http://alexgorbatchev.com/pub/sh/current/styles/shThemeDjango.css' rel='stylesheet' type='text/css'/>
<link href='http://alexgorbatchev.com/pub/sh/current/styles/shThemeEclipse.css' rel='stylesheet' type='text/css'/>
<link href='http://alexgorbatchev.com/pub/sh/current/styles/shThemeEmacs.css' rel='stylesheet' type='text/css'/>
<link href='http://alexgorbatchev.com/pub/sh/current/styles/shThemeFadeToGrey.css' rel='stylesheet' type='text/css'/>
<link href='http://alexgorbatchev.com/pub/sh/current/styles/shThemeMidnight.css' rel='stylesheet' type='text/css'/>
<link href='http://alexgorbatchev.com/pub/sh/current/styles/shThemeRDark.css' rel='stylesheet' type='text/css'/>
After selecting the theme of your choice, just copy the code below the theme and replace the default theme link in the 'Syntax Highlighter' code.
<!--SYNTAX HIGHLIGHTER BEGINS-->
<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>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushCpp.js' type='text/javascript'></script>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushCSharp.js' type='text/javascript'></script>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushCss.js' type='text/javascript'></script>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushJava.js' type='text/javascript'></script>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushJScript.js' type='text/javascript'></script>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushPhp.js' type='text/javascript'></script>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushPython.js' type='text/javascript'></script>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushRuby.js' type='text/javascript'></script>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushSql.js' type='text/javascript'></script>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushVb.js' type='text/javascript'></script>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushXml.js' type='text/javascript'></script>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushPerl.js' type='text/javascript'></script>
<script language='javascript'>
SyntaxHighlighter.config.bloggerMode = true;
SyntaxHighlighter.config.clipboardSwf = 'http://alexgorbatchev.com/pub/sh/current/scripts/clipboard.swf';
SyntaxHighlighter.all();
</script>
<!--SYNTAX HIGHLIGHTER ENDS-->
Customizing your code
You don't need to paste the exact code on your blog. You can choose brushes which you use on your blog. If you are posting HTML & CSS code on your blog and want to highlight HTML or CSS syntax, then use HTML & CSS brushes as shown below and remove the rest of the code.
<!--SYNTAX HIGHLIGHTER BEGINS-->
<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>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushCss.js' type='text/javascript'></script>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushXml.js' type='text/javascript'></script>
<script language='javascript'>
SyntaxHighlighter.config.bloggerMode = true;
SyntaxHighlighter.config.clipboardSwf = 'http://alexgorbatchev.com/pub/sh/current/scripts/clipboard.swf';
SyntaxHighlighter.all();
</script>
<!--SYNTAX HIGHLIGHTER ENDS-->
You can add or remove brushes according to your needs.
Brushes
Here is a list of brushes you can use on your blog. If you don't find a brush you are looking for, here is a long list of brushes you can download.
Now you have got beautiful Syntax Highlighter on your blog.
{ 62 comments }
Thanks so much!!! You're the first one who explain how to add this library to blogger correctly! =)
Amazing! Crystal Clear Tutorial. Great!
Where is zebra ???
THX
nice job..
Grazie mille!!
Thanks, great info!
Thanks, very easy information!!
In my case I have an ugly scroll-bar to the right ... how do i get rid of it?
And the second Q: how do I get those nice "hovering actions" edit, copy2clipboard etc to the < pre > code?
Thanks!
@SCC
Can you post your blog url, so I can figure out the problem =]
Nice trick,
Thanks .
ok Good job!!!
how to add 'copy to clipboard' link, 'print' etc?
@Avink: It was available in previous versions. If you want to use it. Just replace the word 'current' in the links to any previous version of the highlighter that is given in the link below.
Source: http://alexgorbatchev.com/SyntaxHighlighter/hosting.html
Eg. http://alexgorbatchev.com/pub/sh/current/styles/shCore.css to
http://alexgorbatchev.com/pub/sh/2.1.382/styles/shCore.css
--------------------------------------------------------------
how to enable scroll bar in long line code?
Mine just went trough the border and my page look terrible.
______________
example:
I have 20 character wide blog,
then I input 24 character code,
then I just see my page end with 20th character of my code,
and if I blend it with normal text, then my normal text just do the same as the code.
width:20 char
code:24 char
____________
max width :
12345678901234567890
====================
the code :
12345678901234567890
abcdefghijklmnopqrstuvwx
and the normal text is:
12345678901234567890
This is the normal text for blog.
then it should be look like:
____________________
12345678901234567890
====================
abcdefghijklmnopqrstuvwx <-- like this with a scroll bar.
====================
This is the normal
text for blog
____________________
but actually my page was look like:
____________________
12345678901234567890
====================
abcdefghijklmnopqrst <-- the code missing and not end well.
====================
This is the normal t <-- also the normal text be the same.
____________________
I also has a screen shoot on this error
screenshoot
can u help me with that?
thanks for the response
--------------------------------------------------------------
I'm sorry for the sized of the screenshoot before, I just realized that the picture was too small, here the original size ,
would you help me?
@akira: Can you post your blog link, so it would be easy for me to figure out the problem.
well, I just made post to show you me problem,
this is my blog link
need your help.
thanks before.
@akira
Find this line in your template.
#outer-wrapper{width:960px;margin:0px auto 0px auto;padding:0;text-align:$startSide;font:$bodyfont}
and replace with this
#outer-wrapper{width:960px;margin:0px auto;padding:0;text-align:$startSide;font:$bodyfont}
Let me know if it is working or not.
It solved :)
Thanks a lot, you are awesome..
:)
how can I add zebra lines like the format in following code
http://forums.asp.net/t/1599156.aspx
please help.
Sorry, what exactly do you mean by zebra lines? Can you attach a screenshot?
Hi, You can see the syntaxhighligher code in following url
http://forums.asp.net/t/1599156.aspx
And also see my url
http://bnb-development.blogspot.com
You will find the difference in look. Can you please sort it out?
Shailendrasinh: You're using current version of Syntax Highlighter. Change the version of your Syntax Highlighter to get the look you need.
Just replace the word 'current' in the links to any previous version of the highlighter that is given in the link below.
Source: http://alexgorbatchev.com/SyntaxHighlighter/hosting.html
Eg. http://alexgorbatchev.com/pub/sh/current/styles/shCore.css to
http://alexgorbatchev.com/pub/sh/2.1.382/styles/shCore.css
replace all the links containing the word 'current' to '2.1.382'. Let me know if it works or not.
Yes, it worked.. Many thanks.!! But the fonts are little larger than expected. How can I change them to make them little smaller?
- Shailendra
Shailendra: Navigate to Blogger Dashboard > Design > Edit HTML and find this code .post-body in the CSS section.
It will look like this
.post-body {
line-height: 1.4;
font-size: 110%;
position: relative;
}
Set the font-size to 100% or even lesser.
It looks good at 95% =]
Thank you so much...!!
Awesome explanation :-)
Hey Ciber,
I followed your instructions to the letter and I don't get any results, this is what I added to my template html (I had to escape it, otherwise I could not publish it here):
<link href='http://alexgorbatchev.com.s3.amazonaws.com/pub/sh/3.0.83/styles/shCore.css' rel='stylesheet' type='text/css'/>
<link href='http://alexgorbatchev.com.s3.amazonaws.com/pub/sh/3.0.83/styles/shThemeDefault.css' rel='stylesheet' type='text/css'/>
<script src='http://alexgorbatchev.com.s3.amazonaws.com/pub/sh/3.0.83/scripts/shCore.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com.s3.amazonaws.com/pub/sh/3.0.83/scripts/shBrushCpp.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com.s3.amazonaws.com/pub/sh/3.0.83/scripts/shBrushCss.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com.s3.amazonaws.com/pub/sh/3.0.83/scripts/shBrushJava.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com.s3.amazonaws.com/pub/sh/3.0.83/scripts/shBrushJScript.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com.s3.amazonaws.com/pub/sh/3.0.83/scripts/shBrushPhp.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com.s3.amazonaws.com/pub/sh/3.0.83/scripts/shBrushPython.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com.s3.amazonaws.com/pub/sh/3.0.83/scripts/shBrushXml.js' type='text/javascript'/>
<script language='javascript'>
SyntaxHighlighter.config.bloggerMode = true;
SyntaxHighlighter.config.clipboardSwf = 'http://alexgorbatchev.com.s3.amazonaws.com/pub/sh/3.0.83/scripts/clipboard.swf';
SyntaxHighlighter.all();
</script>
I changed uri so they point to latest versions, but still no luck. In one of my posts I include this:
<pre class="brush:html">
<html><body><p>why the hell this is not working!!! grrrrr</p></body></html>
</pre>
And only get a sad gray font with the html tags and pre tags rendered...
Why I'm doing wrong? I checked both in Firefox, Opera and Chrome and get the same results...
Best regards,
Alex
どうもありがとう。
Thanks.
Not working for Delphi!
thanks alot...
i'm just post html, css, n php
work for my blogger.
Its not working for me at all..its prompts message saying
"Can't find brush for: CSharp "
"Can't find brush for: XML "
i used the following fuction in my blog post.
---------------------------------------------
pre class="brush:CSharp">
using (StreamReader sr = new StreamReader(path, true))
{
XDocument xdoc = XDocument.Load(sr);
}
/pre>
----------------------------------------------
check it out at
Code Hippo
@The Internet Mosquito: I had a look at your blog post and it seems to be working fine.
http://internetmosquito.blogspot.com/2011/03/memory-allocation-and-argument-passing.html
Let me know if you're still having problems, I could probably help you.
@Rafael Pimenta: To make it work, you'll need to add Delphi brush to your blog.
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushDelphi.js' type='text/javascript'></script>
@Sangram Nandkhile: I just checked your blog source and didn't find syntax highlighter added on your blog.
Add the below code before </head> tag and let me know if it works.
<!--SYNTAX HIGHLIGHTER BEGINS-->
<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>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushCSharp.js' type='text/javascript'></script>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushXml.js' type='text/javascript'></script>
<script language='javascript'>
SyntaxHighlighter.config.bloggerMode = true;
SyntaxHighlighter.config.clipboardSwf = 'http://alexgorbatchev.com/pub/sh/current/scripts/clipboard.swf';
SyntaxHighlighter.all();
</script>
<!--SYNTAX HIGHLIGHTER ENDS-->
Helpful!
Many many thanks! It work on my Blogge blog :)
I did not even have the need to escape the HTML characters :)
Halloo...
How to use "SyntaxHighlighter html brush"
I don't found "shBrushHtml.js"
Thank you
@wiyono
You can use shBrushXml.js to highlight xml, xhtml, html codes.
http://alexgorbatchev.com/pub/sh/current/scripts/shBrushXml.js
Refer to this link...
SyntaxHighlighter - Bundled Brushes
I always get a "cant find brush for .php" error!
And how can I get rid of the horizontal scroll bar without having to break the lines?
Thank you,
Pemo
@Pemo
Make sure you've included PHP brush in your SyntaxHighlighter code.
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushPhp.js' type='text/javascript'></script>
To get rid of the scroll-bar, you've to use older version of SyntaxHighlighter.
Thank you very much.
How can I align where says View source so my visitors can see the code?
http://gastonacuriogordo.blogspot.com/2011/08/function-replacedated-var-da-d.html
Thanks
Thanks for your help.
Explain is very clear.
thanks,,
its work on my blog..
Thank you for this post.
It helped a lot.
It took me more than one hour to install the plugin for syntax highlighting.:-(
Thanks at least i found one where it's explained pretty well.
But when i am putting my 'c' code in between pre tags i am finding some unwanted texts being displayed in my post, like question mark in the beginning and at the bottom. Help!!
http://howtosolveitbycomputer.blogspot.com/2011/10/supplementary-problems.html
Thank you for a lot!
Thanks for the post.
very good work thanks a lot
Major Projects
Hi.
Thanks for great library!
I have small problem. Can't remove scrolls bars:
http://programowalnasiec.blogspot.com/2012/02/szukamy-ciekawe-zespoy-muzyczne-za.html
Any tips?
It's Work Fine =) thx bro.
Cyberrack, can you help me?
all the code is fine but appear a problem
When I add "<" or "<" and the same kind. The code which I write is not in one line. Please take a look in one of my post.
http://lpuarmy.blogspot.com/2011/04/cara-redirect-page-dengan-html-php-dan.html
Sorry for my bad english, thank you :)
Hi, I´m begin to use highlighter code in blogger and when I insert it on top of my /head, the template of my movile view dissapear and I don´t found the way to fix it, somebody can help me?
wowwwwwwwwwwwwwwwwwwwwwwwwww coollllll :D i love this post.. thanks for this share,, I looking for this from last month.
it not working for my blog .please help me, thanks
http://pydean.blogspot.com/2012/04/test.html
It work on my blog!!
thanks for great tutorial...
i have a problem, when i type
#include
it includes an additional tag like this
#include
why is it considering as an html tag... could you please help me out with it.
very useful, it is what I wanted.
When I do a preview of the template, it says "More than one widget was found with id: Navbar1. Widget IDs should be unique.
Error 500"
Excellent Artical
We'd love to hear from you — Leave us a comment!