2
# $Id: spellcheck.tcl 391 2007-01-25 03:53:59Z mikes@u.washington.edu $
3
# ========================================================================
4
# Copyright 2006 University of Washington
6
# Licensed under the Apache License, Version 2.0 (the "License");
7
# you may not use this file except in compliance with the License.
8
# You may obtain a copy of the License at
10
# http://www.apache.org/licenses/LICENSE-2.0
12
# ========================================================================
16
# Purpose: CGI script to generate html form used to check
17
# body text spelling in the webpine-lite composer
26
# HTML/Javascript/CSS data representing the page to correct spelling
28
# inherit global config
38
# * * * * DONE * * * *
41
cgi_puts [cgi_font size=-1 "When finished, choose action below, then click [cgi_italic OK].[cgi_nl][cgi_nl]"]
42
cgi_radio_button spell=spell
43
cgi_puts "[cgi_nbspace][cgi_font color=white face=Helvetica size=-1 Correct][cgi_nl]"
44
cgi_radio_button spell=cancel
45
cgi_puts "[cgi_nbspace][cgi_font color=white face=Helvetica size=-1 Cancel][cgi_nl]"
47
#cgi_image_button action=[WPimg but_s_do] border=0 alt="Do"
48
cgi_division "style=padding-bottom:4" align=center {
49
cgi_submit_button "action=OK" class=navtext
51
cgi_puts "</fieldset>"
59
# * * * * Done * * * *
60
cgi_submit_button "spell=Apply" class=navtext
68
# * * * * Cancel * * * *
69
cgi_submit_button "spell=Cancel" class=navtext
77
# * * * * Help * * * *
78
cgi_submit_button "help=Get Help" class=navtext
90
global done spellresult line first
101
if {[string length $w]} {
104
lappend spellresult $line
114
if {[catch {WPCmd PEInfo set suspended_composition} msgdata]} {
115
set problem "Can't read message text"
118
if {[string compare [lindex $p 0] body] == 0} {
119
set body [lindex $p 1]
124
if {![info exists body]} {
125
set problem "Can't find body in message text"
127
# spell check and gather results
129
for {set i 0} {$i < 5} {incr i} {
130
set tmpfile [file join $_wp(sockdir) "sc[pid][expr rand()]"]
131
if {[file exists $tmpfile] == 0} {
132
if {[catch {open $tmpfile w} ofp] == 0} {
139
if {![info exists tmpfile]} {
140
set problem "Can't create temporary file"
145
if {![info exists problem]} {
146
if {[string length $repqstr]} {
153
if {[string compare $l "---------- Forwarded message ----------"] == 0} {
155
} elseif {[regexp "^$quoter" $l]} {
158
# regsub -all {\$} $l {\$} l
165
set cmd [list $_wp(ispell) "-a"]
166
set pipe [open "|$cmd < $tmpfile 2> /dev/null" r]
168
fileevent $pipe readable [list spelled $pipe]
172
catch {file delete $tmpfile}
181
WPStdHtmlHdr "Check Spelling"
183
cgi_put "<style type='text/css'>"
184
cgi_put ".correction { font-family: geneva, arial, sans-serif ; font-size: 9pt }"
188
cgi_body BGCOLOR="$_wp(bordercolor)" {
189
cgi_form $_wp(appdir)/wp method=post enctype=multipart/form-data target=_top {
190
cgi_text "page=compose" type=hidden notab
191
cgi_text "cid=[WPCmd PEInfo key]" type=hidden notab
192
cgi_text "restore=1" type=hidden notab
193
cgi_text "style=Spell" type=hidden notab
194
cgi_text "last_line=[llength $spellresult]" type=hidden notab
195
if {[string length $repqstr]} {
196
cgi_text "repqstr=$repqstr" type=hidden notab
199
cgi_table border=0 cellspacing=0 cellpadding=2 width="100%" height="100%" {
202
cgi_table_data $_wp(menuargs) {
203
WPTFCommandMenu check_menu {}
207
cgi_table_data valign=top class=dialog {
210
array set badwords {}
212
for {set n 0} {$n < [llength $spellresult]} {incr n} {
214
foreach ms [lindex $spellresult $n] {
215
if {[regexp {& ([a-zA-Z0-9]*) [0-9]+ ([0-9]+):[ ]?(.*)$} $ms match w o s]} {
217
if {[regsub -all {, } $s { } sugs] < 0} {
221
lappend words [list $w [expr {$o - 1}] $sugs]
222
if {[info exists badwords($w)]} {
227
} elseif {[regexp {# ([a-zA-Z0-9]*) ([0-9]+)$} $ms match w o]} {
229
lappend words [list $w [expr {$o - 1}] {}]
230
if {[info exists badwords($w)]} {
238
if {[llength $words]} {
239
lappend badlines [list $n $words]
243
if {[info exists problem] || $badcount <= 0} {
244
cgi_table align=center valign=top height="100%" {
246
cgi_table_data align=center valign=bottom heigh="20%" {
247
if {[info exists problem]} {
248
cgi_puts "Problem detected: $problem"
250
cgi_puts "No misspelled words found."
255
cgi_table_data align=center valign=top {
257
cgi_submit_button "cancel=Continue" class=navtext
258
cgi_put " to return to your composition."
263
cgi_table width="95%" border=0 align=center valign=top {
265
cgi_table_data align=center "style=padding-top:10;padding-bottom:10" {
266
cgi_puts "Web Alpine found [cgi_bold $badcount] possibly misspelled word[WPplural $badcount]."
267
cgi_puts "Grouped by the line on which they were found, misspelled words can be corrected by either selecting from the list of suggestions, when available (note, first option always blank), or entering the corrected spelling directly."
268
cgi_puts "When finished click [cgi_italic Apply] to correct the text, or [cgi_italic Cancel] to return to the composition unchanged."
272
foreach sl $badlines {
273
set lnum [lindex $sl 0]
277
cgi_table_data bgcolor=white align=left height=20 class=view "style=font-family:courier;padding:8" {
278
set ol [lindex $body $lnum]
281
foreach w [lindex $sl 1] {
282
set offset [lindex $w 1]
283
set word [lindex $w 0]
284
set wordlen [string length $word]
285
append l "[cgi_quote_html [string range $ol $o [expr {$offset - 1}]]][cgi_url $word "#${lnum}_[lindex $w 1]_${wordlen}" class=mispell]"
286
set o [expr {$offset + $wordlen}]
289
append l [string range $ol $o end]
297
cgi_table width=90% align=center border=0 {
299
foreach w [lindex $sl 1] {
300
set word [lindex $w 0]
301
set wordlen [string length $word]
302
set wordloc "${lnum}_[lindex $w 1]_${wordlen}"
306
if {[llength [lindex $w 2]] > 0} {
307
cgi_table_data align=left class=correction nowrap {
308
cgi_put "Replace [cgi_anchor_name $wordloc][cgi_bold $word] with "
311
cgi_table_data align=left class=correction nowrap {
312
cgi_select s_${wordloc} class=correction {
314
foreach sug [lindex $w 2] {
315
cgi_option $sug value=$sug
320
cgi_table_data align=left class=correction nowrap {
321
cgi_put " or change to "
324
cgi_table_data align=left class=correction nowrap {
325
cgi_put "Change [cgi_anchor_name $wordloc][cgi_bold $word] to "
329
cgi_table_data align=left class=correction nowrap {
330
cgi_text r_${wordloc}= "size=[expr {$wordlen + 4}]" maxlength=64 class=correction
333
cgi_table_data align=left class=correction width=90% colspan=3 {
335
if {$badwords($word) > 1} {
336
if {![info exists badseen($word)]} {
337
switch $badwords($word) {
338
2 { set badtimes both }
339
default { set badtimes "all $badwords($word)" }
343
cgi_checkbox a_${wordloc}
344
cgi_put " apply to $badtimes occurrences"
345
set badseencount($word) 1
347
incr badseencount($word)
348
switch $badseencount($word) {
349
2 { set bad1 "second " ; set bad2 "" }
350
3 { set bad1 "third " ; set bad2 "" }
351
4 { set bad1 "fourth " ; set bad2 "" }
352
5 { set bad1 "fifth " ; set bad2 "" }
353
6 { set bad1 "sixth " ; set bad2 "" }
354
default { set bad1 "" ; set bad2 " $badseencount($word)" }
356
cgi_put "(${bad1}occurrence${bad2})"
359
lappend badseen($word) $wordloc
361
cgi_put [cgi_img [WPimg dot2]]
365
lappend locs $wordloc
370
cgi_table_data height=8 {
371
cgi_text "l_$lnum=[join $locs {,}]" type=hidden notab
380
cgi_table_data align=center height=50 {
381
foreach l [array names badseen] {
383
cgi_text "e_[lindex $m 0]=[join [lrange $m 1 end] {,}]" type=hidden notab
386
cgi_submit_button "spell=Apply" class=navtext
387
cgi_submit_button "spell=Cancel" class=navtext