]> git.mxchange.org Git - friendica.git/blob - vendor/pear-pear.php.net/Text_Highlighter/Text/Highlighter/Renderer/Array.php
Update composer.json
[friendica.git] / vendor / pear-pear.php.net / Text_Highlighter / Text / Highlighter / Renderer / Array.php
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 =&gt; 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$
23  * @link       http://pear.php.net/package/Text_Highlighter
24  */
25
26 /**
27  * @ignore
28  */
29
30 require_once 'Text/Highlighter/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         $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);
106         return rtrim($str);
107     }
108
109
110     /**
111      * Resets renderer state
112      *
113      * Descendents of Text_Highlighter call this method from the constructor,
114      * passing $options they get as parameter.
115      *
116      * @access protected
117      */
118     function reset()
119     {
120         $this->_output = array();
121         $this->_lastClass = 'default';
122         if (isset($this->_options['tabsize'])) {
123             $this->_tabsize = $this->_options['tabsize'];
124         }
125         if (isset($this->_options['htmlspecialchars'])) {
126             $this->_htmlspecialchars = $this->_options['htmlspecialchars'];
127         }
128         if (isset($this->_options['enumerated'])) {
129             $this->_enumerated = $this->_options['enumerated'];
130         }
131     }
132
133
134
135     /**
136      * Accepts next token
137      *
138      * @abstract
139      * @access public
140      * @param  string $class   Token class
141      * @param  string $content Token content
142      */
143     function acceptToken($class, $content)
144     {
145
146
147         $theClass = $this->_getFullClassName($class);
148         if ($this->_htmlspecialchars) {
149             $content = htmlspecialchars($content);
150         }
151         if ($this->_enumerated) {
152             $this->_output[] = array($class, $content);
153         } else {
154             $this->_output[][$class] = $content;
155         }
156         $this->_lastClass = $class;
157
158     }
159
160
161     /**
162      * Given a CSS class name, returns the class name
163      * with language name prepended, if necessary
164      *
165      * @access private
166      *
167      * @param  string $class   Token class
168      */
169     function _getFullClassName($class)
170     {
171         if (!empty($this->_options['use_language'])) {
172             $theClass = $this->_language . '-' . $class;
173         } else {
174             $theClass = $class;
175         }
176         return $theClass;
177     }
178
179     /**
180      * Get generated output
181      *
182      * @abstract
183      * @return array Highlighted code as an array
184      * @access public
185      */
186     function getOutput()
187     {
188         return $this->_output;
189     }
190 }
191
192 /*
193  * Local variables:
194  * tab-width: 4
195  * c-basic-offset: 4
196  * c-hanging-comment-ender-p: nil
197  * End:
198  */
199
200 ?>