Imports system.Text.RegularExpressions
Imports System.IO

Module ConvertCodetoHTML

	''' 
	''' a console based code to html converter
	''' 
	''' command line args
	''' 
	Sub Main(ByVal args() As String)
		If (args.Length < 1) OrElse (args.Length > 3) Then
			Console.WriteLine("Usage:")
			Console.WriteLine("ConvertVBtoHTML SourceFile [DestinationFile] [KeyWordFile]")
			Console.WriteLine("SourceFile := Name of visual basic source code file to convert.")
			Console.WriteLine("DestinationFile := Name of HTML file to which results are written")
			Console.WriteLine("If omitted, then output file has the same name as the inputfile with "".html"" appended")
			Console.WriteLine("KeyWordFile := Name of the XML file from which keywords are read")
			Console.WriteLine("If omitted, then KeyWordFile = keywords.xml")

			Exit Sub
		Else
			Dim strMatch As String
			Dim strReplace As String

			Dim filein As String = args(0)
			Dim fileout As String = filein & ".html"
			Dim keyfile As String = My.Application.CurrentDirectory & "\keywords.xml"

			Select Case args.Length
				Case 1
					'no action
				Case 2
					fileout = args(1)
				Case 3
					fileout = args(1) 'vb doesn't fall through case/select
					keyfile = args(2)
			End Select

			'verify infile exists
			If (Not IO.File.Exists(filein)) Then
				Console.WriteLine("InFile not found")
				Exit Sub
			End If
			Dim strworking As String = New StreamReader(filein).ReadToEnd()

			'verify keyword file exists
			If (Not IO.File.Exists(keyfile)) Then
				Console.WriteLine("KeyWordFile not found")
				Exit Sub
			End If

			'&
			strMatch = "&"
			strReplace = "&"
			strworking = RegExReplace(strworking, strMatch, strReplace, -1)

			'<
			strMatch = "\<"
			strReplace = "<"
			strworking = RegExReplace(strworking, strMatch, strReplace, -1)

			'>
			strMatch = "\>"
			strReplace = ">"
			strworking = RegExReplace(strworking, strMatch, strReplace, -1)

			'comments
			'strMatch = "\s'.*"
			'strReplace = "$&"
			strMatch = "(?([ \t][^""]\s*'+[\S ]*))"
			strReplace = ""color:#006400;"">${comment}"
			strworking = RegExReplace(strworking, strMatch, strReplace, -1)

			'summary tags
			strMatch = "</?((summary)|(param.*)|(remarks)|(returns))>"
			strReplace = ""color:#A52A2A;"">$&"
			strworking = RegExReplace(strworking, strMatch, strReplace, -1)

			'test to make sure not picking up internal '
			Dim gigo As String = "blah 'batshit' blah"

			' tags
			strMatch = "(?((.)|(\n))*)"
			strReplace = ""-//W3C//DTD XHTML 1.0 Transitional//EN"" ""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"">"http://www.w3.org/1999/xhtml"">
"font-family:Monospace""> " + ControlChars.CrLf + " ${doc} 
"
+ ControlChars.CrLf strworking = RegExReplace(strworking, strMatch, strReplace, 1) 'Keywords Dim kwords As keywords = New keywords() kwords = CKeyWords.GetKeywords(keyfile, "keywords.xsd") 'the regular expression engine stops at the first match as implemented here 'this results in only partial matches for certain words 'as a workaround, we reverse the sort order 'a DataView and DataRowView approach is possible 'BUT the DataRowView is not castable as a DataRow! 'since we are using a strongly typed dataset, 'this is not desireable 'since RegEx objects stay in memory, 'we need to be conservative about how we loop 'my first inclination was to markup each keyword by itself 'this would allow treatment of bold and italic faces 'testing this, however, results in an "out of memory" exception Dim colors() As String = {"aqua", "black", "black", "blue", "fuchsia", "gray", "green", "lime", "maroon", "navy", "olive", "purple", "red", "silver", "teal", "white", "yellow"} For n As Integer = 0 To colors.GetUpperBound(0) Dim rows() As DataRow rows = kwords.keyword.Select("color = '" & colors(n) & "'", "keyword_text DESC") If (rows.Length > 0) Then Dim strKeyWords As String = "(?(\b)(" For Each dr As DataRow In rows strKeyWords &= "(" & CType(dr, keywords.keywordRow).keyword_text & ")|" Next strKeyWords = Left(strKeyWords, strKeyWords.Length() - 1) strKeyWords &= ")(\b))" 'Trace.WriteLine(strKeyWords) strReplace = ""color:" & colors(n) & """>${keyword}" strworking = RegExReplace(strworking, strKeyWords, strReplace, -1) End If 'rows.Length>0 Next n Dim sw As New StreamWriter(fileout) sw.AutoFlush = True sw.WriteLine(strworking) sw.Close() End If 'args.Length End Sub '''' '''' a file-input based Regex.Replace implementation '''' '''' Source Code File '''' HTML File '''' Regular Expression for search '''' Replacement String '''' Maximum number of replacements; negative numbers mean all '''' T if no errors -- this does not imply that fn made changes you anticipated '''' 'Public Function RegExReplace(ByVal InFile As String, ByVal OutFile As String, ByVal SearchPattern As String, ByVal Replacement As String, ByVal MaxReplace As Integer) As Boolean ' Try ' Dim rx As Regex ' Dim strWorking As String ' rx = New Regex(SearchPattern, RegexOptions.Multiline) ' Dim strmRead As StreamReader = New StreamReader(InFile) ' strWorking = strmRead.ReadToEnd() ' strmRead.Close() ' Dim strmWrite As StreamWriter = New StreamWriter(OutFile, False) ' strmWrite.AutoFlush = True ' Dim strMatch As String = Regex.Match(strWorking, SearchPattern).ToString() ' strWorking = rx.Replace(strWorking, Replacement, MaxReplace) ' strmWrite.Write(strWorking) ' strmWrite.Close() ' rx = Nothing ' GC.Collect() ' Return (True) ' Catch ex As Exception ' Console.WriteLine("Error: " & ex.Message) ' Return (False) ' End Try 'End Function ''' ''' a string based implementation of RegEx.Replace ''' ''' source string ''' regular expression for the search ''' replacement pattern ''' how many replacements to perform (-1 means all) ''' string: the result. if routine fails, original string is returned. ''' Public Function RegExReplace(ByVal InString As String, ByVal SearchPattern As String, ByVal Replacement As String, ByVal MaxReplace As Integer) As String Trace.WriteLine("Entering RegExReplace") Trace.WriteLine(String.Format("Total Memory: {0}", GC.GetTotalMemory(False))) Dim rx As Regex Dim strWorking As String = InString Dim generation As Integer = 0 Try rx = New Regex(SearchPattern, RegexOptions.Multiline) generation = GC.GetGeneration(rx) Dim strMatch As String = Regex.Match(strWorking, SearchPattern).ToString() strWorking = rx.Replace(strWorking, Replacement, MaxReplace) Return (strWorking) Catch ex As Exception Trace.WriteLine("Error: " & ex.Message) Return (InString) Finally Trace.WriteLine("Pre GC") Trace.WriteLine(String.Format("Total Memory: {0}", GC.GetTotalMemory(False))) rx = Nothing GC.Collect() Trace.WriteLine("Post GC") Trace.WriteLine(String.Format("Total Memory: {0}", GC.GetTotalMemory(False))) Trace.WriteLine("Exiting RegExReplace") End Try End Function End Module