<!--- Suspend whitespace output --->
<cfsetting enablecfoutputonly="Yes">

<!--- Incoming variables --->
<cfparam name="ATTRIBUTES.Justify" default="Yes">

<!--- Make sure a closing tag exists in calling template --->
<cfif not THISTAG.hasendtag>
        <cfabort showerror="You need to supply a closing tag for (cf_cleanoutput).">
</cfif> 
 
<!--- When the closing tag is encountered --->
<cfif THISTAG.ExecutionMode eq "End">
       
        <!--- Trim whitespace --->
        <cfset THISTAG.GeneratedContent=#trim(THISTAG.GeneratedContent)#>
       
        <!--- If it has <pre> or <code> tags, just leave the whole page alone!! --->
        <!--- When CF reg expressions have non-greedy searching, I'll deal with this more intelligently --->
        <cfif REFindNoCase("<PRE>|<CODE>", THISTAG.GeneratedContent) eq 0>
       
                <!--- Replace whitespace between CR/LFs with a single CF/LF --->
                <!--- That fixes almost all of the major whitespace --->
                <cfset THISTAG.GeneratedContent=#REReplace(THISTAG.GeneratedContent, "[#Chr(13)##Chr(10)#]+[[:space:]]*[#Chr(13)##Chr(10)#]+", "#Chr(13)##Chr(10)#", "All")#>
               
                <!--- Jsutify --->
                <cfif ATTRIBUTES.Justify eq "Yes" or ATTRIBUTES.Justify eq "yes">
                        <cfset THISTAG.GeneratedContent=#REReplace(THISTAG.GeneratedContent, "[#Chr(13)##Chr(10)#][[:space:]]*", "#Chr(13)##Chr(10)#", "All")#>
                </cfif>
        </cfif>
</cfif>

<!--- Re-enable whitespace output --->
<cfsetting enablecfoutputonly="No">