Formatting Source Code For Blog Posts

Link. February 7, 2008. Comments [4]. Posted in: Blogging | Tools

I've struggled quite a bit over the past few months trying to come up with a good way of including code snippets in my blog posts. The problem, of course, is making the code look good both when browsing the website as well as in the RSS feed. It's not the first time I've ran into this issue, either.

As I've mentioned in the past, one of the things making it hard is my blog engine, as the dasBlog build I'm currently running doesn't properly respect whitespace in the original HTML code. Because of this, posting code is a big pain in the neck, as you need to format the code in HTML manually since using good old <pre><code></code></pre> tags will render the code unreadable in the RSS feed.

Beyond just having the spacing and indentation right, there's also the matter of posting syntax highlighting. There are several options I've tried over the years:

  • CopySourceAsHtml: This is an OK tool, though I've had to build a custom build to make it work on my machine (which has some weird clipboard issues at times; not sure why). I'm not 100% satisfied with it, though. For one thing, it generates pretty ugly HTML. It's also not very useful for posting code in a language not supported by Visual Studio itself, which I occasionally do (example: PowerShell snippets).
  • CodeHTMLer: An online site for converting code snippets to HTML, supporting a bit more languages than CopySourceAsHtml. I've used this one extensively, and it usually does a good job, provided I explicitly check the "convert whitespace" option as well as "Inline Tags" for formatting. You don't get much choice in how the code is formatted, though. I believe there's a Windows Live Writer plugin based on this somewhere, but I always forget where so it's more convenient for me to just use the web application.
  • Syntaxhighlighter: A Javascript Library for formatting code snippets. Looks nifty, but only usable on the website and not RSS feeds, I think. It also seems to rely heavily
  • Iris: This is an interesting project, based on the VIM syntax highlighting. It actually does a pretty good job, though I haven't tried using it yet directly on my blog. The Ajax interface is slick, though there's also a version you can download to use on your own desktop. It only seems to support using CSS for the syntax highlighting, though.
  • TOhtml: I've also been experimenting lately with using Vim's :TOhtml command. This can actually provide very nice HTML if you set up the right options, and given the breath of syntaxes supported by Vim, you can pretty much use it to pretty print almost any code snippet you can think of.
    Another nicety of TOhtml is that it will use the colors in your selected Vim colorcheme, so the generated HTML looks exactly like it does in Vim. It can also generate the formatting using CSS or inline tags.
    As a sample, this is what I used in my last PowerShell post, with the options html_use_css, html_no_pre and use_xhtml. The color scheme I was was using was the black variant of the recently updated moria scheme, aided by Peter Provost's excellent PowerShell syntax files.

I'm sure there are many other options out there. I know there are some very nice plugins for other blogging platforms (like wordpress, from what I've been reading), but for obvious reasons that's not very useful.

Another issue that can be a bit bothersome with code formatting is the choice between using CSS rules or using inline tags.

In an ideal world, using CSS rules would be much more preferred, particularly if you can keep them in an external CSS file. One obvious benefit of this is that if you later decide to change your formatting preferences, your color scheme, or simply change your blog's layout and colors and want your code to match them, it becomes a whole lot easier (though this isn't all that possible for someone like me with 6 years of past blog posts with all kinds of code formatting used).

The downside of using CSS is that it's pretty much a website-only option, so it's not very useful for formatting code in your RSS feed. At least, my experience has been that most RSS generators and/or consumers will strip any inline CSS rules found in blog posts (this was, in fact, what happened to my last PowerShell post mentioned above).

I know of no way to easily reference an external CSS for this, but if anyone knows of a way, I'd sure appreciate knowing about it!

So that pretty much leaves, for now, the only option of not using <pre> tags and resorting to inline tags. Yuck! So, what's the secret sauce others are using for this?

dasBlog, RSS and Whitespace

Link. November 14, 2007. Comments [4]. Posted in: Blogging

Does anyone know why the heck dasBlog insists on screwing up whitespace in a post when generating the RSS Feed? It preserves it correctly when it writes out the feed's HTML, but generates RSS that simply removes it at random places, and it's usually the reason why code samples get screwed up so badly. Is there any workaround for this? Anyone else considers this a problem?

Frankly, I'm starting to seriously consider moving to a new blog engine; though I haven't done any tests to see which one would be a good replacement (any recommendations?). I'm not looking forward to a migration, though, and not sure yet that I can provide a way to keep existing URLs intact...

Yet a New Blog Theme

Link. November 12, 2007. Comments [2]. Posted in: Blogging

I had redesigned my weblog looks back in July, but got bored with it now. It looked fine, but the colors were a bit too strong and the design a bit too complex. I really wanted something a lot more straightforward, that used space more effectively and with softer colors. Here's the result:

Weblog

It's a very simple, minimalist theme, and I'm rather liking it for now. Still some tweaking it a bit, but it seems to do the trick!

Bandwidth Usage

Link. November 1, 2007. Comments [2]. Posted in: Blogging | Personal

