Home
The Toolkit for Online Communities
17606 Community Members, 0 members online, 2663 visitors today
Log In Register
OpenACS Home : ACS API Browser : ad_html_text_convert

ad_html_text_convert (public)

 ad_html_text_convert [ -from from ] [ -to to ] [ -maxlen maxlen ] \
    [ -truncate_len truncate_len ] [ -ellipsis ellipsis ] \
    [ -more more ] text
Defined in packages/acs-tcl/tcl/text-html-procs.tcl

Converts a chunk of text from a variety of formats to either text/html or text/plain.

Example: ad_html_text_convert -from "text/html" -to "text/plain" -- "text"

Putting in the -- prevents Tcl from treating a - in text portion from being treated as a parameter.

Html to html closes any unclosed html tags (see util_close_html_tags).

Text to html does ad_text_to_html, and html to text does a ad_html_to_text. See those procs for details.

When text is empty, then an empty string will be returned regardless of any format. This is especially useful when displaying content that was created with the richtext widget and might contain empty values for content and format.

Switches:
-from (defaults to "text/plain") (optional)
specify what type of text you're providing. Allowed values:
  • text/plain
  • text/enhanced
  • text/fixed-width
  • text/html
-to (defaults to "text/html") (optional)
specify what format you want this translated into. Allowed values:
  • text/plain
  • text/html
-maxlen (defaults to "70") (optional)
The maximum line width when generating text/plain
-truncate_len (defaults to "0") (optional)
The maximum total length of the output, included ellipsis.
-ellipsis (defaults to "...") (optional)
This will get put at the end of the truncated string, if the string was truncated. However, this counts towards the total string length, so that the returned string including ellipsis is guaranteed to be shorter than the 'truncate_len' provided.
-more (optional)
This will get put at the end of the truncated string, if the string was truncated.
Parameters:
text
Author:
Lars Pind <lars@pinds.com>
Created:
19 July 2000
Source code:
 
    # DRB: Modified this to accept mime types (text/plain or
    # text/html).  Simplies things when providing confirmation pages
    # for input destined for the content repository ...

    if { $text eq "" } {
        return ""
    }
    
    # For backwards compatibility
    set from [ad_decode $from html text/html text text/plain plain text/plain pre text/plain $from]
    set to   [ad_decode $to   html text/html text text/plain plain text/plain pre text/plain $to]

    if { ![ad_html_text_convertable_p -from $from -to $to] } {
        error "Illegal mime types for conversion - from: $from to: $to"
    }
    
    # Do the conversion
    switch $from {
        text/enhanced {
            switch $to {
                text/html {
                    set text [ad_enhanced_text_to_html $text]
                }
                text/plain {
                    set text [ad_enhanced_text_to_plain_text -maxlen $maxlen -- $text]
                }
            }
        }
        text/plain {
            switch $to {
                text/html {
                    set text [ad_text_to_html -- $text]
                }
                text/plain {
                    set text [wrap_string $text $maxlen]
                }
            }
        }
        text/fixed-width {
            switch $to {
                text/html {
                    set text "<pre>[ad_text_to_html -no_lines -- $text]</pre>"
                }
                text/plain {
                    set text [wrap_string $text $maxlen]
                }
            }
        } 
        text/html {
            switch $to {
                text/html {
                    # Handled below
                }
                text/plain {
                    set text [ad_html_to_text -maxlen $maxlen -- $text]
                }
            }
        }
        text/xml {
            switch $to {
                text/html {
                    set text "<pre>[ad_text_to_html -no_lines -- $text]</pre>"
                }
                text/plain {
                    set text [wrap_string $text $maxlen]
                }
            }
        }    
    }
    
    # Handle closing of HTML tags, truncation
    switch $to {
        text/html {
            set text [util_close_html_tags $text $truncate_len $truncate_len $ellipsis $more]
        }
        text/plain {
            set text [string_truncate -ellipsis $ellipsis -more $more -len $truncate_len -- $text]
        }
    }
    
    return $text

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