From: Rob H. <for...@us...> - 2002-01-21 05:56:44
|
Update of /cvsroot/sandweb/sandweb/lib/SandWeb In directory usw-pr-cvs1:/tmp/cvs-serv4203 Modified Files: Diff.pm Log Message: started cleaning up code, added a constructor, and reformatted the code Index: Diff.pm =================================================================== RCS file: /cvsroot/sandweb/sandweb/lib/SandWeb/Diff.pm,v retrieving revision 1.1 retrieving revision 1.2 diff -U2 -r1.1 -r1.2 --- Diff.pm 2002/01/21 04:01:23 1.1 +++ Diff.pm 2002/01/21 05:56:41 1.2 @@ -1,134 +1,146 @@ -my %args = @_; -my @diff = args{'diff'}; +#lib/SandWeb/Diff.pm +# +# the diff class. -my @rightCol = (); -my @leftCol = (); -my $state; -my $j; -my $tabstop; -my $hr_breakable; -my $string; -my $pr; -my $prcgi; - -my $diffcolorHeading='#99BBBB'; # color of 'Line'-heading of each diffed file -my $diffcolorEmpty='#CCCCCC'; # color of 'empty' lines -my $diffcolorAdd='#FF9999'; # Removed line(s) (left) ( - ) -my $diffcolorChange='#99FF99'; # Changed line(s) ( both ) -my $diffcolorRemove='#CCCCFF'; # Added line(s) ( - ) (right) -my $diffcolorDarkChange ='#99CC99';# lines, which are empty in change -my $difffontface="Helvetica,Arial"; -my $difffontsize="-1"; - -sub main { - my($title,$file1,$file2) = @_; - - my($i,$difftxt, $where_nd, $filename, $pathname); - - ($where_nd = my $where) =~ s/.diff$//; - ($filename = $where_nd) =~ s/^.*\///; - ($pathname = $where_nd) =~ s/(Attic\/)?[^\/]*$//; - (my $scriptwhere_nd = my $scriptwhere) =~ s/.diff$//; - - print "\n\n<center><font size=+2 face=Arial>$title</font></center><p>\n"; - print "<table border=0 cellspacing=0 cellpadding=0 width=100%>\n"; - print "<tr bgcolor=#ffffff>\n"; - print "<th width=\"50%\" valign=TOP>"; - print "SAMPLE DATA"; - print "</th>\n"; - print "<th width=\"50%\" valign=TOP>"; - print "SAMPLE DATA2"; - print "</th>\n"; - - my $fs="<font face=\"$difffontface\" size=\"$difffontsize\">"; - my $fe="</font>"; - - my $leftRow = 0; - my $rightRow = 0; - - # - # Process diff text - # - foreach my $difftxt (@diff) { - - if ($difftxt =~ /^@@/) { - my($oldline,$newline,$funname) = $difftxt =~ /@@ \-([0-9]+).*\+([0-9]+).*@@(.*)/; - my $state = "dump"; - $leftRow = 0; - $rightRow = 0; - } - else { - my($diffcode,$rest) = $difftxt =~ /^([-+ ])(.*)/; - $_= spacedHtmlText ($rest); - - # Add fontface, size - $_ = "$fs $_$fe"; +package SandWeb::Diff; + +sub new { + my $class = shift; + my %args = @_; + + my $self = bless { + 'diff' => $args{'diff'}, + }, $class; + + return $self; +} + +sub diff { + my @rightCol = (); + my @leftCol = (); + my $state; + my $j; + my $tabstop; + my $hr_breakable; + my $string; + my $pr; + my $prcgi; + my $diffcolorHeading='#99BBBB'; # color of 'Line'-heading of each diffed file + my $diffcolorEmpty='#CCCCCC'; # color of 'empty' lines + my $diffcolorAdd='#FF9999'; # Removed line(s) (left) ( - ) + my $diffcolorChange='#99FF99'; # Changed line(s) ( both ) + my $diffcolorRemove='#CCCCFF'; # Added line(s) ( - ) (right) + my $diffcolorDarkChange ='#99CC99';# lines, which are empty in change + my $difffontface="Helvetica,Arial"; + my $difffontsize="-1"; + + my($title,$file1,$file2) = @_; + + my($i,$difftxt, $where_nd, $filename, $pathname); + + ($where_nd = my $where) =~ s/.diff$//; + ($filename = $where_nd) =~ s/^.*\///; + ($pathname = $where_nd) =~ s/(Attic\/)?[^\/]*$//; + (my $scriptwhere_nd = my $scriptwhere) =~ s/.diff$//; + + print "\n\n<center><font size=+2 face=Arial>$title</font></center><p>\n"; + print "<table border=0 cellspacing=0 cellpadding=0 width=100%>\n"; + print "<tr bgcolor=#ffffff>\n"; + print "<th width=\"50%\" valign=TOP>"; + print "SAMPLE DATA"; + print "</th>\n"; + print "<th width=\"50%\" valign=TOP>"; + print "SAMPLE DATA2"; + print "</th>\n"; + + my $fs="<font face=\"$difffontface\" size=\"$difffontsize\">"; + my $fe="</font>"; + + my $leftRow = 0; + my $rightRow = 0; + + # + # Process diff text + # + foreach my $difftxt (@diff) { + + if ($difftxt =~ /^@@/) { + my($oldline,$newline,$funname) = $difftxt =~ /@@ \-([0-9]+).*\+([0-9]+).*@@(.*)/; + my $state = "dump"; + $leftRow = 0; + $rightRow = 0; + } else { + my($diffcode,$rest) = $difftxt =~ /^([-+ ])(.*)/; + $_= spacedHtmlText ($rest); + # Add fontface, size + $_ = "$fs $_$fe"; - ######### - # little state machine to parse unified-diff output (Hen, ze...@th...) - # in order to get some nice 'ediff'-mode output - # states: - # "dump" - just dump the value - # "PreChangeRemove" - we began with '-' .. so this could be the start of a 'change' area or just remove - # "PreChange" - okey, we got several '-' lines and moved to '+' lines -> this is a change block - ########## - - if ($diffcode eq '+') { - if ($state eq "dump") { # 'change' never begins with '+': just dump out value - print "<tr><td bgcolor=\"$diffcolorEmpty\"> </td><td bgcolor=\"$diffcolorAdd\">$_</td></tr>\n"; - } - else { # we got minus before - $state = "PreChange"; - $rightCol[$rightRow++] = $_; - } - } - elsif ($diffcode eq '-') { - $state = "PreChangeRemove"; - $leftCol[$leftRow++] = $_; - } - else { # empty diffcode - flush_diff_rows( \@leftCol, \@rightCol, $leftRow, $rightRow); - print "<tr><td>$_</td><td>$_</td></tr>\n"; - $state = "dump"; - $leftRow = 0; - $rightRow = 0; - } - } - } - flush_diff_rows( \@leftCol, \@rightCol, $leftRow, $rightRow ); - - # state is empty if we didn't have any change - if (!$state) { - print "<tr><td colspan=2> </td></tr>"; - print "<tr bgcolor=\"$diffcolorEmpty\" >"; - print "<td colspan=2 align=center><b>- No viewable Change -</b></td></tr>"; - } - - print "</table>\n\n"; - print "<br><hr noshade width=100%>\n"; - -} - -sub flush_diff_rows ($$$$) -{ - my $j; - my ($leftColRef,$rightColRef,$leftRow,$rightRow) = @_; - if ($state eq "PreChangeRemove") { # we just got remove-lines before - for ($j = 0 ; $j < $leftRow; $j++) { - print "<tr><td bgcolor=\"$diffcolorRemove\">@$leftColRef[$j]</td>"; - print "<td bgcolor=\"$diffcolorEmpty\"> </td></tr>\n"; - } - } - elsif ($state eq "PreChange") { # state eq "PreChange" - # we got removes with subsequent adds - for ($j = 0; $j < $leftRow || $j < $rightRow ; $j++) { # dump out both cols - print "<tr>"; - if ($j < $leftRow) { print "<td bgcolor=\"$diffcolorChange\">@$leftColRef[$j]</td>"; } - else { print "<td bgcolor=\"$diffcolorDarkChange\"> </td>"; } - if ($j < $rightRow) { print "<td bgcolor=\"$diffcolorChange\">@$rightColRef[$j]</td>"; } - else { print "<td bgcolor=\"$diffcolorDarkChange\"> </td>"; } - print "</tr>\n"; - } - } + ######### + # little state machine to parse unified-diff output (Hen, ze...@th...) + # in order to get some nice 'ediff'-mode output + # states: + # "dump" - just dump the value + # "PreChangeRemove" - we began with '-' .. so this could be the start of a 'change' area or just remove + # "PreChange" - okey, we got several '-' lines and moved to '+' lines -> this is a change block + ########## + + if ($diffcode eq '+') { + if ($state eq "dump") { # 'change' never begins with '+': just dump out value + print "<tr><td bgcolor=\"$diffcolorEmpty\"> </td><td bgcolor=\"$diffcolorAdd\">$_</td></tr>\n"; + } else { + # we got minus before + $state = "PreChange"; + $rightCol[$rightRow++] = $_; + } + } + + elsif ($diffcode eq '-') { + $state = "PreChangeRemove"; + $leftCol[$leftRow++] = $_; + } else { + # empty diffcode + flush_diff_rows( \@leftCol, \@rightCol, $leftRow, $rightRow); + print "<tr><td>$_</td><td>$_</td></tr>\n"; + $state = "dump"; + $leftRow = 0; + $rightRow = 0; + } + } + } + + flush_diff_rows( \@leftCol, \@rightCol, $leftRow, $rightRow ); + + # state is empty if we didn't have any change + if (!$state) { + print "<tr><td colspan=2> </td></tr>"; + print "<tr bgcolor=\"$diffcolorEmpty\" >"; + print "<td colspan=2 align=center><b>- No viewable Change -</b></td></tr>"; + } + print "</table>\n\n"; + print "<br><hr noshade width=100%>\n"; +} + +sub flush_diff_rows ($$$$) { + my $j; + my ($leftColRef,$rightColRef,$leftRow,$rightRow) = @_; + if ($state eq "PreChangeRemove") { # we just got remove-lines before + for ($j = 0 ; $j < $leftRow; $j++) { + print "<tr><td bgcolor=\"$diffcolorRemove\">@$leftColRef[$j]</td>"; + print "<td bgcolor=\"$diffcolorEmpty\"> </td></tr>\n"; + } + } + elsif ($state eq "PreChange") { # state eq "PreChange" + # we got removes with subsequent adds + for ($j = 0; $j < $leftRow || $j < $rightRow ; $j++) { # dump out both cols + print "<tr>"; + if ($j < $leftRow) { print "<td bgcolor=\"$diffcolorChange\">@$leftColRef[$j]</td>"; + } else { print "<td bgcolor=\"$diffcolorDarkChange\"> </td>"; +} + + if ($j < $rightRow) { print "<td bgcolor=\"$diffcolorChange\">@$rightColRef[$j]</td>"; } + else { print "<td bgcolor=\"$diffcolorDarkChange\"> </td>"; } + print "</tr>\n"; + } + } } @@ -188,3 +200,3 @@ } -&main(); +1; |