1: <?php
2: /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
3: /**
4: * HTML renderer that uses only basic html tags
5: *
6: * PHP versions 4 and 5. Based on the "normal" HTML renderer by Andrey Demenev.
7: * It's designed to work with user agents that support only a limited number of
8: * HTML tags. Like the iPod which supports only b, i, u and a.
9: *
10: * LICENSE: This source file is subject to version 3.0 of the PHP license
11: * that is available through the world-wide-web at the following URI:
12: * http://www.php.net/license/3_0.txt. If you did not receive a copy of
13: * the PHP License and are unable to obtain it through the web, please
14: * send a note to license@php.net so we can mail you a copy immediately.
15: *
16: * @category Text
17: * @package Text_Highlighter
18: * @author Stoyan Stefanov <ssttoo@gmail.com>
19: * @copyright 2005 Stoyan Stefanov
20: * @license http://www.php.net/license/3_0.txt PHP License
21: * @version CVS: $Id: HtmlTags.php,v 1.1 2007/06/03 02:37:09 ssttoo Exp $
22: * @link http://pear.php.net/package/Text_Highlighter
23: */
24:
25: /**
26: * @ignore
27: */
28:
29: require_once dirname(__FILE__).'/../Renderer.php';
30: require_once dirname(__FILE__).'/../Renderer/Array.php';
31:
32: /**
33: * HTML basic tags renderer, based on Andrey Demenev's HTML renderer.
34: *
35: * Elements of $options argument of constructor (each being optional):
36: *
37: * - 'numbers' - Line numbering TRUE or FALSE. Default is FALSE.
38: * - 'tabsize' - Tab size, default is 4.
39: * - 'tags' - Array, containing the tags to be used for highlighting
40: *
41: * Here's the listing of the default tags:
42: * - 'default' => '',
43: * - 'code' => '',
44: * - 'brackets' => 'b',
45: * - 'comment' => 'i',
46: * - 'mlcomment' => 'i',
47: * - 'quotes' => '',
48: * - 'string' => 'i',
49: * - 'identifier' => 'b',
50: * - 'builtin' => 'b',
51: * - 'reserved' => 'u',
52: * - 'inlinedoc' => 'i',
53: * - 'var' => 'b',
54: * - 'url' => 'i',
55: * - 'special' => '',
56: * - 'number' => '',
57: * - 'inlinetags' => ''
58: *
59: * @author Stoyan Stefanov <ssttoo@gmail.com>
60: * @category Text
61: * @package Text_Highlighter
62: * @copyright 2005 Stoyan Stefanov
63: * @license http://www.php.net/license/3_0.txt PHP License
64: * @version Release: 0.5.0
65: * @link http://pear.php.net/package/Text_Highlighter
66: */
67:
68: class Text_Highlighter_Renderer_HtmlTags extends Text_Highlighter_Renderer_Array
69: {
70:
71: /**#@+
72: * @access private
73: */
74:
75: /**
76: * Line numbering - will use 'ol' tag
77: *
78: * @var boolean
79: */
80: var $_numbers = false;
81:
82: /**
83: * HTML tags map
84: *
85: * @var array
86: */
87: var $_hilite_tags = array(
88: 'default' => '',
89: 'code' => '',
90: 'brackets' => 'b',
91: 'comment' => 'i',
92: 'mlcomment' => 'i',
93: 'quotes' => '',
94: 'string' => 'i',
95: 'identifier' => 'b',
96: 'builtin' => 'b',
97: 'reserved' => 'u',
98: 'inlinedoc' => 'i',
99: 'var' => 'b',
100: 'url' => 'i',
101: 'special' => '',
102: 'number' => '',
103: 'inlinetags' => '',
104: );
105:
106: /**#@-*/
107:
108: /**
109: * Resets renderer state
110: *
111: * @access protected
112: *
113: *
114: * Descendents of Text_Highlighter call this method from the constructor,
115: * passing $options they get as parameter.
116: */
117: function reset()
118: {
119: parent::reset();
120: if (isset($this->_options['numbers'])) {
121: $this->_numbers = $this->_options['numbers'];
122: }
123: if (isset($this->_options['tags'])) {
124: $this->_hilite_tags = array_merge($this->_tags, $this->_options['tags']);
125: }
126: }
127:
128:
129: /**
130: * Signals that no more tokens are available
131: *
132: * @abstract
133: * @access public
134: *
135: */
136: function finalize()
137: {
138:
139: // get parent's output
140: parent::finalize();
141: $output = parent::getOutput();
142:
143: $html_output = '';
144:
145: // loop through each class=>content pair
146: foreach ($output AS $token) {
147:
148: if ($this->_enumerated) {
149: $class = $token[0];
150: $content = $token[1];
151: } else {
152: $key = key($token);
153: $class = $key;
154: $content = $token[$key];
155: }
156:
157: $iswhitespace = ctype_space($content);
158: if (!$iswhitespace && !empty($this->_hilite_tags[$class])) {
159: $html_output .= '<'. $this->_hilite_tags[$class] . '>' . $content . '</'. $this->_hilite_tags[$class] . '>';
160: } else {
161: $html_output .= $content;
162: }
163: }
164:
165:
166: if ($this->_numbers) {
167: /* additional whitespace for browsers that do not display
168: empty list items correctly */
169: $html_output = '<li> ' . str_replace("\n", "</li>\n<li> ", $html_output) . '</li>';
170: $this->_output = '<ol>' . str_replace(' ', ' ', $html_output) . '</ol>';
171: } else {
172: $this->_output = '<pre>' . $html_output . '</pre>';
173: }
174: }
175:
176:
177: }
178:
179: /*
180: * Local variables:
181: * tab-width: 4
182: * c-basic-offset: 4
183: * c-hanging-comment-ender-p: nil
184: * End:
185: */
186:
187: ?>