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: /**
25: * @ignore
26: */
27:
28: require_once dirname(__FILE__).'/../Renderer.php';
29:
30: /**
31: * BB code renderer, based on Andrey Demenev's HTML renderer.
32: *
33: * Elements of $options argument of constructor (each being optional):
34: *
35: * - 'numbers' - Line numbering TRUE or FALSE
36: * - 'tabsize' - Tab size, default is 4
37: * - 'bb_tags' - An array containing three BB tags, see below
38: * - 'tag_brackets' - An array that conains opening and closing tags, [ and ]
39: * - 'colors' - An array with all the colors to be used for highlighting
40: *
41: * The default BB tags are:
42: * - 'color' => 'color'
43: * - 'list' => 'list'
44: * - 'list_item' => '*'
45: *
46: * The default colors for the highlighter are:
47: * - 'default' => 'Black',
48: * - 'code' => 'Gray',
49: * - 'brackets' => 'Olive',
50: * - 'comment' => 'Orange',
51: * - 'mlcomment' => 'Orange',
52: * - 'quotes' => 'Darkred',
53: * - 'string' => 'Red',
54: * - 'identifier' => 'Blue',
55: * - 'builtin' => 'Teal',
56: * - 'reserved' => 'Green',
57: * - 'inlinedoc' => 'Blue',
58: * - 'var' => 'Darkblue',
59: * - 'url' => 'Blue',
60: * - 'special' => 'Navy',
61: * - 'number' => 'Maroon',
62: * - 'inlinetags' => 'Blue',
63: *
64: *
65: * @author Stoyan Stefanov <ssttoo@gmail.com>
66: * @category Text
67: * @package Text_Highlighter
68: * @copyright 20045 Stoyan Stefanov
69: * @license http://www.php.net/license/3_0.txt PHP License
70: * @version Release: 0.5.0
71: * @link http://pear.php.net/package/Text_Highlighter
72: */
73:
74: class Text_Highlighter_Renderer_BB extends Text_Highlighter_Renderer_Array
75: {
76:
77: /**#@+
78: * @access private
79: */
80:
81: /**
82: * Line numbering - will use the specified BB tag for listings
83: *
84: * @var boolean
85: */
86: var $_numbers = false;
87:
88: /**
89: * BB tags to be used
90: *
91: * @var array
92: */
93: var $_bb_tags = array (
94: 'color' => 'color',
95: 'list' => 'list',
96: 'list_item' => '*',
97: 'code' => 'code',
98: );
99:
100: /**
101: * BB brackets - [ and ]
102: *
103: * @var array
104: */
105: var $_tag_brackets = array ('start' => '[', 'end' => ']');
106:
107: /**
108: * Colors map
109: *
110: * @var boolean
111: */
112: var $_colors = array(
113: 'default' => 'Black',
114: 'code' => 'Gray',
115: 'brackets' => 'Olive',
116: 'comment' => 'Orange',
117: 'mlcomment' => 'Orange',
118: 'quotes' => 'Darkred',
119: 'string' => 'Red',
120: 'identifier' => 'Blue',
121: 'builtin' => 'Teal',
122: 'reserved' => 'Green',
123: 'inlinedoc' => 'Blue',
124: 'var' => 'Darkblue',
125: 'url' => 'Blue',
126: 'special' => 'Navy',
127: 'number' => 'Maroon',
128: 'inlinetags' => 'Blue',
129: );
130:
131: /**#@-*/
132:
133: /**
134: * Resets renderer state
135: *
136: * @access protected
137: *
138: *
139: * Descendents of Text_Highlighter call this method from the constructor,
140: * passing $options they get as parameter.
141: */
142: function reset()
143: {
144: parent::reset();
145: if (isset($this->_options['numbers'])) {
146: $this->_numbers = $this->_options['numbers'];
147: }
148: if (isset($this->_options['bb_tags'])) {
149: $this->_bb_tags = array_merge($this->_bb_tags, $this->_options['bb_tags']);
150: }
151: if (isset($this->_options['tag_brackets'])) {
152: $this->_tag_brackets = array_merge($this->_tag_brackets, $this->_options['tag_brackets']);
153: }
154: if (isset($this->_options['colors'])) {
155: $this->_colors = array_merge($this->_colors, $this->_options['colors']);
156: }
157: }
158:
159:
160: /**
161: * Signals that no more tokens are available
162: *
163: * @abstract
164: * @access public
165: *
166: */
167: function finalize()
168: {
169:
170: // get parent's output
171: parent::finalize();
172: $output = parent::getOutput();
173:
174: $bb_output = '';
175:
176: $color_start = $this->_tag_brackets['start'] . $this->_bb_tags['color'] . '=%s' . $this->_tag_brackets['end'];
177: $color_end = $this->_tag_brackets['start'] . '/' . $this->_bb_tags['color'] . $this->_tag_brackets['end'];
178:
179: // loop through each class=>content pair
180: foreach ($output AS $token) {
181:
182: if ($this->_enumerated) {
183: $class = $token[0];
184: $content = $token[1];
185: } else {
186: $key = key($token);
187: $class = $key;
188: $content = $token[$key];
189: }
190:
191: $iswhitespace = ctype_space($content);
192: if (!$iswhitespace && !empty($this->_colors[$class])) {
193: $bb_output .= sprintf($color_start, $this->_colors[$class]);
194: $bb_output .= $content;
195: $bb_output .= $color_end;
196: } else {
197: $bb_output .= $content;
198: }
199: }
200:
201: if ($this->_numbers) {
202:
203: $item_tag = $this->_tag_brackets['start'] .
204: $this->_bb_tags['list_item'] .
205: $this->_tag_brackets['end'];
206: $this->_output = $item_tag . str_replace("\n", "\n". $item_tag .' ', $bb_output);
207: $this->_output = $this->_tag_brackets['start'] .
208: $this->_bb_tags['list'] .
209: $this->_tag_brackets['end'] .
210: $this->_output .
211: $this->_tag_brackets['start'] .
212: '/'.
213: $this->_bb_tags['list'] .
214: $this->_tag_brackets['end']
215: ;
216: } else {
217: $this->_output = $this->_tag_brackets['start'] .
218: $this->_bb_tags['code'] .
219: $this->_tag_brackets['end'] .
220: $bb_output .
221: $this->_tag_brackets['start'] .
222: '/' .
223: $this->_bb_tags['code'] .
224: $this->_tag_brackets['end'];
225: }
226: }
227:
228: }
229:
230: /*
231: * Local variables:
232: * tab-width: 4
233: * c-basic-offset: 4
234: * c-hanging-comment-ender-p: nil
235: * End:
236: */
237:
238: ?>