And combining merlyn's and my answers I give you:
#!/usr/bin/perl -w
use strict;
use Algorithm::Diff qw( traverse_sequences );
exit main();
{
my( $outState, %markUp, %htmlEnt );
BEGIN {
%markUp= (
same => ['',''],
new => ['<strong>','</strong>'],
old => ['<font size="-1"><strike>','</strike></font>'],
);
%htmlEnt= ( "<"=>"lt", "&"=>"amp", ">"=>"gt",
"["=>"#91", "]"=>"#93" );
}
sub output {
my( $style, $text )= @_;
if( ! defined $outState ) {
print $markUp{ $outState= $style }[0];
} elsif( $style ne $outState ) {
print $markUp{$outState}[1], $markUp{$style}[0];
$outState= $style;
}
$text =~ s#([][<>&])#&$htmlEnt{$1};#g;
$text =~ s#\n#<br>\n#g;
print $text;
}
}
sub flush {
my( $meth, $rOld, $rNew )= @_;
return if "" eq $$rOld && "" eq $$rNew;
if( "" eq $$rOld ) {
output( "new", $$rNew );
} elsif( "" eq $$rNew ) {
output( "old", $$rOld );
} elsif( ! $meth ) {
output( "old", $$rOld );
output( "new", $$rNew );
} else {
my( $meth, $old, $new )= &$meth( $rOld, $rNew );
compare( $meth, $old, $new );
}
$$rOld= $$rNew= "";
}
sub compare {
my( $meth, $old, $new )= @_;
my( $oldTemp, $newTemp )= ( "", "" );
traverse_sequences( $old, $new, {
MATCH => sub { flush( $meth, \$oldTemp, \$newTemp );
output( "same", $old->[$_[0]] ) },
DISCARD_A => sub { $oldTemp .= $old->[$_[0]] },
DISCARD_B => sub { $newTemp .= $new->[$_[1]] },
} );
flush( $meth, \$oldTemp, \$newTemp );
}
sub SentToWord {
my( $rOld, $rNew )= @_;
return(
undef,
[ split /(?<=\s)(?=\S)/, $$rOld ],
[ split /(?<=\s)(?=\S)/, $$rNew ],
);
}
sub ParaToSent {
my( $rOld, $rNew )= @_;
return(
\&SentToWord,
[ split /(?<=[.?!]\s)/, $$rOld ],
[ split /(?<=[.?!]\s)/, $$rNew ],
);
}
sub main {
die "Usage: $0 old new >dif\n" unless 2 == @ARGV;
my( $old, $new );
{ local($/,@ARGV)= ('',$ARGV[0]); $old= [<>]; }
{ local($/,@ARGV)= ('',$ARGV[1]); $new= [<>]; }
compare( \&ParaToSent, $old, $new );
output( "same", "" );
exit 0;
}
This compares non-HTML files in a way that produces HTML that shows the differences.
Update: I added support for quoting [ and ] and (temporailly) put some sample output from this on my home node.
-
tye
(but my friends call me "Tye")
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.