Home
Manage Your Code
Snippet: FormatCodeToHtml (VB.NET)
Title: FormatCodeToHtml Language: VB.NET
Description: Jeff Atwood's Code to html formatter Views: 136
Author: Brandon Dimperio Date Added: 3/10/2010
Copy Code  
1Option Explicit On
2Option Strict On
3
4Imports System
5Imports EnvDTE
6Imports System.Diagnostics
7Imports System.Windows.Forms
8Imports System.Text.RegularExpressions
9
10''' <summary>

11''' copies the selected code to the clipboard in HTML format using 

12''' a relatively simple RTF to HTML Regex conversion

13''' </summary>

14''' <remarks>

15''' Jeff Atwood

16''' http://www.codinghorror.com

17''' Version 04/07/2006

18''' </remarks>

19Public Module FormatToHtml
20
21    Private Const _CodeFontName As String = "Monospace"
22    Private Const _CodeFontSize As String = "10pt"
23    Private Const _CodeFontLegacySize As Integer = -1
24    Private Const _CodeBackgroundColor As String = "white"
25    Private Const _CodeForegroundColor As String = "Black"
26    Private Const _TabSpaces As Integer = 4
27    Private Const _RemoveExcessIndentation As Boolean = True
28    Private _n As String = Environment.NewLine
29
30    ''' <description>

31    ''' format RTF to modern HTML

32    ''' </description>

33    Public Sub Modern()
34        PerformRtfConversion(False, True)
35    End Sub
36
37    ''' <description>

38    ''' format RTF to modern HTML, leaving only plain text in the clipboard

39    ''' </description>

40    Public Sub ModernText()
41        PerformRtfConversion(True, True)
42    End Sub
43
44    ''' <description>

45    ''' format RTF to legacy HTML

46    ''' </description>

47    Public Sub Legacy()
48        PerformRtfConversion(False, False)
49    End Sub
50
51    ''' <description>

52    ''' format RTF to legacy HTML, leaving only plain text in the clipboard

53    ''' </description>

54    Public Sub LegacyText()
55        PerformRtfConversion(True, False)
56    End Sub
57
58    Private Sub PerformRtfConversion(ByVal asTextOnly As Boolean, ByVal useModernHtml As Boolean)
59        If Not SelectionToClipboard() Then Return
60
61        '-- retrieve text and rtf from clipboard

62        GetClipboard(DataFormats.Text)
63        Dim text As String = Convert.ToString(_ClipboardObj)
64        GetClipboard(DataFormats.Rtf)
65        Dim rtf As String = Convert.ToString(_ClipboardObj)
66
67        If _RemoveExcessIndentation Then text = RemoveTextIndents(text)
68
69        Dim html As String = RtfToHtml(rtf, useModernHtml)
70        If Not useModernHtml Then
71            html = ConvertSpanToFont(html)
72        End If
73
74        If asTextOnly Then
75            SetClipboardHtml(html, html)
76        Else
77            SetClipboardHtml(html, text)
78        End If
79
80    End Sub
81
82    Private Function RemoveTextIndents(ByVal text As String) As String
83        Dim min As Integer = GetMinimumTextIndent(text)
84        If min = 0 Then
85            Return text
86        Else
87            Return Regex.Replace(text, "^\s{" & min & "}", "", RegexOptions.Multiline)
88        End If
89    End Function
90#Region "  Clipboard"   ...
Usage
import this file as a macro (alt+f11 to get to macro explorer).
then highlight the code and use macro explorer to activate it
Notes
Jeff Atwood Version 04/07/2006 http://www.codinghorror.com/blog/archives/000429.html