1 diff.inc public DiffFormatter::format($diff)

Format a diff.

Parameters

$diff Diff A Diff object.:

Return value

string The formatted output.:

File

core/includes/diff.inc, line 989
A PHP diff engine for phpwiki. (Taken from phpwiki-1.3.3)

Class

DiffFormatter
Base class for diff formatters

Code

public function format($diff) {
  $xi = $yi = 1;
  $block = FALSE;
  $context = array();

  $nlead = $this->leadingContextLines;
  $ntrail = $this->trailingContextLines;

  $this->startDiff();

  // Initialize $x0 and $y0 to prevent IDEs from getting confused.
  $x0 = $y0 = 0;
  foreach ($diff->edits as $edit) {
    if ($edit->type == 'copy') {
      if (is_array($block)) {
        if (count($edit->orig) <= $nlead + $ntrail) {
          $block[] = $edit;
        }
        else {
          if ($ntrail) {
            $context = array_slice($edit->orig, 0, $ntrail);
            $block[] = new DiffOpCopy($context);
          }
          $this->block($x0, $ntrail + $xi - $x0, 
          $y0, $ntrail + $yi - $y0, 
          $block);
          $block = FALSE;
        }
      }
      $context = $edit->orig;
    }
    else {
      if (!is_array($block)) {
        $context = array_slice($context, count($context) - $nlead);
        $x0 = $xi - count($context);
        $y0 = $yi - count($context);
        $block = array();
        if ($context) {
          $block[] = new DiffOpCopy($context);
        }
      }
      $block[] = $edit;
    }

    if ($edit->orig) {
      $xi += count($edit->orig);
    }
    if ($edit->closing) {
      $yi += count($edit->closing);
    }
  }

  if (is_array($block)) {
    $this->block($x0, $xi - $x0, 
    $y0, $yi - $y0, 
    $block);
  }

  $end = $this->endDiff();

  return $end;
}