File:  [LON-CAPA] / loncom / interface / lonspeller.pm
Revision 1.17: download - view: text, annotated - select for diffs
Mon Jul 3 13:40:42 2006 UTC (17 years, 10 months ago) by raeburn
Branches: MAIN
CVS tags: version_2_9_X, version_2_9_99_0, version_2_9_1, version_2_9_0, version_2_8_X, version_2_8_99_1, version_2_8_99_0, version_2_8_2, version_2_8_1, version_2_8_0, version_2_7_X, version_2_7_99_1, version_2_7_99_0, version_2_7_1, version_2_7_0, version_2_6_X, version_2_6_99_1, version_2_6_99_0, version_2_6_3, version_2_6_2, version_2_6_1, version_2_6_0, version_2_5_X, version_2_5_99_1, version_2_5_99_0, version_2_5_2, version_2_5_1, version_2_5_0, version_2_4_X, version_2_4_99_0, version_2_4_2, version_2_4_1, version_2_4_0, version_2_3_X, version_2_3_99_0, version_2_3_2, version_2_3_1, version_2_3_0, version_2_2_X, version_2_2_99_1, version_2_2_99_0, version_2_2_2, version_2_2_1, version_2_2_0, version_2_1_99_3, version_2_1_99_2, version_2_1_99_1, version_2_1_99_0, version_2_12_X, version_2_11_X, version_2_11_4_uiuc, version_2_11_4_msu, version_2_11_4, version_2_11_3_uiuc, version_2_11_3_msu, version_2_11_3, version_2_11_2_uiuc, version_2_11_2_msu, version_2_11_2_educog, version_2_11_2, version_2_11_1, version_2_11_0_RC3, version_2_11_0_RC2, version_2_11_0_RC1, version_2_11_0, version_2_10_X, version_2_10_1, version_2_10_0_RC2, version_2_10_0_RC1, version_2_10_0, loncapaMITrelate_1, language_hyphenation_merge, language_hyphenation, bz6209-base, bz6209, bz5969, bz2851, PRINT_INCOMPLETE_base, PRINT_INCOMPLETE, HEAD, GCI_3, GCI_2, GCI_1, BZ5971-printing-apage, BZ5434-fox, BZ4492-merge, BZ4492-feature_horizontal_radioresponse
Removing debug line.

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

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