My website usually has fairly low traffic, given that much of it (or this weblog, for that matter) isn't all that well known and I host few big files. Normally, the monthly bandwidth usage for winterdom.com is between 4.5 and 6 GB.

However, bandwidth usage during october jumped to 16.28 GB! That was a surprise, so I started looking at what might have caused it. From my adsense stats and my usage of Google Analytics, I knew that traffic had increased a bit during this last month on the weblog, but not on the rest of the site, which is normal given that I don't update it often and it is mostly used to host the downloads for my samples.

Also, the RSS subscriber base hasn't changed much in the past few months, and is usually pretty stable, right now between 900-980 subscribers.

A few interesting things I noticed:

  • Pageviews The number of people visiting the site certainly increased, a bit more than I had thought, actually. Usually my weblog didn't got more than 500 pageviews a day, but since October 16 I could see that going a bit above 1000 pageviews.
  • As far as I can see, the increase in the number of visitors has not translated into more RSS subscribers, or at least not to the main RSS feed hosted by feedburner.
  • The largest usage of bandwidth during october was, apparently PNG image files, accounting for well over 12GB of traffic this month.
  • A significant amount of traffic driven to my site during october came from the Visual Studio Express site on MSDN; apparently someone over there liked my Visual Studio Color Schemes :-).
    The rest of the traffic sources are pretty much the usual ones, like the Windows Workflow Foundation community site, Scott Hanselman's weblog (always a good source of visitors), a few pages scattered around blogs.msdn.com, and  Damien Guard's site.

It's pretty obvious at this point to me, that the most popular entries on this weblog are, by a large margin, my various posts on Visual Studio Color Schemes and my old post about Inconsolata. I'm not sure whether that's a good think or simply means that the "serious" posts I write on technical topics aren't nearly as interesting as I thought they could be ;-).

Anyway, looks like I may want to start considering hosting a few of the screenshots I post on my various posts on a separate site (like flickr) or at least stop posting them as large PNG files. Might as well go over Jeff Atwood's Reducing Your Website's Bandwidth Usage post and see what else might be useful there.

No rush, though, I would still need to serve about 4GB more a month to go over my hosting plan's monthly quota, and I doubt that's going to happen anytime soon! And even so, I suspect the current spike in traffic won't last too long.

Articles vs. Blogs

Link. July 13, 2007. Comments [0]. Posted in: Blogging

Larry O'Brien posted an interesting piece as a response to Jackob Nielsen's post on Articles vs. Blog Postings. Jackob Nielsen's opinion was that experts should spend their time writing longer, complete articles instead of blogging. Larry shoots down Nielsen's theories based on his own experience, and I'll agree with Larry this time, if not for the same reasons.

I'd first like to point out that articles and blogs are not at odds with each other; you can do both and still get good results. I've written articles for magazines in the past and I'll say it is a lot of work and can usually take a lot of time. Personally, I admire those that have the drive, guts and patience to do that month after month. Blogging can certainly take a lot of time as well, particularly if you write the occasional long article, but not anywhere on the same scale. I favor blogging now not because it takes less work, but because I find it more useful for myself.

A lot of what I write about on my blog is about stuff I've learned (or am learning) during my own projects, and it's a great resource that I constantly search and refer back to when needed. So, while I try to write so that other people will find my blog useful, my primary drive has always been personal benefit: it provides not only the constant reference (so that I don't forget things), but it's also an excellent way to learn at the same time. When I force myself to write about a topic, I need to make things a lot clearer on my head and thus end up with a better understanding of what I'm writing about.

Monetary gain [1], or leads, while useful, have not been the main motivation for having this blog for me. To be honest, while I've gotten some work from my blog, it hasn't been all that much (I'd sure appreciate any leads :-)). Then again, that might be because I don't have all that many readers...

[1] Yes, I do have some ads on this blog through Google adsense, but if I told you what those ad clicks have amounted to, you'd be laughing ;-)

About

Tomas Restrepo is co-founder of devdeo ltda. His interests include .NET, Connected Systems, PowerShell and, lately, dynamic programming languages. More...

email: tomas@winterdom.com
msn: tomasr@passport.com
twitter: tomas_restrepo

Technorati Profile

devdeo logo

View my profile on LinkedIn

MVP logo

Syndicate

Ads

Links

Tag Cloud

.NET (232) Architecture (47) ASP.NET (6) BizTalk (170) Blogging (64) C++ (3) Castle (2) Commerce Server (3) Development (118) DLR (7) Enterprise Services (25) Fonts (4) Host Integration Server (1) LINQ (3) Linux (5) NHibernate (1) Personal (143) PowerShell (22) QuickCounters (4) Tools (74) Vista (38) VS Color Scheme (10) VSTO (2) WCF (64) Web Services (87) WinFX (80) Workflow (47) WPF (5) XML (21)

Statistics

Total Posts: 986
This Year: 56
This Month: 6
This Week: 0
Comments: 755

Blogroll

Post Archive

Other

Copyright © 2002-2008, Tomas Restrepo.

Powered by: newtelligence dasBlog 1.9.7174.0

Sign In