File:  [LON-CAPA] / loncom / interface / lonspeller.pm
Revision 1.16: download - view: text, annotated - select for diffs
Fri Mar 24 21:40:11 2006 UTC (18 years, 2 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- making the speller popup use a dynamically defined function rather than packing everything into the link URL (ie has a limit on href cvalues of 512 characters or something)
- html_encode was supposeed to do <>& too
- Firefox in standards compliance mode thinks that any instance of </script> means to end that last script so fooling it with making </script> in the script be '....</script'+'>....'

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

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