📄 renderer.php
字号:
<?php/** ** @package phpBB3* @version $Id: renderer.php,v 1.2 2006/11/17 19:37:57 acydburn Exp $* @copyright (c) 2006 phpBB Group * @license http://opensource.org/licenses/gpl-license.php GNU Public License **//***/if (!defined('IN_PHPBB')){ exit;}/*** Code from pear.php.net, Text_Diff-0.2.1 (beta) package* http://pear.php.net/package/Text_Diff/** Modified by phpBB Group to meet our coding standards* and being able to integrate into phpBB** A class to render Diffs in different formats.** This class renders the diff in classic diff format. It is intended that* this class be customized via inheritance, to obtain fancier outputs.** @package phpBB3*/class diff_renderer{ /** * Number of leading context "lines" to preserve. * * This should be left at zero for this class, but subclasses may want to * set this to other values. */ var $_leading_context_lines = 0; /** * Number of trailing context "lines" to preserve. * * This should be left at zero for this class, but subclasses may want to * set this to other values. */ var $_trailing_context_lines = 0; /** * Constructor. */ function diff_renderer($params = array()) { foreach ($params as $param => $value) { $v = '_' . $param; if (isset($this->$v)) { $this->$v = $value; } } } /** * Get any renderer parameters. * * @return array All parameters of this renderer object. */ function get_params() { $params = array(); foreach (get_object_vars($this) as $k => $v) { if ($k[0] == '_') { $params[substr($k, 1)] = $v; } } return $params; } /** * Renders a diff. * * @param diff &$diff A diff object. * * @return string The formatted output. */ function render(&$diff) { $xi = $yi = 1; $block = false; $context = array(); // Create a new diff object if it is a 3-way diff if (is_a($diff, 'diff3')) { $diff3 = &$diff; $diff = &new diff($diff3->get_original(), $diff3->merged_output()); unset($diff3); } $nlead = $this->_leading_context_lines; $ntrail = $this->_trailing_context_lines; $output = $this->_start_diff(); $diffs = $diff->get_diff(); foreach ($diffs as $i => $edit) { if (is_a($edit, 'diff_op_copy')) { if (is_array($block)) { $keep = ($i == sizeof($diffs) - 1) ? $ntrail : $nlead + $ntrail; if (sizeof($edit->orig) <= $keep) { $block[] = $edit; } else { if ($ntrail) { $context = array_slice($edit->orig, 0, $ntrail); $block[] = &new diff_op_copy($context); } $output .= $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, sizeof($context) - $nlead); $x0 = $xi - sizeof($context); $y0 = $yi - sizeof($context); $block = array(); if ($context) { $block[] = &new diff_op_copy($context); } } $block[] = $edit; } $xi += ($edit->orig) ? sizeof($edit->orig) : 0; $yi += ($edit->final) ? sizeof($edit->final) : 0; } if (is_array($block)) { $output .= $this->_block($x0, $xi - $x0, $y0, $yi - $y0, $block); } return $output . $this->_end_diff(); } function _block($xbeg, $xlen, $ybeg, $ylen, &$edits) { $output = $this->_start_block($this->_block_header($xbeg, $xlen, $ybeg, $ylen)); foreach ($edits as $edit) { switch (get_class($edit)) { case 'diff_op_copy': $output .= $this->_context($edit->orig); break; case 'diff_op_add': $output .= $this->_added($edit->final); break; case 'diff_op_delete': $output .= $this->_deleted($edit->orig); break; case 'diff_op_change': $output .= $this->_changed($edit->orig, $edit->final); break; } } return $output . $this->_end_block(); } function _start_diff() { return ''; } function _end_diff() { return ''; } function _block_header($xbeg, $xlen, $ybeg, $ylen) { if ($xlen > 1) { $xbeg .= ',' . ($xbeg + $xlen - 1); } if ($ylen > 1) { $ybeg .= ',' . ($ybeg + $ylen - 1); } return $xbeg . ($xlen ? ($ylen ? 'c' : 'd') : 'a') . $ybeg; } function _start_block($header) { return $header . "\n"; } function _end_block() { return ''; } function _lines($lines, $prefix = ' ') { return $prefix . implode("\n$prefix", $lines) . "\n"; } 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) . "---\n" . $this->_added($final); } /** * Our function to get the diff */ function get_diff_content($diff) { return $this->render($diff); }}/*** Renders a unified diff* @package phpBB3*/class diff_renderer_unified 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 nl2br($this->render($diff)); } function _block_header($xbeg, $xlen, $ybeg, $ylen) { if ($xlen != 1) { $xbeg .= ',' . $xlen; } if ($ylen != 1) { $ybeg .= ',' . $ylen; } return '<div class="diff"><big class="info">@@ -' . $xbeg . ' +' . $ybeg . ' @@</big></div>'; } function _context($lines) { return '<pre class="diff context">' . htmlspecialchars($this->_lines($lines, ' ')) . '</pre>'; } function _added($lines) { return '<pre class="diff added">' . htmlspecialchars($this->_lines($lines, '+')) . '</pre>'; } function _deleted($lines) { return '<pre class="diff removed">' . htmlspecialchars($this->_lines($lines, '-')) . '</pre>'; } function _changed($orig, $final) { return $this->_deleted($orig) . $this->_added($final); } function _start_diff() { $start = '<div class="file">'; return $start; } function _end_diff() { return '</div>'; } function _end_block() { return ''; }}/*** "Inline" diff renderer.** This class renders diffs in the Wiki-style "inline" format.** @author Ciprian Popovici* @package phpBB3*/class diff_renderer_inline extends diff_renderer{ var $_leading_context_lines = 10000; var $_trailing_context_lines = 10000; // Prefix and suffix for inserted text var $_ins_prefix = '<span class="ins">'; var $_ins_suffix = '</span>'; // Prefix and suffix for deleted text var $_del_prefix = '<span class="del">'; var $_del_suffix = '</span>'; var $_block_head = ''; // What are we currently splitting on? Used to recurse to show word-level var $_split_level = 'lines'; /** * Our function to get the diff */ function get_diff_content($diff) { return '<pre>' . nl2br($this->render($diff)) . '</pre>'; } function _start_diff() { return ''; } function _end_diff() { return ''; } function _block_header($xbeg, $xlen, $ybeg, $ylen) { return $this->_block_head; } function _start_block($header) { return $header; } function _lines($lines, $prefix = ' ', $encode = true) { if ($encode) { array_walk($lines, array(&$this, '_encode')); } if ($this->_split_level == 'words') { return implode('', $lines); } else { return implode("\n", $lines) . "\n"; } } function _added($lines) { array_walk($lines, array(&$this, '_encode')); $lines[0] = $this->_ins_prefix . $lines[0]; $lines[sizeof($lines) - 1] .= $this->_ins_suffix; return $this->_lines($lines, ' ', false); } function _deleted($lines, $words = false) { array_walk($lines, array(&$this, '_encode')); $lines[0] = $this->_del_prefix . $lines[0]; $lines[sizeof($lines) - 1] .= $this->_del_suffix;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -