summaryrefslogtreecommitdiff
path: root/web/cgi/alpine/1.0/spellcheck.tcl
blob: da4dd565bef6a5ae3fefdcc9696417fcd6b65d47 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
#!./tclsh
# $Id: spellcheck.tcl 1204 2009-02-02 19:54:23Z hubert@u.washington.edu $
# ========================================================================
# Copyright 2006 University of Washington
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# ========================================================================

#  spellcheck.tcl
#
#  Purpose:  CGI script to generate html form used to check
#            body text spelling in the webpine-lite composer

#  Input:
set query_vars {
  {repqstr	""	""}
}

#  Output:
#
#	HTML/Javascript/CSS data representing the page to correct spelling

# inherit global config
source ./alpine.tcl
source cmdfunc.tcl


set check_menu {
  {
    {expr 0}
    {
      {
	# * * * * DONE * * * *
	cgi_puts "<fieldset>"

	cgi_puts [cgi_font size=-1 "When finished, choose action below, then click [cgi_italic OK].[cgi_nl][cgi_nl]"]
	cgi_radio_button spell=spell
	cgi_puts "[cgi_nbspace][cgi_font color=white face=Helvetica size=-1 Correct][cgi_nl]"
	cgi_radio_button spell=cancel
	cgi_puts "[cgi_nbspace][cgi_font color=white face=Helvetica size=-1 Cancel][cgi_nl]"
	cgi_br
	#cgi_image_button action=[WPimg but_s_do] border=0 alt="Do"
	cgi_division "style=padding-bottom:4" align=center {
	  cgi_submit_button "action=OK" class=navtext
	}
	cgi_puts "</fieldset>"
      }
    }
  }
  {
    {}
    {
      {
	# * * * * Done * * * *
	cgi_submit_button "spell=Apply" class=navtext
      }
    }
  }
  {
    {}
    {
      {
	# * * * * Cancel * * * *
	cgi_submit_button "spell=Cancel" class=navtext
      }
    }
  }
  {
    {expr 0}
    {
      {
	# * * * * Help * * * *
	cgi_submit_button "help=Get Help" class=navtext
      }
    }
  }
}

set done 0
set first 1
set spellresult {}
set line {}

proc spelled {pipe} {
  global done spellresult line first

  if {[eof $pipe]} {
    catch {close $pipe}
    set done 1
    return
  }

  gets $pipe w

  if {$first == 0} {
    if {[string length $w]} {
      lappend line $w
    } else {
      lappend spellresult $line
      set line {}
    }
  } else {
    set first 0
  }
}


