<% Response.Buffer = False Class RTFConvert 'Version 3.01 Beta 3 'More information can be found at 'http://www2.bitstream.net/~bradyh/downloads/rtf2htmlrm.html 'Converts Rich Text encoded text to HTML format 'if you find some text that this function doesn't 'convert properly please email the text to 'bradyh@bitstream.net Public strCurPhrase Public strHTML Public Codes() Public NextCodes() Public CodesBeg() 'beginning codes Public NextCodesBeg() 'beginning codes for next text Public CodesTmp() 'temp stack for copying Public CodesTmpBeg() 'temp stack for copying beg Public strCR 'string to use for CRs - blank if +CR not chosen in options Public strBeforeText Public strBeforeText2 Public strBeforeText3 Public gPlain 'true if all codes shouls be popped before next text Public gWBPlain 'plain will be true after next text Public strColorTable() 'table of colors Public lColors '# of colors Public strFontTable() 'table of fonts Public lFonts '# of fonts Public strEOL 'string to include before
Public strBOL 'string to include after
Public lSkipWords 'number od words to skip from current Public gBOL 'a
was inserted but no non-whitespace text has been inserted Public strFont Public strTable Public strFontColor 'current font color for setting up fontstring Public strFontSize 'current font size for setting up fontstring Public lFontSize Sub ClearCodes() ReDim Codes(0) ReDim NextCodes(0) ReDim CodesBeg(0) ReDim NextCodesBeg(0) End Sub Sub ClearFont() strFont = "" strTable = "" strFontColor = "" strFontSize = "" lFontSize = 0 End Sub Function Codes2NextTill(strCode) Dim strTmp Dim strTmpbeg ' strTmp = PopCode ' strTmpbeg = PopCodeBeg ' If strTmp <> strCode Then ' PushNext strTmp ' PushNextBeg strTmpbeg ' End If ' While strTmp <> "" And strTmp <> strCode ' strTmp = PopCode ' strTmpbeg = PopCodeBeg ' If strTmp <> strCode Then ' PushNext strTmp ' PushNextBeg strTmpbeg ' End If ' Wend ' If strTmp = strCode Then ' PushCode strTmp ' PushCodeBeg "" 'blank beginning so it won't get output next time ' End If Dim l l = UBound(Codes) While Codes(l) <> strCode And l >= 0 l = l - 1 Wend CodesBeg(l) = "" l = l + 1 While l <= UBound(Codes) PushNext (Codes(l)) PushNextBeg (CodesBeg(l)) CodesBeg(l) = "" l = l + 1 Wend End Function Private Function IIf(IIFStatement, IIFTrue, IIFFalse) If Eval(IIFStatement) Then IIf = Eval(IIFTrue) Else IIf = Eval(IIFFalse) End If End Function Public Sub GetColorTable(strSecTmp, strColorTable()) 'get color table data and fill in strColorTable array Dim lColors Dim lBOS Dim lEOS Dim strTmp lBOS = InStr(strSecTmp, "\colortbl") ReDim strColorTable(0) lColors = 1 If lBOS <> 0 Then lEOS = InStr(lBOS, strSecTmp, ";}") If lEOS <> 0 Then lBOS = InStr(lBOS, strSecTmp, "\red") While ((lBOS <= lEOS) And (lBOS <> 0)) ReDim Preserve strColorTable(lColors) strTmp = Trim(Hex(Mid(strSecTmp, lBOS + 4, 1) & IIf(IsNumeric(Mid(strSecTmp, lBOS + 5, 1)), Mid(strSecTmp, lBOS + 5, 1), "") & IIf(IsNumeric(Mid(strSecTmp, lBOS + 6, 1)), Mid(strSecTmp, lBOS + 6, 1), ""))) If Len(strTmp) = 1 Then strTmp = "0" & strTmp strColorTable(lColors) = strColorTable(lColors) & strTmp lBOS = InStr(lBOS, strSecTmp, "\green") strTmp = Trim(Hex(Mid(strSecTmp, lBOS + 6, 1) & IIf(IsNumeric(Mid(strSecTmp, lBOS + 7, 1)), Mid(strSecTmp, lBOS + 7, 1), "") & IIf(IsNumeric(Mid(strSecTmp, lBOS + 8, 1)), Mid(strSecTmp, lBOS + 8, 1), ""))) If Len(strTmp) = 1 Then strTmp = "0" & strTmp strColorTable(lColors) = strColorTable(lColors) & strTmp lBOS = InStr(lBOS, strSecTmp, "\blue") strTmp = Trim(Hex(Mid(strSecTmp, lBOS + 5, 1) & IIf(IsNumeric(Mid(strSecTmp, lBOS + 6, 1)), Mid(strSecTmp, lBOS + 6, 1), "") & IIf(IsNumeric(Mid(strSecTmp, lBOS + 7, 1)), Mid(strSecTmp, lBOS + 7, 1), ""))) If Len(strTmp) = 1 Then strTmp = "0" & strTmp strColorTable(lColors) = strColorTable(lColors) & strTmp lBOS = InStr(lBOS, strSecTmp, "\red") lColors = lColors + 1 Wend End If End If End Sub Public Sub GetFontTable(strSecTmp As String, strFontTable() As String) 'get font table data and fill in strFontTable array Dim lFonts Dim lBOS Dim lEOS Dim strTmp lBOS = InStr(strSecTmp, "\fonttbl") ReDim strFontTable(0) lFonts = 0 If lBOS <> 0 Then lEOS = InStr(lBOS, strSecTmp, ";}}") If lEOS <> 0 Then lBOS = InStr(lBOS, strSecTmp, "\f0") While ((lBOS <= lEOS) And (lBOS <> 0)) ReDim Preserve strFontTable(lFonts) While ((Mid(strSecTmp, lBOS, 1) <> " ") And (lBOS <= lEOS)) lBOS = lBOS + 1 Wend lBOS = lBOS + 1 strTmp = Mid(strSecTmp, lBOS, InStr(lBOS, strSecTmp, ";") - lBOS) strFontTable(lFonts) = strFontTable(lFonts) & strTmp lBOS = InStr(lBOS, strSecTmp, "\f" & (lFonts + 1)) lFonts = lFonts + 1 Wend End If End If End Sub Public Function InNext(strTmp) Dim gTmp Dim l l = 1 gTmp = False While l <= UBound(NextCodes) And Not gTmp If NextCodes(l) = strTmp Then gTmp = True l = l + 1 Wend InNext = gTmp End Function Public Function InCodes(strTmp) Dim gTmp Dim l l = 1 gTmp = False While l <= UBound(Codes) And Not gTmp If Codes(l) = strTmp And Len(CodesBeg(l)) > 0 Then gTmp = True l = l + 1 Wend InCodes = gTmp End Function Public Function NabNextLine(strRTF) Dim l l = InStr(strRTF, vbCrLf) If l = 0 Then l = Len(strRTF) NabNextLine = TrimAll(Left(strRTF, l)) If l = Len(strRTF) Then strRTF = "" Else strRTF = TrimAll(Mid(strRTF, l)) End If End Function Public Function NabNextWord(strLine) Dim l Dim lvl Dim gEndofWord Dim gInCommand 'current word is command instead of plain word gInCommand = False l = 0 lvl = 0 'strLine = TrimifCmd(strLine) If Left(strLine, 1) = "}" Then strLine = Mid(strLine, 2) NabNextWord = "}" GoTo finally End If While Not gEndofWord l = l + 1 If l >= Len(strLine) Then If l = Len(strLine) Then l = l + 1 gEndofWord = True ElseIf InStr("\{}", Mid(strLine, l, 1)) Then If l = 1 And Mid(strLine, l, 1) = "\" Then gInCommand = True If Mid(strLine, l + 1, 1) <> "\" And l > 1 And lvl = 0 Then gEndofWord = True End If ElseIf Mid(strLine, l, 1) = " " And lvl = 0 And gInCommand Then gEndofWord = True End If Wend If l = 0 Then l = Len(strLine) NabNextWord = Left(strLine, l - 1) While Len(NabNextWord) > 0 And InStr("{}", Right(NabNextWord, 1)) NabNextWord = Left(NabNextWord, Len(NabNextWord) - 1) Wend While Len(NabNextWord) > 0 And InStr("{}", Left(NabNextWord, 1)) NabNextWord = Right(NabNextWord, Len(NabNextWord) - 1) Wend strLine = Mid(strLine, l) If Left(strLine, 1) = " " Then strLine = Mid(strLine, 2) finally: End Function Public Function NabSection(strRTF, lPos) 'grab section surrounding lPos, strip section out of strRTF and return it Dim lBOS 'beginning of section Dim lEOS 'ending of section Dim strChar Dim lLev 'level of brackets/parens Dim lRTFLen lRTFLen = Len(strRTF) lBOS = lPos strChar = Mid(strRTF, lBOS, 1) lLev = 1 While lLev > 0 lBOS = lBOS - 1 If lBOS <= 0 Then lLev = lLev - 1 Else strChar = Mid(strRTF, lBOS, 1) If strChar = "}" Then lLev = lLev + 1 ElseIf strChar = "{" Then lLev = lLev - 1 End If End If Wend lBOS = lBOS - 1 If lBOS < 1 Then lBOS = 1 lEOS = lPos strChar = Mid(strRTF, lEOS, 1) lLev = 1 While lLev > 0 lEOS = lEOS + 1 If lEOS >= lRTFLen Then lLev = lLev - 1 Else strChar = Mid(strRTF, lEOS, 1) If strChar = "{" Then lLev = lLev + 1 ElseIf strChar = "}" Then lLev = lLev - 1 End If End If Wend lEOS = lEOS + 1 If lEOS > lRTFLen Then lEOS = lRTFLen NabSection = Mid(strRTF, lBOS + 1, lEOS - lBOS - 1) strRTF = Mid(strRTF, 1, lBOS) & Mid(strRTF, lEOS) strRTF = rtf2html_replace(strRTF, vbCrLf & vbCrLf, vbCrLf) End Function Public Sub Next2Codes() 'move codes from pending ("next") stack to current stack Dim lNumCodes Dim l If UBound(NextCodes) > 0 Then lNumCodes = UBound(Codes) ReDim Preserve Codes(lNumCodes + UBound(NextCodes)) ReDim Preserve CodesBeg(lNumCodes + UBound(NextCodes)) For l = 1 To UBound(NextCodes) Codes(lNumCodes + l) = NextCodes(l) CodesBeg(lNumCodes + l) = NextCodesBeg(l) Next l ReDim NextCodes(0) ReDim NextCodesBeg(0) End If End Sub Public Sub Codes2Next() 'move codes from "current" stack to pending ("next") stack Dim lNumCodes Dim l If UBound(Codes) > 0 Then lNumCodes = UBound(NextCodes) ReDim Preserve NextCodes(lNumCodes + UBound(Codes)) ReDim Preserve NextCodesBeg(lNumCodes + UBound(Codes)) For l = 1 To UBound(Codes) NextCodes(lNumCodes + l) = Codes(l) NextCodesBeg(lNumCodes + l) = CodesBeg(l) Next l ReDim Codes(0) ReDim CodesBeg(0) End If End Sub Public Function ParseFont(strColor, strSize) Dim strTmpFont strTmpFont = " "" Then strTmpFont = strTmpFont & " color=""" & strColor & """" End If If strSize <> "" And strSize <> "2" Then strTmpFont = strTmpFont & " size=" & strSize End If strTmpFont = strTmpFont & ">" ParseFont = strTmpFont End Function Function PopCode() If UBound(Codes) > 0 Then PopCode = Codes(UBound(Codes)) ReDim Preserve Codes(UBound(Codes) - 1) End If End Function Function GetAllCodes() Dim strTmp Dim l strTmp = "" If UBound(Codes) > 0 Then For l = UBound(Codes) To 1 Step -1 strTmp = strTmp & Codes(l) Next l End If GetAllCodes = strTmp End Function Function GetAllNextCodes() Dim strTmp Dim l strTmp = "" If UBound(NextCodes) > 0 Then For l = 1 To UBound(NextCodes) strTmp = strTmp & NextCodes(l) Next l End If GetAllNextCodes = strTmp End Function Function GetAllCodesBeg() Dim strTmp Dim l strTmp = "" If UBound(CodesBeg) > 0 Then For l = 1 To UBound(CodesBeg) strTmp = strTmp & CodesBeg(l) Next l End If GetAllCodesBeg = strTmp End Function Function GetAllNextCodesBeg() Dim strTmp Dim l strTmp = "" If UBound(NextCodesBeg) > 0 Then For l = 1 To UBound(NextCodesBeg) strTmp = strTmp & NextCodesBeg(l) Next l End If GetAllNextCodesBeg = strTmp End Function Function PopCodeBeg() If UBound(CodesBeg) > 0 Then PopCodeBeg = CodesBeg(UBound(CodesBeg)) ReDim Preserve CodesBeg(UBound(CodesBeg) - 1) End If End Function Function PopTmp() If UBound(CodesTmp) > 0 Then PopTmp = CodesTmp(UBound(CodesTmp)) ReDim Preserve CodesTmp(UBound(CodesTmp) - 1) End If End Function Function PopTmpBeg() If UBound(CodesTmp) > 0 Then PopTmpBeg = CodesTmpBeg(UBound(CodesTmpBeg)) ReDim Preserve CodesTmpBeg(UBound(CodesTmpBeg) - 1) End If End Function Function PopNext() If UBound(NextCodes) > 0 Then PopNext = NextCodes(UBound(NextCodes)) ReDim Preserve NextCodes(UBound(NextCodes) - 1) End If End Function Function PopNextBeg() If UBound(NextCodesBeg) > 0 Then PopNextBeg = NextCodesBeg(UBound(NextCodesBeg)) ReDim Preserve NextCodesBeg(UBound(NextCodesBeg) - 1) End If End Function Sub ProcessWord(strWord) Dim strTmp As String Dim strTmpbeg As String Dim l As Long Dim gPopAll As Boolean Dim strTableAlign As String 'current table alignment for setting up tablestring Dim strTableWidth As String 'current table width for setting up tablestring If lSkipWords > 0 Then lSkipWords = lSkipWords - 1 Exit Function End If If Left(strWord, 1) = "\" Or Left(strWord, 1) = "{" Or Left(strWord, 1) = "}" Then Select Case Left(strWord, 2) Case "}" For l = 1 To UBound(CodesBeg) CodesBeg(l) = "" Next l ClearFont Case "\b" 'bold If strWord = "\b" Then If Codes(UBound(Codes)) <> "" Or (Codes(UBound(Codes)) = "" And CodesBeg(UBound(Codes)) = "") Then PushNext ("") PushNextBeg ("") End If ElseIf strWord = "\bullet" Then ElseIf strWord = "\b0" Then 'bold off If InCodes("") Then Codes2NextTill ("") ElseIf InNext("") Then RemoveFromNext ("") End If End If Case "\c" If strWord = "\cf0" Then 'color font off If InCodes("") Then Codes2NextTill ("") ElseIf InNext("") Then RemoveFromNext ("") End If ElseIf Left(strWord, 3) = "\cf" And IsNumeric(Mid(strWord, 4)) Then 'color font 'get color code l = Val(Mid(strWord, 4)) If l <= UBound(strColorTable) And l > 0 Then strFontColor = "#" & strColorTable(l) End If 'insert color If strFontColor <> "#" Then strFont = ParseFont(strFontColor, strFontSize) If InNext("") Then ReplaceInNextBeg "", strFont ElseIf InCodes("") Then PushNext ("") PushNextBeg (strFont) Codes2NextTill "" Else PushNext ("") PushNextBeg (strFont) End If End If End If Case "\f" If Left(strWord, 3) = "\fs" And IsNumeric(Mid(strWord, 4)) Then 'font size l = Val(Mid(strWord, 4)) lFontSize = Int((l / 6) - 0) 'calc to convert RTF to HTML sizes If lFontSize > 8 Then lFontSize = 8 If lFontSize < 1 Then lFontSize = 1 strFontSize = Trim(lFontSize) 'insert size If strFontSize <> "2" And strFontSize <> "" Then strFont = ParseFont(strFontColor, strFontSize) If InNext("") Then ReplaceInNextBeg "", strFont ElseIf InCodes("") Then PushNext ("") PushNextBeg (strFont) Codes2NextTill "" Else PushNext ("") PushNextBeg (strFont) End If End If End If Case "\i" If strWord = "\i" Then 'italics If Codes(UBound(Codes)) <> "" Or (Codes(UBound(Codes)) = "" And CodesBeg(UBound(Codes)) = "") Then PushNext ("") PushNextBeg ("") End If ElseIf strWord = "\i0" Then 'italics off If InCodes("") Then Codes2NextTill ("") ElseIf InNext("") Then RemoveFromNext ("") End If End If Case "\l" 'If strWord = "\listname" Then ' lSkipWords = 1 'End If Case "\p" If strWord = "\par" Then strBeforeText2 = strBeforeText2 & strEOL & "
" & strCR gBOL = True 'If Len(strBOL) > 0 Then ' PushNext ("") ' PushNextBeg ("
  • ") 'End If ElseIf strWord = "\pard" Then For l = 1 To UBound(CodesBeg) CodesBeg(l) = "" Next l ClearFont ElseIf strWord = "\plain" Then For l = 1 To UBound(CodesBeg) CodesBeg(l) = "" Next l ClearFont ElseIf strWord = "\pnlvlblt" Then 'bulleted list 'If Codes(UBound(Codes)) = "" Then ' strTmp = PopCode ' strTmp = PopCodeBeg 'End If 'PushNext ("") 'PushNextBeg ("