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

1.1       www         1: # The LearningOnline Network with CAPA
                      2: # Interface routines for Aspell
                      3: #
1.15    ! albertel    4: # $Id: lonspeller.pm,v 1.14 2006/03/23 22:32:11 albertel 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.11      albertel   36: use Apache::lonnet;
1.7       www        37: use Apache::lontexconvert();
1.5       www        38: use HTML::LCParser;
1.1       www        39: use strict;
1.5       www        40: 
1.2       www        41: my $speller;
1.6       www        42: my $insidelink;
1.1       www        43: 
1.4       www        44: sub spellcheck_language {
1.11      albertel   45:     if ($env{'form.lang'}) { return $env{'form.lang'}; }
1.4       www        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: 
1.5       www        57: sub textsection {
1.2       www        58:     my $input=shift;
                     59:     my $output='';
1.4       www        60:     &set_language();
1.6       www        61:     foreach my $word (split(/\b/,$input)) {
                     62: 	if (($word=~/\W/) || ($word=~/^(lt|gt|nbsp|amp)$/i)
                     63: 	    || ($speller->check($word))) {
                     64: 	    $output.=$word;
1.2       www        65: 	} else {
                     66: 	    my $suggestions=join(' ',$speller->suggest($word));
                     67: 	    $suggestions=~s/\'/\\\'/gs;
1.6       www        68: 	    if (($suggestions) && (!$insidelink)) {
1.13      albertel   69: 		my $start_page=
                     70: 		    &Apache::loncommon::start_page('Speller Suggestions',undef,
1.15    ! albertel   71: 						   {'only_body'   => 1,
        !            72: 						    'js_ready'    => 1,
        !            73: 						    'html_encode' => 1,
        !            74: 						    'bgcolor'     => '#FFFFFF'});
1.13      albertel   75: 		my $end_page=
1.15    ! albertel   76: 		    &Apache::loncommon::end_page({'js_ready'    => 1,
        !            77: 						  'html_encode' => 1,});
1.8       www        78: 		$output.='<a href="javascript:spellwin=window.open('.
                     79: 		    &Apache::lonhtmlcommon::javascript_nothing().
                     80: 		    ',\'spellwin\',\'height=140,width=280,resizable=yes,scrollbars=yes,location=no,menubar=no,toolbar=no\');'.
1.13      albertel   81:                     'spellwin.'.&Apache::lonhtmlcommon::javascript_docopen().';spellwin.document.writeln(\''.$start_page.'<h3>'.$word.
                     82:                     '</h3>'.$suggestions.$end_page.'\');spellwin.document.close();spellwin.focus()">';
1.2       www        83: 	    }
                     84: 	    $output.='<font color="red">'.$word.'</font>';
1.6       www        85: 	    if (($suggestions) && (!$insidelink)) { $output.='</a>'; }
1.2       www        86: 	}
                     87:     }
                     88:     return $output;
                     89: }
                     90: 
1.5       www        91: 
                     92: sub markeduptext {
                     93:     my $input=shift;
                     94:     my $output='';
                     95:     my $parser=HTML::LCParser->new(\$input);
1.6       www        96:     $insidelink=0;
1.5       www        97:     my $token;
                     98:     while ($token=$parser->get_token) {
                     99: 	if ($token->[0] eq 'T') {
                    100: 	    $output.=&textsection($token->[1]);
                    101: 	} elsif ($token->[0] eq 'S') {
                    102: 	    $output.=$token->[4];
1.6       www       103: 	    foreach my $tag ('m','script') {
                    104: 		if ($token->[1] eq $tag) {
                    105: 		    $output.=$parser->get_text('/'.$tag);
                    106: 		}
                    107: 	    }
                    108: 	    if ($token->[1] eq 'a') {
                    109: 		$insidelink=1;
1.5       www       110: 	    }
                    111: 	} elsif ($token->[0] eq 'E') {
                    112: 	    $output.=$token->[2];
1.6       www       113: 	    if ($token->[1] eq 'a') {
                    114: 		$insidelink=0;
                    115: 	    }
1.5       www       116: 	}
                    117:     }
1.6       www       118:     $insidelink=0;
1.5       www       119:     return $output;
                    120: }
                    121: 
1.3       www       122: sub initspeller {
                    123:     unless (defined($speller)) {
                    124: 	$speller = Text::Aspell->new;
                    125: 	$speller->set_option('lang','en_US');
                    126:     }
1.6       www       127:     $insidelink=0;
1.3       www       128: }
                    129: 
                    130: sub handler {
                    131:     my $r = shift;
                    132:     &Apache::loncommon::content_type($r,'text/html');
                    133:     $r->send_http_header;
                    134:     return OK if $r->header_only;
                    135: 
                    136:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'});
                    137: 
                    138:     &initspeller();
1.14      albertel  139:     my $start_page = 
                    140: 	&Apache::loncommon::start_page('Spell Checker',undef,
                    141: 				       {'only_body' => 1,
                    142: 					'bgcolor'   => '#DDDDDD'});
                    143:     $r->print($start_page.
1.7       www       144: 	      &Apache::lontexconvert::msgtexconverted(
1.11      albertel  145: 				     &markeduptext($env{'form.text'})).
1.12      albertel  146: 	      &Apache::loncommon::end_page());
1.3       www       147:     return OK;
                    148: }
                    149: 
1.2       www       150: BEGIN {
1.3       www       151:     &initspeller();
1.1       www       152: }
                    153: 
                    154: 1;
                    155: 
                    156: __END__

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