WPEval $query_vars {
  if {[catch {WPCmd PEInfo set suspended_composition} msgdata]} {
    set problem "Can't read message text"
  } else {
    foreach p $msgdata {
      if {[string compare [lindex $p 0] body] == 0} {
	set body [lindex $p 1]
	break
      }
    }

    if {![info exists body]} {
      set problem "Can't find body in message text"
    } else {
      # spell check and gather results
      # set tmpfile 
      for {set i 0} {$i < 5} {incr i} {
	set tmpfile [file join $_wp(sockdir) "sc[pid][expr rand()]"]
	if {[file exists $tmpfile] == 0} {
	  if {[catch {open $tmpfile w} ofp] == 0} {
	    break
	  }
	}
	unset tmpfile
      }

      if {![info exists tmpfile]} {
	set problem "Can't create temporary file"
      }
    }
  }

  if {![info exists problem]} {
    if {[string length $repqstr]} {
      set quoter $repqstr
    } else {
      set quoter "> "
    }

    foreach l $body {
      if {[string compare $l "---------- Forwarded message ----------"] == 0} {
	break;
      } elseif {[regexp "^$quoter" $l]} {
	puts $ofp ""
      } else {
#	regsub -all {\$} $l {\$} l
	puts $ofp "^${l}"
      }
    }

    close $ofp

    set cmd [list $_wp(ispell) "-a"]
    set pipe [open "|$cmd < $tmpfile 2> /dev/null" r]

    fileevent $pipe readable [list spelled $pipe]

    vwait done

    catch {file delete $tmpfile}
  }

  cgi_http_head {
    WPStdHttpHdrs
  }

  cgi_html {
    cgi_head {
      WPStdHtmlHdr "Check Spelling"
      WPStyleSheets
      cgi_put  "<style type='text/css'>"
      cgi_put  ".correction	{ font-family: geneva, arial, sans-serif ; font-size: 9pt }"
      cgi_puts "</style>"
    }

    cgi_body BGCOLOR="$_wp(bordercolor)" {
      cgi_form $_wp(appdir)/$_wp(ui1dir)/wp method=post enctype=multipart/form-data target=_top {
	cgi_text "page=compose" type=hidden notab
	cgi_text "cid=[WPCmd PEInfo key]" type=hidden notab
	cgi_text "restore=1" type=hidden notab
	cgi_text "style=Spell" type=hidden notab
	cgi_text "last_line=[llength $spellresult]" type=hidden notab
	if {[string length $repqstr]} {
	  cgi_text "repqstr=$repqstr" type=hidden notab
	}

	cgi_table border=0 cellspacing=0 cellpadding=2 width="100%" height="100%" {
	  cgi_table_row {
	    eval {
	      cgi_table_data $_wp(menuargs) {
		WPTFCommandMenu check_menu {}
	      }
	    }

	    cgi_table_data valign=top class=dialog {

	      set badlines {}
	      array set badwords {}
	      set badcount 0
	      for {set n 0} {$n < [llength $spellresult]} {incr n} {
		set words {}
		foreach ms [lindex $spellresult $n] {
		  if {[regexp {& ([a-zA-Z0-9]*) [0-9]+ ([0-9]+):[ ]?(.*)$} $ms match w o s]} {
		    incr badcount
		    if {[regsub -all {, } $s { } sugs] < 0} {
		      continue
		    }

		    lappend words [list $w [expr {$o - 1}] $sugs]
		    if {[info exists badwords($w)]} {
		      incr badwords($w)
		    } else {
		      set badwords($w) 1
		    }
		  } elseif {[regexp {# ([a-zA-Z0-9]*) ([0-9]+)$} $ms match w o]} {
		    incr badcount
		    lappend words [list $w [expr {$o - 1}] {}]
		    if {[info exists badwords($w)]} {
		      incr badwords($w)
		    } else {
		      set badwords($w) 1
		    }
		  }
		}

		if {[llength $words]} {
		  lappend badlines [list $n $words]
		}
	      }

	      if {[info exists problem] || $badcount <= 0} {
		cgi_table align=center valign=top height="100%" {
		  cgi_table_row {
		    cgi_table_data align=center valign=bottom height="20%" {
		      if {[info exists problem]} {
			cgi_puts "Problem detected: $problem"
		      } else {
			cgi_puts "No misspelled words found."
		      }
		    }
		  }
		  cgi_table_row {
		    cgi_table_data align=center valign=top {
		      cgi_put "Click "
		      cgi_submit_button "cancel=Continue" class=navtext
		      cgi_put " to return to your composition."
		    }
		  }
		}
	      } else {
		cgi_table width="95%" border=0 align=center valign=top {
		  cgi_table_row {
		    cgi_table_data align=center "style=padding-top:10;padding-bottom:10" {
		      cgi_puts "Web Alpine found [cgi_bold $badcount] possibly misspelled word[WPplural $badcount]."
		      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."
		      cgi_puts "When finished click [cgi_italic Apply] to correct the text, or [cgi_italic Cancel] to return to the composition unchanged."
		    }
		  }

		  foreach sl $badlines {
		    set lnum [lindex $sl 0]
		    set locs {}

		    cgi_table_row {
		      cgi_table_data bgcolor=white align=left height=20 class=view "style=font-family:courier;padding:8" {
			set ol [lindex $body $lnum]
			set l ""
			set o 0
			foreach w [lindex $sl 1] {
			  set offset [lindex $w 1]
			  set word [lindex $w 0]
			  set wordlen [string length $word]
			  append l "[cgi_quote_html [string range $ol $o [expr {$offset - 1}]]][cgi_url $word "#${lnum}_[lindex $w 1]_${wordlen}" class=mispell]"
			  set o [expr {$offset + $wordlen}]
			}

			append l [string range $ol $o end]

			cgi_put $l
		      }
		    }

		    cgi_table_row {
		      cgi_table_data {
			cgi_table width=90% align=center border=0 {

			  foreach w [lindex $sl 1] {
			    set word [lindex $w 0]
			    set wordlen [string length $word]
			    set wordloc "${lnum}_[lindex $w 1]_${wordlen}"
			    
			    cgi_table_row {

				if {[llength [lindex $w 2]] > 0} {
				  cgi_table_data align=left class=correction nowrap {
				    cgi_put "Replace [cgi_anchor_name $wordloc][cgi_bold $word] with "
				  }

				  cgi_table_data align=left class=correction nowrap {
				    cgi_select s_${wordloc} class=correction {
				      cgi_option "" value=
				      foreach sug [lindex $w 2] {
					cgi_option $sug value=$sug
				      }
				    }
				  }

				  cgi_table_data align=left class=correction nowrap {
				    cgi_put " or change to "
				  }
				} else {
				  cgi_table_data align=left class=correction nowrap {
				    cgi_put "Change [cgi_anchor_name $wordloc][cgi_bold $word] to "
				  }
				}

				cgi_table_data align=left class=correction nowrap {
				  cgi_text r_${wordloc}= "size=[expr {$wordlen + 4}]" maxlength=64 class=correction
				}

				cgi_table_data align=left class=correction width=90% colspan=3 {

				  if {$badwords($word) > 1} {
				    if {![info exists badseen($word)]} {
				      switch $badwords($word) {
					2 { set badtimes both }
					default { set badtimes "all $badwords($word)" }
				      }

				      cgi_put " and "
				      cgi_checkbox a_${wordloc} 
				      cgi_put " apply to $badtimes occurrences"
				      set badseencount($word) 1
				    } else {
				      incr badseencount($word)
				      switch $badseencount($word) {
					2 { set bad1 "second " ; set bad2 "" }
					3 { set bad1 "third " ; set bad2 "" }
					4 { set bad1 "fourth " ; set bad2 "" }
					5 { set bad1 "fifth " ; set bad2 "" }
					6 { set bad1 "sixth " ; set bad2 "" }
					default { set bad1 "" ; set bad2 " $badseencount($word)" }
				      }
				      cgi_put "(${bad1}occurrence${bad2})"
				    }

				    lappend badseen($word) $wordloc
				  } else {
				    cgi_put [cgi_img [WPimg dot2]]
				  }
			      }

			      lappend locs $wordloc
			    }
			  }

			  cgi_table_row {
			    cgi_table_data height=8 {
			      cgi_text "l_$lnum=[join $locs {,}]" type=hidden notab
			    }
			  }
			}
		      }
		    }
		  }

		  cgi_table_row {
		    cgi_table_data align=center height=50 {
		      foreach l [array names badseen] {
			set m $badseen($l)
			cgi_text "e_[lindex $m 0]=[join [lrange $m 1 end] {,}]" type=hidden notab
		      }

		      cgi_submit_button "spell=Apply" class=navtext
		      cgi_submit_button "spell=Cancel" class=navtext
		    }
		  }
		}
	      }
	    }
	  }
	}
      }
    }
  }
}