1: # The LearningOnline Network with CAPA
2: # Interface routines for Aspell
3: #
4: # $Id: lonspeller.pm,v 1.6 2004/07/27 23:35:34 www Exp $
5: #
6: # Copyright Michigan State University Board of Trustees
7: #
8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
9: #
10: # LON-CAPA is free software; you can redistribute it and/or modify
11: # it under the terms of the GNU General Public License as published by
12: # the Free Software Foundation; either version 2 of the License, or
13: # (at your option) any later version.
14: #
15: # LON-CAPA is distributed in the hope that it will be useful,
16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18: # GNU General Public License for more details.
19: #
20: # You should have received a copy of the GNU General Public License
21: # along with LON-CAPA; if not, write to the Free Software
22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23: #
24: # /home/httpd/html/adm/gpl.txt
25: #
26: # http://www.lon-capa.org/
27: #
28: ######################################################################
29: ######################################################################
30:
31: package Apache::lonspeller;
32:
33: use Apache::Constants qw(:common);
34: use Text::Aspell;
35: use Apache::lonlocal;
36: use HTML::LCParser;
37: use strict;
38:
39: my $speller;
40: my $insidelink;
41:
42: sub spellcheck_language {
43: if ($ENV{'form.lang'}) { return $ENV{'form.lang'}; }
44: if (&mt('spellcheck_lang') ne 'spellcheck_lang') {
45: return &mt('spellcheck_lang');
46: }
47: return 'en_US';
48: }
49:
50: sub set_language {
51: my $lang=&spellcheck_language();
52: $speller->set_option('lang',$lang);
53: }
54:
55: sub textsection {
56: my $input=shift;
57: my $output='';
58: &set_language();
59: foreach my $word (split(/\b/,$input)) {
60: if (($word=~/\W/) || ($word=~/^(lt|gt|nbsp|amp)$/i)
61: || ($speller->check($word))) {
62: $output.=$word;
63: } else {
64: my $suggestions=join(' ',$speller->suggest($word));
65: $suggestions=~s/\'/\\\'/gs;
66: if (($suggestions) && (!$insidelink)) {
67: $output.='<a href="javascript:alert('."'".
68: $suggestions."');".
69: '">';
70: }
71: $output.='<font color="red">'.$word.'</font>';
72: if (($suggestions) && (!$insidelink)) { $output.='</a>'; }
73: }
74: }
75: return $output;
76: }
77:
78:
79: sub markeduptext {
80: my $input=shift;
81: my $output='';
82: my $parser=HTML::LCParser->new(\$input);
83: $insidelink=0;
84: my $token;
85: while ($token=$parser->get_token) {
86: if ($token->[0] eq 'T') {
87: $output.=&textsection($token->[1]);
88: } elsif ($token->[0] eq 'S') {
89: $output.=$token->[4];
90: foreach my $tag ('m','script') {
91: if ($token->[1] eq $tag) {
92: $output.=$parser->get_text('/'.$tag);
93: }
94: }
95: if ($token->[1] eq 'a') {
96: $insidelink=1;
97: }
98: } elsif ($token->[0] eq 'E') {
99: $output.=$token->[2];
100: if ($token->[1] eq 'a') {
101: $insidelink=0;
102: }
103: }
104: }
105: $insidelink=0;
106: return $output;
107: }
108:
109: sub initspeller {
110: unless (defined($speller)) {
111: $speller = Text::Aspell->new;
112: $speller->set_option('lang','en_US');
113: }
114: $insidelink=0;
115: }
116:
117: sub handler {
118: my $r = shift;
119: &Apache::loncommon::content_type($r,'text/html');
120: $r->send_http_header;
121: return OK if $r->header_only;
122:
123: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'});
124:
125: &initspeller();
126: $r->print('<html><head><title>'.
127: &mt('Spell Checker').
128: '</title></head><body bgcolor="#DDDDDD">'.
129: &markeduptext($ENV{'form.text'}).
130: '</body></html>');
131: return OK;
132: }
133:
134: BEGIN {
135: &initspeller();
136: }
137:
138: 1;
139:
140: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>