Turning text to html


Author: David Hood
Contents Page - Text to HTML
Date Last Modified: 25th August 2003
E-mail: caversham@otago.ac.nz

Previous Page 2 of 2 Contents Page

This script includes translations for all the character entities I commonly encounter, but can easily be expanded to cope with others. It also adds <BR> tags wherever it sees a new line symbol. This can also easily be changed as needed.

set nl to (ASCII character 10) --unix newline ending
set cr to (ASCII character 13) --mac newline ending
set text2html to (the clipboard) as text
set text2html to replaceFn(text2html, "&", "ampersandreplace")
set text2html to replaceFn(text2html, ";", "semicolreplace")
set text2html to replaceFn(text2html, "#", "hashmarkreplace")
set text2html to replaceFn(text2html, "<", "&lt;")
set text2html to replaceFn(text2html, ">", "&gt;")
set text2html to replaceFn(text2html, "\"", "&quot;")
set text2html to replaceFn(text2html, ":", "&#58;")
set text2html to replaceFn(text2html, "'", "&#39;")
set text2html to replaceFn(text2html, "(", "&#40;")
set text2html to replaceFn(text2html, ")", "&#41;")
set text2html to replaceFn(text2html, "*", "&#42;")
set text2html to replaceFn(text2html, "{", "&#123;")
set text2html to replaceFn(text2html, "}", "&#125;")
set text2html to replaceFn(text2html, "⁄", "&frasl;")
set text2html to replaceFn(text2html, "\\", "&#92;")
set text2html to replaceFn(text2html, "‘ ", "&lsquo; ")
set text2html to replaceFn(text2html, "’ ", "&rsquo; ")
set text2html to replaceFn(text2html, "“ ", "&ldquo; ")
set text2html to replaceFn(text2html, "” ", "&rdquo; ")
--add new entries below this

--add new entries above this
set text2html to replaceFn(text2html, "ampersandreplace", "&amp;")
set text2html to replaceFn(text2html, "semicolreplace", "&#59;")
set text2html to replaceFn(text2html, "hashmarkreplace", "&#35;")
set text2html to replaceFn(text2html, nl, ("<BR>" & nl))
set text2html to replaceFn(text2html, cr, ("<BR>" & cr))
set the clipboard to text2html

on replaceFn(givenText, replaceFrom, replaceWith)
set AppleScript's text item delimiters to the replaceFrom
set the listOfChars to every text item of givenText
set AppleScript's text item delimiters to the replaceWith
set givenText to the listOfChars as string
set AppleScript's text item delimiters to ""
return givenText
end replaceFn

Return to Contents Page