Syntax coloring makes it much easier to read source code, especially when you become accustomed to a particular color scheme. For example, I’m used to the default color scheme in Visual Studio: comments are green, keywords are blue, string literals are red, etc. Once you get used to color-coded source code, it’s hard to go back to black-and-white. However, the code samples here are monochrome and I’ve been thinking about doing something about that.
I tried to use it. Unfortunately the scrolling errors I see on your and other sites are too much to bear.
More recently I heard that StackOverflow is using prettify.js to add syntax coloring for their code samples. I haven’t noticed any problems on that site, so I’m starting to try out
prettify. I’ve heard that the script doesn’t always handle VB code correctly, but that doesn’t matter to me.
prettify script is easy to use. You don’t have to tell it what programming language you’re highlighting. However, you do have the option of specifying the language, which presumably can’t hurt. I’ve tried it on a page containing C++ code and on another page containing Python code and they both look OK. My only complaint is that the default color scheme is not what I would have chosen. However, the color scheme can be modified by editing a style sheet and I intend to do that.
I’m going to start by experimenting with static files on my site. I’m more cautious about incorporating syntax coloring with the blog since I don’t know what interaction problems there could be with the WordPress software on the server or with blog reader software on the clients. Scott Hanselman says on his blog
… I couldn’t find a syntax highlighting solution that worked in EVERY feed reader. There are lots of problems with online ones like Google Reader and BlogLines if I, as the publisher, try to get tricky with the CSS.
So I may leave the code samples on the blog alone. Also, I’m also not sure what I’ll do about PowerShell samples. The
prettify script works well with C-family languages, but PowerShell syntax may be too far afield from what it expects.
Does anyone have suggestions in general? For PowerShell in particular?
6 thoughts on “Syntax coloring for code samples in HTML”
Oh, let me count the ways. :)
Few ways to go about it. for complete scripts, I put in poshcode.org and use the embed script and it generate
I also use windows live writer a lot there is a nice plugin for doing various code embedding, I look for url
WLW Code Snippet plugin is nice: http://tinyurl.com/5eppdt
also, PowerShellPlus can copy as HTML or RTF
I looked at highlight. It seemed to be functional enough for my purposes. It’s well supported on Linux. I liked the Windows version. It has some nice features.
There’s always GNU Source-highlight:
And if you use KDE, the Kate editor has a nice ‘Export to HTML’ feature, and supports very many different languages. (And you can easily download support for new ones from within the program.)
Karl, you’re right. I was confused.