Copy Code in HTML format with Visual Studio 2010

Copy Code in HTML format with Visual Studio 2010

Rate This
  • Comments 23

Today Jason has announced the Visual Studio 2010 Productivity Power Tools – a set of VS 2010 extensions that we released to complement and enhance the built-in 2010 features. You can either install the Pro Power Tools from here or just go to the Tools –> Extension Manager and type "Pro Power Tools" in the search box:

image

For the Pro Power Tools, I wrote a feature to copy formatted source code to clipboard in HTML format. The traditional VS editor only copies the plain text and RTF formats to clipboard, and there are numerous tools available to convert RTF to HTML. With the Pro Power Tools installed, pressing Ctrl+C or otherwise copying the code editor selection to clipboard, it will place the formatted and colorized HTML fragment on clipboard, alongsize the plain text and RTF format.

Then you can paste the code anywhere HTML is supported, such as Windows Live Writer:

image

Ctrl+Shift+V (Paste Special), Alt+K (Keep Formatting) and Enter to insert the colorized code to Live Writer.

In a future version of the power tools I will introduce customization – you will be able to fully control what HTML gets generated:

  1. Prepend any HTML to the code fragment
  2. Append any HTML to the code fragment
  3. Prepend any HTML before every line of code
  4. Append any HTML after every line of code
  5. Include line numbers and the format string for them
  6. Replace the space with  
  7. Replace the carriage return line feed with <br />
  8. Emit <span style="color: blue">void</span> vs. <span class="keyword">void</span>

Also, the current version has a bug: Cut (Ctrl+X) doesn't place formatted HTML to clipboard, only Copy does. I forgot to hook up the Edit.Cut command and never got around to fixing it. I'll fix it for the next release though.

Hope you like it, and as always, your feedback is appreciated!

P.S. Here's the sample code formatted using the extension:

        private static string IntersperseLineBreaks(string text)
        {
            text = text.Replace("\n\r", "\n \r");
            return text;
        }
You can View Source on this page, look for IntersperseLineBreaks and view the HTML that I generate. In a future release you will be able to customize the output of the tool, but for now this is what it defaults to.
  • Great Kirill. Fantastic job... :) I'll use it a lot in my blog - www.leniel.net.

  • Thanks Leniel ;)

  • Shouldn't there be some kind of credit to Colin Coller here?  Unsung hero, these things matter.  Link: copysourceashtml.codeplex.com

  • May I suggest the ultimate productivity enhancement that I have submitted to the visual studio dream wish queue for years now? Let's say you have "foreach(int i in y) {}". Now pretend the implementation between the {} is really long - like more than a page. Wouldn't it be nirvana if mousing over the closing } showed a tooltip of "foreach(int i in y)" so you knew just what the } was actually closing without having to scroll up, adding a guideline, or whatever? This must be like splitting the atom, it never makes the short list. Color me "still dreaming". - Jerry Nixon

  • May I suggest the ultimate productivity enhancement that I have submitted to the visual studio dream wish queue for years now? Let's say you have "foreach(int i in y) {}". Now pretend the implementation between the {} is really long - like more than a page. Wouldn't it be nirvana if mousing over the closing } showed a tooltip of "foreach(int i in y)" so you knew just what the } was actually closing without having to scroll up, adding a guideline, or whatever? This must be like splitting the atom, it never makes the short list. Color me "still dreaming".

  • Not exactly about this VS extension, but related anyways. I got everything working ok until i should do "paste special" -> "keep formatting". I'm using live writer to do this, and there is no option for "keep formatting", only the first two options. Anyone have any ideas?

  • juarola - can you please shoot me an e-mail through my blog Contact page so I can reply to you personally and help you investigate this issue?

  • Jerry - absolutely, we've taken note and will try to implement something in the future. Unfortunately this is low priority, so I can't promise we'll get to it soon, but it's definitely in the list.

  • nobugs - absolutely, several people have done a fantastic job before, so I'm not even trying to claim anything. Huge kudos to all those folks, of course.

  • Why do I have to paste into an application "where HTML is supported" like Windows Live Writer?

    Can't we just get a menu option "copy as HTML" and then be able to paste into notepad or anything else?

  • Thank you very much. This is a wonderful feature. This feature saved me from using thrid party java script based code formatting scripts, which makes the sites awfully slow

  • Great! For a future version you could consider the option to copy the code formatted with BB Code as well. :-)

  • I would also very much like an option that enables pasting the HTML in notepad for example

  • Nice, but would be even better if it copied the background color as well.  I use a dark theme and it fails to set the background color on the pre tag.  Not a big deal, I can do it manually but would be nice if it took that in to account.

  • Does not work under my installed VS2010 Ultimate. Tried uninstalled and then reinstalling. No matter I copy code from the IDE and paste into notepad, HomeSite, WeBuilder and simply get the text uncolorized except in MS-Word I get the colorization as without the "Copy To HTML" Will check back in your next release. Any ways thanks for all your efforts!

    kevindotsdotgallagheratdordotstatedotordotus

Page 1 of 2 (23 items) 12
Leave a Comment
  • Please add 3 and 5 and type the answer here:
  • Post