File:  [LON-CAPA] / loncom / interface / lonspeller.pm
Revision 1.5: download - view: text, annotated - select for diffs
Fri Jul 23 19:47:57 2004 UTC (19 years, 11 months ago) by www
Branches: MAIN
CVS tags: HEAD
Leave HTML tags alone and do not try to spellcheck equations

    1: # The LearningOnline Network with CAPA
    2: # Interface routines for Aspell
    3: #
    4: # $Id: lonspeller.pm,v 1.5 2004/07/23 19:47:57 www Exp $
    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: 
   33: use Apache::Constants qw(:common);
   34: use Text::Aspell;
   35: use Apache::lonlocal;
   36: use HTML::LCParser;
   37: use strict;
   38: 
   39: my $speller;
   40: 
   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: 
   54: sub textsection {
   55:     my $input=shift;
   56:     my $output='';
   57:     &set_language();
   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: 
   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: 
   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: 
  124: BEGIN {
  125:     &initspeller();
  126: }
  127: 
  128: 1;
  129: 
  130: __END__

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