Wednesday, October 3, 2007

Finally: A Code Snippet Plugin that Works!

 

I am new to blogging and am using Windows Live Writer (beta) and am really liking it. However, I did have an initial stumbling block: I could not easily post source code and get it to display properly.

I downloaded the Code Snippet Plugin for Windows Live Writer authored by Leo Vildosola and here is the best result I could obtain:

 

using System;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.ViewInfo;
 
namespace CustomSimpleButton {
 
 public class CustomButton : SimpleButton {
 
 public CustomButton() {
 this.TabStop = this.AllowFocus = false;
 }
 
 CustomButtonViewInfo m_customButtonViewInfo;
 
 protected override BaseStyleControlViewInfo CreateViewInfo() {
 if (m_customButtonViewInfo == null)
 m_customButtonViewInfo = new CustomButtonViewInfo(this);
 return m_customButtonViewInfo;
 }
 }
}

 

If it displays on this web page for you, the same as it does for me, all of my lines of code are shifted or justified left (it's hard to read) and it does not use the colors or formatting that I use in Visual Studio. It seems like a potentially decent plugin with some unusable flaws for me; although, other users do not seem to have the same justification problem I am experiencing. I wrote the author about my justification problem and have not received a response, so I moved on to look for something better...

 

I tried every plugin I could find for WLW ( Windows Live Writer ) and I could tire you by demonstrating that none of them would display properly. Either they lost the basic formatting similar to above, or they used hard-coded color schemes that were offensive to me, or something just not acceptable happened with each plugin I tried... and I tried every one available for download (approx. 5 to 8).

 

I did, however, find a clear winner and it was not a plugin for Windows Live Writer, it is a plugin for Visual Studio! It uses the formatting settings that I use in Visual Studio and is very simple to use. You tell me, does this second sample appear better to you?

 

Code Snippet

using System;

using DevExpress.XtraEditors;

using DevExpress.XtraEditors.ViewInfo;

 

namespace CustomSimpleButton {

 

    public class CustomButton : SimpleButton {

 

        public CustomButton() {

            this.TabStop = this.AllowFocus = false;

        }

 

        CustomButtonViewInfo m_customButtonViewInfo;

 

        protected override BaseStyleControlViewInfo CreateViewInfo() {

            if (m_customButtonViewInfo == null)

                m_customButtonViewInfo = new CustomButtonViewInfo(this);

            return m_customButtonViewInfo;

        }

    }

}

 

What is this magical plugin? It is CopySourceAsHTML written by Colin Coller and released by J.T. Leigh & Associates Inc.

Note: the Code Snippet box is my own wrapper and is not created as part of the plugin: the plugin just auto-creates wonderfully formatted code for the web.

The steps I take are simple:

I insert my Code Snippet template in Live Writer, I switch to Visual Studio and highlight my code, then I right-click and select CopyAsHTML from the context menu. I then switch back to Live Writer and place my mouse cursor in the Code Snippet box. The final step is choose Edit, Paste Special, and select the option, Keep Formatting. Wa La - it's done!

All of this is done with an easy to use WYSIWYG blog post editor: Windows Live Writer (beta) and the assistance of a very helpful plugin.

2 comments:

Anonymous said...

Thanks for the info trevor. If and when I start blogging this will be a great help!

Leo Vildosola said...

I'm happy to see you found a plugin that works for you. I stumbled across your post while reviewing my site stats.

I did not recall receiving any e-mails with these issues. Although, the way things have been going laterly it would not surprise me that I may have over looked it. I apologize for not appearing responsive.

I am not aware of indentation issues but I would be happy to work with you to figure them out.

As for the highlighting, the plugin can use your own CSS so that you can get it to appear closer to your VS syntax highliting. I just have not spent the time to do so.