Hack 96. Blog Code from Visual Studio![]() easier to post that code on a web site or web log. Then, share your snippets with your adoring public.It is easy to grab a piece of code from your project and then turn around and write about it in an article or in a post on your web log. One tricky part of the process is getting the nice code coloring that Visual Studio provides for that code. When publishing your code to an online article or weblog, you need to convert that code to HTML and hand-code all the coloring, a tedious process. A couple of hacks make it much easier to cut and paste code directly from Visual Studio and transform it into HTML so you can easily post it to your web log. 13.6.1. Convert to HTML with a MacroCory Smith (http://addressof.com/blog) has come up with an interesting solution to this problem. He has written a macro that will take the code you have selected, copy it to Microsoft Word, and then save it from Microsoft Word in HTML. This is possible for a couple of different reasons. This works because Visual Studio lets you copy and paste the code from Visual Studio with formatting intact. So, if you copy text from Visual Studio and simply paste it into Microsoft Word, you will notice that all of the formatting and coloring comes with the text. This gets around the funky HTML that Word typically generates because Microsoft Word now allows you to save filtered HTML, which is much cleaner than before.
create and manage macros please refer to [Hack #51]. Option Explicit OnAfter you have added and saved this macro, you can then select a piece of text in the code editor and then double-click on this macro. You will see the hourglass for a couple of moments, the macro has to open Microsoft Word, which takes a few seconds. When the hourglass goes away, you will have the formatted HTML code in your clipboard. You can then go to your favorite blogging tool and paste the HTML directly into that tool. This macro works by copying the code from Word. This means that, when you paste the code, it has to be into an application that will understand the rich formatting. For instance, pasting this into the design view of a richtextbox will work perfectly; pasting into Notepad will not work. 13.6.2. Convert to HTML with an Add-inAnother way to get from code to HTML is through the use of a freely available add-in called CopySourceAsHTML. First, you will need to download and install the add-in. It can be downloaded from http://www.jtleigh.com/CopySourceAsHTML.After downloading and installing the add-in, you will see a new item on the right-click menu called Copy Source as HTML. You can select text in Visual Studio and then click this item on the right-click menu. You will then see the dialog shown in Figure 13-15. Figure 13-15. Copy Source As HTML dialog![]() are copying, either C#, VB, or HTML/XML/ASPX. You can also configure the following options: You can set whether the HTML should include line numbers and, if so, what number the add-in should start from. This option allows you to turn on line coloring, similar to what you might find in a report in which every other line is colored to make it easier to read. This option determines whether the add-in will embed its stylesheet in the document or reference an external stylesheet. If you are posting a large number of snippets, it might make sense to not include the stylesheet and instead reference one copy of it. Since HTML treats tabs as whitespace, the add-in has to convert all of those tabs to spaces; this option determines how many spaces should replace each tab. After you click the OK button, the HTML formatted text will be copied to your clipboard. You can then turn around and paste it into your blogging tool.Both of these solutions are great ways to get the color formatting of Visual Studio out of the application and onto your web log. |