Blogging code samples stays easy [Update to free ConvertClipboardRtfToHtmlText tool and source code for Visual Studio 2010!]
A big part of my blog is sharing code and so most of the posts I write include sample source. Therefore, it's pretty important to me that the code I share be easy for readers to understand and use. For me, that means I want it to be static, syntax-highlighted, and in text form so it's copy+paste-able and indexable by search engines.
I'm a big fan of keeping things simple and avoiding dependencies, so I ended up writing a very simple tool about two years ago called ConvertClipboardRtfToHtmlText
. As you can see from the original ConvertClipboardRtfToHtmlText post and the subsequent follow-up, it's a very simple tool intended for a very specific scenario. That said, it works beautifully for my purposes and I've used it to blog every snippet of source code since then!
So I was surprised and a little disappointed when it stopped working recently... Why? Because I switched to Visual Studio 2010 (Beta 2) and they've changed the RTF clipboard format slightly with that release. Now, while I'm sure VS 2010's RTF is still 100% legal RTF, it's different enough from VS 2008's output that ConvertClipboardRtfToHtmlText
chokes on it. Clearly, it was time to dust off the source code and make it work again!
Not surprisingly, the update process was quite painless - and by tweaking the code slightly, I've arrived at an implementation that works well for both versions of Visual Studio: 2008 and 2010! The source code download includes a VS 2010 solution and project, but takes advantage of the multi-targeting capabilities of Visual Studio to compile for the .NET 2.0 Framework, ensuring that the resulting executable runs pretty much everywhere.
As long as I was touching the code, I added the following simple banner text:
ConvertClipboardRtfToHtmlText Version 2009-12-19 - http://blogs.msdn.com/delay/ Converts the Visual Studio 2008/2010 RTF clipboard format to HTML by replacing the RTF clipboard contents with its HTML representation in text form. Instructions for use: 1. Copy syntax-highlighted text to the clipboard in Visual Studio 2. Run ConvertClipboardRtfToHtmlText (which has no UI and exits immediately) 3. Paste HTML text into an editor, web page, blog post, etc.
So if you're in the market for a nice way to blog code and you're using Visual Studio 2008 or 2010, maybe ConvertClipboardRtfToHtmlText
can help you out!
[Click here to download the ConvertClipboardRtfToHtmlText tool along with its complete source code.]
Here's the complete source code for ConvertClipboardRtfToHtmlText
, provided by - you guessed it! - ConvertClipboardRtfToHtmlText
:
using System; using System.Collections.Generic; using System.Drawing; using System.Text; using System.Windows.Forms; // NOTE: This is NOT a general-purpose RTF-to-HTML converter! It works well // enough on the input I've tried, but may break in other scenarios. // TODO: Convert into a real application with a notify icon and hotkey. namespace ConvertClipboardRtfToHtmlText { static class ConvertClipboardRtfToHtmlText { private const string colorTbl = "\\colortbl;"; private const string colorFieldTag = "cf"; private const string tabExpansion = " "; [STAThread] static void Main() { Console.WriteLine("ConvertClipboardRtfToHtmlText"); Console.WriteLine("Version 2009-12-19 - http://blogs.msdn.com/delay/"); Console.WriteLine(); Console.WriteLine("Converts the Visual Studio 2008/2010 RTF clipboard format to HTML by replacing"); Console.WriteLine("the RTF clipboard contents with its HTML representation in text form."); Console.WriteLine(); Console.WriteLine("Instructions for use:"); Console.WriteLine("1. Copy syntax-highlighted text to the clipboard in Visual Studio"); Console.WriteLine("2. Run ConvertClipboardRtfToHtmlText (which has no UI and exits immediately)"); Console.WriteLine("3. Paste HTML text into an editor, web page, blog post, etc."); if (Clipboard.ContainsText(TextDataFormat.Rtf)) { // Create color table, populate with default color List<Color> colors = new List<Color>(); Color defaultColor = Color.FromArgb(0, 0, 0); colors.Add(defaultColor); // Get RTF string rtf = Clipboard.GetText(TextDataFormat.Rtf); // Strip meaningless "\r\n" pairs (used by VS 2008) rtf = rtf.Replace("\r\n", ""); // Parse color table int i = rtf.IndexOf(colorTbl); if (-1 != i) { i += colorTbl.Length; while ((i < rtf.Length) && ('}' != rtf[i])) { // Add color to color table SkipExpectedText(rtf, ref i, "\\red"); byte red = (byte)ParseNumericField(rtf, ref i); SkipExpectedText(rtf, ref i, "\\green"); byte green = (byte)ParseNumericField(rtf, ref i); SkipExpectedText(rtf, ref i, "\\blue"); byte blue = (byte)ParseNumericField(rtf, ref i); colors.Add(Color.FromArgb(red, green, blue)); SkipExpectedText(rtf, ref i, ";"); } } else { throw new NotSupportedException("Missing/unknown colorTbl."); } // Find start of text and parse i = rtf.IndexOf("\\fs"); if (-1 != i) { // Skip font size tag while ((i < rtf.Length) && (' ' != rtf[i])) { i++; } i++; // Begin building HTML text StringBuilder sb = new StringBuilder(); sb.AppendFormat("<pre><span style='color:#{0:x2}{1:x2}{2:x2}'>", defaultColor.R, defaultColor.G, defaultColor.B); while (i < rtf.Length) { if ('\\' == rtf[i]) { // Parse escape code i++; if ((i < rtf.Length) && (('{' == rtf[i]) || ('}' == rtf[i]) || ('\\' == rtf[i]))) { // Escaped '{' or '}' or '\' sb.Append(rtf[i]); } else { // Parse tag int tagEnd = rtf.IndexOf(' ', i); if (-1 != tagEnd) { if (rtf.Substring(i, tagEnd - i).StartsWith(colorFieldTag)) { // Parse color field tag i += colorFieldTag.Length; int colorIndex = ParseNumericField(rtf, ref i); if ((colorIndex < 0) || (colors.Count <= colorIndex)) { throw new NotSupportedException("Bad color index."); } // Change to new color sb.AppendFormat( "</span><span style='color:#{0:x2}{1:x2}{2:x2}'>", colors[colorIndex].R, colors[colorIndex].G, colors[colorIndex].B); } else if ("tab" == rtf.Substring(i, tagEnd - i)) { sb.Append(tabExpansion); } else if ("par" == rtf.Substring(i, tagEnd - i)) { sb.Append("\r\n"); } // Skip tag i = tagEnd; } else { throw new NotSupportedException("Malformed tag."); } } } else if ('}' == rtf[i]) { // Terminal curly; done break; } else { // Normal character; HTML-escape '<', '>', and '&' switch (rtf[i]) { case '<': sb.Append("<"); break; case '>': sb.Append(">"); break; case '&': sb.Append("&"); break; default: sb.Append(rtf[i]); break; } } i++; } // Trim any trailing empty lines while ((2 <= sb.Length) && ('\r' == sb[sb.Length - 2]) && ('\n' == sb[sb.Length - 1])) { sb.Length -= 2; } // Finish building HTML text sb.Append("</span></pre>"); // Update the clipboard text Clipboard.SetText(sb.ToString()); } else { throw new NotSupportedException("Missing text section."); } } } // Skip the specified text private static void SkipExpectedText(string s, ref int i, string text) { foreach (char c in text) { if ((s.Length <= i) || (c != s[i])) { throw new NotSupportedException("Expected text missing."); } i++; } } // Parse a numeric field private static int ParseNumericField(string s, ref int i) { int value = 0; while ((i < s.Length) && char.IsDigit(s[i])) { value *= 10; value += s[i] - '0'; i++; } return value; } } }