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