|
1 <?php |
|
2 // vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: |
|
3 /** |
|
4 * Base rendering class for parsed and tokenized text. |
|
5 * |
|
6 * PHP versions 4 and 5 |
|
7 * |
|
8 * @category Text |
|
9 * @package Text_Wiki |
|
10 * @author Paul M. Jones <pmjones@php.net> |
|
11 * @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1 |
|
12 * @version CVS: $Id: Render.php,v 1.9 2006/02/17 05:42:55 toggg Exp $ |
|
13 * @link http://pear.php.net/package/Text_Wiki |
|
14 */ |
|
15 |
|
16 /** |
|
17 * Base rendering class for parsed and tokenized text. |
|
18 * |
|
19 * @category Text |
|
20 * @package Text_Wiki |
|
21 * @author Paul M. Jones <pmjones@php.net> |
|
22 * @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1 |
|
23 * @version Release: @package_version@ |
|
24 * @link http://pear.php.net/package/Text_Wiki |
|
25 */ |
|
26 class Text_Wiki_Render { |
|
27 |
|
28 |
|
29 /** |
|
30 * |
|
31 * Configuration options for this render rule. |
|
32 * |
|
33 * @access public |
|
34 * |
|
35 * @var string |
|
36 * |
|
37 */ |
|
38 |
|
39 var $conf = array(); |
|
40 |
|
41 |
|
42 /** |
|
43 * |
|
44 * The name of this rule's format. |
|
45 * |
|
46 * @access public |
|
47 * |
|
48 * @var string |
|
49 * |
|
50 */ |
|
51 |
|
52 var $format = null; |
|
53 |
|
54 |
|
55 /** |
|
56 * |
|
57 * The name of this rule's token array elements. |
|
58 * |
|
59 * @access public |
|
60 * |
|
61 * @var string |
|
62 * |
|
63 */ |
|
64 |
|
65 var $rule = null; |
|
66 |
|
67 |
|
68 /** |
|
69 * |
|
70 * A reference to the calling Text_Wiki object. |
|
71 * |
|
72 * This is needed so that each rule has access to the same source |
|
73 * text, token set, URLs, interwiki maps, page names, etc. |
|
74 * |
|
75 * @access public |
|
76 * |
|
77 * @var object |
|
78 */ |
|
79 |
|
80 var $wiki = null; |
|
81 |
|
82 |
|
83 /** |
|
84 * |
|
85 * Constructor for this render format or rule. |
|
86 * |
|
87 * @access public |
|
88 * |
|
89 * @param object &$obj The calling "parent" Text_Wiki object. |
|
90 * |
|
91 */ |
|
92 |
|
93 function Text_Wiki_Render(&$obj) |
|
94 { |
|
95 // keep a reference to the calling Text_Wiki object |
|
96 $this->wiki =& $obj; |
|
97 |
|
98 // get the config-key-name for this object, |
|
99 // strip the Text_Wiki_Render_ part |
|
100 // 01234567890123456 |
|
101 $tmp = get_class($this); |
|
102 $tmp = substr($tmp, 17); |
|
103 |
|
104 // split into pieces at the _ mark. |
|
105 // first part is format, second part is rule. |
|
106 $part = explode('_', $tmp); |
|
107 $this->format = isset($part[0]) ? ucwords(strtolower($part[0])) : null; |
|
108 $this->rule = isset($part[1]) ? ucwords(strtolower($part[1])) : null; |
|
109 |
|
110 // is there a format but no rule? |
|
111 // then this is the "main" render object, with |
|
112 // pre() and post() methods. |
|
113 if ($this->format && ! $this->rule && |
|
114 isset($this->wiki->formatConf[$this->format]) && |
|
115 is_array($this->wiki->formatConf[$this->format])) { |
|
116 |
|
117 // this is a format render object |
|
118 $this->conf = array_merge( |
|
119 $this->conf, |
|
120 $this->wiki->formatConf[$this->format] |
|
121 ); |
|
122 |
|
123 } |
|
124 |
|
125 // is there a format and a rule? |
|
126 if ($this->format && $this->rule && |
|
127 isset($this->wiki->renderConf[$this->format][$this->rule]) && |
|
128 is_array($this->wiki->renderConf[$this->format][$this->rule])) { |
|
129 |
|
130 // this is a rule render object |
|
131 $this->conf = array_merge( |
|
132 $this->conf, |
|
133 $this->wiki->renderConf[$this->format][$this->rule] |
|
134 ); |
|
135 } |
|
136 } |
|
137 |
|
138 |
|
139 /** |
|
140 * |
|
141 * Simple method to safely get configuration key values. |
|
142 * |
|
143 * @access public |
|
144 * |
|
145 * @param string $key The configuration key. |
|
146 * |
|
147 * @param mixed $default If the key does not exist, return this value |
|
148 * instead. |
|
149 * |
|
150 * @return mixed The configuration key value (if it exists) or the |
|
151 * default value (if not). |
|
152 * |
|
153 */ |
|
154 |
|
155 function getConf($key, $default = null) |
|
156 { |
|
157 if (isset($this->conf[$key])) { |
|
158 return $this->conf[$key]; |
|
159 } else { |
|
160 return $default; |
|
161 } |
|
162 } |
|
163 |
|
164 |
|
165 /** |
|
166 * |
|
167 * Simple method to wrap a configuration in an sprintf() format. |
|
168 * |
|
169 * @access public |
|
170 * |
|
171 * @param string $key The configuration key. |
|
172 * |
|
173 * @param string $format The sprintf() format string. |
|
174 * |
|
175 * @return mixed The formatted configuration key value (if it exists) |
|
176 * or null (if it does not). |
|
177 * |
|
178 */ |
|
179 |
|
180 function formatConf($format, $key) |
|
181 { |
|
182 if (isset($this->conf[$key])) { |
|
183 return sprintf($format, $this->conf[$key]); |
|
184 } else { |
|
185 return null; |
|
186 } |
|
187 } |
|
188 |
|
189 /** |
|
190 * Default method to render url |
|
191 * |
|
192 * @access public |
|
193 * @param string $urlChunk a part of an url to render |
|
194 * @return rendered url |
|
195 * |
|
196 */ |
|
197 |
|
198 function urlEncode($urlChunk) |
|
199 { |
|
200 return rawurlencode($urlChunk); |
|
201 } |
|
202 |
|
203 /** |
|
204 * Default method to render text (htmlspecialchars) |
|
205 * |
|
206 * @access public |
|
207 * @param string $text the text to render |
|
208 * @return rendered text |
|
209 * |
|
210 */ |
|
211 |
|
212 function textEncode($text) |
|
213 { |
|
214 return htmlspecialchars($text); |
|
215 } |
|
216 } |
|
217 ?> |