2 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
8 * LICENSE: This source file is subject to version 3.0 of the PHP license
9 * that is available through the world-wide-web at the following URI:
10 * http://www.php.net/license/3_0.txt. If you did not receive a copy of
11 * the PHP License and are unable to obtain it through the web, please
12 * send a note to license@php.net so we can mail you a copy immediately.
15 * @package Text_Highlighter
16 * @author Andrey Demenev <demenev@gmail.com>
17 * @copyright 2004-2006 Andrey Demenev
18 * @license http://www.php.net/license/3_0.txt PHP License
19 * @version CVS: $Id: Console.php,v 1.1 2007/06/03 02:37:08 ssttoo Exp $
20 * @link http://pear.php.net/package/Text_Highlighter
27 require_once dirname(__FILE__).'/../Renderer.php';
29 define ('HL_CONSOLE_DEFCOLOR', "\033[0m");
34 * Suitable for displaying text on color-capable terminals, directly
37 * Elements of $options argument of constructor (each being optional):
39 * - 'numbers' - whether to add line numbers
40 * - 'tabsize' - Tab size
41 * - 'colors' - additional colors
43 * @author Andrey Demenev <demenev@gmail.com>
45 * @package Text_Highlighter
46 * @copyright 2004-2006 Andrey Demenev
47 * @license http://www.php.net/license/3_0.txt PHP License
48 * @version Release: 0.7.0
49 * @link http://pear.php.net/package/Text_Highlighter
52 class Text_Highlighter_Renderer_Console extends Text_Highlighter_Renderer
60 * class of last outputted text chunk
71 var $_numbers = false;
89 var $_colors = array();
91 var $_defColors = array(
92 'default' => "\033[0m",
93 'inlinetags' => "\033[31m",
94 'brackets' => "\033[36m",
95 'quotes' => "\033[34m",
96 'inlinedoc' => "\033[34m",
98 'types' => "\033[32m",
99 'number' => "\033[32m",
100 'string' => "\033[31m",
101 'reserved' => "\033[35m",
102 'comment' => "\033[33m",
103 'mlcomment' => "\033[33m",
106 function preprocess($str)
108 // normalize whitespace and tabs
109 $str = str_replace("\r\n","\n", $str);
110 $str = str_replace("\t",str_repeat(' ', $this->_tabsize), $str);
116 * Resets renderer state
121 * Descendents of Text_Highlighter call this method from the constructor,
122 * passing $options they get as parameter.
126 $this->_lastClass = '';
127 if (isset($this->_options['numbers'])) {
128 $this->_numbers = (bool)$this->_options['numbers'];
130 $this->_numbers = false;
132 if (isset($this->_options['tabsize'])) {
133 $this->_tabsize = $this->_options['tabsize'];
137 if (isset($this->_options['colors'])) {
138 $this->_colors = array_merge($this->_defColors, $this->_options['colors']);
140 $this->_colors = $this->_defColors;
152 * @param string $class Token class
153 * @param string $content Token content
155 function acceptToken($class, $content)
157 if (isset($this->_colors[$class])) {
158 $color = $this->_colors[$class];
160 $color = $this->_colors['default'];
162 if ($this->_lastClass != $class) {
163 $this->_output .= $color;
165 $content = str_replace("\n", $this->_colors['default'] . "\n" . $color, $content);
166 $content .= $this->_colors['default'];
167 $this->_output .= $content;
171 * Signals that no more tokens are available
178 if ($this->_numbers) {
179 $nlines = substr_count($this->_output, "\n") + 1;
180 $len = strlen($nlines);
182 $this->_output = preg_replace('~^~em', '" " . str_pad($i++, $len, " ", STR_PAD_LEFT) . ": "', $this->_output);
184 $this->_output .= HL_CONSOLE_DEFCOLOR . "\n";
188 * Get generated output
190 * @return string Highlighted code
196 return $this->_output;
204 * c-hanging-comment-ender-p: nil