1: <?php
2: /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
3: /**
4: * Console renderer
5: *
6: * PHP versions 4 and 5
7: *
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.
13: *
14: * @category Text
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
21: */
22:
23: /**
24: * @ignore
25: */
26:
27: require_once dirname(__FILE__).'/../Renderer.php';
28:
29: define ('HL_CONSOLE_DEFCOLOR', "\033[0m");
30:
31: /**
32: * Console renderer
33: *
34: * Suitable for displaying text on color-capable terminals, directly
35: * or trough less -r
36: *
37: * Elements of $options argument of constructor (each being optional):
38: *
39: * - 'numbers' - whether to add line numbers
40: * - 'tabsize' - Tab size
41: * - 'colors' - additional colors
42: *
43: * @author Andrey Demenev <demenev@gmail.com>
44: * @category Text
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.1
49: * @link http://pear.php.net/package/Text_Highlighter
50: */
51:
52: class Text_Highlighter_Renderer_Console extends Text_Highlighter_Renderer
53: {
54:
55: /**#@+
56: * @access private
57: */
58:
59: /**
60: * class of last outputted text chunk
61: *
62: * @var string
63: */
64: var $_lastClass;
65:
66: /**
67: * Line numbering
68: *
69: * @var boolean
70: */
71: var $_numbers = false;
72:
73: /**
74: * Tab size
75: *
76: * @var integer
77: */
78: var $_tabsize = 4;
79:
80: /**
81: * Highlighted code
82: *
83: * @var string
84: */
85: var $_output = '';
86:
87: /**#@-*/
88:
89: var $_colors = array();
90:
91: var $_defColors = array(
92: 'default' => "\033[0m",
93: 'inlinetags' => "\033[31m",
94: 'brackets' => "\033[36m",
95: 'quotes' => "\033[34m",
96: 'inlinedoc' => "\033[34m",
97: 'var' => "\033[1m",
98: 'types' => "\033[32m",
99: 'number' => "\033[32m",
100: 'string' => "\033[31m",
101: 'reserved' => "\033[35m",
102: 'comment' => "\033[33m",
103: 'mlcomment' => "\033[33m",
104: );
105:
106: function preprocess($str)
107: {
108: // normalize whitespace and tabs
109: $str = str_replace("\r\n","\n", $str);
110: $str = str_replace("\t",str_repeat(' ', $this->_tabsize), $str);
111: return rtrim($str);
112: }
113:
114:
115: /**
116: * Resets renderer state
117: *
118: * @access protected
119: *
120: *
121: * Descendents of Text_Highlighter call this method from the constructor,
122: * passing $options they get as parameter.
123: */
124: function reset()
125: {
126: $this->_lastClass = '';
127: if (isset($this->_options['numbers'])) {
128: $this->_numbers = (bool)$this->_options['numbers'];
129: } else {
130: $this->_numbers = false;
131: }
132: if (isset($this->_options['tabsize'])) {
133: $this->_tabsize = $this->_options['tabsize'];
134: } else {
135: $this->_tabsize = 4;
136: }
137: if (isset($this->_options['colors'])) {
138: $this->_colors = array_merge($this->_defColors, $this->_options['colors']);
139: } else {
140: $this->_colors = $this->_defColors;
141: }
142: $this->_output = '';
143: }
144:
145:
146:
147: /**
148: * Accepts next token
149: *
150: * @access public
151: *
152: * @param string $class Token class
153: * @param string $content Token content
154: */
155: function acceptToken($class, $content)
156: {
157: if (isset($this->_colors[$class])) {
158: $color = $this->_colors[$class];
159: } else {
160: $color = $this->_colors['default'];
161: }
162: if ($this->_lastClass != $class) {
163: $this->_output .= $color;
164: }
165: $content = str_replace("\n", $this->_colors['default'] . "\n" . $color, $content);
166: $content .= $this->_colors['default'];
167: $this->_output .= $content;
168: }
169:
170: /**
171: * Signals that no more tokens are available
172: *
173: * @access public
174: *
175: */
176: function finalize()
177: {
178: if ($this->_numbers) {
179: $nlines = substr_count($this->_output, "\n") + 1;
180: $len = strlen($nlines);
181: $i = 1;
182: $this->_output = preg_replace('~^~em', '" " . str_pad($i++, $len, " ", STR_PAD_LEFT) . ": "', $this->_output);
183: }
184: $this->_output .= HL_CONSOLE_DEFCOLOR . "\n";
185: }
186:
187: /**
188: * Get generated output
189: *
190: * @return string Highlighted code
191: * @access public
192: *
193: */
194: function getOutput()
195: {
196: return $this->_output;
197: }
198: }
199:
200: /*
201: * Local variables:
202: * tab-width: 4
203: * c-basic-offset: 4
204: * c-hanging-comment-ender-p: nil
205: * End:
206: */
207:
208: ?>
209: