Getting formatted code onto the web

This post is mostly for me as I recently switched machines and forgot the steps and software needed to get nicely formatted code into my blog:

  1. Download CopySourceAsHtml (as recommended by Hanselman)
  2. Extract the files from the MSI using Less MSIérables as the installer doesn't work with VS 2008.
  3. Copy the files to the VS 2008 plugin directory as per these instructions.
  4. Open Visual Studio Enable the plugin from Tools > Add-in Manager
  5. Select text and choose "Copy AS HTML" from the context menu.
  6. Right click in windows live writer and select "Paste Special". Choose "Keep Formatting" from the dialog.
  7. 1 Console.WriteLine("Beautifully formatted Code!");

    I almost didn't bother trying this tool as I didn't like the way Visual Studio put boxes around blocks of SQL code.  I forgot of course that these boxes couldn't be carried over the HTML even if I'd wanted them too.  The SQL looks just as nice as it does in management studio:

        1 -- Comment: Comment goes here

        2 SELECT * from Products

    The only problem I am finding is that code quite quickly gets too wide and wraps messing up the line numbers. That is probably more to do with my blog template.

    Update: I have just come across a neat way of solving the wrapping problem. 

    1. Uncheck wrap words in the copy as html dialog.
    2. Wrap my code in overflow divs:
    <DIV style="OVERFLOW: auto">

    I can now have nice scrollbars:

       11 [Test]
       12         public void ShouldRetrieveSpeakerByEmail()
       13         {
       14             Conference anConference = new Conference("tea party", "");
       15             using (ISession session = getSession())
       16             {
       17                 session.SaveOrUpdate(anConference);
       18                 session.Flush();
       19             }
       20             string email = "brownie@brownie.com.au";
       21             Speaker speaker =
       22                 new Speaker("Andrew", "Browne", "http://blog.brownie.com.au", "the comment", anConference,
       23                             email, "http://blog.brownie.com.au/avatar.jpg", "Info about how important I am to go here.","password", "salt");
       24 
       25             ISpeakerRepository repository = new SpeakerRepository(_sessionBuilder);
       26             repository.Save(speaker);
       27 
       28             Speaker rehydratedSpeaker = null;
       29             //get Attendee back from database to ensure it was saved correctly
       30             using (ISession session = getSession())
       31             {
       32                 rehydratedSpeaker = repository.GetSpeakerByEmail(email);
       33 
       34                 Assert.That(rehydratedSpeaker != null);
       35                 Assert.That(rehydratedSpeaker.Contact.FirstName, Is.EqualTo("Andrew"));
       36                 Assert.That(rehydratedSpeaker.Website, Is.EqualTo("http://blog.brownie.com.au"));
       37                 Assert.That(rehydratedSpeaker.Comment, Is.EqualTo("the comment"));
       38                 Assert.That(rehydratedSpeaker.Conference, Is.EqualTo(anConference));
       39             }
       40         }
    Print | posted on Saturday, January 05, 2008 7:54 PM
    Comments have been closed on this topic.