From: sven d. <sv...@us...> - 2004-03-22 12:10:02
|
Update of /cvsroot/twiki/twiki/bin In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv30853 Modified Files: rdiff Log Message: re-implemented rdiff to seperate parsing the diff and rendering it 3 url param options : ?render=sidebyside ?render=sequential ?render=debug defaults to ?render=sidebyside Index: rdiff =================================================================== RCS file: /cvsroot/twiki/twiki/bin/rdiff,v retrieving revision 1.37 retrieving revision 1.38 diff -C2 -r1.37 -r1.38 *** rdiff 20 Mar 2004 11:55:13 -0000 1.37 --- rdiff 22 Mar 2004 11:59:30 -0000 1.38 *************** *** 35,38 **** --- 35,49 ---- &main(); + #SVEN - new design. + #main gets the info (NO MAJOR CHANGES NEEDED) + #parseDiffs reads the diffs and interprets the information into types {"+", "-", "u", "c", "l"} (add, remove, unchanged, changed, lineNumber} where line number is for diffs that skip unchanged lines (diff -u etc) + #so renderDiffs would get an array of [changeType, $oldstring, $newstring] + # corresponding to Algorithm::Diff's output + #renderDiffs iterates through the interpreted info and makes it into TML / HTML? (mmm) + #and can be over-ridden :) + #(now can we do this in a way that automagically can cope eith word / letter based diffs?) + #NOTE: if we do our own diffs in perl we can go straight to renderDiffs + #CPAN: there is a Diffs module - it outputs ********** + # ========================= sub renderCellData *************** *** 78,99 **** sub renderSideBySide { ! my ( $diffstr, $left, $right ) = @_; my $result = ""; ! if ( $diffstr ne "" ) { ! $diffstr =~ /^([0-9]+)[0-9\,]*([adc])([0-9]+)/; ! $result .= qq(<tr bgcolor="#eeeeee"><th>Line: $1</th><th>Line: $3</th></tr>\n); ! if ( $2 eq "c" ) { $lcolor = $rcolor = "#99ff99"; ! } elsif ( $2 eq "a" ) { $lcolor = "#cccccc"; $rcolor = "#ccccff"; ! } elsif ( $2 eq "d" ) { $lcolor = "#ff9999"; $rcolor = "#cccccc"; } ! $right = renderCellData( $right, $topic ); $left = renderCellData( $left, $topic ); - $result .= qq(<tr><td bgcolor="$lcolor" valign="top">$left </td><td bgcolor="$rcolor" valign="top">$right </td></tr>\n); } --- 89,112 ---- sub renderSideBySide { ! my ( $diffType, $left, $right ) = @_; my $result = ""; ! if ( $diffType eq "c" ) { ! $lcolor = $rcolor = "#99ff99"; ! } elsif ( $diffType eq "u" ) { $lcolor = $rcolor = "#99ff99"; ! } elsif ( $diffType eq "+" ) { $lcolor = "#cccccc"; $rcolor = "#ccccff"; ! } elsif ( $diffType eq "-" ) { $lcolor = "#ff9999"; $rcolor = "#cccccc"; + } + if ( $diffType eq "l" ) { + if (( $left ne "" ) && ($right ne "" )) { + $result .= qq(<tr bgcolor="#eeeeee"><th>Line: $left</th><th>Line: $right</th></tr>\n) } ! } else { $right = renderCellData( $right, $topic ); $left = renderCellData( $left, $topic ); $result .= qq(<tr><td bgcolor="$lcolor" valign="top">$left </td><td bgcolor="$rcolor" valign="top">$right </td></tr>\n); } *************** *** 101,169 **** } # ========================= ! sub renderRevisionDiff { my( $text, $topic, $rev2, $rev1 ) = @_; $text =~ s/\r//go; # cut CR my $result = "<table width=\"100%\" cellspacing=\"0\">\n<tr><td>"; my $data = ""; ! #BEGIN - Side by side diffs ! my $sideBySide = 1;#temporarily default to side-by-side (options come later :) ! if ( $sideBySide ) { ! my $left = ""; ! my $right = ""; ! my $diffstr = ""; ! my $lcolor = ""; ! my $rcolor = ""; ! $result .= "<tr><td width=\"50%\"><b>Rev. $rev2</b></td><td width=\"50%\"><b>Rev. $rev1</b></td></tr>\n"; ! foreach( split( /\n/, $text ) ) { ! if( /^[0-9]/ ) { ! $result .= renderSideBySide ( $diffstr, $left, $right ); ! ! $left = ""; ! $right = ""; ! $diffstr = $_; ! } elsif ( /^</ ) { ! s/^< //go; ! $left .= "\n$_"; ! } elsif ( /^>/ ) { ! s/^> //go; ! $right .= "\n$_"; ! } ! } ! $result .= renderSideBySide ( $diffstr, $left, $right ); ! $text = "$result\n<\/table>"; ! #END - Side by side diffs ! } else { ! foreach( split( /\n/, $text ) ) { ! if( /^[0-9]/ ) { ! $data = renderCellData( $data, $topic ); ! $result .= $data; ! $data = ""; ! s/^([0-9\,]+)d([0-9\,]+)/\n<\/td><\/tr>\n<tr><td bgcolor="#FFD7D7" colspan ="2"><b> Deleted: <\/b><\/td><\/tr>\n<tr><td bgcolor="#FF4040" valign="top" width="1%"><<br \/><<\/td><td>/go; ! s/^([0-9\,]+)a([0-9\,]+)/\n<\/td><\/tr>\n<tr><td bgcolor="#D0FFD0" colspan ="2"><b> Added: <\/b><\/td><\/tr>\n<tr><td bgcolor="#40FF40" valign="top" width="1%">><br \/>><\/td><td>/go; ! s/^([0-9\,]+)c([0-9\,]+)/\n<\/td><\/tr>\n<tr><td bgcolor="#D0FFD0" colspan ="2"><b> Changed: <\/b><\/td><\/tr>\n<tr><td bgcolor="#FF4040" valign="top" width="1%"><<br \/><<\/td><td>/go; ! $result .= $_; ! } elsif( /^[<>]/ ) { ! s/^[<>] (.*)/$1/go; ! $data .= "\n$_"; ! ! } elsif( /^--*/ ) { ! $data = renderCellData( $data, $topic ); ! $result .= $data; ! $data = ""; ! $result .= "\n</td></tr><tr><td bgcolor=\"#40FF40\" valign=\"top\" width=\"1%\">><br />></td><td>\n"; ! } ! } ! $data = renderCellData( $data, $topic ); ! $result .= $data; ! $data = ""; ! $text = "$result<\/td><\/tr>\n<\/table>"; } return $text; } ! sub getRevInfo --- 114,224 ---- } + + ###### + #========================== + sub renderDebug + { + my ( $diffType, $left, $right ) = @_; + my $result = ""; + + $result = "<hr>type: $diffType<p style=\"border: 1px dotted;\">$left</p><p style=\"border: 1px dotted;\">$right</p>"; + + return $result; + } + + + #========================== + sub renderSequential + { + my ( $diffType, $left, $right ) = @_; + my $result = ""; + + if ( $diffType eq "-") { + $result = "\n<\/td><\/tr>\n<tr><td bgcolor=\"#FFD7D7\" colspan =\"2\"><b> Deleted: <\/b><\/td><\/tr>\n<tr><td bgcolor=\"#FF4040\" valign=\"top\" width=\"1%\"><<br \/><<\/td><td>" ; + $result .= renderCellData( $left, $topic ); + } elsif ( $diffType eq "+") { + $result = "\n<\/td><\/tr>\n<tr><td bgcolor=\"#D0FFD0\" colspan =\"2\"><b> Added: <\/b><\/td><\/tr>\n<tr><td bgcolor=\"#40FF40\" valign=\"top\" width=\"1%\">><br \/>><\/td><td>" ; + $result .= renderCellData( $right, $topic ); + } elsif ( $diffType eq "c") { + $result = "\n<\/td><\/tr>\n<tr><td bgcolor=\"#D0FFD0\" colspan =\"2\"><b> Changed: <\/b><\/td><\/tr>\n<tr><td bgcolor=\"#FF4040\" valign=\"top\" width=\"1%\"><<br \/><<\/td><td>" ; + $result .= renderCellData( $left, $topic ); + $result .= renderCellData( $right, $topic ); + } + + return $result; + } + + # ========================= ! sub parseRevisionDiff { my( $text, $topic, $rev2, $rev1 ) = @_; + my ( $diffFormat ) = "normal"; #or rcs, unified... + my ( @diffArray ); + + $diffFormat = "unified" if ( $text =~ /^---/ ); + + if ( $diffFormat eq "unified" ) { + #skip the 2 header lines + $text =~ s/^---.*$//go; # --- WebHome.txt 2000/08/03 08:37:18 1.1 + $text =~ s/^\+\+\+.*$//go; # +++ WebHome.txt 2000/08/19 00:08:37 1.3 + } + $text =~ s/\r//go; # cut CR + + #"normal" rcsdiff output + foreach( split( /\n/, $text ) ) { + if ( /^([0-9]+)[0-9\,]*([acd])([0-9]+)/ ) { + #line number + push @diffArray, ["l", $1, $3]; + } elsif ( /^</ ) { + s/^< //go; + if ( @{@diffArray[$#diffArray]}[0] eq "-" ) { + @{@diffArray[$#diffArray]}[1] .= "\n$_"; + } else { + push @diffArray, ["-", $_, ""]; + } + } elsif ( /^>/ ) { + s/^> //go; + if ( @{@diffArray[$#diffArray]}[0] eq "+" ) { + @{@diffArray[$#diffArray]}[2] .= "\n$_"; + } else { + push @diffArray, ["+", "", $_]; + } + } else { + #empty lines and the --- selerator in the diff + #push @diffArray, ["u", "$_", $_]; + } + } + return \@diffArray; + } + # ========================= + sub renderRevisionDiff + { + my( $diffArray_ref, $renderStyle ) = @_; + + my @diffArray = @{ $diffArray_ref }; + my $result = "<table width=\"100%\" cellspacing=\"0\">\n<tr><td>"; my $data = ""; ! for my $diff_ref ( @diffArray ) { ! if (( @$diff_ref[0] eq "l" ) && ( @$diff_ref[1] eq 0 ) && (@$diff_ref[2] eq 0)) { ! next; } + if ( $renderStyle eq "sequential" ) { + $result .= renderSequential ( @$diff_ref ); + } elsif ( $renderStyle eq "sidebyside" ) { + $result .= "<tr><td width=\"50%\"></td><td width=\"50%\"></td></tr>\n"; + $result .= renderSideBySide ( @$diff_ref ); + } elsif ( $renderStyle eq "debug" ) { + $result .= renderDebug ( @$diff_ref ); + } + } + $text = "$result\n<\/table>"; return $text; } ! ###### sub getRevInfo *************** *** 195,198 **** --- 250,255 ---- my $theTopic = $query->param('topic'); my $theUrl = $query->url; + my $renderStyle = $query->param('render'); + $renderStyle = "sidebyside" if ( ! $renderStyle ); ( $topic, $webName, $scriptUrlPath, $userName ) = *************** *** 296,300 **** $diff =~ s/%REVINFO1%/$rInfo/go; $text = &TWiki::Store::getRevisionDiff( $webName, $topic, "1.$r2", "1.$r1" ); ! $text = renderRevisionDiff( $text, $topic, "1.$r2", "1.$r1" ); $diff =~ s/%TEXT%/$text/go; $diff =~ s/( ?) *<\/?(nop|noautolink)\/?>\n?/$1/gois; # remove <nop> and <noautolink> tags --- 353,358 ---- $diff =~ s/%REVINFO1%/$rInfo/go; $text = &TWiki::Store::getRevisionDiff( $webName, $topic, "1.$r2", "1.$r1" ); ! my $diffArray = parseRevisionDiff( $text, $topic, "1.$r2", "1.$r1" ); ! $text = renderRevisionDiff( $diffArray, $renderStyle ); $diff =~ s/%TEXT%/$text/go; $diff =~ s/( ?) *<\/?(nop|noautolink)\/?>\n?/$1/gois; # remove <nop> and <noautolink> tags |