template::util::spellcheck::get_element_formtext (public)

 template::util::spellcheck::get_element_formtext -text text [ -html ] \
    -var_to_spellcheck var_to_spellcheck [ -language language ] \
    -error_num_ref error_num_ref [ -no_abort ] \
    -formtext_to_display_ref formtext_to_display_ref \
    [ -just_the_errwords_ref just_the_errwords_ref ]

Defined in packages/acs-templating/tcl/spellcheck-procs.tcl

Switches:
-text
(required)
The string to check for spelling errors.
-html
(boolean) (defaults to "0") (optional)
-var_to_spellcheck
(required)
The name of the text input type or textarea that holds this text (e.g., "email_body")
-language
(optional)
-error_num_ref
(required)
-no_abort
(boolean) (defaults to "0") (optional)
-formtext_to_display_ref
(required)
-just_the_errwords_ref
(optional)

Partial Call Graph (max 5 caller/called nodes):
%3 test_spellcheck__get_element_formtext spellcheck__get_element_formtext (test acs-templating) template::util::spellcheck::get_element_formtext template::util::spellcheck::get_element_formtext test_spellcheck__get_element_formtext->template::util::spellcheck::get_element_formtext acs_package_root_dir acs_package_root_dir (public) template::util::spellcheck::get_element_formtext->acs_package_root_dir ad_return_error ad_return_error (public) template::util::spellcheck::get_element_formtext->ad_return_error ad_script_abort ad_script_abort (public) template::util::spellcheck::get_element_formtext->ad_script_abort ad_tmpdir ad_tmpdir (public) template::util::spellcheck::get_element_formtext->ad_tmpdir template::data::transform::spellcheck template::data::transform::spellcheck (public) template::data::transform::spellcheck->template::util::spellcheck::get_element_formtext

