Annotation of loncom/interface/lonspeller.pm, revision 1.7
1.1 www 1: # The LearningOnline Network with CAPA
2: # Interface routines for Aspell
3: #
1.7 ! www 4: # $Id: lonspeller.pm,v 1.6 2004/07/27 23:35:34 www Exp $
1.1 www 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:
1.5 www 33: use Apache::Constants qw(:common);
1.1 www 34: use Text::Aspell;
35: use Apache::lonlocal;
1.7 ! www 36: use Apache::lontexconvert();
1.5 www 37: use HTML::LCParser;
1.1 www 38: use strict;
1.5 www 39:
1.2 www 40: my $speller;
1.6 www 41: my $insidelink;
1.1 www 42:
1.4 www 43: sub spellcheck_language {
44: if ($ENV{'form.lang'}) { return $ENV{'form.lang'}; }
45: if (&mt('spellcheck_lang') ne 'spellcheck_lang') {
46: return &mt('spellcheck_lang');
47: }
48: return 'en_US';
49: }
50:
51: sub set_language {
52: my $lang=&spellcheck_language();
53: $speller->set_option('lang',$lang);
54: }
55:
1.5 www 56: sub textsection {
1.2 www 57: my $input=shift;
58: my $output='';
1.4 www 59: &set_language();
1.6 www 60: foreach my $word (split(/\b/,$input)) {
61: if (($word=~/\W/) || ($word=~/^(lt|gt|nbsp|amp)$/i)
62: || ($speller->check($word))) {
63: $output.=$word;
1.2 www 64: } else {
65: my $suggestions=join(' ',$speller->suggest($word));
66: $suggestions=~s/\'/\\\'/gs;
1.6 www 67: if (($suggestions) && (!$insidelink)) {
1.2 www 68: $output.='<a href="javascript:alert('."'".
69: $suggestions."');".
70: '">';
71: }
72: $output.='<font color="red">'.$word.'</font>';
1.6 www 73: if (($suggestions) && (!$insidelink)) { $output.='</a>'; }
1.2 www 74: }
75: }
76: return $output;
77: }
78:
1.5 www 79:
80: sub markeduptext {
81: my $input=shift;
82: my $output='';
83: my $parser=HTML::LCParser->new(\$input);
1.6 www 84: $insidelink=0;
1.5 www 85: my $token;
86: while ($token=$parser->get_token) {
87: if ($token->[0] eq 'T') {
88: $output.=&textsection($token->[1]);
89: } elsif ($token->[0] eq 'S') {
90: $output.=$token->[4];
1.6 www 91: foreach my $tag ('m','script') {
92: if ($token->[1] eq $tag) {
93: $output.=$parser->get_text('/'.$tag);
94: }
95: }
96: if ($token->[1] eq 'a') {
97: $insidelink=1;
1.5 www 98: }
99: } elsif ($token->[0] eq 'E') {
100: $output.=$token->[2];
1.6 www 101: if ($token->[1] eq 'a') {
102: $insidelink=0;
103: }
1.5 www 104: }
105: }
1.6 www 106: $insidelink=0;
1.5 www 107: return $output;
108: }
109:
1.3 www 110: sub initspeller {
111: unless (defined($speller)) {
112: $speller = Text::Aspell->new;
113: $speller->set_option('lang','en_US');
114: }
1.6 www 115: $insidelink=0;
1.3 www 116: }
117:
118: sub handler {
119: my $r = shift;
120: &Apache::loncommon::content_type($r,'text/html');
121: $r->send_http_header;
122: return OK if $r->header_only;
123:
124: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'});
125:
126: &initspeller();
127: $r->print('<html><head><title>'.
128: &mt('Spell Checker').
129: '</title></head><body bgcolor="#DDDDDD">'.
1.7 ! www 130: &Apache::lontexconvert::msgtexconverted(
! 131: &markeduptext($ENV{'form.text'})).
1.3 www 132: '</body></html>');
133: return OK;
134: }
135:
1.2 www 136: BEGIN {
1.3 www 137: &initspeller();
1.1 www 138: }
139:
140: 1;
141:
142: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>