📄 renderer.php
字号:
return $this->_lines($lines, ' ', false); } function _changed($orig, $final) { // If we've already split on words, don't try to do so again - just display. if ($this->_split_level == 'words') { $prefix = ''; while ($orig[0] !== false && $final[0] !== false && substr($orig[0], 0, 1) == ' ' && substr($final[0], 0, 1) == ' ') { $prefix .= substr($orig[0], 0, 1); $orig[0] = substr($orig[0], 1); $final[0] = substr($final[0], 1); } return $prefix . $this->_deleted($orig) . $this->_added($final); } $text1 = implode("\n", $orig); $text2 = implode("\n", $final); // Non-printing newline marker. $nl = "\0"; // We want to split on word boundaries, but we need to preserve whitespace as well. // Therefore we split on words, but include all blocks of whitespace in the wordlist. $diff = &new diff($this->_split_on_words($text1, $nl), $this->_split_on_words($text2, $nl)); // Get the diff in inline format. $renderer = &new diff_renderer_inline(array_merge($this->get_params(), array('split_level' => 'words'))); // Run the diff and get the output. return str_replace($nl, "\n", $renderer->render($diff)) . "\n"; } function _split_on_words($string, $newline_escape = "\n") { // Ignore \0; otherwise the while loop will never finish. $string = str_replace("\0", '', $string); $words = array(); $length = strlen($string); $pos = 0; $tab_there = true; while ($pos < $length) { // Check for tabs... do not include them if ($tab_there && substr($string, $pos, 1) === "\t") { $words[] = "\t"; $pos++; continue; } else { $tab_there = false; } // Eat a word with any preceding whitespace. $spaces = strspn(substr($string, $pos), " \n"); $nextpos = strcspn(substr($string, $pos + $spaces), " \n"); $words[] = str_replace("\n", $newline_escape, substr($string, $pos, $spaces + $nextpos)); $pos += $spaces + $nextpos; } return $words; } function _encode(&$string) { $string = htmlspecialchars($string); }}/*** "raw" diff renderer.* This class could be used to output a raw unified patch file** @package phpBB3*/class diff_renderer_raw extends diff_renderer { var $_leading_context_lines = 4; var $_trailing_context_lines = 4; /** * Our function to get the diff */ function get_diff_content($diff) { return '<textarea style="height: 400px;" class="full">' . htmlspecialchars($this->render($diff)) . '</textarea>'; } function _block_header($xbeg, $xlen, $ybeg, $ylen) { if ($xlen != 1) { $xbeg .= ',' . $xlen; } if ($ylen != 1) { $ybeg .= ',' . $ylen; } return '@@ -' . $xbeg . ' +' . $ybeg . ' @@'; } function _context($lines) { return $this->_lines($lines, ' '); } function _added($lines) { return $this->_lines($lines, '+'); } function _deleted($lines) { return $this->_lines($lines, '-'); } function _changed($orig, $final) { return $this->_deleted($orig) . $this->_added($final); }}/*** "chora (Horde)" diff renderer - similar style.* This renderer class is a modified human_readable function from the Horde Framework.** @package phpBB3*/class diff_renderer_side_by_side extends diff_renderer { var $_leading_context_lines = 3; var $_trailing_context_lines = 3; var $lines = array(); // Hold the left and right columns of lines for change blocks. var $cols; var $state; var $data = false; /** * Our function to get the diff */ function get_diff_content($diff) { global $user; $output = ''; $output .= '<table cellspacing="0" class="hrdiff"><caption> <span class="unmodified"> </span> ' . $user->lang['LINE_UNMODIFIED'] . ' <span class="added"> </span> ' . $user->lang['LINE_ADDED'] . ' <span class="modified"> </span> ' . $user->lang['LINE_MODIFIED'] . ' <span class="removed"> </span> ' . $user->lang['LINE_REMOVED'] . '</caption><tbody>'; $this->render($diff); // Is the diff empty? if (!sizeof($this->lines)) { $output .= '<tr><th colspan="2">' . $user->lang['NO_VISIBLE_CHANGES'] . '</th></tr>'; } else { // Iterate through every header block of changes foreach ($this->lines as $header) { $output .= '<tr><th>Line ' . $header['oldline'] . '</th><th>' . $user->lang['LINE'] . ' ' . $header['newline'] . '</th></tr>'; // Each header block consists of a number of changes (add, remove, change). $current_context = ''; foreach ($header['contents'] as $change) { if (!empty($current_context) && $change['type'] != 'empty') { $line = $current_context; $current_context = ''; $output .= '<tr class="unmodified"><td><pre>' . ((strlen($line)) ? $line : ' ') . '</pre></td> <td><pre>' . ((strlen($line)) ? $line : ' ') . '</pre></td></tr>'; } switch ($change['type']) { case 'add': $line = ''; foreach ($change['lines'] as $_line) { $line .= htmlspecialchars($_line) . '<br />'; } $output .= '<tr><td class="added_empty"> </td><td class="added"><pre>' . ((strlen($line)) ? $line : ' ') . '</pre></td></tr>'; break; case 'remove': $line = ''; foreach ($change['lines'] as $_line) { $line .= htmlspecialchars($_line) . '<br />'; } $output .= '<tr><td class="removed"><pre>' . ((strlen($line)) ? $line : ' ') . '</pre></td><td class="removed_empty"> </td></tr>'; break; case 'empty': $current_context .= htmlspecialchars($change['line']) . '<br />'; break; case 'change': // Pop the old/new stacks one by one, until both are empty. $oldsize = sizeof($change['old']); $newsize = sizeof($change['new']); $left = $right = ''; for ($row = 0, $row_max = max($oldsize, $newsize); $row < $row_max; ++$row) { $left .= isset($change['old'][$row]) ? htmlspecialchars($change['old'][$row]) : ''; $left .= '<br />'; $right .= isset($change['new'][$row]) ? htmlspecialchars($change['new'][$row]) : ''; $right .= '<br />'; } $output .= '<tr>'; if (!empty($left)) { $output .= '<td class="modified"><pre>' . $left . '</pre></td>'; } else if ($row < $oldsize) { $output .= '<td class="modified"> </td>'; } else { $output .= '<td class="unmodified"> </td>'; } if (!empty($right)) { $output .= '<td class="modified"><pre>' . $right . '</pre></td>'; } else if ($row < $newsize) { $output .= '<td class="modified"> </td>'; } else { $output .= '<td class="unmodified"> </td>'; } $output .= '</tr>'; break; } } if (!empty($current_context)) { $line = $current_context; $current_context = ''; $output .= '<tr class="unmodified"><td><pre>' . ((strlen($line)) ? $line : ' ') . '</pre></td>'; $output .= '<td><pre>' . ((strlen($line)) ? $line : ' ') . '</pre></td></tr>'; } } } $output .= '</tbody></table>'; return $output; } function _start_diff() { $this->lines = array(); $this->data = false; $this->cols = array(array(), array()); $this->state = 'empty'; return ''; } function _end_diff() { // Just flush any remaining entries in the columns stack. switch ($this->state) { case 'add': $this->data['contents'][] = array('type' => 'add', 'lines' => $this->cols[0]); break; case 'remove': // We have some removal lines pending in our stack, so flush them. $this->data['contents'][] = array('type' => 'remove', 'lines' => $this->cols[0]); break; case 'change': // We have both remove and addition lines, so this is a change block. $this->data['contents'][] = array('type' => 'change', 'old' => $this->cols[0], 'new' => $this->cols[1]); break; } if ($this->data !== false) { $this->lines[] = $this->data; } return ''; } function _block_header($xbeg, $xlen, $ybeg, $ylen) { // Push any previous header information to the return stack. if ($this->data !== false) { $this->lines[] = $this->data; } $this->data = array('type' => 'header', 'oldline' => $xbeg, 'newline' => $ybeg, 'contents' => array()); $this->state = 'dump'; } function _added($lines) { array_walk($lines, array(&$this, '_perform_add')); } function _perform_add($line) { if ($this->state == 'empty') { return ''; } // This is just an addition line. if ($this->state == 'dump' || $this->state == 'add') { // Start adding to the addition stack. $this->cols[0][] = $line; $this->state = 'add'; } else { // This is inside a change block, so start accumulating lines. $this->state = 'change'; $this->cols[1][] = $line; } } function _deleted($lines) { array_walk($lines, array(&$this, '_perform_delete')); } function _perform_delete($line) { // This is a removal line. $this->state = 'remove'; $this->cols[0][] = $line; } function _context($lines) { array_walk($lines, array(&$this, '_perform_context')); } function _perform_context($line) { // An empty block with no action. switch ($this->state) { case 'add': $this->data['contents'][] = array('type' => 'add', 'lines' => $this->cols[0]); break; case 'remove': // We have some removal lines pending in our stack, so flush them. $this->data['contents'][] = array('type' => 'remove', 'lines' => $this->cols[0]); break; case 'change': // We have both remove and addition lines, so this is a change block. $this->data['contents'][] = array('type' => 'change', 'old' => $this->cols[0], 'new' => $this->cols[1]); break; } $this->cols = array(array(), array()); $this->data['contents'][] = array('type' => 'empty', 'line' => $line); $this->state = 'dump'; } function _changed($orig, $final) { return $this->_deleted($orig) . $this->_added($final); }}?>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -