Diff for /loncom/interface/lonspeller.pm between versions 1.2 and 1.17

version 1.2, 2004/07/22 19:43:46 version 1.17, 2006/07/03 13:40:42
Line 30 Line 30
   
 package Apache::lonspeller;  package Apache::lonspeller;
   
   use Apache::Constants qw(:common);
 use Text::Aspell;  use Text::Aspell;
 use Apache::lonlocal;  use Apache::lonlocal;
   use Apache::lonnet;
   use Apache::lontexconvert();
   use HTML::LCParser;
 use strict;  use strict;
   
 my $speller;  my $speller;
   my $insidelink;
   
 sub markeduptext {  sub spellcheck_language {
       if ($env{'form.lang'}) { return $env{'form.lang'}; }
       if (&mt('spellcheck_lang') ne 'spellcheck_lang') {
    return &mt('spellcheck_lang');
       }
       return 'en_US';
   }
   
   sub set_language {
       my $lang=&spellcheck_language();
       $speller->set_option('lang',$lang);
   }
   
   {
       my $uniq;
       sub get_uniq {
    return ++$uniq;
       }
   }
   
   sub textsection {
     my $input=shift;      my $input=shift;
     my $output='';      my $output='';
     foreach my $word (split(/\W+/,$input)) {      &set_language();
  if ($speller->check($word)) {      foreach my $word (split(/\b/,$input)) {
     $output.=$word.' ';   if (($word=~/\W/) || ($word=~/^(lt|gt|nbsp|amp)$/i)
       || ($speller->check($word))) {
       $output.=$word;
  } else {   } else {
     my $suggestions=join(' ',$speller->suggest($word));      my $suggestions=join(' ',$speller->suggest($word));
     &Apache::lonnet::logthis($suggestions);      $suggestions = &Apache::loncommon::js_ready($suggestions);
     $suggestions=~s/\'/\\\'/gs;      if (($suggestions) && (!$insidelink)) {
     if ($suggestions) {   my $start_page=
  $output.='<a href="javascript:alert('."'".      &Apache::loncommon::start_page('Speller Suggestions',undef,
     $suggestions."');".     {'only_body'   => 1,
     '">';      'js_ready'    => 1,
       'bgcolor'     => '#FFFFFF'});
    my $end_page=
       &Apache::loncommon::end_page({'js_ready'    => 1,});
    my $num = &get_uniq();
    my $info  ='<h3>'.$word.'</h3>'.$suggestions;
    $output .= "<script type=\"text/javascript\">
   //<!--
    function LONCAPA_lonspeller_$num() {
       spellwin=open(".&Apache::lonhtmlcommon::javascript_nothing().",'spellwin','width=140,height=280,resizable=yes,scrollbars=yes,location=no,menubar=no,toolbar=no');
       spellwin.".&Apache::lonhtmlcommon::javascript_docopen().";
       spellwin.document.writeln('$start_page $info $end_page');
       spellwin.document.close();
       spellwin.focus();
   }
   //-->
   </script><a href=\"javascript:LONCAPA_lonspeller_$num();void(0);\">";
     }      }
     $output.='<font color="red">'.$word.'</font>';      $output.='<font color="red">'.$word.'</font>';
     if ($suggestions) { $output.='</a>'; }      if (($suggestions) && (!$insidelink)) { $output.='</a>'; }
     $output.=' ';   }
       }
       return $output;
   }
   
   
   sub markeduptext {
       my $input=shift;
       my $output='';
       my $parser=HTML::LCParser->new(\$input);
       $insidelink=0;
       my $token;
       while ($token=$parser->get_token) {
    if ($token->[0] eq 'T') {
       $output.=&textsection($token->[1]);
    } elsif ($token->[0] eq 'S') {
       $output.=$token->[4];
       foreach my $tag ('m','script') {
    if ($token->[1] eq $tag) {
       $output.=$parser->get_text('/'.$tag);
    }
       }
       if ($token->[1] eq 'a') {
    $insidelink=1;
       }
    } elsif ($token->[0] eq 'E') {
       $output.=$token->[2];
       if ($token->[1] eq 'a') {
    $insidelink=0;
       }
  }   }
     }      }
       $insidelink=0;
     return $output;      return $output;
 }  }
   
   sub initspeller {
       unless (defined($speller)) {
    $speller = Text::Aspell->new;
    $speller->set_option('lang','en_US');
       }
       $insidelink=0;
   }
   
   sub handler {
       my $r = shift;
       &Apache::loncommon::content_type($r,'text/html');
       $r->send_http_header;
       return OK if $r->header_only;
   
       &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'});
   
       &initspeller();
       my $start_page = 
    &Apache::loncommon::start_page('Spell Checker',undef,
          {'only_body' => 1,
    'bgcolor'   => '#DDDDDD'});
       $r->print($start_page.
         &Apache::lontexconvert::msgtexconverted(
        &markeduptext($env{'form.text'})).
         &Apache::loncommon::end_page());
       return OK;
   }
   
 BEGIN {  BEGIN {
     $speller = Text::Aspell->new;      &initspeller();
     $speller->set_option('lang','en_US');  
     $speller->set_option('sug-mode','fast');  
 #    $speller->set_option('mode','sgml');  
 }  }
   
 1;  1;

Removed from v.1.2  
changed lines
  Added in v.1.17


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