BB.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. /**
  4. * BB code renderer.
  5. *
  6. * This BB renderer produces BB code, ready to be pasted in bulletin boards and
  7. * other applications that accept BB code. Based on the HTML renderer by Andrey Demenev.
  8. *
  9. * LICENSE: This source file is subject to version 3.0 of the PHP license
  10. * that is available through the world-wide-web at the following URI:
  11. * http://www.php.net/license/3_0.txt. If you did not receive a copy of
  12. * the PHP License and are unable to obtain it through the web, please
  13. * send a note to license@php.net so we can mail you a copy immediately.
  14. *
  15. * @category Text
  16. * @package Text_Highlighter
  17. * @author Stoyan Stefanov <ssttoo@gmail.com>
  18. * @copyright 2005 Stoyan Stefanov
  19. * @license http://www.php.net/license/3_0.txt PHP License
  20. * @version CVS: $Id: BB.php,v 1.1 2007/06/03 02:37:08 ssttoo Exp $
  21. * @link http://pear.php.net/package/Text_Highlighter
  22. */
  23. /**
  24. * @ignore
  25. */
  26. require_once dirname(__FILE__).'/../Renderer.php';
  27. /**
  28. * BB code renderer, based on Andrey Demenev's HTML renderer.
  29. *
  30. * Elements of $options argument of constructor (each being optional):
  31. *
  32. * - 'numbers' - Line numbering TRUE or FALSE
  33. * - 'tabsize' - Tab size, default is 4
  34. * - 'bb_tags' - An array containing three BB tags, see below
  35. * - 'tag_brackets' - An array that conains opening and closing tags, [ and ]
  36. * - 'colors' - An array with all the colors to be used for highlighting
  37. *
  38. * The default BB tags are:
  39. * - 'color' => 'color'
  40. * - 'list' => 'list'
  41. * - 'list_item' => '*'
  42. *
  43. * The default colors for the highlighter are:
  44. * - 'default' => 'Black',
  45. * - 'code' => 'Gray',
  46. * - 'brackets' => 'Olive',
  47. * - 'comment' => 'Orange',
  48. * - 'mlcomment' => 'Orange',
  49. * - 'quotes' => 'Darkred',
  50. * - 'string' => 'Red',
  51. * - 'identifier' => 'Blue',
  52. * - 'builtin' => 'Teal',
  53. * - 'reserved' => 'Green',
  54. * - 'inlinedoc' => 'Blue',
  55. * - 'var' => 'Darkblue',
  56. * - 'url' => 'Blue',
  57. * - 'special' => 'Navy',
  58. * - 'number' => 'Maroon',
  59. * - 'inlinetags' => 'Blue',
  60. *
  61. *
  62. * @author Stoyan Stefanov <ssttoo@gmail.com>
  63. * @category Text
  64. * @package Text_Highlighter
  65. * @copyright 20045 Stoyan Stefanov
  66. * @license http://www.php.net/license/3_0.txt PHP License
  67. * @version Release: 0.5.0
  68. * @link http://pear.php.net/package/Text_Highlighter
  69. */
  70. class Text_Highlighter_Renderer_BB extends Text_Highlighter_Renderer_Array
  71. {
  72. /**#@+
  73. * @access private
  74. */
  75. /**
  76. * Line numbering - will use the specified BB tag for listings
  77. *
  78. * @var boolean
  79. */
  80. var $_numbers = false;
  81. /**
  82. * BB tags to be used
  83. *
  84. * @var array
  85. */
  86. var $_bb_tags = array (
  87. 'color' => 'color',
  88. 'list' => 'list',
  89. 'list_item' => '*',
  90. 'code' => 'code',
  91. );
  92. /**
  93. * BB brackets - [ and ]
  94. *
  95. * @var array
  96. */
  97. var $_tag_brackets = array ('start' => '[', 'end' => ']');
  98. /**
  99. * Colors map
  100. *
  101. * @var boolean
  102. */
  103. var $_colors = array(
  104. 'default' => 'Black',
  105. 'code' => 'Gray',
  106. 'brackets' => 'Olive',
  107. 'comment' => 'Orange',
  108. 'mlcomment' => 'Orange',
  109. 'quotes' => 'Darkred',
  110. 'string' => 'Red',
  111. 'identifier' => 'Blue',
  112. 'builtin' => 'Teal',
  113. 'reserved' => 'Green',
  114. 'inlinedoc' => 'Blue',
  115. 'var' => 'Darkblue',
  116. 'url' => 'Blue',
  117. 'special' => 'Navy',
  118. 'number' => 'Maroon',
  119. 'inlinetags' => 'Blue',
  120. );
  121. /**#@-*/
  122. /**
  123. * Resets renderer state
  124. *
  125. * @access protected
  126. *
  127. *
  128. * Descendents of Text_Highlighter call this method from the constructor,
  129. * passing $options they get as parameter.
  130. */
  131. function reset()
  132. {
  133. parent::reset();
  134. if (isset($this->_options['numbers'])) {
  135. $this->_numbers = $this->_options['numbers'];
  136. }
  137. if (isset($this->_options['bb_tags'])) {
  138. $this->_bb_tags = array_merge($this->_bb_tags, $this->_options['bb_tags']);
  139. }
  140. if (isset($this->_options['tag_brackets'])) {
  141. $this->_tag_brackets = array_merge($this->_tag_brackets, $this->_options['tag_brackets']);
  142. }
  143. if (isset($this->_options['colors'])) {
  144. $this->_colors = array_merge($this->_colors, $this->_options['colors']);
  145. }
  146. }
  147. /**
  148. * Signals that no more tokens are available
  149. *
  150. * @abstract
  151. * @access public
  152. *
  153. */
  154. function finalize()
  155. {
  156. // get parent's output
  157. parent::finalize();
  158. $output = parent::getOutput();
  159. $bb_output = '';
  160. $color_start = $this->_tag_brackets['start'] . $this->_bb_tags['color'] . '=%s' . $this->_tag_brackets['end'];
  161. $color_end = $this->_tag_brackets['start'] . '/' . $this->_bb_tags['color'] . $this->_tag_brackets['end'];
  162. // loop through each class=>content pair
  163. foreach ($output AS $token) {
  164. if ($this->_enumerated) {
  165. $class = $token[0];
  166. $content = $token[1];
  167. } else {
  168. $key = key($token);
  169. $class = $key;
  170. $content = $token[$key];
  171. }
  172. $iswhitespace = ctype_space($content);
  173. if (!$iswhitespace && !empty($this->_colors[$class])) {
  174. $bb_output .= sprintf($color_start, $this->_colors[$class]);
  175. $bb_output .= $content;
  176. $bb_output .= $color_end;
  177. } else {
  178. $bb_output .= $content;
  179. }
  180. }
  181. if ($this->_numbers) {
  182. $item_tag = $this->_tag_brackets['start'] .
  183. $this->_bb_tags['list_item'] .
  184. $this->_tag_brackets['end'];
  185. $this->_output = $item_tag . str_replace("\n", "\n". $item_tag .' ', $bb_output);
  186. $this->_output = $this->_tag_brackets['start'] .
  187. $this->_bb_tags['list'] .
  188. $this->_tag_brackets['end'] .
  189. $this->_output .
  190. $this->_tag_brackets['start'] .
  191. '/'.
  192. $this->_bb_tags['list'] .
  193. $this->_tag_brackets['end']
  194. ;
  195. } else {
  196. $this->_output = $this->_tag_brackets['start'] .
  197. $this->_bb_tags['code'] .
  198. $this->_tag_brackets['end'] .
  199. $bb_output .
  200. $this->_tag_brackets['start'] .
  201. '/' .
  202. $this->_bb_tags['code'] .
  203. $this->_tag_brackets['end'];
  204. }
  205. }
  206. }
  207. /*
  208. * Local variables:
  209. * tab-width: 4
  210. * c-basic-offset: 4
  211. * c-hanging-comment-ender-p: nil
  212. * End:
  213. */
  214. ?>