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