Option Public
Option Explicit
Sub Initialize
Dim testString As String
testString = "This is a <b>test</b> of the <i>new</i> <div>function</div>. " & Chr(13) & Chr(10) & _
"It should also handle <a hReF=""http://blah"">http://links</A>http://links" & _
" blah <> blah hTtP://something" & Chr(0) & Chr(0) & "> < and http://qwerty/blah?asdf (http://blah)."
Print ConvertTags(testString)
End Sub
Function ConvertTags (comment As String) As String
Dim lastPos As Integer, startPos As Integer, endPos As Integer
Dim tagString As String
Dim newString As String
lastPos = 1
startPos = Instr(comment, "<")
Do While (startPos > 0)
newString = newString & ReplaceSubstring(Mid$(comment, lastPos, startPos - lastPos), ">", ">")
endPos = Instr(startPos, comment, ">")
If (endPos > 0) Then
tagString = Mid$(comment, startPos + 1, endPos - startPos - 1)
Select Case Trim$(Lcase$(tagString))
Case "/a", "b", "/b", "i", "/i", "u", "/u", "p", "br", "pre", "/pre", "blockquote", "/blockquote" :
newString = newString & "<" & Trim$(tagString) & ">"
Case Else :
If (Left$(Trim$(Lcase$(tagString)), 7) = "a href=") Then
newString = newString & "<" & Trim$(tagString) & ">"
Else
newString = newString & "<" & tagString & ">"
End If
End Select
Else
newString = newString & ReplaceSubstring(Mid$(comment, startPos), "<", "<")
endPos = Len(comment)
End If
lastPos = endPos + 1
startPos = Instr(lastPos - 1, comment, "<")
Loop
If (lastPos <= Len(comment)) Then
newString = newString & ReplaceSubstring(Mid$(comment, lastPos), ">", ">")
End If
Dim hrefStartPos As Integer, hrefEndPos As Integer
Dim hrefEndChars As String, hrefString As String
hrefEndChars = " " & Chr(0) & Chr(9) & Chr(10) & Chr(13)
hrefStartPos = Instr(1, newString, "http://", 5)
Do While (hrefStartPos > 0)
startPos = Instr(hrefStartPos, newString, "<")
endPos = Instr(hrefStartPos, newString, ">")
If (endPos = 0) Or ((endPos > startPos) And (startPos > 0)) Then
hrefEndPos = hrefStartPos + 7
Do While (hrefEndPos <= Len(newString))
If (Instr(hrefEndChars, Mid$(newString, hrefEndPos, 1)) > 0) Then
Exit Do
End If
hrefEndPos = hrefEndPos + 1
Loop
Do While (hrefEndPos > hrefStartPos)
If (Instr(".,?!&:-()[]<>{}'""", Mid$(newString, hrefEndPos - 1, 1)) = 0) Then
Exit Do
End If
hrefEndPos = hrefEndPos - 1
Loop
hrefString = Mid$(newString, hrefStartPos, hrefEndPos - hrefStartPos)
newString = Left$(newString, hrefStartPos - 1) & "<a href=""" & hrefString & """>" & _
hrefString & "</a>" & Mid$(newString, hrefStartPos + Len(hrefString))
hrefEndPos = hrefEndPos + Len("<a href='" & hrefString & "'></a>")
Elseif (endPos < startPos) And (endPos > 0) Then
hrefEndPos = Instr(endPos, newString, "</a>", 5)
If (hrefEndPos = 0) Then
hrefEndPos = Len(newString)
End If
Else
hrefEndPos = endPos
End If
hrefStartPos = Instr(hrefEndPos, newString, "http://", 5)
Loop
Dim linefeed As String
linefeed = Chr(13) & Chr(10)
newString = ReplaceSubstring(newString, Chr(13) & Chr(10), Chr(0))
newString = ReplaceSubstring(newString, Chr(13), Chr(0))
newString = ReplaceSubstring(newString, Chr(10), Chr(0))
newString = ReplaceSubstring(newString, Chr(0) & Chr(0), "<p>" & linefeed)
newString = ReplaceSubstring(newString, Chr(0), "<br>" & linefeed)
ConvertTags = newString
End Function
Function ReplaceSubstring (Byval fullString As String, oldString As String, newString As String) As String
Dim pos As Integer
pos = Instr(fullString, oldString)
Do While pos > 0
fullString = Left$(fullString, pos - 1) & newString & Mid$(fullString, pos + Len(oldString))
pos = Instr(pos + Len(newString), fullString, oldString)
Loop
ReplaceSubstring = fullString
End Function
This LotusScript was converted to HTML using the ls2html routine,
provided by Julian Robichaux at nsftools.com.