Monday 24 December 2007

Blogger Code Formatting

I like blogger in general but it's probably not the standard geek's first choice; I suggest this simply because the editor has no UI functionality to preserve the formatting of the code samples most geeks want to paste in. HTML, God bless it, is HTML and Blogger doesn't help you write anything more than basic HTML. Moreover, Blogger's line break "helper" apparently interferes with the old standby tricks like pre, code, and blockquote. I say apparently because either there are a lot of untested myths out there or Blogger has changed how it deals with these tags over time.

So, to get my code samples to paste in effortlessly and to set the story straight, I present below the way I learned to beat the blogger editor and it's as simple, as--wait for it--the trusty pre tag. To paste in the sample below, I simply dropped into HTML mode, pasted in the class definition, wrapped it in a pre tag and fixed up a few line breaks. In my case, I've got the Convert line breaks option (Settings --> Formatting) set to to Yes.

Definitely better than before but pre does not preserve HTML and XML fragments and Blogger simply removes them from the post. The problem here lies mainly with the HTML and XML bits that look like HTML--I'm talking mainly about less than and greater than brackets. To get around this, I simply use an HTML encoder to drop in my markup, encode it, and then copy the output into Blogger. It's a pain but what can you do...

class Base
public virtual void DoWork ()
Console.WriteLine ("Base.DoWork");


Spam comments will be deleted

Note: only a member of this blog may post a comment.