#!/usr/bin/perl
use strict;
use warnings;
#use Data::Dumper;
my $Port = 8080;
my $Highlight = 1;
#----------------------------------------------------------
my $Pre = $Highlight?
'' : '';
my $Post = $Highlight?
'' : '';
my @Typos = ();
open(TYPO, "$ENV{HOME}/.typo") and do
{
@Typos = ();
while ()
{
chomp;
my ($wrong, $right) = split /\t+/;
next if not $right;
next if length($wrong) < 2;
push(@Typos,
[ $wrong, $right ]);
push(@Typos,
[ ucfirst($wrong), ucfirst($right) ])
if ucfirst($wrong) ne $wrong;
}
close(TYPO);
};
die "No typos loaded from ~/.typo.\n" if not @Typos;
print STDERR "$0: ", scalar @Typos, " typos filtered on port $Port.\n";
# Longer corrections first.
@Typos =
map { $_->[1] }
sort { $b->[0] <=> $a->[0] }
map { [ length($_->[0]), $_ ] }
@Typos;
# Spaces are lenient.
$_->[0] =~ s/ \s+ /\\s+/gx
foreach @Typos;
# Precompile the correction patterns.
$_->[0] = qr/ (?] ) \b ( $_->[0] ) \b/x
foreach @Typos;
#print Dumper $Typos[0], $Typos[-1];
#----------------------------------------------------------
use HTTP::Proxy;
my $proxy = HTTP::Proxy->new(port => $Port);
$proxy->push_body_filter( response => \&typo_filter );
$proxy->start();
#----------------------------------------------------------
sub typo_filter
{
foreach (@Typos)
{
${$_[0]} =~ s|$_->[0]|$Pre$_->[1]$Post|g;
}
}