Testcases:
spellcheck__get_element_formtext
Source code:

    # We need a "var_to_spellcheck" argument so that we may name the hidden errnum vars
    # differently on each input field by prepending the varname.

    set text_to_spell_check $text

    # if HTML then substitute out all HTML tags
    if { $html_p } {
        regsub -all -- {<[^<]*>} $text_to_spell_check "" text_to_spell_check
    }

    set f [file tempfile tmpfile [ad_tmpdir]/webspell-XXXXXX]
    puts $f $text_to_spell_check
    close $f

    set lines [split $text "\n"]

    # Support for local, localized, dictionaries (UI to add to them is not implemented yet!)
    set suffix [expr {$language ne "" ? "-$language" : ""}]
    set dictionaryfile [file join [acs_package_root_dir acs-templating] resources forms webspell-local-dict$suffix]

    # The webspell wrapper is used to achieve independence from 
    # nsd's env(HOME).

    set spelling_wrapper [file join $::acs::rootdir bin webspell]
    if {![file executable $spelling_wrapper]} {
        #
        # In case no_abort is given we just return the error
        # message. Otherwise an ad_return_error is raised and the
        # script is ad_script_aborted.
        #
        if {!$no_abort_p} {
            ad_return_error "Webspell could not be executed"  "Spell-checking is enabled but the spell-check wrapper ($::acs::rootdir/bin/webspell) returns  not be executed. Check that the wrapper exists, and that its permissions are correct."
            ad_script_abort
        } else {
            error $errmsg
        }
    }

    set spellchecker_path [nsv_get spellchecker path]

    #
    # Force default language to en_US
    #set ::env(LANG) en_US.UTF-8

    # the --lang switch only works with aspell and if it is not present
    # aspell's (or ispell's) default language will have to do.
    set lang_and_enc "--encoding=utf-8"
    if { $language ne "" } {
        append lang_and_enc " --lang=$language"
    }

    #ns_log notice WRAPPER=[list |$spelling_wrapper [ns_info home] $spellchecker_path $lang_and_enc $dictionaryfile $tmpfile]

    if {[catch {
        set ispell_lines [exec $spelling_wrapper [ns_info home] $spellchecker_path $lang_and_enc $dictionaryfile $tmpfile]
    } errmsg]} {
        #ns_log notice "errorMsg = $errmsg"

        #
        # In case no_abort is given we just return the error
        # message. Otherwise an ad_return_error is raised and the
        # script is ad_script_aborted.
        #
        if {!$no_abort_p} {
            ad_return_error "No dictionary found"  "Spell-checking is enabled but the spell-check dictionary could not be reached. Check that the dictionary exists, and that its permissions are correct. <p>Here is the error message: <pre>$errmsg</pre>"
            ad_script_abort
        } else {
            error $errmsg
        }
    }

    file delete -- $tmpfile

    ####
    #
    # Ispell is done. Start manipulating the result string.
    #
    ####

    set ispell_lines [split $ispell_lines "\n"]
    # Remove the version line.
    if { [llength $ispell_lines] > 0 } {
        set ispell_lines [lreplace $ispell_lines 0 0]
    }

    ####
    # error_num
    ####
    upvar $error_num_ref error_num

    set error_num 0
    set errors [list]

    set processed_text ""

    set line [lindex $lines 0]

    foreach ispell_line $ispell_lines {
        switch -glob -- $ispell_line {
            \#* {
                regexp "^\# (\[^ \]+) (\[0-9\]+)" $ispell_line dummy word pos
                regsub $word $line "\#$error_num\#" line
                lappend errors [list miss $error_num $word dummy]
                incr error_num
            }
            &* {
                regexp {^& ([^ ]+) ([0-9]+) ([0-9]+): (.*)$} $ispell_line dummy word n_options pos options
                regsub $word $line "\#$error_num\#" line
                lappend errors [list nearmiss $error_num $word $options]
                incr error_num
            }
            "" {
                append processed_text "$line\n"
                if { [llength $lines] > 0 } {
                    set lines [lreplace $lines 0 0]
                    set line [lindex $lines 0]
                }
            }
        }
    }

    set formtext $processed_text

    set error_list [join $errors]

    foreach { errtype errnum errword erroptions } $error_list {
        set wordlen [string length $errword]

        if {"miss" eq $errtype} {
            regsub "\#$errnum\#" $formtext "<input type=\"text\" name=\"${var_to_spellcheck}.error_$errnum\" value=\"$errword\" size=\"$wordlen\" >" formtext
        } elseif {"nearmiss" eq $errtype} {
            regsub -all -- ", " $erroptions "," erroptions
            set options [split $erroptions ","]
            set select_text "<select name=\"${var_to_spellcheck}.error_$errnum\">\n<option value=\"$errword\">$errword</option>\n"
            foreach option $options {
                append select_text "<option value=\"$option\">$option</option>\n"
            }
            append select_text "</select>"
            regsub "\#$errnum\#" $formtext $select_text formtext
        }
    }

    ####
    # formtext_to_display
    ####
    upvar $formtext_to_display_ref formtext_to_display

    regsub -all -- "\r\n" $formtext "<br>" formtext_to_display

    # We replace <a></a> with  <u></u> because misspelled text in link titles
    # would lead to strange browser behavior where the select boxes with the
    # proposed changes would itself be a link!!!
    # It seemed like an okay idea to make the text underlined so it would a) work,
    # b) still resemble a link ...
    regsub -all -- {<a [^<]*>} $formtext_to_display "<u>" formtext_to_display
    regsub -all -- {</a>} $formtext_to_display "</u>" formtext_to_display

    append formtext_to_display "<input type=\"hidden\" name=\"${var_to_spellcheck}.merge_text\" value=\"[ns_quotehtml $processed_text]\" >"


    ####
    # just_the_errwords
    ####

    if { $just_the_errwords_ref ne ""} {

        upvar $just_the_errwords_ref just_the_errwords

        set just_the_errwords [list]
        foreach err $errors {
            lappend just_the_errwords [lindex $err 2]
        }

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