PageRenderTime 41ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/diff/difftableformatter.php

https://github.com/gmarrot/jelix
PHP | 196 lines | 158 code | 30 blank | 8 comment | 17 complexity | 5195292285b5775ad4030e47010e59c1 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, BSD-3-Clause
  1. <?php
  2. /**
  3. * Wikipedia Table style diff formatter.
  4. * @author Olivier Demah (for Jelix)
  5. * @contributor Laurent Jouanneau
  6. * @copyright 2008 Olivier Demah, 2009 Laurent Jouanneau
  7. */
  8. require_once(__DIR__.'/diffhtml.php');
  9. class _HtmlTableDiffFormatter extends DiffFormatter
  10. {
  11. protected $originalLineNum = 0;
  12. protected $finalLineNum = 0;
  13. function __construct($version1,$version2) {
  14. $this->version1 = $version1;
  15. $this->version2 = $version2;
  16. $this->leading_context_lines = 2;
  17. $this->trailing_context_lines = 2;
  18. }
  19. function _end_diff() {
  20. echo '</table>';
  21. $val = ob_get_contents();
  22. ob_end_clean();
  23. return $val;
  24. }
  25. function _start_block( $header ) {
  26. echo '<tbody class="block">';
  27. echo '<tr class="headblock">'.$header.'</tr>';
  28. }
  29. function _end_block() {
  30. echo '</tbody>';
  31. }
  32. function _lines( $lines, $prefix=' ', $color="white" ) {
  33. }
  34. function _changed( $orig, $closing ) {
  35. $diff = new WordLevelDiff( $orig, $closing );
  36. $del = $diff->orig();
  37. $add = $diff->_final();
  38. while ( $line = array_shift( $del ) ) {
  39. $aline = array_shift( $add );
  40. $this->changedLine($line, $aline);
  41. $this->originalLineNum++;
  42. $this->finalLineNum++;
  43. }
  44. $this->_added( $add ); // If any leftovers
  45. }
  46. }
  47. class HtmlTableDiffFormatter extends _HtmlTableDiffFormatter
  48. {
  49. function addedLine($line) {
  50. $line = str_replace(' ','&nbsp; ', $line);
  51. return '<th>'.$this->finalLineNum.'</th><td class="final">' .$line.'</td>';
  52. }
  53. function deletedLine($line) {
  54. $line = str_replace(' ','&nbsp; ',$line);
  55. return '<th>'.$this->originalLineNum.'</th><td class="original">' .$line.'</td>';
  56. }
  57. function changedLine($original, $final) {
  58. print( '<tr class="modified">' . $this->deletedLine($original) . $this->addedLine($final) . "</tr>\n" );
  59. }
  60. function emptyLine() {
  61. //$line = str_replace(' ','&nbsp; ',$line);
  62. return '<td colspan="2">&nbsp;</td>';
  63. }
  64. function contextLine($line, $number) {
  65. $line = str_replace(' ','&nbsp; ',$line);
  66. return '<th>'.$number.'</th><td>'.$line.'</td>';
  67. }
  68. function _start_diff() {
  69. ob_start();
  70. echo '<table class="diff sidebyside">'."\n".
  71. '<colgroup class="l"><col class="lineno" /><col class="diffcontent" /></colgroup>'."\n".
  72. '<colgroup class="r"><col class="lineno" /><col class="diffcontent" /></colgroup>'."\n";
  73. if($this->version1 != '' || $this->version2 != '')
  74. echo '<thead><tr><th colspan="2">'.$this->version1."</th>\n" .
  75. '<th colspan="2">'.$this->version2."</th></tr></thead>\n";
  76. }
  77. function _block_header( $xbeg, $xlen, $ybeg, $ylen ) {
  78. $this->originalLineNum = $xbeg;
  79. $this->finalLineNum = $ybeg;
  80. if ($xlen != 1)
  81. $xbeg .= "," . $xlen;
  82. if ($ylen != 1)
  83. $ybeg .= "," . $ylen;
  84. $r = '<th>'.$xbeg.'</th> <th>&nbsp;</th> <th>'.$ybeg.'</th> <th>&nbsp;</th>';
  85. return $r;
  86. }
  87. function _added($lines) {
  88. foreach ($lines as $line) {
  89. print( '<tr class="added">' . $this->emptyLine() . $this->addedLine($line) . "</tr>\n" );
  90. $this->finalLineNum++;
  91. }
  92. }
  93. function _deleted($lines) {
  94. foreach ($lines as $line) {
  95. print( '<tr class="deleted">' . $this->deletedLine($line) . $this->emptyLine() . "</tr>\n" );
  96. $this->originalLineNum++;
  97. }
  98. }
  99. function _context( $lines ) {
  100. foreach ($lines as $line) {
  101. print( '<tr class="context">' . $this->contextLine($line,$this->originalLineNum) . $this->contextLine($line,$this->finalLineNum) . "</tr>\n" );
  102. $this->originalLineNum++;
  103. $this->finalLineNum++;
  104. }
  105. }
  106. }
  107. class HtmlInlineTableDiffFormatter extends _HtmlTableDiffFormatter
  108. {
  109. function changedLine($original, $final) {
  110. print( '<tr class="modified">' . $this->deletedLine($original) . "</tr>\n" );
  111. print( '<tr class="modified">' . $this->addedLine($final) . "</tr>\n" );
  112. }
  113. function addedLine( $line ) {
  114. $line = str_replace(' ','&nbsp; ',$line);
  115. return '<th>&nbsp;</th><th>'.$this->finalLineNum.'</th><td class="final">' .$line.'</td>';
  116. }
  117. function deletedLine( $line ) {
  118. $line = str_replace(' ','&nbsp; ',$line);
  119. return '<th>'.$this->originalLineNum.'</th><th>&nbsp;</th><td class="original">' .$line.'</td>';
  120. }
  121. function emptyLine() {
  122. //$line = str_replace(' ','&nbsp; ',$line);
  123. return '<th>'.$this->originalLineNum.'</th><th>'.$this->finalLineNum.'</th><td>&nbsp;</td>';
  124. }
  125. function contextLine($line) {
  126. $line = str_replace(' ','&nbsp; ',$line);
  127. return '<th>'.$this->originalLineNum.'</th><th>'.$this->finalLineNum.'</th><td>'.$line.'</td>';
  128. }
  129. function _start_diff() {
  130. ob_start();
  131. echo '<table class="diff inlinetable">'."\n".
  132. '<colgroup><col class="lineno" /><col class="lineno" /><col class="diffcontent" /></colgroup>'."\n";
  133. if($this->version1 != '' || $this->version2 != '')
  134. echo '<thead><tr><th>'.$this->version1."</th>\n" .
  135. '<th>'.$this->version2."</th><th>&nbsp;</th></tr></thead>\n";
  136. }
  137. function _block_header( $xbeg, $xlen, $ybeg, $ylen ) {
  138. $this->originalLineNum = $xbeg;
  139. $this->finalLineNum = $ybeg;
  140. if ($xlen != 1)
  141. $xbeg .= "," . $xlen;
  142. if ($ylen != 1)
  143. $ybeg .= "," . $ylen;
  144. $r = '<th>'.$xbeg.'</th> <th>'.$ybeg.'</th> <th>&nbsp;</th>';
  145. return $r;
  146. }
  147. function _added($lines) {
  148. foreach ($lines as $line) {
  149. print( '<tr class="added">'. $this->addedLine($line) . "</tr>\n" );
  150. $this->finalLineNum++;
  151. }
  152. }
  153. function _deleted($lines) {
  154. foreach ($lines as $line) {
  155. print( '<tr class="deleted">'. $this->deletedLine($line) ."</tr>\n" );
  156. $this->originalLineNum++;
  157. }
  158. }
  159. function _context( $lines ) {
  160. foreach ($lines as $line) {
  161. print( '<tr class="context">' . $this->contextLine($line) ."</tr>\n" );
  162. $this->originalLineNum++;
  163. $this->finalLineNum++;
  164. }
  165. }
  166. }