~ubuntu-branches/debian/squeeze/alpine/squeeze

« back to all changes in this revision

Viewing changes to web/cgi/alpine/spellcheck.tcl

  • Committer: Bazaar Package Importer
  • Author(s): Asheesh Laroia
  • Date: 2007-02-17 13:17:42 UTC
  • Revision ID: james.westby@ubuntu.com-20070217131742-99x5c6cpg1pbkdhw
Tags: upstream-0.82+dfsg
ImportĀ upstreamĀ versionĀ 0.82+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!./tclsh
 
2
# $Id: spellcheck.tcl 391 2007-01-25 03:53:59Z mikes@u.washington.edu $
 
3
# ========================================================================
 
4
# Copyright 2006 University of Washington
 
5
#
 
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
 
9
#
 
10
#     http://www.apache.org/licenses/LICENSE-2.0
 
11
#
 
12
# ========================================================================
 
13
 
 
14
#  spellcheck.tcl
 
15
#
 
16
#  Purpose:  CGI script to generate html form used to check
 
17
#            body text spelling in the webpine-lite composer
 
18
 
 
19
#  Input:
 
20
set query_vars {
 
21
  {repqstr      ""      ""}
 
22
}
 
23
 
 
24
#  Output:
 
25
#
 
26
#       HTML/Javascript/CSS data representing the page to correct spelling
 
27
 
 
28
# inherit global config
 
29
source ./alpine.tcl
 
30
source cmdfunc.tcl
 
31
 
 
32
 
 
33
set check_menu {
 
34
  {
 
35
    {expr 0}
 
36
    {
 
37
      {
 
38
        # * * * * DONE * * * *
 
39
        cgi_puts "<fieldset>"
 
40
 
 
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]"
 
46
        cgi_br
 
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
 
50
        }
 
51
        cgi_puts "</fieldset>"
 
52
      }
 
53
    }
 
54
  }
 
55
  {
 
56
    {}
 
57
    {
 
58
      {
 
59
        # * * * * Done * * * *
 
60
        cgi_submit_button "spell=Apply" class=navtext
 
61
      }
 
62
    }
 
63
  }
 
64
  {
 
65
    {}
 
66
    {
 
67
      {
 
68
        # * * * * Cancel * * * *
 
69
        cgi_submit_button "spell=Cancel" class=navtext
 
70
      }
 
71
    }
 
72
  }
 
73
  {
 
74
    {expr 0}
 
75
    {
 
76
      {
 
77
        # * * * * Help * * * *
 
78
        cgi_submit_button "help=Get Help" class=navtext
 
79
      }
 
80
    }
 
81
  }
 
82
}
 
83
 
 
84
set done 0
 
85
set first 1
 
86
set spellresult {}
 
87
set line {}
 
88
 
 
89
proc spelled {pipe} {
 
90
  global done spellresult line first
 
91
 
 
92
  if {[eof $pipe]} {
 
93
    catch {close $pipe}
 
94
    set done 1
 
95
    return
 
96
  }
 
97
 
 
98
  gets $pipe w
 
99
 
 
100
  if {$first == 0} {
 
101
    if {[string length $w]} {
 
102
      lappend line $w
 
103
    } else {
 
104
      lappend spellresult $line
 
105
      set line {}
 
106
    }
 
107
  } else {
 
108
    set first 0
 
109
  }
 
110
}
 
111
 
 
112
 
 
113
WPEval $query_vars {
 
114
  if {[catch {WPCmd PEInfo set suspended_composition} msgdata]} {
 
115
    set problem "Can't read message text"
 
116
  } else {
 
117
    foreach p $msgdata {
 
118
      if {[string compare [lindex $p 0] body] == 0} {
 
119
        set body [lindex $p 1]
 
120
        break
 
121
      }
 
122
    }
 
123
 
 
124
    if {![info exists body]} {
 
125
      set problem "Can't find body in message text"
 
126
    } else {
 
127
      # spell check and gather results
 
128
      # set tmpfile 
 
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} {
 
133
            break
 
134
          }
 
135
        }
 
136
        unset tmpfile
 
137
      }
 
138
 
 
139
      if {![info exists tmpfile]} {
 
140
        set problem "Can't create temporary file"
 
141
      }
 
142
    }
 
143
  }
 
144
 
 
145
  if {![info exists problem]} {
 
146
    if {[string length $repqstr]} {
 
147
      set quoter $repqstr
 
148
    } else {
 
149
      set quoter "> "
 
150
    }
 
151
 
 
152
    foreach l $body {
 
153
      if {[string compare $l "---------- Forwarded message ----------"] == 0} {
 
154
        break;
 
155
      } elseif {[regexp "^$quoter" $l]} {
 
156
        puts $ofp ""
 
157
      } else {
 
158
#       regsub -all {\$} $l {\$} l
 
159
        puts $ofp "^${l}"
 
160
      }
 
161
    }
 
162
 
 
163
    close $ofp
 
164
 
 
165
    set cmd [list $_wp(ispell) "-a"]
 
166
    set pipe [open "|$cmd < $tmpfile 2> /dev/null" r]
 
167
 
 
168
    fileevent $pipe readable [list spelled $pipe]
 
169
 
 
170
    vwait done
 
171
 
 
172
    catch {file delete $tmpfile}
 
173
  }
 
174
 
 
175
  cgi_http_head {
 
176
    WPStdHttpHdrs
 
177
  }
 
178
 
 
179
  cgi_html {
 
180
    cgi_head {
 
181
      WPStdHtmlHdr "Check Spelling"
 
182
      WPStyleSheets
 
183
      cgi_put  "<style type='text/css'>"
 
184
      cgi_put  ".correction     { font-family: geneva, arial, sans-serif ; font-size: 9pt }"
 
185
      cgi_puts "</style>"
 
186
    }
 
187
 
 
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
 
197
        }
 
