- Log into your blogspot account, select the "Layout" tab, and then click "Edit HTML."
- Click "Download Full Template" to make a backup of your current template.
- Make a copy of your template, open it in an editor, and find the </head> closing tag. Before that tag, add the following:
<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'/>
<!-- add brushes here -->
<script type='text/javascript'>
SyntaxHighlighter.config.bloggerMode = true;
SyntaxHighlighter.all();
</script> - After the comment that says "add brushes here," add the languages you plan to use. For example, I'm using brushes for Javascript, Bash, SQL, XML/HTML, and C++:
<!-- add brushes here -->
See the full list of supported syntaxes.
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushJScript.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushBash.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushSql.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushXml.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushCpp.js' type='text/javascript'/> - Save your modified template, and upload it to Blogger (again, under Layout > Edit HTML).
- You should now be able to add syntax highlighting to your blog. Wrap your code in a <pre> tag and specify the brush to use in the class attribute. For example, to highlight a block of SQL:
<pre class="brush:sql">
SELECT *
FROM users
WHERE user_id = 1212;
</pre>
Here's how that looks when rendered:
SELECT *
FROM users
WHERE user_id = 1212;
The code you're formatting should be HTML encoded first. Also note that the Blogger preview won't show the formatting; you'll need to either go ahead and publish it or post it to a test blog first.
40 comments:
Awesome - I just posted a comment to someone else's blog that was using this to find out how to use it. Thanks for the steps. Trying it now but seems like either the preview of a blog doesn't include it OR I did something wrong :-(
@Mike: Yes, the preview window doesn't show the formatting.
Hi! Now I have installed SyntaxHighlighter sucessfully, then trying to create a new weblog for Java Programming. Bye and thanks for this information!
Excellent. Had just about given up when I found your post.
That's an excellent article, thanks!
With your explanation it was easy for me to installand use SyntaxHighlighter.
Thank you.
Thank you very much for this post.
I was looking for this kind of informations since longtime.
Instead of hitting Alex's site, maybe dropping the code onto Dropbox would be better.
Any idea of how that would perform?
Thanks for the post. Really helpful :)
Doesn't seem to be working for me. :( I get the source code but no coloring or anything. Any ideas?
http://blog.ianlee.info/2011/08/using-external-button-led-with-netmf.html
@Richard:
I see a javascript syntax error on this line:
SyntaxHighlighter.config.clipboardSwf = http://alexgorbatchev.com/pub/sh/current/scripts/clipboard.swf;
I think you need to quote the URL. Does that fix it?
@mla: That did it! You da man! Thanks!
I have done all of it, but I still get in the published page:
<pre class="brush:sql">
SELECT *
FROM users
WHERE user_id = 1212;
</pre>
@Dzenan: Are you editing in HTML mode (under the "Edit Html" tab)?
im using this!
thanks a lot!!
@Mike: Thanks a lot
Thanks a lot, this worked like a charm on my new blog. Did a stellar job on C#: http://danaltdel.blogspot.com/2012/05/automatically-generating-enum-from.html
very neat - worked beautifully on my blog http://rubyflewtoo.blogspot.co.uk before this i was using gists which are an enormous pain....
Great! Just what I was looking for.
Thanks! Saved me a lot of time.
It doesn't work for me
Note:
I've found a page where you can create valid code to include in your page
http://www.mybloggerlab.com/2012/07/how-to-add-syntax-highlighter-v3-in_31.html
thanks :)
................... .
Thanks for the help. It worked perfectly on my blog.
Nice Post, thanks for sharing.
In case you need a CSS based Syntax Highlighting of your C# code (no JavaScript at all), so that you can embed it in your website or blog easier, take a look at this Free Online C# Code Colorizer.
Thanks.. Worked for my blog.
www.gopiportal.in
Thanks, It works on my blog as well.
Post a Comment