Array.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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: 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. * @ignore
  27. */
  28. require_once dirname(__FILE__).'/../Renderer.php';
  29. /**
  30. * Array renderer, based on Andrey Demenev's HTML renderer.
  31. *
  32. * In addition to the options supported by the HTML renderer,
  33. * the following options were also introduced:
  34. * <ul><li>htmlspecialchars - whether or not htmlspecialchars() will
  35. * be called on the content, default TRUE</li>
  36. * <li>enumerated - type of array produced, default FALSE,
  37. * meaning associative array</li>
  38. * </ul>
  39. *
  40. *
  41. * @author Stoyan Stefanov <ssttoo@gmail.com>
  42. * @category Text
  43. * @package Text_Highlighter
  44. * @copyright 2006 Stoyan Stefanov
  45. * @license http://www.php.net/license/3_0.txt PHP License
  46. * @version Release: 0.5.0
  47. * @link http://pear.php.net/package/Text_Highlighter
  48. */
  49. class Text_Highlighter_Renderer_Array extends Text_Highlighter_Renderer
  50. {
  51. /**#@+
  52. * @access private
  53. */
  54. /**
  55. * Tab size
  56. *
  57. * @var integer
  58. */
  59. var $_tabsize = 4;
  60. /**
  61. * Should htmlentities() will be called
  62. *
  63. * @var boolean
  64. */
  65. var $_htmlspecialchars = true;
  66. /**
  67. * Enumerated or associative array
  68. *
  69. * @var integer
  70. */
  71. var $_enumerated = false;
  72. /**
  73. * Array containing highlighting rules
  74. *
  75. * @var array
  76. */
  77. var $_output = array();
  78. /**#@-*/
  79. /**
  80. * Preprocesses code
  81. *
  82. * @access public
  83. *
  84. * @param string $str Code to preprocess
  85. * @return string Preprocessed code
  86. */
  87. function preprocess($str)
  88. {
  89. // normalize whitespace and tabs
  90. $str = str_replace("\r\n","\n", $str);
  91. // some browsers refuse to display empty lines
  92. $str = preg_replace('~^$~m'," ", $str);
  93. $str = str_replace("\t",str_repeat(' ', $this->_tabsize), $str);
  94. return rtrim($str);
  95. }
  96. /**
  97. * Resets renderer state
  98. *
  99. * Descendents of Text_Highlighter call this method from the constructor,
  100. * passing $options they get as parameter.
  101. *
  102. * @access protected
  103. */
  104. function reset()
  105. {
  106. $this->_output = array();
  107. $this->_lastClass = 'default';
  108. if (isset($this->_options['tabsize'])) {
  109. $this->_tabsize = $this->_options['tabsize'];
  110. }
  111. if (isset($this->_options['htmlspecialchars'])) {
  112. $this->_htmlspecialchars = $this->_options['htmlspecialchars'];
  113. }
  114. if (isset($this->_options['enumerated'])) {
  115. $this->_enumerated = $this->_options['enumerated'];
  116. }
  117. }
  118. /**
  119. * Accepts next token
  120. *
  121. * @abstract
  122. * @access public
  123. * @param string $class Token class
  124. * @param string $content Token content
  125. */
  126. function acceptToken($class, $content)
  127. {
  128. $theClass = $this->_getFullClassName($class);
  129. if ($this->_htmlspecialchars) {
  130. $content = htmlspecialchars($content);
  131. }
  132. if ($this->_enumerated) {
  133. $this->_output[] = array($class, $content);
  134. } else {
  135. $this->_output[][$class] = $content;
  136. }
  137. $this->_lastClass = $class;
  138. }
  139. /**
  140. * Given a CSS class name, returns the class name
  141. * with language name prepended, if necessary
  142. *
  143. * @access private
  144. *
  145. * @param string $class Token class
  146. */
  147. function _getFullClassName($class)
  148. {
  149. if (!empty($this->_options['use_language'])) {
  150. $theClass = $this->_language . '-' . $class;
  151. } else {
  152. $theClass = $class;
  153. }
  154. return $theClass;
  155. }
  156. /**
  157. * Get generated output
  158. *
  159. * @abstract
  160. * @return array Highlighted code as an array
  161. * @access public
  162. */
  163. function getOutput()
  164. {
  165. return $this->_output;
  166. }
  167. }
  168. /*
  169. * Local variables:
  170. * tab-width: 4
  171. * c-basic-offset: 4
  172. * c-hanging-comment-ender-p: nil
  173. * End:
  174. */
  175. ?>