1: <?php
2: /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
3: /**
4: * Array renderer.
5: *
6: * Produces an array that contains class names and content pairs.
7: * The array can be enumerated or associative. Associative means
8: * <code>class => content</code> pairs.
9: * Based on the HTML renderer by Andrey Demenev.
10: *
11: * LICENSE: This source file is subject to version 3.0 of the PHP license
12: * that is available through the world-wide-web at the following URI:
13: * http://www.php.net/license/3_0.txt. If you did not receive a copy of
14: * the PHP License and are unable to obtain it through the web, please
15: * send a note to license@php.net so we can mail you a copy immediately.
16: *
17: * @category Text
18: * @package Text_Highlighter
19: * @author Stoyan Stefanov <ssttoo@gmail.com>
20: * @copyright 2006 Stoyan Stefanov
21: * @license http://www.php.net/license/3_0.txt PHP License
22: * @version CVS: $Id: Array.php,v 1.1 2007/06/03 02:37:08 ssttoo Exp $
23: * @link http://pear.php.net/package/Text_Highlighter
24: */
25:
26: /**
27: * @ignore
28: */
29:
30: require_once dirname(__FILE__).'/../Renderer.php';
31:
32: /**
33: * Array renderer, based on Andrey Demenev's HTML renderer.
34: *
35: * In addition to the options supported by the HTML renderer,
36: * the following options were also introduced:
37: * <ul><li>htmlspecialchars - whether or not htmlspecialchars() will
38: * be called on the content, default TRUE</li>
39: * <li>enumerated - type of array produced, default FALSE,
40: * meaning associative array</li>
41: * </ul>
42: *
43: *
44: * @author Stoyan Stefanov <ssttoo@gmail.com>
45: * @category Text
46: * @package Text_Highlighter
47: * @copyright 2006 Stoyan Stefanov
48: * @license http://www.php.net/license/3_0.txt PHP License
49: * @version Release: 0.5.0
50: * @link http://pear.php.net/package/Text_Highlighter
51: */
52:
53: class Text_Highlighter_Renderer_Array extends Text_Highlighter_Renderer
54: {
55:
56: /**#@+
57: * @access private
58: */
59:
60: /**
61: * Tab size
62: *
63: * @var integer
64: */
65: var $_tabsize = 4;
66:
67: /**
68: * Should htmlentities() will be called
69: *
70: * @var boolean
71: */
72: var $_htmlspecialchars = true;
73:
74: /**
75: * Enumerated or associative array
76: *
77: * @var integer
78: */
79: var $_enumerated = false;
80:
81: /**
82: * Array containing highlighting rules
83: *
84: * @var array
85: */
86: var $_output = array();
87:
88: /**#@-*/
89:
90: /**
91: * Preprocesses code
92: *
93: * @access public
94: *
95: * @param string $str Code to preprocess
96: * @return string Preprocessed code
97: */
98: function preprocess($str)
99: {
100: // normalize whitespace and tabs
101: $str = str_replace("\r\n","\n", $str);
102: // some browsers refuse to display empty lines
103: $str = preg_replace('~^$~m'," ", $str);
104: $str = str_replace("\t",str_repeat(' ', $this->_tabsize), $str);
105: return rtrim($str);
106: }
107:
108:
109: /**
110: * Resets renderer state
111: *
112: * Descendents of Text_Highlighter call this method from the constructor,
113: * passing $options they get as parameter.
114: *
115: * @access protected
116: */
117: function reset()
118: {
119: $this->_output = array();
120: $this->_lastClass = 'default';
121: if (isset($this->_options['tabsize'])) {
122: $this->_tabsize = $this->_options['tabsize'];
123: }
124: if (isset($this->_options['htmlspecialchars'])) {
125: $this->_htmlspecialchars = $this->_options['htmlspecialchars'];
126: }
127: if (isset($this->_options['enumerated'])) {
128: $this->_enumerated = $this->_options['enumerated'];
129: }
130: }
131:
132:
133:
134: /**
135: * Accepts next token
136: *
137: * @abstract
138: * @access public
139: * @param string $class Token class
140: * @param string $content Token content
141: */
142: function acceptToken($class, $content)
143: {
144:
145:
146: $theClass = $this->_getFullClassName($class);
147: if ($this->_htmlspecialchars) {
148: $content = htmlspecialchars($content);
149: }
150: if ($this->_enumerated) {
151: $this->_output[] = array($class, $content);
152: } else {
153: $this->_output[][$class] = $content;
154: }
155: $this->_lastClass = $class;
156:
157: }
158:
159:
160: /**
161: * Given a CSS class name, returns the class name
162: * with language name prepended, if necessary
163: *
164: * @access private
165: *
166: * @param string $class Token class
167: */
168: function _getFullClassName($class)
169: {
170: if (!empty($this->_options['use_language'])) {
171: $theClass = $this->_language . '-' . $class;
172: } else {
173: $theClass = $class;
174: }
175: return $theClass;
176: }
177:
178: /**
179: * Get generated output
180: *
181: * @abstract
182: * @return array Highlighted code as an array
183: * @access public
184: */
185: function getOutput()
186: {
187: return $this->_output;
188: }
189: }
190:
191: /*
192: * Local variables:
193: * tab-width: 4
194: * c-basic-offset: 4
195: * c-hanging-comment-ender-p: nil
196: * End:
197: */
198:
199: ?>