File:  [LON-CAPA] / loncom / interface / lonspeller.pm
Revision 1.10: download - view: text, annotated - select for diffs
Thu Feb 17 08:29:43 2005 UTC (19 years, 3 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- <html> -> &Apache::lonxml::xmlbegin, thus valid doctypes are now getting output, Yeah! StandardsCmpliance

- backing out the encoding changes for now

- some xhtml cleanups
- one icon -> lonhttpd

    1: # The LearningOnline Network with CAPA
    2: # Interface routines for Aspell
    3: #
    4: # $Id: lonspeller.pm,v 1.10 2005/02/17 08:29:43 albertel 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 Apache::lontexconvert();
   37: use HTML::LCParser;
   38: use strict;
   39: 
   40: my $speller;
   41: my $insidelink;
   42: 
   43: sub spellcheck_language {
   44:     if ($ENV{'form.lang'}) { return $ENV{'form.lang'}; }
   45:     if (&mt('spellcheck_lang') ne 'spellcheck_lang') {
   46: 	return &mt('spellcheck_lang');
   47:     }
   48:     return 'en_US';
   49: }
   50: 
   51: sub set_language {
   52:     my $lang=&spellcheck_language();
   53:     $speller->set_option('lang',$lang);
   54: }
   55: 
   56: sub textsection {
   57:     my $input=shift;
   58:     my $output='';
   59:     &set_language();
   60:     foreach my $word (split(/\b/,$input)) {
   61: 	if (($word=~/\W/) || ($word=~/^(lt|gt|nbsp|amp)$/i)
   62: 	    || ($speller->check($word))) {
   63: 	    $output.=$word;
   64: 	} else {
   65: 	    my $suggestions=join(' ',$speller->suggest($word));
   66: 	    $suggestions=~s/\'/\\\'/gs;
   67: 	    if (($suggestions) && (!$insidelink)) {
   68: 		my $html=&Apache::lonxml::xmlbegin('encode');
   69: 		$output.='<a href="javascript:spellwin=window.open('.
   70: 		    &Apache::lonhtmlcommon::javascript_nothing().
   71: 		    ',\'spellwin\',\'height=140,width=280,resizable=yes,scrollbars=yes,location=no,menubar=no,toolbar=no\');'.
   72:                     'spellwin.'.&Apache::lonhtmlcommon::javascript_docopen().';spellwin.document.writeln(\''.$html.'<head></head><body><h3>'.$word.
   73:                     '</h3>'.$suggestions.'</body></html>\');spellwin.document.close();spellwin.focus()">';
   74: 	    }
   75: 	    $output.='<font color="red">'.$word.'</font>';
   76: 	    if (($suggestions) && (!$insidelink)) { $output.='</a>'; }
   77: 	}
   78:     }
   79:     return $output;
   80: }
   81: 
   82: 
   83: sub markeduptext {
   84:     my $input=shift;
   85:     my $output='';
   86:     my $parser=HTML::LCParser->new(\$input);
   87:     $insidelink=0;
   88:     my $token;
   89:     while ($token=$parser->get_token) {
   90: 	if ($token->[0] eq 'T') {
   91: 	    $output.=&textsection($token->[1]);
   92: 	} elsif ($token->[0] eq 'S') {
   93: 	    $output.=$token->[4];
   94: 	    foreach my $tag ('m','script') {
   95: 		if ($token->[1] eq $tag) {
   96: 		    $output.=$parser->get_text('/'.$tag);
   97: 		}
   98: 	    }
   99: 	    if ($token->[1] eq 'a') {
  100: 		$insidelink=1;
  101: 	    }
  102: 	} elsif ($token->[0] eq 'E') {
  103: 	    $output.=$token->[2];
  104: 	    if ($token->[1] eq 'a') {
  105: 		$insidelink=0;
  106: 	    }
  107: 	}
  108:     }
  109:     $insidelink=0;
  110:     return $output;
  111: }
  112: 
  113: sub initspeller {
  114:     unless (defined($speller)) {
  115: 	$speller = Text::Aspell->new;
  116: 	$speller->set_option('lang','en_US');
  117:     }
  118:     $insidelink=0;
  119: }
  120: 
  121: sub handler {
  122:     my $r = shift;
  123:     &Apache::loncommon::content_type($r,'text/html');
  124:     $r->send_http_header;
  125:     return OK if $r->header_only;
  126: 
  127:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'});
  128: 
  129:     &initspeller();
  130:     my $html=&Apache::lonxml::xmlbegin();
  131:     $r->print($html.'<head><title>'.
  132: 	      &mt('Spell Checker').
  133: 	      '</title></head><body bgcolor="#DDDDDD">'.
  134: 	      &Apache::lontexconvert::msgtexconverted(
  135: 				     &markeduptext($ENV{'form.text'})).
  136: 	      '</body></html>');
  137:     return OK;
  138: }
  139: 
  140: BEGIN {
  141:     &initspeller();
  142: }
  143: 
  144: 1;
  145: 
  146: __END__

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