util_convert_line_breaks_to_html (public)

 util_convert_line_breaks_to_html [ -includes_html ] [ -contains_pre ] \
    text

Defined in packages/acs-tcl/tcl/text-html-procs.tcl

Convert line breaks to <p> and <br> tags, respectively.

Switches:
-includes_html (optional, boolean)
-contains_pre (optional, boolean)
Parameters:
text (required)

Partial Call Graph (max 5 caller/called nodes):
%3 test_ad_text_to_html ad_text_to_html (test acs-tcl) util_convert_line_breaks_to_html util_convert_line_breaks_to_html test_ad_text_to_html->util_convert_line_breaks_to_html test_util_convert_line_breaks_to_html util_convert_line_breaks_to_html (test acs-tcl) test_util_convert_line_breaks_to_html->util_convert_line_breaks_to_html acs::icanuse acs::icanuse (public) util_convert_line_breaks_to_html->acs::icanuse ad_text_to_html ad_text_to_html (public) ad_text_to_html->util_convert_line_breaks_to_html

Testcases:
util_convert_line_breaks_to_html, ad_text_to_html
Source code:
    # Remove any leading or trailing whitespace
    regsub {^[\s]+} $text {} text
    regsub {[\s]+$} $text {} text

    # Make sure all line breaks are single \n's
    regsub -all -- {\r\n} $text "\n" text
    regsub -all -- {\r} $text "\n" text

    # Remove whitespace before \n's
    regsub -all -- {[ \t]+\n} $text "\n" text

    # Wrap P's around paragraphs
    regsub -all -- {([^\n\s])\n\n+([^\n\s])} $text {\1<p>\2} text

    # Remove line breaks right before and after HTML tags that will
    # insert a paragraph break themselves.
    if { $includes_html_p } {
        set tags [join { ul ol li blockquote p div table tr td th } |]
        #ns_log notice "... 3 RE <\[\r\n\]*(</?($tags)\[\r\n\]*\[^>\]*>)\[\r\n\]*>"
        regsub -all -nocase "\[\r\n\]*(</?($tags)\[\r\n\]*\[^>\]*>)\[\r\n\]*" $text {\1} text
    }

    if {[::acs::icanuse "ns_parsehtml"] && $contains_pre_p} {
        #
        # Convert _single_ CRLF's to <br>'s to preserve line breaks
        # unless inside <PRE> elements.
        #
        set parsed [ns_parsehtml -- $text]
        set out ""
        set inside_pre 0
        foreach token $parsed {
            lassign $token kind chunk parsed
            if {$kind eq "tag"} {
                set tag [string tolower [lindex $parsed 0]]
                if {$tag eq "pre"} {
                    incr inside_pre
                } elseif {$tag eq "/pre"} {
                    incr inside_pre -1
                }
            }
            if {$inside_pre == 0} {
                regsub -all -- {\n} $chunk "<br>\n" chunk
            }
            append out $chunk
        }
        set text $out
    } else {
        # Convert _single_ CRLF's to <br>'s to preserve line breaks
        regsub -all -- {\n} $text "<br>\n" text
    }

    # Add line breaks to P tags
    #regsub -all -- {</p>} $text "</p>\n" text

    return $text
XQL Not present:
Generic, PostgreSQL, Oracle
[ hide source ] | [ make this the default ]
Show another procedure: