--- loncom/interface/lonspeller.pm 2004/07/23 19:47:57 1.5 +++ loncom/interface/lonspeller.pm 2006/07/03 13:40:42 1.17 @@ -1,7 +1,7 @@ # The LearningOnline Network with CAPA # Interface routines for Aspell # -# $Id: lonspeller.pm,v 1.5 2004/07/23 19:47:57 www Exp $ +# $Id: lonspeller.pm,v 1.17 2006/07/03 13:40:42 raeburn Exp $ # # Copyright Michigan State University Board of Trustees # @@ -33,13 +33,16 @@ package Apache::lonspeller; use Apache::Constants qw(:common); use Text::Aspell; use Apache::lonlocal; +use Apache::lonnet; +use Apache::lontexconvert(); use HTML::LCParser; use strict; my $speller; +my $insidelink; sub spellcheck_language { - if ($ENV{'form.lang'}) { return $ENV{'form.lang'}; } + if ($env{'form.lang'}) { return $env{'form.lang'}; } if (&mt('spellcheck_lang') ne 'spellcheck_lang') { return &mt('spellcheck_lang'); } @@ -51,25 +54,48 @@ sub set_language { $speller->set_option('lang',$lang); } +{ + my $uniq; + sub get_uniq { + return ++$uniq; + } +} + sub textsection { my $input=shift; my $output=''; &set_language(); - foreach my $word (split(/\W+/,$input)) { - if ($speller->check($word)) { - $output.=$word.' '; + 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)); - &Apache::lonnet::logthis($suggestions); - $suggestions=~s/\'/\\\'/gs; - if ($suggestions) { - $output.=''; + $suggestions = &Apache::loncommon::js_ready($suggestions); + if (($suggestions) && (!$insidelink)) { + my $start_page= + &Apache::loncommon::start_page('Speller Suggestions',undef, + {'only_body' => 1, + 'js_ready' => 1, + 'bgcolor' => '#FFFFFF'}); + my $end_page= + &Apache::loncommon::end_page({'js_ready' => 1,}); + my $num = &get_uniq(); + my $info ='

'.$word.'

'.$suggestions; + $output .= "
"; } $output.=''.$word.''; - if ($suggestions) { $output.=''; } - $output.=' '; + if (($suggestions) && (!$insidelink)) { $output.=''; } } } return $output; @@ -80,19 +106,29 @@ 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]; - if ($token->[1] eq 'm') { - $output.=$parser->get_text('/m'); + 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; } @@ -100,8 +136,8 @@ sub initspeller { unless (defined($speller)) { $speller = Text::Aspell->new; $speller->set_option('lang','en_US'); - $speller->set_option('sug-mode','fast'); } + $insidelink=0; } sub handler { @@ -113,11 +149,14 @@ sub handler { &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'}); &initspeller(); - $r->print(''. - &mt('Spell Checker'). - ''. - &markeduptext($ENV{'form.text'}). - ''); + my $start_page = + &Apache::loncommon::start_page('Spell Checker',undef, + {'only_body' => 1, + 'bgcolor' => '#DDDDDD'}); + $r->print($start_page. + &Apache::lontexconvert::msgtexconverted( + &markeduptext($env{'form.text'})). + &Apache::loncommon::end_page()); return OK; }