Skip to content

Commit

Permalink
Rewrite and simplify the package, add tests
Browse files Browse the repository at this point in the history
No support for plaintext mode, no classes in del/ins tags

Signed-off-by: Peter Legierski <[email protected]>
  • Loading branch information
Peter Legierski committed Jul 31, 2014
1 parent 472213b commit 889dff6
Show file tree
Hide file tree
Showing 10 changed files with 547 additions and 865 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
}
],
"require": {
"cogpowered/finediff": "0.0.1"
"cogpowered/finediff": "0.3.1",
"ext-tidy": "*"
},
"require-dev": {
"phpunit/phpunit": "4.*",
Expand Down
21 changes: 21 additions & 0 deletions src/GatherContent/Htmldiff/Differ.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace GatherContent\Htmldiff;

use \cogpowered\FineDiff\Diff;
use \cogpowered\FineDiff\Granularity\Paragraph as Granularity;

class Differ implements DifferInterface
{
private $diff;

public function __construct(Processor $processor = null, Diff $diff = null)
{
$this->diff = $diff ?: new Diff(new Granularity, new GitDiffRenderer);
}

public function diff($old, $new)
{
return $this->diff->render($old, $new);
}
}
8 changes: 8 additions & 0 deletions src/GatherContent/Htmldiff/DifferInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace GatherContent\Htmldiff;

interface DifferInterface
{
public function diff($old, $new);
}
43 changes: 43 additions & 0 deletions src/GatherContent/Htmldiff/GitDiffRenderer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/*
* Here we try to end up with an output that looks like Git's diff.
* It's a modified version of \cogpowered\FineDiff\Render\Html
*/

namespace GatherContent\Htmldiff;

use \cogpowered\FineDiff\Parser\OpcodeInterface;
use \cogpowered\FineDiff\Render\Renderer;

class GitDiffRenderer extends Renderer
{
public function callback($opcode, $from, $from_offset, $from_len)
{
if ($opcode === 'c') {

$str = ' '.implode("\n ", explode("\n", substr($from, $from_offset, $from_len)))."\n";
$str = rtrim($str, "\n ")."\n";

} elseif ($opcode === 'd') {

$deletion = substr($from, $from_offset, $from_len);

if (strcspn($deletion, " \n\r") === 0) {

$deletion = str_replace(array("\n","\r"), array('\n','\r'), $deletion);
}

$str = '-'.implode("\n-", explode("\n", $deletion))."\n";
$str = rtrim($str, "-\n")."\n";

} else /* if ( $opcode === 'i' ) */ {

$str = '+'.implode("\n+", explode("\n", substr($from, $from_offset, $from_len)))."\n";
$str = rtrim($str, "+\n")."\n";

}

return $str;
}
}
Loading

0 comments on commit 889dff6

Please sign in to comment.