2 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
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.
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.
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
23 * @link http://pear.php.net/package/Text_Highlighter
30 require_once 'Text/Highlighter/Renderer.php';
33 * Array renderer, based on Andrey Demenev's HTML renderer.
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>
44 * @author Stoyan Stefanov <ssttoo@gmail.com>
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
53 class Text_Highlighter_Renderer_Array extends Text_Highlighter_Renderer
68 * Should htmlentities() will be called
72 var $_htmlspecialchars = true;
75 * Enumerated or associative array
79 var $_enumerated = false;
82 * Array containing highlighting rules
86 var $_output = array();
95 * @param string $str Code to preprocess
96 * @return string Preprocessed code
98 function preprocess($str)
100 // normalize whitespace and tabs
101 $str = str_replace("\r\n","\n", $str);
102 $str = str_replace("\r","\n", $str);
103 // some browsers refuse to display empty lines
104 $str = preg_replace('~^$~m'," ", $str);
105 $str = str_replace("\t",str_repeat(' ', $this->_tabsize), $str);
111 * Resets renderer state
113 * Descendents of Text_Highlighter call this method from the constructor,
114 * passing $options they get as parameter.
120 $this->_output = array();
121 $this->_lastClass = 'default';
122 if (isset($this->_options['tabsize'])) {
123 $this->_tabsize = $this->_options['tabsize'];
125 if (isset($this->_options['htmlspecialchars'])) {
126 $this->_htmlspecialchars = $this->_options['htmlspecialchars'];
128 if (isset($this->_options['enumerated'])) {
129 $this->_enumerated = $this->_options['enumerated'];
140 * @param string $class Token class
141 * @param string $content Token content
143 function acceptToken($class, $content)
147 $theClass = $this->_getFullClassName($class);
148 if ($this->_htmlspecialchars) {
149 $content = htmlspecialchars($content);
151 if ($this->_enumerated) {
152 $this->_output[] = array($class, $content);
154 $this->_output[][$class] = $content;
156 $this->_lastClass = $class;
162 * Given a CSS class name, returns the class name
163 * with language name prepended, if necessary
167 * @param string $class Token class
169 function _getFullClassName($class)
171 if (!empty($this->_options['use_language'])) {
172 $theClass = $this->_language . '-' . $class;
180 * Get generated output
183 * @return array Highlighted code as an array
188 return $this->_output;
196 * c-hanging-comment-ender-p: nil