Last active
April 25, 2019 07:40
-
-
Save JamoCA/442fc15b4714b848f9882e840185b256 to your computer and use it in GitHub Desktop.
HTMLParagraphFormat - Replaces ColdFusion ParagraphFormat Tag. (Influenced by 3 other UDFs.)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<cfscript> | |
/* 20190920 HTMLParagraphFormat | |
https://gist.github.com/JamoCA/442fc15b4714b848f9882e840185b256 | |
Based on prior examples from: | |
https://cfdocs.org/paragraphformat | |
https://www.petefreitag.com/item/344.cfm Pete Freitag May 06, 2005 | |
https://cflib.org/udf/XHTMLParagraphFormat Jeff Howden ([email protected]) 1, July 2, 2008 | |
https://cflib.org/udf/ParagraphFormat2 Ben Forta ([email protected]) June 26, 2002 | |
Returns text wrapped in properly formatted paragraph <p> tags. | |
@param string String you want formatted. (Required) | |
@param attributeString Optional tag attributes to add to all paragraph tags (ie attributeString="class=""c"" style=""font-family:tahoma"";"). | |
@return Returns a string. | |
@author James Moberg http://www.sunstarmedia.com/ | |
@version 1, August 20, 2018 */ | |
function HTMLParagraphFormat(plainText){ | |
var response = javacast("string", arguments.plainText); | |
if(Len(Trim(response)) AND NOT Find("<p", response)){ | |
response = replace(response, chr(13) & chr(10), chr(31), "ALL"); /* Windows to Unix */ | |
response = replace(response, chr(13), chr(31), "ALL"); /* Mac to Unix */ | |
response = "<p>" & rereplace(response, "(#chr(31)#){2,}", "</p><p>", "ALL") & "</p>"; | |
response = rereplace(response, chr(31), "<br>", "all"); | |
response = rereplace(response, "\t", " ", "ALL"); /* Tab */ | |
response = replace(response, "</p>", "</p>" & Chr(13) & Chr(10) & Chr(13) & Chr(10), "all"); | |
response = trim(replace(response, "<br>", "<br>" & Chr(13) & Chr(10), "all")); | |
} | |
if(ArrayLen(arguments) GTE 2 AND LEN(trim(arguments[2])) AND isSimpleValue(arguments[2])){ | |
response = replacenocase(response, "<p>", "<p " & trim(arguments[2]) & ">", "all"); | |
} | |
return response; | |
} | |
</cfscript> | |
<CFSAVECONTENT VARIABLE="temp">This is a test. | |
This is on the very next line (without a double space.) | |
This is next line with a tab. | |
This should be on a line all by itself.</CFSAVECONTENT> | |
<CFOUTPUT> | |
<fieldset><legend>TEXT/HTML Source</legend> | |
<textarea cols="70" rows="10">#Temp#</textarea> | |
<textarea cols="70" rows="10">#HTMLEditFormat(HTMLParagraphFormat(Temp))#</textarea> | |
</fieldset> | |
<fieldset><legend>Plain Text Render</legend> | |
#Temp# | |
</fieldset> | |
<fieldset><legend>HTML Render</legend> | |
#HTMLParagraphFormat(Temp)# | |
</fieldset> | |
</CFOUTPUT> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment