Sidebyside.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. /**
  3. * "Side-by-Side" diff renderer.
  4. *
  5. * This class renders the diff in "side-by-side" format, like Wikipedia.
  6. *
  7. * @package Text_Diff
  8. */
  9. class Horde_Text_Diff_Renderer_Sidebyside extends Horde_Text_Diff_Renderer {
  10. protected $_character_diff = true;
  11. protected $_leading_context_lines = 4;
  12. protected $_trailing_context_lines = 4;
  13. protected function _startDiff() {
  14. ob_start();
  15. echo '<table class="normal diff">';
  16. }
  17. protected function _endDiff() {
  18. echo '</table>';
  19. $val = ob_get_contents();
  20. ob_end_clean();
  21. return $val;
  22. }
  23. protected function _blockHeader($xbeg, $xlen, $ybeg, $ylen) {
  24. return "$xbeg,$xlen,$ybeg,$ylen";
  25. }
  26. protected function _startBlock($header) {
  27. $h = split(",", $header);
  28. echo '<tr class="diffheader"><td colspan="2">';
  29. if ($h[1] == 1)
  30. echo "Line:&nbsp;" . $h[0];
  31. else {
  32. $h[1] = $h[0] + $h[1] - 1;
  33. echo "Lines:&nbsp;" . $h[0] . '-' . $h[1];
  34. }
  35. echo '</td><td colspan="2">';
  36. if ($h[3] == 1)
  37. echo "Line:&nbsp;" . $h[2];
  38. else {
  39. $h[3] = $h[2] + $h[3] - 1;
  40. echo "Lines:&nbsp;" . $h[2] . '-' . $h[3];
  41. }
  42. echo '</td></tr>';
  43. }
  44. protected function _endBlock() {
  45. }
  46. protected function _lines($type, $lines, $prefix = '') {
  47. if ($type == 'context') {
  48. foreach ($lines as $line) {
  49. if (!empty($line))
  50. echo "<tr class='diffbody'><td>&nbsp;</td><td>$line</td><td>&nbsp;</td><td>$line</td></tr>\n";
  51. }
  52. } elseif ($type == 'added') {
  53. foreach ($lines as $line) {
  54. if (!empty($line))
  55. echo "<tr><td colspan='2'>&nbsp;</td><td class='diffadded'>$prefix</td><td class='diffadded'>$line</td></tr>\n";
  56. }
  57. } elseif ($type == 'deleted') {
  58. foreach ($lines as $line) {
  59. if (!empty($line))
  60. echo "<tr><td class='diffdeleted'>$prefix</td><td class='diffdeleted'>$line</td><td colspan='2'>&nbsp;</td></tr>\n";
  61. }
  62. } elseif ($type == 'change-deleted') {
  63. echo '<tr><td class="diffdeleted" valign="top">' . $prefix . '</td><td class="diffdeleted" valign="top">' . implode("<br />", $lines) .
  64. "</td>\n";
  65. } elseif ($type == 'change-added') {
  66. echo '<td class="diffadded" valign="top">' . $prefix . '</td><td class="diffadded" valign="top">' . implode("<br />", $lines) . "</td></tr>\n";
  67. }
  68. }
  69. protected function _context($lines) {
  70. $this->_lines('context', $lines);
  71. }
  72. protected function _added($lines, $changemode = false) {
  73. if ($changemode) {
  74. $this->_lines('change-added', $lines, '+');
  75. } else {
  76. $this->_lines('added', $lines, '+');
  77. }
  78. }
  79. protected function _deleted($lines, $changemode = false) {
  80. if ($changemode) {
  81. $this->_lines('change-deleted', $lines, '-');
  82. } else {
  83. $this->_lines('deleted', $lines, '-');
  84. }
  85. }
  86. protected function _changed($orig, $final) {
  87. if ($this->_character_diff) {
  88. $lines = $this->diffChar($orig, $final);
  89. $this->_deleted([$lines[0]], true);
  90. $this->_added([$lines[1]], true);
  91. } else {
  92. $this->_deleted($orig, true);
  93. $this->_added($final, true);
  94. }
  95. }
  96. //refactor!!!
  97. public function diffChar($orig, $final) {
  98. $line1 = preg_split('//', implode("<br />", $orig), -1, PREG_SPLIT_NO_EMPTY);
  99. $line2 = preg_split('//', implode("<br />", $final), -1, PREG_SPLIT_NO_EMPTY);
  100. $z = new Horde_Text_Diff($line1, $line2);
  101. if ($z->isEmpty()) {
  102. return [$orig[0], $final[0]];
  103. }
  104. // echo "<pre>";print_r($z);echo "</pre>";
  105. $renderer = new Horde_Text_Diff_Renderer_Character(10000);
  106. return $renderer->render($z);
  107. }
  108. }