#!/usr/bin/perl # # LJ::SpellCheck class # See perldoc documentation at the end of this file. # # ------------------------------------------------------------------------- # # This package is released under the LGPL (GNU Library General Public License) # # A copy of the license has been included with the software as LGPL.txt. # If not, the license is available at: # http://www.gnu.org/copyleft/library.txt # # ------------------------------------------------------------------------- package LJ::SpellCheck; use strict; use FileHandle; use IPC::Open2; use POSIX ":sys_wait_h"; use vars qw($VERSION); $VERSION = '1.0'; # Good spellcommand values: # ispell -a -h (default) # /usr/local/bin/aspell pipe -H --sug-mode=fast --ignore-case sub new { my ($class, $args) = @_; my $self = {}; bless $self, ref $class || $class; $self->{'command'} = $args->{'spellcommand'} || "ispell -a -h"; $self->{'color'} = $args->{'color'} || "#FF0000"; return $self; } # This function takes a block of text to spell-check and returns HTML # to show suggesting correction, if any. If the return from this # function is empty, then there were no misspellings found. sub check_html { my $self = shift; my $journal = shift; my $iread = new FileHandle; my $iwrite = new FileHandle; my $ierr = new FileHandle; my $pid; # work-around for mod_perl my $tie_stdin = tied *STDIN; untie *STDIN if $tie_stdin; $iwrite->autoflush(1); $pid = open2($iread, $iwrite, $self->{'command'}) || die "spell process failed"; die "Couldn't find spell checker\n" unless $pid; my $banner = <$iread>; die "banner=$banner\n" unless ($banner =~ /^@\(\#\)/); print $iwrite "!\n"; my $output = ""; my $footnotes = ""; my ($srcidx, $lineidx, $mscnt, $other_bad); $lineidx = 1; $mscnt = 0; foreach my $inline (split(/\n/, $$journal)) { $srcidx = 0; chomp($inline); print $iwrite "^$inline\n"; my $idata; do { $idata = <$iread>; chomp($idata); if ($idata =~ /^& /) { $idata =~ s/^& (\S+) (\d+) (\d+): //; $mscnt++; my ($word, $sugcount, $ofs) = ($1, $2, $3); $ofs -= 1; # because ispell reports "1" for first character $output .= substr($inline, $srcidx, $ofs-$srcidx); $output .= "{'color'}\">$word"; $footnotes .= "
Suggestions: