%
'Web RTF2HTML
'adapted by Andrew Powell for vbscript from rtf2html 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
'problems with vbscript email ajp@infoaccelerator.net
Class rtf2html
Dim strCurPhrase
Dim strHTML
Dim Codes()
Dim NextCodes()
Dim CodesBeg() 'beginning codes
Dim NextCodesBeg 'beginning codes for next text
Dim CodesTmp() 'temp stack for copying
Dim CodesTmpBeg() 'temp stack for copying beg
Public strCR 'string to use for CRs - blank if +CR not chosen in options
Dim strBeforeText
Dim strBeforeText2
Dim strBeforeText3
Dim gPlain 'true if all codes shouls be popped before next text
Dim gWBPlain 'plain will be true after next text
Dim strColorTable() 'table of colors
Dim lColors '# of colors
Dim strFontTable() 'table of fonts
Dim lFonts '# of fonts
Dim strEOL 'string to include before
Dim strBOL 'string to include after
Dim lSkipWords 'number od words to skip from current
Dim gBOL 'a
was inserted but no non-whitespace text has been inserted
Dim strFont
Dim strTable
Dim strFontColor 'current font color for setting up fontstring
Dim strFontSize 'current font size for setting up fontstring
Dim lFontSize
Function ClearCodes()
ReDim Codes(0)
ReDim NextCodes(0)
ReDim CodesBeg(0)
ReDim NextCodesBeg(0)
End Function
Function ClearFont()
strFont = ""
strTable = ""
strFontColor = ""
strFontSize = ""
lFontSize = 0
End Function
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
Function IIf(blnExpression, vTrueResult, vFalseResult)
If blnExpression Then
IIf = vTrueResult
Else
IIf = vFalseResult
End If
End Function
Function 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 Function
Function GetFontTable(strSecTmp, strFontTable())
'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 Function
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
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
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
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 = "}"
Exit Function
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)
End Function
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
Function 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))
' May need to change to l = 0 to UBound(NextCodes)
For l = 1 To UBound(NextCodes)
Codes(lNumCodes + l) = NextCodes(l)
CodesBeg(lNumCodes + l) = NextCodesBeg(l)
Next
ReDim NextCodes(0)
ReDim NextCodesBeg(0)
End If
End Function
Function 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
ReDim Codes(0)
ReDim CodesBeg(0)
End If
End Function
Function ParseFont(strColor, strSize)
Dim strTmpFont
strTmpFont = " "" Then
strTmpFont = strTmpFont & " color=""" & strColor & """"
End If
' Let the font size be determined by the page's stylesheet / style
'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
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
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
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
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
Function ProcessWord(strWord)
Dim strTmp
Dim strTmpbeg
Dim l
Dim gPopAll
Dim strTableAlign 'current table alignment for setting up tablestring
Dim strTableWidth '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
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 = CLng(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 = CLng(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 If InNext(" |
| " End If If InNext(" |