version 1.1, 2004/07/22 16:17:43
|
version 1.9, 2004/12/02 20:54:26
|
Line 30
|
Line 30
|
|
|
package Apache::lonspeller; |
package Apache::lonspeller; |
|
|
|
use Apache::Constants qw(:common); |
use Text::Aspell; |
use Text::Aspell; |
use Apache::lonlocal; |
use Apache::lonlocal; |
|
use Apache::lontexconvert(); |
|
use HTML::LCParser; |
use strict; |
use strict; |
|
|
sub getspeller { |
my $speller; |
my $speller = Text::Aspell->new; |
my $insidelink; |
$speller->set_option('lang','en_US'); |
|
$speller->set_option('sug-mode','fast'); |
sub spellcheck_language { |
return $speller; |
if ($ENV{'form.lang'}) { return $ENV{'form.lang'}; } |
|
if (&mt('spellcheck_lang') ne 'spellcheck_lang') { |
|
return &mt('spellcheck_lang'); |
|
} |
|
return 'en_US'; |
|
} |
|
|
|
sub set_language { |
|
my $lang=&spellcheck_language(); |
|
$speller->set_option('lang',$lang); |
|
} |
|
|
|
sub textsection { |
|
my $input=shift; |
|
my $output=''; |
|
&set_language(); |
|
foreach my $word (split(/\b/,$input)) { |
|
if (($word=~/\W/) || ($word=~/^(lt|gt|nbsp|amp)$/i) |
|
|| ($speller->check($word))) { |
|
$output.=$word; |
|
} else { |
|
my $suggestions=join(' ',$speller->suggest($word)); |
|
$suggestions=~s/\'/\\\'/gs; |
|
if (($suggestions) && (!$insidelink)) { |
|
$output.='<a href="javascript:spellwin=window.open('. |
|
&Apache::lonhtmlcommon::javascript_nothing(). |
|
',\'spellwin\',\'height=140,width=280,resizable=yes,scrollbars=yes,location=no,menubar=no,toolbar=no\');'. |
|
'spellwin.'.&Apache::lonhtmlcommon::javascript_docopen().';spellwin.document.writeln(\'<html><body><h3>'.$word. |
|
'</h3>'.$suggestions.'</body></html>\');spellwin.document.close();spellwin.focus()">'; |
|
} |
|
$output.='<font color="red">'.$word.'</font>'; |
|
if (($suggestions) && (!$insidelink)) { $output.='</a>'; } |
|
} |
|
} |
|
return $output; |
|
} |
|
|
|
|
|
sub markeduptext { |
|
my $input=shift; |
|
my $output=''; |
|
my $parser=HTML::LCParser->new(\$input); |
|
$insidelink=0; |
|
my $token; |
|
while ($token=$parser->get_token) { |
|
if ($token->[0] eq 'T') { |
|
$output.=&textsection($token->[1]); |
|
} elsif ($token->[0] eq 'S') { |
|
$output.=$token->[4]; |
|
foreach my $tag ('m','script') { |
|
if ($token->[1] eq $tag) { |
|
$output.=$parser->get_text('/'.$tag); |
|
} |
|
} |
|
if ($token->[1] eq 'a') { |
|
$insidelink=1; |
|
} |
|
} elsif ($token->[0] eq 'E') { |
|
$output.=$token->[2]; |
|
if ($token->[1] eq 'a') { |
|
$insidelink=0; |
|
} |
|
} |
|
} |
|
$insidelink=0; |
|
return $output; |
|
} |
|
|
|
sub initspeller { |
|
unless (defined($speller)) { |
|
$speller = Text::Aspell->new; |
|
$speller->set_option('lang','en_US'); |
|
} |
|
$insidelink=0; |
|
} |
|
|
|
sub handler { |
|
my $r = shift; |
|
&Apache::loncommon::content_type($r,'text/html'); |
|
$r->send_http_header; |
|
return OK if $r->header_only; |
|
|
|
&Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'}); |
|
|
|
&initspeller(); |
|
$r->print('<html><head><title>'. |
|
&mt('Spell Checker'). |
|
'</title></head><body bgcolor="#DDDDDD">'. |
|
&Apache::lontexconvert::msgtexconverted( |
|
&markeduptext($ENV{'form.text'})). |
|
'</body></html>'); |
|
return OK; |
|
} |
|
|
|
BEGIN { |
|
&initspeller(); |
} |
} |
|
|
1; |
1; |