property CR : (ASCII character 13)
property NL : (ASCII character 10)
property return_delim : “|||”
property para_delim : “
” –change to “” to eliminate leading
s
set the_text to "I am using Applescript to write CGI scripts on my MacOS9 server. I have a web page which allows users to enter text into a textfield (essentially a bulletin board), and would like to use Applescript to turn that text into an html file when someone submits that message. I have the basic part working (puts the appropriate html tags at the top and bottom, finds new paragraphs and inserts
as needed).
someone@mail.com
However, if someone types in a web address, I would like to encode that so that www.yahoo.com or http://www.yahoo.com turns into
www.yahoo.com
so that the link will be active when viewed in a broswer. Similarly, I would like to encode email addresses.
Does anyone have a clever way to do this or to just convert a whole chuck of raw text to html? someone@mail.com"
set the_text to [color=green]encode_HREFs/color
return the_text
on [color=green]encode_HREFs/color
set search_strings to {CR, NL, return}
repeat 5 times –remove multiple spaces from the end of paragraphs
repeat with i from 1 to count of search_strings
set the_text to snr(the_text, (" " & (item i of search_strings)), (item i of search_strings))
set the_text to snr(the_text, (item i of search_strings), return_delim)
end repeat
end repeat
set the_paragraphs to string_to_list(the_text, return_delim)
repeat with i from 1 to count of the_paragraphs
set the_paragraph to para_delim & (item i of the_paragraphs)
set word_list to [color=green]string_to_list/color
repeat with list_item in word_list
set the_word to (contents of list_item)
if the_word is not para_delim then
if the_word starts with para_delim then
set the_word to (text (1 + (count of characters of para_delim)) thru -1 of the_word) as string
set replace_para_delim to true
else
set replace_para_delim to false
end if
if the_word contains “@” then
set the_word to (“<a href=“mailto:” & the_word & “”>” & the_word & “”)
else if the_word starts with “http://” then
set the_word to (“<a href=”" & the_word & “”>" & the_word & “”)
else if the_word starts with “www.” then
set the_word to (“<a href=“http://” & the_word & “”>” & the_word & “”)
else if the_word starts with “ftp.” then
set the_word to (“<a href=“ftp://” & the_word & “”>” & the_word & “”)
end if
if replace_para_delim = true then set the_word to para_delim & the_word
set contents of list_item to the_word
end if
end repeat
set (item i of the_paragraphs) to [color=green]list_to_string/color
end repeat
return list_to_string(the_paragraphs, return)
end encode_HREFs
on [color=green]atid/color
set AppleScript’s text item delimiters to the_delim
end atid
on snr(the_string, search_string, replace_string)
return my list_to_string((my string_to_list(the_string, search_string)), replace_string)
end snr
on string_to_list(the_string, the_delim)
my [color=green]atid/color
set the_list to (every text item of the_string) as list
my [color=green]atid/color
return the_list
end string_to_list
on list_to_string(the_list, the_delim)
my [color=green]atid/color
set the_string to (every text item of the_list) as string
my [color=green]atid/color
return the_string
end list_to_string
–this script was automatically tagged for
–color coded syntax by Script to Markup Code
–written by Jonathan Nathan