Annotation of loncom/interface/lonspeller.pm, revision 1.5

1.1       www         1: # The LearningOnline Network with CAPA
                      2: # Interface routines for Aspell
                      3: #
1.5     ! www         4: # $Id: lonspeller.pm,v 1.4 2004/07/22 23:50:55 www 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.5     ! www        36: use HTML::LCParser;
1.1       www        37: use strict;
1.5     ! www        38: 
1.2       www        39: my $speller;
1.1       www        40: 
1.4       www        41: sub spellcheck_language {
                     42:     if ($ENV{'form.lang'}) { return $ENV{'form.lang'}; }
                     43:     if (&mt('spellcheck_lang') ne 'spellcheck_lang') {
                     44: 	return &mt('spellcheck_lang');
                     45:     }
                     46:     return 'en_US';
                     47: }
                     48: 
                     49: sub set_language {
                     50:     my $lang=&spellcheck_language();
                     51:     $speller->set_option('lang',$lang);
                     52: }
                     53: 
1.5     ! www        54: sub textsection {
1.2       www        55:     my $input=shift;
                     56:     my $output='';
1.4       www        57:     &set_language();
1.2       www        58:     foreach my $word (split(/\W+/,$input)) {
                     59: 	if ($speller->check($word)) {
                     60: 	    $output.=$word.' ';
                     61: 	} else {
                     62: 	    my $suggestions=join(' ',$speller->suggest($word));
                     63: 	    &Apache::lonnet::logthis($suggestions);
                     64: 	    $suggestions=~s/\'/\\\'/gs;
                     65: 	    if ($suggestions) {
                     66: 		$output.='<a href="javascript:alert('."'".
                     67: 		    $suggestions."');".
                     68: 		    '">';
                     69: 	    }
                     70: 	    $output.='<font color="red">'.$word.'</font>';
                     71: 	    if ($suggestions) { $output.='</a>'; }
                     72: 	    $output.=' ';
                     73: 	}
                     74:     }
                     75:     return $output;
                     76: }
                     77: 
1.5     ! www        78: 
        !            79: sub markeduptext {
        !            80:     my $input=shift;
        !            81:     my $output='';
        !            82:     my $parser=HTML::LCParser->new(\$input);
        !            83:     my $token;
        !            84:     while ($token=$parser->get_token) {
        !            85: 	if ($token->[0] eq 'T') {
        !            86: 	    $output.=&textsection($token->[1]);
        !            87: 	} elsif ($token->[0] eq 'S') {
        !            88: 	    $output.=$token->[4];
        !            89: 	    if ($token->[1] eq 'm') {
        !            90: 		$output.=$parser->get_text('/m');
        !            91: 	    }
        !            92: 	} elsif ($token->[0] eq 'E') {
        !            93: 	    $output.=$token->[2];
        !            94: 	}
        !            95:     }
        !            96:     return $output;
        !            97: }
        !            98: 
1.3       www        99: sub initspeller {
                    100:     unless (defined($speller)) {
                    101: 	$speller = Text::Aspell->new;
                    102: 	$speller->set_option('lang','en_US');
                    103: 	$speller->set_option('sug-mode','fast');
                    104:     }
                    105: }
                    106: 
                    107: sub handler {
                    108:     my $r = shift;
                    109:     &Apache::loncommon::content_type($r,'text/html');
                    110:     $r->send_http_header;
                    111:     return OK if $r->header_only;
                    112: 
                    113:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'});
                    114: 
                    115:     &initspeller();
                    116:     $r->print('<html><head><title>'.
                    117: 	      &mt('Spell Checker').
                    118: 	      '</title></head><body bgcolor="#DDDDDD">'.
                    119: 	      &markeduptext($ENV{'form.text'}).
                    120: 	      '</body></html>');
                    121:     return OK;
                    122: }
                    123: 
1.2       www       124: BEGIN {
1.3       www       125:     &initspeller();
1.1       www       126: }
                    127: 
                    128: 1;
                    129: 
                    130: __END__

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>