Friday, September 18, 2009

Posting Code Snippets to Blogger

I want to be able to post code snippets and I found a little utility that integrates with Visual Studio 2008 called CopySourceAsHtml. This utility lets you select source code in a code window and copy it verbatim to the clipboard, which can then be pasted into the blog.

In this shot, I selected some source code, and right-clicked. I then chose Copy As HTML...



When you do this, the utility's dialog box appears allowing you to choose options. I have set options on two of the tabs.




On the File Style tab, I have added some more style settings for the  <div>  that it creates for the snippet.  The  overflow:auto  setting will add scroll bars as needed, the  white-space:nowrap  setting will turn off word-wrapping, the  height:500px  setting sets the box height to 500 pixels, and the  border:inset silver  sets the box's border style to inset with silver color.

When you press OK the HTML is placed on the clipboard and is ready to paste into the blog post.



For this to work, I also had to make a change to my Blogger template, otherwise the word-wrapping setting was ignored and was always on.



This is the final result:

  296                 else if (path.EndsWith(":"))
  297                 {
  298                     path += @".\";
  299                 }
  300                 else if (!path.EndsWith(@"\"))
  301                 {
  302                     path += @"\";
  303                 }
  304 
  305                 path = System.IO.Path.GetFullPath(path);
  306                 if (path == null)
  307                 {
  308                     throw new System.ApplicationException("GetFullPath() returned NULL unexpectedly.");
  309                 }
  310 
  311                 if (fileType == FileTypes.File)
  312                 {
  313                     // Special cases for when x.txt is truly a FileInfo, this code
  314                     // must come after the GetFullPath() call above:
  315                     //      c:\x.txt\
  316                     //          Path: c:\x.txt\
  317                     //          File: 
  318                     //      c:\x.txt\.
  319                     //          Path: c:\x.txt\
  320                     //          File: .
  321                     //      c:\x.txt\.\.\.\\\.\.\..\x.txt\\\\\.\.\.\..\x.txt\.\
  322                     //          Path: c:\x.txt\
  323                     //          File: 
  324                     //      c:\x.txt\.\.\.\\\.\.\..\x.txt\\\\\.\.\.\..\x.txt\.\.
  325                     //          Path: c:\x.txt\
  326                     //          File: .
  327                     //      c:\x.txt\.\.\.\\\.\.\..\x.txt\\\\\.\.\.\..\x.txt\.\.\\\
  328                     //          Path: c:\x.txt\
  329                     //          File: .
  330                     if (((fileName == string.Empty) || (fileName == ".")) &&
  331                         path.EndsWith(@"\"))
  332                     {
  333                         fileSpec = path.Substring(0, path.Length - 1);  // Make the new filespec be the earlier resolved Path minus the trailing \
  334                         path = System.IO.Path.GetDirectoryName(fileSpec);
  335 
  336                         if (path == null)
  337                         {

One thing I note is that you cannot easily copy text out of this window, because it doesn't have normal line ends. I am looking into how to remedy that. I do plan to post downloadable code when I post snippets. The other thing is that this is displaying with a Courier font, rather than Consolas as in Visual Studio.

No comments:

Post a Comment