--- loncom/interface/lonspeller.pm 2004/07/22 16:17:43 1.1 +++ loncom/interface/lonspeller.pm 2004/07/22 23:50:55 1.4 @@ -1,7 +1,7 @@ # The LearningOnline Network with CAPA # Interface routines for Aspell # -# $Id: lonspeller.pm,v 1.1 2004/07/22 16:17:43 www Exp $ +# $Id: lonspeller.pm,v 1.4 2004/07/22 23:50:55 www Exp $ # # Copyright Michigan State University Board of Trustees # @@ -33,12 +33,72 @@ package Apache::lonspeller; use Text::Aspell; use Apache::lonlocal; use strict; +my $speller; -sub getspeller { - my $speller = Text::Aspell->new; - $speller->set_option('lang','en_US'); - $speller->set_option('sug-mode','fast'); - return $speller; +sub spellcheck_language { + 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 markeduptext { + my $input=shift; + my $output=''; + &set_language(); + foreach my $word (split(/\W+/,$input)) { + if ($speller->check($word)) { + $output.=$word.' '; + } else { + my $suggestions=join(' ',$speller->suggest($word)); + &Apache::lonnet::logthis($suggestions); + $suggestions=~s/\'/\\\'/gs; + if ($suggestions) { + $output.=''; + } + $output.=''.$word.''; + if ($suggestions) { $output.=''; } + $output.=' '; + } + } + return $output; +} + +sub initspeller { + unless (defined($speller)) { + $speller = Text::Aspell->new; + $speller->set_option('lang','en_US'); + $speller->set_option('sug-mode','fast'); + } +} + +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(''. + &mt('Spell Checker'). + ''. + &markeduptext($ENV{'form.text'}). + ''); + return OK; +} + +BEGIN { + &initspeller(); } 1;