File:  [LON-CAPA] / loncom / interface / lonspeller.pm
Revision 1.14: download - view: text, annotated - select for diffs
Thu Mar 23 22:32:11 2006 UTC (18 years, 2 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
-start_page
-eliminate calls to bodytag in favor of start_page
- elimintate call to xmlbegin

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

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