Console.php 4.9 KB

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