File:
[LON-CAPA] /
loncom /
interface /
lonspeller.pm
Revision
1.11:
download - view:
text,
annotated -
select for diffs
Thu Apr 7 06:56:23 2005 UTC (19 years, 5 months ago) by
albertel
Branches:
MAIN
CVS tags:
version_2_1_X,
version_2_1_3,
version_2_1_2,
version_2_1_1,
version_2_1_0,
version_2_0_X,
version_2_0_99_1,
version_2_0_2,
version_2_0_1,
version_2_0_0,
version_1_99_3,
version_1_99_2,
version_1_99_1_tmcc,
version_1_99_1,
version_1_99_0_tmcc,
version_1_99_0,
HEAD
- ENV -> env
1: # The LearningOnline Network with CAPA
2: # Interface routines for Aspell
3: #
4: # $Id: lonspeller.pm,v 1.11 2005/04/07 06:56:23 albertel 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 Apache::lonnet;
37: use Apache::lontexconvert();
38: use HTML::LCParser;
39: use strict;
40:
41: my $speller;
42: my $insidelink;
43:
44: sub spellcheck_language {
45: if ($env{'form.lang'}) { return $env{'form.lang'}; }
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:
57: sub textsection {
58: my $input=shift;
59: my $output='';
60: &set_language();
61: foreach my $word (split(/\b/,$input)) {
62: if (($word=~/\W/) || ($word=~/^(lt|gt|nbsp|amp)$/i)
63: || ($speller->check($word))) {
64: $output.=$word;
65: } else {
66: my $suggestions=join(' ',$speller->suggest($word));
67: $suggestions=~s/\'/\\\'/gs;
68: if (($suggestions) && (!$insidelink)) {
69: my $html=&Apache::lonxml::xmlbegin('encode');
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\');'.
73: 'spellwin.'.&Apache::lonhtmlcommon::javascript_docopen().';spellwin.document.writeln(\''.$html.'<head></head><body><h3>'.$word.
74: '</h3>'.$suggestions.'</body></html>\');spellwin.document.close();spellwin.focus()">';
75: }
76: $output.='<font color="red">'.$word.'</font>';
77: if (($suggestions) && (!$insidelink)) { $output.='</a>'; }
78: }
79: }
80: return $output;
81: }
82:
83:
84: sub markeduptext {
85: my $input=shift;
86: my $output='';
87: my $parser=HTML::LCParser->new(\$input);
88: $insidelink=0;
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];
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;
102: }
103: } elsif ($token->[0] eq 'E') {
104: $output.=$token->[2];
105: if ($token->[1] eq 'a') {
106: $insidelink=0;
107: }
108: }
109: }
110: $insidelink=0;
111: return $output;
112: }
113:
114: sub initspeller {
115: unless (defined($speller)) {
116: $speller = Text::Aspell->new;
117: $speller->set_option('lang','en_US');
118: }
119: $insidelink=0;
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();
131: my $html=&Apache::lonxml::xmlbegin();
132: $r->print($html.'<head><title>'.
133: &mt('Spell Checker').
134: '</title></head><body bgcolor="#DDDDDD">'.
135: &Apache::lontexconvert::msgtexconverted(
136: &markeduptext($env{'form.text'})).
137: '</body></html>');
138: return OK;
139: }
140:
141: BEGIN {
142: &initspeller();
143: }
144:
145: 1;
146:
147: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>