|
 |
asp_web_howto thread: Creating Tagen
Message #1 by George Smyth <george.smyth@U...> on Thu, 15 Feb 2001 14:46:57 -0500
|
|
Here's a good one.
I am writing code that will take text and display it. I would like to find
Web addresses within the text and convert them into tags. My first instinct
was that this would be no big deal, just look for the "http://" (I get to
choose what to look for) and replace it with the appropriate code.
To identify the address, I wrote the following code:
WebLoc = InStr(Text, "http://")
If WebLoc > 0 Then
WebAddress = Mid(Text, WebLoc, (InStr(WebLoc, Text, " ") - WebLoc))
End If
This works just fine and does just what I want - almost.
The problem I've got is that if the address is at the end of a sentence,
then the period is selected (since it is not a space). If it is at the end
of a paragraph, not only is the period selected, but so is the Carriage
Return and the first word of the next paragraph. I'm sure that there are
other possibilities out there lurking.
So how does one do this? Do I look for .com, .org, .net, etc. and not worry
about foreign addresses? I'd really rather not go that route, but I'm
somewhat stumped at trying to figure out how best to do this.
Has anyone here done this? Any ideas?
Thanks -
George L Smyth, Webmaster
US Naval Academy Alumni Association
Message #2 by "Ken Schaefer" <ken@a...> on Fri, 16 Feb 2001 12:27:27 +1100
|
|
What if you replaced all the full-stops with spaces?
Cheers
Ken
----- Original Message -----
From: "George Smyth" <george.smyth@U...>
To: "ASP Web HowTo" <asp_web_howto@p...>
Sent: Friday, February 16, 2001 6:46 AM
Subject: [asp_web_howto] Creating Tagen
> Here's a good one.
>
> I am writing code that will take text and display it. I would like to
find
> Web addresses within the text and convert them into tags. My first
instinct
> was that this would be no big deal, just look for the "http://" (I get to
> choose what to look for) and replace it with the appropriate code.
>
> To identify the address, I wrote the following code:
>
> WebLoc = InStr(Text, "http://")
> If WebLoc > 0 Then
> WebAddress = Mid(Text, WebLoc, (InStr(WebLoc, Text, " ") - WebLoc))
> End If
>
> This works just fine and does just what I want - almost.
>
> The problem I've got is that if the address is at the end of a sentence,
> then the period is selected (since it is not a space). If it is at the
end
> of a paragraph, not only is the period selected, but so is the Carriage
> Return and the first word of the next paragraph. I'm sure that there are
> other possibilities out there lurking.
>
> So how does one do this? Do I look for .com, .org, .net, etc. and not
worry
> about foreign addresses? I'd really rather not go that route, but I'm
> somewhat stumped at trying to figure out how best to do this.
>
> Has anyone here done this? Any ideas?
>
> Thanks -
>
> George L Smyth, Webmaster
> US Naval Academy Alumni Association
Message #3 by Imar Spaanjaars <Imar@S...> on Fri, 16 Feb 2001 09:33:06 +0100
|
|
The following code takes a complete text sting, looks for any http:// in it
and converts to a hrefs.
At about a third, you see " test for vbCRLF" This is where the checking
starts: first it checks for a normal line break. If that isn't found,
(iLinkEnd is still 0) then it looks for a <BR>. If that isn't found, it
looks for a period..
Just add new lines for any item (commas, semi colons etc) you expect to be
at the end of the line AND that cannot be a part of an URL)
This function is a bit outdated / not really optimized, but it'll show you
the principle. You could modify it for example to check for the various
end-tags in a more recursive way instead of just adding more lines.
This function was originally taken from www.asp101.com but has been
modified a little.
Hope this helps
Imar
' This function takes a string as input and links any http's it
' finds so that they are then clickable in a browser. If only
' looks for http:// so www.asp101.com alone wouldn't link, but
' http://www.asp101.com would.
Function LinkURLs(strInput, target)
Dim iCurrentLocation ' Our current position in the input string
Dim iLinkStart ' Beginning position of the current link
Dim iLinkEnd ' Ending position of the current link
Dim strLinkText ' Text we're converting to a link
Dim strOutput ' Return string with links in it
Dim target_
' Start at the first character in the string
iCurrentLocation = 1
' Look for http:// in the text from the current position to
' the end of the string. If we find it then we start the
' linking process otherwise we're done because there are no
' more http://'s in the string.
Do While InStr(iCurrentLocation, strInput, "http://", 1) <> 0
' Set the position of the beginning of the link
iLinkStart = InStr(iCurrentLocation, strInput, "http://", 1)
' Set the position of the end of the link. I use the
' first space as the determining factor.
iLinkEnd = InStr(iLinkStart, strInput, " ", 1)
' test for vbCRLF
If iLinkEnd = 0 Then iLinkEnd = InStr(iLinkStart,
strInput, vbCRLF, 1)
' test for <BR>
If iLinkEnd = 0 Then iLinkEnd = InStr(iLinkStart,
strInput, "<br>", 1)
' test for period
If iLinkEnd = 0 Then iLinkEnd = InStr(iLinkStart,
strInput, ".", 1)
' If we didn't find a space then we link to the
' end of the string
If iLinkEnd = 0 Then iLinkEnd = Len(strInput) + 1
' Take care of any punctuation we picked up
Select Case Mid(strInput, iLinkEnd - 1, 1)
Case ".", "!", "?"
iLinkEnd = iLinkEnd - 1
End Select
' This adds to the output string all the non linked stuff
' up to the link we're curently processing.
strOutput = strOutput & Mid(strInput, iCurrentLocation,
iLinkStart - iCurrentLocation)
' Get the text we're linking and store it in a variable
strLinkText = Mid(strInput, iLinkStart, iLinkEnd - iLinkStart)
if not target = "" then ' parameter target can contain
_Blank for example
target = " target=""" & target & """"
end if
' Build our link and append it to the output string
strOutput = strOutput & "<A HREF=""" & strLinkText & """"
& target & ">" & strLinkText & "</A>" & writeShortCut()
' Reset our current location to the end of that link
iCurrentLocation = iLinkEnd
Loop
' Tack on the end of the string. I need to do this so we
' don't miss any trailing non-linked text
strOutput = strOutput & Mid(strInput, iCurrentLocation)
' Set the return value
LinkURLs = strOutput
End Function
At 12:27 PM 2/16/2001 +1100, you wrote:
>What if you replaced all the full-stops with spaces?
>
>Cheers
>Ken
Message #4 by George Smyth <george.smyth@U...> on Fri, 16 Feb 2001 07:59:51 -0500
|
|
Thanks for the code, I will take a look at it later today. I have a feeling
that I will need to add a number of other characters to seek, but it's a
great start.
Cheers -
George L Smyth, Webmaster
US Naval Academy Alumni Association
-----Original Message-----
From: Imar Spaanjaars [mailto:Imar@S...]
Sent: Friday, February 16, 2001 3:33 AM
To: ASP Web HowTo
Subject: [asp_web_howto] Re: Creating Tagen
The following code takes a complete text sting, looks for any http:// in it
and converts to a hrefs.
[clip]
Message #5 by Imar Spaanjaars <Imar@S...> on Fri, 16 Feb 2001 15:13:36 +0100
|
|
I found a more recent version in one of our programmer's "goodie-bag":
This one was used for a large number of texts formatted by Lotus Notes: it
contained a lot of irrelevant HTML tags. This code looks for all kinds of
tags as "end-chars" as well.
It checks for all the different tags, but only changes the value of the END
character (iLinkEndSmallest) if the value found is smaller than the
previous value.
The guy promised me to write a new / faster version ASAP, so I'll post that
when it comes around. Don't bet on it too much, though. We just use this
function for one-time conversion only, not in a real-time environment, so
speed wasn't the essence here.
Hope this helps,
Imar
Function LinkURLs(strInput, target)
Dim iCurrentLocation ' Our current position in the input string
Dim iLinkStart ' Beginning position of the current link
Dim iLinkEnd ' Ending position of the current link
Dim strLinkText ' Text we're converting to a link
Dim strOutput ' Return string with links in it
' Start at the first character in the string
iCurrentLocation = 1
if not target = "" then
target = " TARGET=""" & target & """"
end if
Do While InStr(iCurrentLocation, strInput, "http://", 1) <> 0
' Set the position of the beginning of the link
iLinkStart = InStr(iCurrentLocation, strInput, "http://", 1)
' Set the position of the end of the link. I use the
' ) as the first determining factor.
Dim iLinkEndSmallest ' as Integer
iLinkEndSmallest = 0
iLinkEnd = InStr(iLinkStart, strInput, ")", 1)
if iLinkEnd > 0 then
if iLinkEndSmallest > 0 then
if iLinkEnd < iLinkEndSmallest then
iLinkEndSmallest = iLinkEnd
end if
else
iLinkEndSmallest = iLinkEnd
end if
end if
iLinkEnd = InStr(iLinkStart, strInput, "</font>", 1)
if iLinkEnd > 0 then
if iLinkEndSmallest > 0 then
if iLinkEnd < iLinkEndSmallest then
iLinkEndSmallest = iLinkEnd
end if
else
iLinkEndSmallest = iLinkEnd
end if
end if
iLinkEnd = InStr(iLinkStart, strInput, " ", 1)
if iLinkEnd > 0 then
if iLinkEndSmallest > 0 then
if iLinkEnd < iLinkEndSmallest then
iLinkEndSmallest = iLinkEnd
end if
else
iLinkEndSmallest = iLinkEnd
end if
end if
iLinkEnd = InStr(iLinkStart, strInput, vbCRLF, 1)
if iLinkEnd > 0 then
if iLinkEndSmallest > 0 then
if iLinkEnd < iLinkEndSmallest then
iLinkEndSmallest = iLinkEnd
end if
else
iLinkEndSmallest = iLinkEnd
end if
end if
iLinkEnd = InStr(iLinkStart, strInput, "<BR>", 1)
if iLinkEnd > 0 then
if iLinkEndSmallest > 0 then
if iLinkEnd < iLinkEndSmallest then
iLinkEndSmallest = iLinkEnd
end if
else
iLinkEndSmallest = iLinkEnd
end if
end if
iLinkEnd = InStr(iLinkStart, strInput, "<i>", 1)
if iLinkEnd > 0 then
if iLinkEndSmallest > 0 then
if iLinkEnd < iLinkEndSmallest then
iLinkEndSmallest = iLinkEnd
end if
else
iLinkEndSmallest = iLinkEnd
end if
end if
iLinkEnd = InStr(iLinkStart, strInput, "<b>", 1)
if iLinkEnd > 0 then
if iLinkEndSmallest > 0 then
if iLinkEnd < iLinkEndSmallest then
iLinkEndSmallest = iLinkEnd
end if
else
iLinkEndSmallest = iLinkEnd
end if
end if
iLinkEnd = InStr(iLinkStart, strInput, "'", 1)
if iLinkEnd > 0 then
if iLinkEndSmallest > 0 then
if iLinkEnd < iLinkEndSmallest then
iLinkEndSmallest = iLinkEnd
end if
else
iLinkEndSmallest = iLinkEnd
end if
end if
iLinkEnd = InStr(iLinkStart, strInput, "<p>", 1)
if iLinkEnd > 0 then
if iLinkEndSmallest > 0 then
if iLinkEnd < iLinkEndSmallest then
iLinkEndSmallest = iLinkEnd
end if
else
iLinkEndSmallest = iLinkEnd
end if
end if
iLinkEnd = iLinkEndSmallest
' If we didn't find a space or any of the other characters
then we link to the
' end of the string
If iLinkEnd = 0 Then iLinkEnd = Len(strInput) + 1
' Take care of any punctuation we picked up
Select Case Mid(strInput, iLinkEnd - 1, 1)
Case ".", "!", "?"
iLinkEnd = iLinkEnd - 1
End Select
' This adds to the output string all the non linked stuff
' up to the link we're curently processing.
strOutput = strOutput & Mid(strInput, iCurrentLocation,
iLinkStart - iCurrentLocation)
' Get the text we're linking and store it in a variable
strLinkText = Mid(strInput, iLinkStart, iLinkEnd - iLinkStart)
' Build our link and append it to the output string
strOutput = strOutput & "<A HREF=""" & strLinkText & """"
& target & ">" & strLinkText & "</a>" & " <IMG
SRC=""/images/shortcut.gif"" alt=""This will open a new browser window"">"
' Some good old debugging
'Response.Write iLinkStart & "," & iLinkEnd & "<BR>" & vbCrLf
' Reset our current location to the end of that link
iCurrentLocation = iLinkEnd
Loop
' Tack on the end of the string. I need to do this so we
' don't miss any trailing non-linked text
strOutput = strOutput & Mid(strInput, iCurrentLocation)
' Set the return value
LinkURLs = strOutput
End Function
At 07:59 AM 2/16/2001 -0500, you wrote:
>Thanks for the code, I will take a look at it later today. I have a feeling
>that I will need to add a number of other characters to seek, but it's a
>great start.
>
>Cheers -
>
>George L Smyth, Webmaster
>US Naval Academy Alumni Association
Message #6 by George Smyth <george.smyth@U...> on Fri, 16 Feb 2001 09:58:33 -0500
|
|
Well, it just goes to show that you can learn something every day. I didn't
know that you can have multiple possibilities in the same Case statement.
That will make things much easier.
Thanks!
George L Smyth, Webmaster
US Naval Academy Alumni Association
-----Original Message-----
From: Imar Spaanjaars [mailto:Imar@S...]
Sent: Friday, February 16, 2001 9:14 AM
To: ASP Web HowTo
Subject: [asp_web_howto] Re: Creating Tagen
I found a more recent version in one of our programmer's "goodie-bag":
[clip]
Select Case Mid(strInput, iLinkEnd - 1, 1)
Case ".", "!", "?"
iLinkEnd = iLinkEnd - 1
End Select
[clip]
|
|
 |