CopySourceAsHtml (Updated)
I've released CopySourceAsHtml version 1.2.0 with changes suggested by Scott Galloway, Phil Haack, Rick Lobrecht, Geoff Appleby, and others.
CopySourceAsHtml 1.2.0 Installer (283 KB) (built and tested on VS.NET 2003)
CopySourceAsHtml 1.2.0 Source (32 KB)
Update: I forgot to say anything about how to use CSAH. See this post and this post for background.
The biggest change in this release is that CSAH uses VS.NET's syntax highlighting and VS.NET's font and color settings automatically. If VS.NET can highlight it, CSAH can copy it, and your code should look the same in your browser as it does in your editor. Here are some examples:
1 <%@ Page Language="c#" Inherits="WebApplication1.WebForm1" AutoEventWireup="false" %>
2 <html>
3 <body>
4 <a href="<%=Something%>" target="_new">Something</a>
5 </body>
6 </html>
1 BODY
2 {
3 padding: 5px;
4 overflow: auto;
5 width: 95%;
6 max-height: 450px;
7 margin: 0px auto;
8 line-height: 125%;
9 border: silver 2px dotted;
10 }
12 protected void ParseRtf( string rtf )
13 {
14
15 int first;
16 int last;
17 int index;
18 int mark;
19 string controlWord;
20 bool hasParameter;
21 long parameter;
22
23 first = 1;
24 last = rtf.Length - 1;
25 for ( index = first ; index < last ; index++ )
26 {
27
28 switch ( rtf[index] )
29 {
30 case '{':
31 index += ParseOpeningBrace( rtf , index + 1 );
32 break;
33 case '}':
34 index += ParseClosingBrace( rtf , index + 1 );
35 break;
36 case '\\':
37 index++;
38 if ( char.IsLetter( rtf[index] ) )
39 {
40 mark = index;
41 while ( ( index < last ) && ( char.IsLetter( rtf[index] ) ) )
42 {
43 index++;
44 }
45 controlWord = rtf.Substring( mark , index - mark );
46 if ( ( index < last ) && ( ( char.IsDigit( rtf[index] ) ) || ( rtf[index] == '-' ) ) )
47 {
48 mark = index;
49 while ( ( index < last ) && ( ( char.IsDigit( rtf[index] ) ) || ( rtf[index] == '-' ) ) )
50 {
51 index++;
52 }
53 hasParameter = true;
54 parameter = Int64.Parse( rtf.Substring( mark , index - mark ) );
55 }
56 else
57 {
58 hasParameter = false;
59 parameter = 0;
60 }
61 if ( rtf[index] != ' ' )
62 {
63 index--;
64 }
65 index += ParseControlWord( rtf , index + 1 , controlWord , hasParameter , parameter );
66 }
67 else
68 {
69 index += ParseControlSymbol( rtf , index + 1 , rtf[index] );
70 }
71 break;
72 case '\r':
73 case '\n':
74 break;
75 default:
76 index += ParseLiteralText( rtf , index + 1 , rtf[index] );
77 break;
78 }
79 }
80 }
Other changes:
- You can turn word wrapping on and off. If "wrap words" is checked, lines are rendered in <p> blocks. Otherwise, lines are rendered in <pre> blocks. Check this if your blog layout isn't wide enough for your code.
- You can strip line breaks from the generated HTML. Handy if your blog software converts newlines automatically.
- You can add additional RSS rules to the file, line, and block styles. Use this to add borders, scroll bars, etc. to your code.
- You can embed styles or use a stylesheet. If "embed styles" is checked, tags have style attributes. If "embed styles" is unchecked, tags have class attributes, and the generated HTML includes a style block.
- You can change which menu items are added to the context menus from within the add-in.
- The generated HTML is copied to the clipboard in text and CF_HTML formats.
- The code is cleaner and more modular, and the object model should make it easy to add new features in future versions.
- There's an "About" tab with the version, copyright, license, my contact information, and my Amazon.com wish list (just kidding :).
If you find CSAH useful, find a bug, have a suggestion, or want to review or contribute code, please let me know. I'd really like it if someone reviewed the code, actually, and helped me improve on it.
Cheers,
Colin
08:32 PM | Colin

Comments
# RE: CopySourceAsHtml (Updated)
Hey Colin, one issue I've found is that if you embed multiple times in the same HTML page, you get funky results.
The problem is that the css classes are not idempotent. In one Copy As Html operation, a particular CSS class means something different than in another.
See http://haacked.com/archive/2004/11.aspx as an example. If you scroll down, you'll see how my first posting is mussed up by the second.
THe fix for now is to always embed the css.
05:20 PM | Haacked
# RE: CopySourceAsHtml (Updated)
Phil: That's a limitation of the aproach I took to syntax highlighting. The "paste into Word and save as HTML" approaches have the same limitation, though they don't give you the option to generate a stylesheet, so you can't really tell. I'm going to try working around it in the next version.
07:00 PM | Colin
# RE: CopySourceAsHtml (Updated)
Perfect!
03:44 PM | Mikael Söderström
# RE: CopySourceAsHtml (Updated)
Cheers!
11:35 PM | Daniel Moth
# RE: CopySourceAsHtml (Updated)
I've updated CopySourceAsHtml since the last comment was posted. See:
http://www.jtleigh.com/people/colin/blog/archives/2004/11/copysourceashtm_2.html
11:12 PM | Colin
# RE: CopySourceAsHtml (Updated)
Excellent work.
Trackback is:
http://analystdeveloper.com/blogs/gurkan/archive/2005/03/13/184.aspx
04:14 PM | Gurkan Yeniceri
# RE: CopySourceAsHtml (Updated)
Any chance you will be porting this to VS 2005 now that it is RTM?
I miss it terribly!
Bob
09:25 PM | Robert Porter