198
 
 
199
        cgi_table border=0 cellspacing=0 cellpadding=2 width="100%" height="100%" {
 
200
          cgi_table_row {
 
201
            eval {
 
202
              cgi_table_data $_wp(menuargs) {
 
203
                WPTFCommandMenu check_menu {}
 
204
              }
 
205
            }
 
206
 
 
207
            cgi_table_data valign=top class=dialog {
 
208
 
 
209
              set badlines {}
 
210
              array set badwords {}
 
211
              set badcount 0
 
212
              for {set n 0} {$n < [llength $spellresult]} {incr n} {
 
213
                set words {}
 
214
                foreach ms [lindex $spellresult $n] {
 
215
                  if {[regexp {& ([a-zA-Z0-9]*) [0-9]+ ([0-9]+):[ ]?(.*)$} $ms match w o s]} {
 
216
                    incr badcount
 
217
                    if {[regsub -all {, } $s { } sugs] < 0} {
 
218
                      continue
 
219
                    }
 
220
 
 
221
                    lappend words [list $w [expr {$o - 1}] $sugs]
 
222
                    if {[info exists badwords($w)]} {
 
223
                      incr badwords($w)
 
224
                    } else {
 
225
                      set badwords($w) 1
 
226
                    }
 
227
                  } elseif {[regexp {# ([a-zA-Z0-9]*) ([0-9]+)$} $ms match w o]} {
 
228
                    incr badcount
 
229
                    lappend words [list $w [expr {$o - 1}] {}]
 
230
                    if {[info exists badwords($w)]} {
 
231
                      incr badwords($w)
 
232
                    } else {
 
233
                      set badwords($w) 1
 
234
                    }
 
235
                  }
 
236
                }
 
237
 
 
238
                if {[llength $words]} {
 
239
                  lappend badlines [list $n $words]
 
240
                }
 
241
              }
 
242
 
 
243
              if {[info exists problem] || $badcount <= 0} {
 
244
                cgi_table align=center valign=top height="100%" {
 
245
                  cgi_table_row {
 
246
                    cgi_table_data align=center valign=bottom heigh="20%" {
 
247
                      if {[info exists problem]} {
 
248
                        cgi_puts "Problem detected: $problem"
 
249
                      } else {
 
250
                        cgi_puts "No misspelled words found."
 
251
                      }
 
252
                    }
 
253
                  }
 
254
                  cgi_table_row {
 
255
                    cgi_table_data align=center valign=top {
 
256
                      cgi_put "Click "
 
257
                      cgi_submit_button "cancel=Continue" class=navtext
 
258
                      cgi_put " to return to your composition."
 
259
                    }
 
260
                  }
 
261
                }
 
262
              } else {
 
263
                cgi_table width="95%" border=0 align=center valign=top {
 
264
                  cgi_table_row {
 
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."
 
269
                    }
 
270
                  }
 
271
 
 
272
                  foreach sl $badlines {
 
273
                    set lnum [lindex $sl 0]
 
274
                    set locs {}
 
275
 
 
276
                    cgi_table_row {
 
277
                      cgi_table_data bgcolor=white align=left height=20 class=view "style=font-family:courier;padding:8" {
 
278
                        set ol [lindex $body $lnum]
 
279
                        set l ""
 
280
                        set o 0
 
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}]
 
287
                        }
 
288
 
 
289
                        append l [string range $ol $o end]
 
290
 
 
291
                        cgi_put $l
 
292
                      }
 
293
                    }
 
294
 
 
295
                    cgi_table_row {
 
296
                      cgi_table_data {
 
297
                        cgi_table width=90% align=center border=0 {
 
298
 
 
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}"
 
303
                            
 
304
                            cgi_table_row {
 
305
 
 
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 "
 
309
                                  }
 
310
 
 
311
                                  cgi_table_data align=left class=correction nowrap {
 
312
                                    cgi_select s_${wordloc} class=correction {
 
313
                                      cgi_option "" value=
 
314
                                      foreach sug [lindex $w 2] {
 
315
                                        cgi_option $sug value=$sug
 
316
                                      }
 
317
                                    }
 
318
                                  }
 
319
 
 
320
                                  cgi_table_data align=left class=correction nowrap {
 
321
                                    cgi_put " or change to "
 
322
                                  }
 
323
                                } else {
 
324
                                  cgi_table_data align=left class=correction nowrap {
 
325
                                    cgi_put "Change [cgi_anchor_name $wordloc][cgi_bold $word] to "
 
326
                                  }
 
327
                                }
 
328
 
 
329
                                cgi_table_data align=left class=correction nowrap {
 
330
                                  cgi_text r_${wordloc}= "size=[expr {$wordlen + 4}]" maxlength=64 class=correction
 
331
                                }
 
332
 
 
333
                                cgi_table_data align=left class=correction width=90% colspan=3 {
 
334
 
 
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)" }
 
340
                                      }
 
341
 
 
342
                                      cgi_put " and "
 
343
                                      cgi_checkbox a_${wordloc} 
 
344
                                      cgi_put " apply to $badtimes occurrences"
 
345
                                      set badseencount($word) 1
 
346
                                    } else {
 
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)" }
 
355
                                      }
 
356
                                      cgi_put "(${bad1}occurrence${bad2})"
 
357
                                    }
 
358
 
 
359
                                    lappend badseen($word) $wordloc
 
360
                                  } else {
 
361
                                    cgi_put [cgi_img [WPimg dot2]]
 
362
                                  }
 
363
                              }
 
364
 
 
365
                              lappend locs $wordloc
 
366
                            }
 
367
                          }
 
368
 
 
369
                          cgi_table_row {
 
370
                            cgi_table_data height=8 {
 
371
                              cgi_text "l_$lnum=[join $locs {,}]" type=hidden notab
 
372
                            }
 
373
                          }
 
374
                        }
 
375
                      }
 
376
                    }
 
377
                  }
 
378
 
 
379
                  cgi_table_row {
 
380
                    cgi_table_data align=center height=50 {
 
381
                      foreach l [array names badseen] {
 
382
                        set m $badseen($l)
 
383
                        cgi_text "e_[lindex $m 0]=[join [lrange $m 1 end] {,}]" type=hidden notab
 
384
                      }
 
385
 
 
386
                      cgi_submit_button "spell=Apply" class=navtext
 
387
                      cgi_submit_button "spell=Cancel" class=navtext
 
388
                    }
 
389
                  }
 
390
                }
 
391
              }
 
392
            }
 
393
          }
 
394
        }
 
395
      }
 
396
    }
 
397
  }
 
398
}
 
399