Formatting source code in a blog post

For some time now I'm using Word 2007 to write my blog posts. Which works quite well, except when it comes to source code. This is a public experiment.

This source was inserted with using a straigth copy.

[Test]

public void IsDeceased()

{

VecozoLib.ServiceWrapper sw = new ServiceWrapper();

VecozoLib.VecozoService.RetourinfoCovType data = sw.PersonData(DateTime.Parse("1-5-1945"), 141919723);

Assert.AreEqual(VecozoLib.ResultaatRaadpleging.PersoonIsOverleden, (VecozoLib.ResultaatRaadpleging) data.ResultaatcodeCOV);

}

Looks great in Word, but not that good online. The line spacing is far to great and all the indentation is gone.

To compare, this is the same using CopySourceAsHtml to do the copy.

[Test]

public void IsDeceased()

{

   VecozoLib.ServiceWrapper sw = new ServiceWrapper();

   VecozoLib.VecozoService.RetourinfoCovType data = sw.PersonData(DateTime.Parse("1-5-1945"), 141919723);

   Assert.AreEqual(VecozoLib.ResultaatRaadpleging.PersoonIsOverleden, (VecozoLib.ResultaatRaadpleging) data.ResultaatcodeCOV);

}

Which does not look that good in Word, but looks better on line. The line spacing is still too big, but the indentation is preserved.

And this is CopySourceAsHtml with linebreaks stripped

[Test]



public



void IsDeceased()



{



   VecozoLib.ServiceWrapper sw = new



ServiceWrapper();



   VecozoLib.VecozoService.RetourinfoCovType data = sw.PersonData(DateTime.Parse("1-5-1945"), 141919723);



   Assert.AreEqual(VecozoLib.ResultaatRaadpleging.PersoonIsOverleden, (VecozoLib.ResultaatRaadpleging) data.ResultaatcodeCOV);



}



Which is a mess.

Brendan pointed me to dp.SyntaxHighlighter. Since then this post keeps getting updated.

This source code formatter requires the syntaxhighligher scriptlibrary, which is available on Codebetter. For this tool to work you have to enclose the source code in a pair of tags.

<textarea name="code" class="c#" cols="60" rows="10">   

… source code here …   

</textarea>   

This is difficult to do in Word 2007, as you cannot directly edit the html it publishes. Just like images this requires some fiddling by hand in the HTML in the blog dashboard. I paste the source as plain text, and do the HTML fiddling in the dashboard.

Due to the "inteligence" built into the CS rich text control this can be quite a job. But as you see the result can be nice.

Word 2007 is a great blogwriter. But the downside is that it's not that good with source code and there is no way (I know) to edit specific HTML. CopyAsHtml can come in handy.

This entry was posted in Uncategorized. Bookmark the permalink. Follow any comments here with the RSS feed for this post.
  • http://iskbank.com iskbank.com

    Pingback from iskbank.com
    Eve Isk, Eve Online

  • http://starksolutions.blogspot.com/ Start Solutions

    Cool I will implement this @ my blog Stark Solutions

  • http://www.andrewstark.com Andrew Stark

    This is just what I was looking for to enter html code as part of a tutorial I wrote on my blog.

    Thanks a lot

    Andrew

  • http://codebetter.com/blogs/peter.van.ooijen/ pvanooijen

    That’s looking good !
    How did you remove the html encoding ? By hand in the “blog-dashboard” ? That’s quite an amount of work.
    I absolutely admit this is the nicest result. Now I just have to find a way to combine it with Word 2007. To me a big reason for using an office app is the great spelling and grammar support.

  • btompkins

    Peter,

    I removed the HTML encoding of your source code within the Textareas, and it looks pretty good to me….!

  • btompkins

    Peter, why don’t you try using the dp.SyntaxHighlighter (we have it installed you know :) ?

    http://www.dreamprojections.com/SyntaxHighlighter/