Wednesday, August 22, 2007 12:40 AM
The How
You should already have Visual Studio 2005, Visual Studio 2008 Beta 2, and CopySourceAsHtml 2.0.0 installed (the version for VS 2K+5).
- Shut down any Visual Studio instances you may have running.
- Open file explorer and open the folder called “My Documents\Visual Studio 2005\AddIns” (or your relevant location).
- Copy the following files (installed as part of CopySourceAsHtml 2.0.0):
- CopySourceAsHtml.AddIn
- CopySourceAsHtml.dll
- CopySourceAsHtml.dll.config
- CopySourceAsHtml.pdb
- Create or Open the Directory “My Documents\Visual Studio 2008\AddIns”
- Paste the 4 files you copied above into “My Documents\Visual Studio 2008\AddIns”
- Edit (e.g., Notepad) the file
CopySourceAsHtml.AddIn and make the changes highlighted in the following listing:
<?xml version="1.0" encoding="UTF-16" standalone="no"?>
<Extensibility xmlns="http://schemas.microsoft.com/AutomationExtensibility">
<HostApplication>
<Name>Microsoft Visual Studio Macros</Name>
<Version>9.0</Version>
</HostApplication>
<HostApplication>
<Name>Microsoft Visual Studio</Name>
<Version>9.0</Version>
</HostApplication>
<Addin>
- Start up Visual Studio 2008 Beta 2 and copy source code snippets like a ninja!
The Why
I’m still in the process of fine tuning the toolset I use when blogging. I wrote a technical post the other day (Linq Line by Line – Part 1) and I found the process of working in SubText’s WYSIWYG editor not as fast as I would have liked. No fault of SubText. WYSIWYG editors are just like that (to me anyway). I resorted to typing the blog in notepad2 and then translating it into HTML.
The code examples were particularily painful. I found some excellent code to HTML converters. My favorite is C# Code Format at manoli.net. However, it was still a pain to copy the source out of Visual Studio, paste them into C# formatter translate the style sheet CSS rules into embedded style tags (for the benefit of feed readers), and paste the resulting HTML into the source view of SubText’s WYSIWYG.
The What
Then I found the tool. CopySourceAsHtml or CSAH is an an addin for Visual Studio (standard edition or better). You simply highlight the code you want, right-click, and choose the menu item “Copy As Html…” menu item. CSAH produces HTML and CSS so close to what you see in Visual Studio you will at first glance think it’s a screen shot.
Any weird, snazy, cool settings you may have for your editor will be represented in plain old HTML and CSS. Better still, anything that Visual Studio knows how to highlight (XML, .config, ASP.NET, etc…) can now be copied into code snippets as is. Check out the code below complete with line numbers and the new keywords for LINQ highlighted in “Vibrant Ink” like formatting.
9 class Program
10 {
11 static void Main(string[] args)
12 {
13 var processes =
14 from p in Process.GetProcesses()
15 where p.ProcessName.StartsWith("s")
16 orderby p.ProcessName
17 select p.ProcessName;
18
19 foreach (var p in processes)
20 {
21 Console.WriteLine(p);
22 }
23 }
The What Else
What could possibly make styling and copying code snippets for blog consumption anymore efficient? Assign keyboard shortcuts to CSAH. If you go to Tools > Customize… > Keyboard… and search for “copysource”, you will see two options: Copy and Copy Now. Copy brings up the Add-in dialog, Copy Now uses your last settings.
Hats off to you Colin Coller (author of CSAH). You nailed it.
Tags: CSAH, Visual Studio, VibrantInk