Add a diff system

Thanks to https://github.com/paulgb/simplediff/
This commit is contained in:
Némunaire 2012-01-20 17:43:25 +01:00
parent 3898e64c97
commit 75f83329af
4 changed files with 95 additions and 5 deletions

View File

@ -22,7 +22,10 @@ foreach ($_GET as $k => $t)
$geshi = new GeSHi($paste->content, $paste->language); $geshi = new GeSHi($paste->content, $paste->language);
$geshi->enable_line_numbers(GESHI_FANCY_LINE_NUMBERS, 5); if (!empty($kout[3]) && is_file(Paste::get_path($kout[3])))
$diff = new Paste($kout[3]);
$geshi->enable_line_numbers(GESHI_FANCY_LINE_NUMBERS, 5);
?> ?>
<div id="corps" style="text-align: center;"> <div id="corps" style="text-align: center;">
@ -33,10 +36,13 @@ foreach ($_GET as $k => $t)
<div id="content"> <div id="content">
<div class="answer"> <div class="answer">
<a href="/?a=<?php echo $kout[1]; ?>">Répondre</a> <a href="/?a=<?php echo $kout[1]; ?>">Répondre</a>
<?php echo $paste->get_ref(); ?> <?php echo $paste->get_ref(isset($diff)); ?>
</div> </div>
<?php <?php
echo $geshi->parse_code(); if (isset($diff))
echo $paste->get_diff($diff);
else
echo $geshi->parse_code();
?> ?>
</div> </div>
</div> </div>

View File

@ -113,3 +113,12 @@ div.answer {
font-size: 65%; font-size: 65%;
font-style: italic; font-style: italic;
} }
ins
{
color: #00DF22;
}
del
{
color: #DF2200;
}

View File

@ -166,13 +166,34 @@ class Paste
$this->date); $this->date);
} }
function get_ref() function get_ref($is_diff)
{ {
if (!empty($this->ref)) if (!empty($this->ref))
return '<a href="/?'.$this->ref.'">Voir l\'original</a>'; {
if (empty($is_diff))
return '<a href="/?'.$this->ref.'">Voir l\'original</a> '.
'<a href="/?'.$this->fileref.':'.$this->ref.'">Voir la différence</a>';
else
return '<a href="/?'.$this->fileref.'">Cacher les différences</a> ';
}
else else
return ""; return "";
} }
function get_diff($diff)
{
require_once("geshi/geshi.php");
require_once("simplediff.php");
$geshi = new GeSHi(htmlDiff($diff->content, $this->content),
$this->language);
return str_replace("&lt;ins&gt;", "<ins>",
str_replace("&lt;/ins&gt;", "</ins>",
str_replace("&lt;del&gt;", "<del>",
str_replace("&lt;/del&gt;", "</del>",
$geshi->parse_code()))));
}
} }
?> ?>

54
simplediff.php Normal file
View File

@ -0,0 +1,54 @@
<?php
/*
Paul's Simple Diff Algorithm v 0.1
(C) Paul Butler 2007 <http://www.paulbutler.org/>
May be used and distributed under the zlib/libpng license.
This code is intended for learning purposes; it was written with short
code taking priority over performance. It could be used in a practical
application, but there are a few ways it could be optimized.
Given two arrays, the function diff will return an array of the changes.
I won't describe the format of the array, but it will be obvious
if you use print_r() on the result of a diff on some test data.
htmlDiff is a wrapper for the diff command, it takes two strings and
returns the differences in HTML. The tags used are <ins> and <del>,
which can easily be styled with CSS.
*/
function diff($old, $new){
$maxlen = 0;
foreach($old as $oindex => $ovalue){
$nkeys = array_keys($new, $ovalue);
foreach($nkeys as $nindex){
$matrix[$oindex][$nindex] = isset($matrix[$oindex - 1][$nindex - 1]) ?
$matrix[$oindex - 1][$nindex - 1] + 1 : 1;
if($matrix[$oindex][$nindex] > $maxlen){
$maxlen = $matrix[$oindex][$nindex];
$omax = $oindex + 1 - $maxlen;
$nmax = $nindex + 1 - $maxlen;
}
}
}
if($maxlen == 0) return array(array('d'=>$old, 'i'=>$new));
return array_merge(
diff(array_slice($old, 0, $omax), array_slice($new, 0, $nmax)),
array_slice($new, $nmax, $maxlen),
diff(array_slice($old, $omax + $maxlen), array_slice($new, $nmax + $maxlen)));
}
function htmlDiff($old, $new){
$ret = '';
$diff = diff(explode(' ', $old), explode(' ', $new));
foreach($diff as $k){
if(is_array($k))
$ret .= (!empty($k['d'])?"<del>".implode(' ',$k['d'])."</del> ":'').
(!empty($k['i'])?"<ins>".implode(' ',$k['i'])."</ins> ":'');
else $ret .= $k . ' ';
}
return $ret;
}
?>