File:  [LON-CAPA] / loncom / interface / lonspeller.pm
Revision 1.4: download - view: text, annotated - select for diffs
Thu Jul 22 23:50:55 2004 UTC (19 years, 9 months ago) by www
Branches: MAIN
CVS tags: HEAD
Set languages - requires German (de) and Portuguese (pt) dictionaries installed

# The LearningOnline Network with CAPA
# Interface routines for Aspell
#
# $Id: lonspeller.pm,v 1.4 2004/07/22 23:50:55 www Exp $
#
# Copyright Michigan State University Board of Trustees
#
# This file is part of the LearningOnline Network with CAPA (LON-CAPA).
#
# LON-CAPA is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# LON-CAPA is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with LON-CAPA; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# /home/httpd/html/adm/gpl.txt
#
# http://www.lon-capa.org/
#
######################################################################
######################################################################

package Apache::lonspeller;

use Text::Aspell;
use Apache::lonlocal;
use strict;
my $speller;

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);
}

sub markeduptext {
    my $input=shift;
    my $output='';
    &set_language();
    foreach my $word (split(/\W+/,$input)) {
	if ($speller->check($word)) {
	    $output.=$word.' ';
	} else {
	    my $suggestions=join(' ',$speller->suggest($word));
	    &Apache::lonnet::logthis($suggestions);
	    $suggestions=~s/\'/\\\'/gs;
	    if ($suggestions) {
		$output.='<a href="javascript:alert('."'".
		    $suggestions."');".
		    '">';
	    }
	    $output.='<font color="red">'.$word.'</font>';
	    if ($suggestions) { $output.='</a>'; }
	    $output.=' ';
	}
    }
    return $output;
}

sub initspeller {
    unless (defined($speller)) {
	$speller = Text::Aspell->new;
	$speller->set_option('lang','en_US');
	$speller->set_option('sug-mode','fast');
    }
}

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();
    $r->print('<html><head><title>'.
	      &mt('Spell Checker').
	      '</title></head><body bgcolor="#DDDDDD">'.
	      &markeduptext($ENV{'form.text'}).
	      '</body></html>');
    return OK;
}

BEGIN {
    &initspeller();
}

1;

__END__

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