views:

137

answers:

8

When putting code samples in my blog I would like the code to have all the appropriate colours. How can I do that?

Basically if I write:

foreach (ListItem item in items)
{
    item.Text = "something";
}

I want it to appear in the correct colours like it does in my Visual Studio or here at stack*overflow*.

I'm bored of styling the colours of each key word manually. Is there a pre-existing javascript library I can use?

EDIT: I'm using blogger as my blogging platform if that helps

+1  A: 

Google "syntax highlighter". There are a bunch of libraries out there, many use javascript.

Dave Swersky
+2  A: 

A common approach is to use a JavaScript library to style up the code samples. You can use prettify to do syntax highlighting on a webapage, the README explains exactly how to do it.

Brabster
+1  A: 

Try this syntax highlighter. If you are using a particular CMS, there are probably modules already created for this.

Zack
Thanks! Although this link is out of date
BritishDeveloper
+2  A: 

You can also use a place that hosts the code (like http://gist.github.com , http://snipplr.com/ , and various other 'pastebin' services) and then just use the embed script that they provide.

SeanJA
He's talking about http://gist.github.com/
spilth
right... fixed that.
SeanJA
+1  A: 

I used Windows Live Writer for a while, and there is a plugin to paste from Visual Studio, that does the colour coding for you. I then coded that content into my Blogger/Wordpress post.

Now I use a java based plug-in for Wordpress SyntaxHighlighter Evolved, which provides line wrapping.

Simeon Pilgrim
+1  A: 

UPDATED:

link tutorial

aSeptik
A: 

You can also use vim's export as html, and then copy&paste in blogger. No the best solution, but probably the simplest.

fortran
A: 

Really I'd like to have jballs comment as the answer... but I can't. He was right though - SyntaxHighlighter seems to be the easiest to get/use and there is a lot of support for it too.

I implemented it really quickly. Tested it and explained my steps here: Colouring code with Syntax Highlighter

BritishDeveloper