author | Dan |
Sun, 01 Jul 2007 14:19:20 -0400 | |
changeset 34 | 80f52593bee9 |
parent 32 | 4d87aad3c4c0 |
child 35 | efae425e9b98 |
permissions | -rw-r--r-- |
1 | 1 |
<?php |
2 |
/** |
|
3 |
* Parse structured wiki text and render into arbitrary formats such as XHTML. |
|
4 |
* |
|
5 |
* PHP versions 4 and 5 |
|
6 |
* |
|
7 |
* @category Text |
|
8 |
* @package Text_Wiki |
|
9 |
* @author Paul M. Jones <pmjones@php.net> |
|
10 |
* @license http://www.gnu.org/licenses/lgpl.html |
|
11 |
* @version CVS: $Id: Wiki.php,v 1.44 2006/03/02 04:04:59 justinpatrin Exp $ |
|
12 |
* @link http://wiki.ciaweb.net/yawiki/index.php?area=Text_Wiki |
|
13 |
* |
|
14 |
* This code was modified for use in Enano. The Text_Wiki engine is licensed |
|
15 |
* under the GNU Lesser General Public License; see |
|
16 |
* http://www.gnu.org/licenses/lgpl.html for details. |
|
17 |
* |
|
18 |
*/ |
|
19 |
||
20 |
require_once ENANO_ROOT.'/includes/wikiengine/Parse.php'; |
|
21 |
require_once ENANO_ROOT.'/includes/wikiengine/Render.php'; |
|
22 |
||
23 |
class Text_Wiki { |
|
24 |
||
25 |
var $rules = array( |
|
26 |
'Prefilter', |
|
27 |
'Delimiter', |
|
28 |
'Code', |
|
29 |
'Function', |
|
30 |
'Html', |
|
31 |
'Raw', |
|
32 |
'Include', |
|
33 |
'Embed', |
|
34 |
'Anchor', |
|
35 |
'Heading', |
|
36 |
'Toc', |
|
37 |
'Horiz', |
|
38 |
'Break', |
|
39 |
'Blockquote', |
|
40 |
'List', |
|
41 |
'Deflist', |
|
42 |
'Table', |
|
43 |
'Image', |
|
44 |
'Phplookup', |
|
45 |
'Center', |
|
46 |
'Newline', |
|
47 |
'Paragraph', |
|
48 |
'Url', |
|
49 |
'Freelink', |
|
50 |
'Interwiki', |
|
51 |
'Wikilink', |
|
52 |
'Colortext', |
|
53 |
'Strong', |
|
54 |
'Bold', |
|
55 |
'Emphasis', |
|
56 |
'Italic', |
|
57 |
'Underline', |
|
58 |
'Tt', |
|
59 |
'Superscript', |
|
60 |
'Subscript', |
|
61 |
'Revise', |
|
62 |
'Tighten' |
|
63 |
); |
|
64 |
||
65 |
var $disable = array( |
|
66 |
'Html', |
|
67 |
'Include', |
|
32
4d87aad3c4c0
Finished everything on the TODO list (yay!); several CSS cleanups; tons more changes in this commit - see the patch for details
Dan
parents:
1
diff
changeset
|
68 |
'Embed', |
4d87aad3c4c0
Finished everything on the TODO list (yay!); several CSS cleanups; tons more changes in this commit - see the patch for details
Dan
parents:
1
diff
changeset
|
69 |
'Tighten' |
1 | 70 |
); |
71 |
||
72 |
var $parseConf = array(); |
|
73 |
||
74 |
var $renderConf = array( |
|
75 |
'Docbook' => array(), |
|
76 |
'Latex' => array(), |
|
77 |
'Pdf' => array(), |
|
78 |
'Plain' => array(), |
|
79 |
'Rtf' => array(), |
|
80 |
'Xhtml' => array() |
|
81 |
); |
|
82 |
||
83 |
var $formatConf = array( |
|
84 |
'Docbook' => array(), |
|
85 |
'Latex' => array(), |
|
86 |
'Pdf' => array(), |
|
87 |
'Plain' => array(), |
|
88 |
'Rtf' => array(), |
|
89 |
'Xhtml' => array() |
|
90 |
); |
|
91 |
var $delim = "\xFF"; |
|
92 |
var $tokens = array(); |
|
93 |
var $_countRulesTokens = array(); |
|
94 |
var $source = ''; |
|
95 |
var $parseObj = array(); |
|
96 |
var $renderObj = array(); |
|
97 |
var $formatObj = array(); |
|
98 |
var $path = array( |
|
99 |
'parse' => array(), |
|
100 |
'render' => array() |
|
101 |
); |
|
102 |
var $_dirSep = DIRECTORY_SEPARATOR; |
|
103 |
function Text_Wiki($rules = null) |
|
104 |
{ |
|
105 |
if (is_array($rules)) { |
|
106 |
$this->rules = $rules; |
|
107 |
} |
|
108 |
||
109 |
$this->addPath( |
|
110 |
'parse', |
|
111 |
$this->fixPath(ENANO_ROOT) . 'includes/wikiengine/Parse/Default/' |
|
112 |
); |
|
113 |
$this->addPath( |
|
114 |
'render', |
|
115 |
$this->fixPath(ENANO_ROOT) . 'includes/wikiengine/Render/' |
|
116 |
); |
|
117 |
||
118 |
} |
|
119 |
||
120 |
function &singleton($parser = 'Default', $rules = null) |
|
121 |
{ |
|
122 |
static $only = array(); |
|
123 |
if (!isset($only[$parser])) { |
|
124 |
$ret =& Text_Wiki::factory($parser, $rules); |
|
125 |
if (!$ret) { |
|
126 |
return $ret; |
|
127 |
} |
|
128 |
$only[$parser] =& $ret; |
|
129 |
} |
|
130 |
return $only[$parser]; |
|
131 |
} |
|
132 |
||
133 |
function &factory($parser = 'Default', $rules = null) |
|
134 |
{ |
|
135 |
$d=getcwd(); |
|
136 |
chdir(ENANO_ROOT); |
|
137 |
||
138 |
$class = 'Text_Wiki_' . $parser; |
|
139 |
$c2 = '._includes_wikiengine_' . $parser; |
|
140 |
$file = str_replace('_', '/', $c2).'.php'; |
|
141 |
if (!class_exists($class)) { |
|
142 |
$fp = @fopen($file, 'r', true); |
|
143 |
if ($fp === false) { |
|
144 |
die_semicritical('Wiki formatting engine error', '<p>Could not find file '.$file.' in include_path</p>'); |
|
145 |
} |
|
146 |
fclose($fp); |
|
147 |
include_once($file); |
|
148 |
if (!class_exists($class)) { |
|
149 |
die_semicritical('Wiki formatting engine error', '<p>Class '.$class.' does not exist after including '.$file.'</p>'); |
|
150 |
} |
|
151 |
} |
|
152 |
||
153 |
chdir($d); |
|
154 |
||
155 |
$obj =& new $class($rules); |
|
156 |
return $obj; |
|
157 |
} |
|
158 |
||
159 |
function setParseConf($rule, $arg1, $arg2 = null) |
|
160 |
{ |
|
161 |
$rule = ucwords(strtolower($rule)); |
|
162 |
||
163 |
if (! isset($this->parseConf[$rule])) { |
|
164 |
$this->parseConf[$rule] = array(); |
|
165 |
} |
|
166 |
||
167 |
if (is_array($arg1)) { |
|
168 |
$this->parseConf[$rule] = $arg1; |
|
169 |
} else { |
|
170 |
$this->parseConf[$rule][$arg1] = $arg2; |
|
171 |
} |
|
172 |
} |
|
173 |
||
174 |
function getParseConf($rule, $key = null) |
|
175 |
{ |
|
176 |
$rule = ucwords(strtolower($rule)); |
|
177 |
||
178 |
if (! isset($this->parseConf[$rule])) { |
|
179 |
return null; |
|
180 |
} |
|
181 |
||
182 |
if (is_null($key)) { |
|
183 |
return $this->parseConf[$rule]; |
|
184 |
} |
|
185 |
||
186 |
if (isset($this->parseConf[$rule][$key])) { |
|
187 |
return $this->parseConf[$rule][$key]; |
|
188 |
} else { |
|
189 |
return null; |
|
190 |
} |
|
191 |
} |
|
192 |
||
193 |
function setRenderConf($format, $rule, $arg1, $arg2 = null) |
|
194 |
{ |
|
195 |
$format = ucwords(strtolower($format)); |
|
196 |
$rule = ucwords(strtolower($rule)); |
|
197 |
||
198 |
if (! isset($this->renderConf[$format])) { |
|
199 |
$this->renderConf[$format] = array(); |
|
200 |
} |
|
201 |
||
202 |
if (! isset($this->renderConf[$format][$rule])) { |
|
203 |
$this->renderConf[$format][$rule] = array(); |
|
204 |
} |
|
205 |
||
206 |
if (is_array($arg1)) { |
|
207 |
$this->renderConf[$format][$rule] = $arg1; |
|
208 |
} else { |
|
209 |
$this->renderConf[$format][$rule][$arg1] = $arg2; |
|
210 |
} |
|
211 |
} |
|
212 |
||
213 |
function getRenderConf($format, $rule, $key = null) |
|
214 |
{ |
|
215 |
$format = ucwords(strtolower($format)); |
|
216 |
$rule = ucwords(strtolower($rule)); |
|
217 |
||
218 |
if (! isset($this->renderConf[$format]) || |
|
219 |
! isset($this->renderConf[$format][$rule])) { |
|
220 |
return null; |
|
221 |
} |
|
222 |
||
223 |
if (is_null($key)) { |
|
224 |
return $this->renderConf[$format][$rule]; |
|
225 |
} |
|
226 |
||
227 |
if (isset($this->renderConf[$format][$rule][$key])) { |
|
228 |
return $this->renderConf[$format][$rule][$key]; |
|
229 |
} else { |
|
230 |
return null; |
|
231 |
} |
|
232 |
||
233 |
} |
|
234 |
||
235 |
function setFormatConf($format, $arg1, $arg2 = null) |
|
236 |
{ |
|
237 |
if (! is_array($this->formatConf[$format])) { |
|
238 |
$this->formatConf[$format] = array(); |
|
239 |
} |
|
240 |
||
241 |
if (is_array($arg1)) { |
|
242 |
$this->formatConf[$format] = $arg1; |
|
243 |
} else { |
|
244 |
$this->formatConf[$format][$arg1] = $arg2; |
|
245 |
} |
|
246 |
} |
|
247 |
||
248 |
function getFormatConf($format, $key = null) |
|
249 |
{ |
|
250 |
if (! isset($this->formatConf[$format])) { |
|
251 |
return null; |
|
252 |
} |
|
253 |
||
254 |
if (is_null($key)) { |
|
255 |
return $this->formatConf[$format]; |
|
256 |
} |
|
257 |
||
258 |
if (isset($this->formatConf[$format][$key])) { |
|
259 |
return $this->formatConf[$format][$key]; |
|
260 |
} else { |
|
261 |
return null; |
|
262 |
} |
|
263 |
} |
|
264 |
||
265 |
function insertRule($name, $tgt = null) |
|
266 |
{ |
|
267 |
$name = ucwords(strtolower($name)); |
|
268 |
if (! is_null($tgt)) { |
|
269 |
$tgt = ucwords(strtolower($tgt)); |
|
270 |
} |
|
271 |
if (in_array($name, $this->rules)) { |
|
272 |
return null; |
|
273 |
} |
|
274 |
||
275 |
if (! is_null($tgt) && $tgt != '' && |
|
276 |
! in_array($tgt, $this->rules)) { |
|
277 |
return false; |
|
278 |
} |
|
279 |
||
280 |
if (is_null($tgt)) { |
|
281 |
$this->rules[] = $name; |
|
282 |
return true; |
|
283 |
} |
|
284 |
||
285 |
if ($tgt == '') { |
|
286 |
array_unshift($this->rules, $name); |
|
287 |
return true; |
|
288 |
} |
|
289 |
||
290 |
$tmp = $this->rules; |
|
291 |
$this->rules = array(); |
|
292 |
||
293 |
foreach ($tmp as $val) { |
|
294 |
$this->rules[] = $val; |
|
295 |
if ($val == $tgt) { |
|
296 |
$this->rules[] = $name; |
|
297 |
} |
|
298 |
} |
|
299 |
||
300 |
return true; |
|
301 |
} |
|
302 |
||
303 |
function deleteRule($name) |
|
304 |
{ |
|
305 |
$name = ucwords(strtolower($name)); |
|
306 |
$key = array_search($name, $this->rules); |
|
307 |
if ($key !== false) { |
|
308 |
unset($this->rules[$key]); |
|
309 |
} |
|
310 |
} |
|
311 |
||
312 |
function changeRule($old, $new) |
|
313 |
{ |
|
314 |
$old = ucwords(strtolower($old)); |
|
315 |
$new = ucwords(strtolower($new)); |
|
316 |
$key = array_search($old, $this->rules); |
|
317 |
if ($key !== false) { |
|
318 |
$this->deleteRule($new); |
|
319 |
$this->rules[$key] = $new; |
|
320 |
} |
|
321 |
} |
|
322 |
||
323 |
function enableRule($name) |
|
324 |
{ |
|
325 |
$name = ucwords(strtolower($name)); |
|
326 |
$key = array_search($name, $this->disable); |
|
327 |
if ($key !== false) { |
|
328 |
unset($this->disable[$key]); |
|
329 |
} |
|
330 |
} |
|
331 |
||
332 |
function disableRule($name) |
|
333 |
{ |
|
334 |
$name = ucwords(strtolower($name)); |
|
335 |
$key = array_search($name, $this->disable); |
|
336 |
if ($key === false) { |
|
337 |
$this->disable[] = $name; |
|
338 |
} |
|
339 |
} |
|
340 |
||
341 |
function transform($text, $format = 'Xhtml') |
|
342 |
{ |
|
343 |
$this->parse($text); |
|
344 |
return $this->render($format); |
|
345 |
} |
|
346 |
||
347 |
function parse($text) |
|
348 |
{ |
|
349 |
$this->source = $text; |
|
350 |
||
351 |
$this->tokens = array(); |
|
352 |
$this->_countRulesTokens = array(); |
|
353 |
||
354 |
foreach ($this->rules as $name) { |
|
355 |
if (! in_array($name, $this->disable)) { |
|
356 |
$this->loadParseObj($name); |
|
357 |
||
358 |
if (is_object($this->parseObj[$name])) { |
|
359 |
$this->parseObj[$name]->parse(); |
|
360 |
} |
|
32
4d87aad3c4c0
Finished everything on the TODO list (yay!); several CSS cleanups; tons more changes in this commit - see the patch for details
Dan
parents:
1
diff
changeset
|
361 |
// For debugging |
4d87aad3c4c0
Finished everything on the TODO list (yay!); several CSS cleanups; tons more changes in this commit - see the patch for details
Dan
parents:
1
diff
changeset
|
362 |
// echo('<p>' . $name . ':</p><pre>'.htmlspecialchars($this->source).'</pre>'); |
1 | 363 |
} |
364 |
} |
|
365 |
} |
|
366 |
||
367 |
function render($format = 'Xhtml') |
|
368 |
{ |
|
369 |
$format = ucwords(strtolower($format)); |
|
370 |
||
371 |
$output = ''; |
|
372 |
||
373 |
$in_delim = false; |
|
374 |
||
375 |
$key = ''; |
|
376 |
||
377 |
$result = $this->loadFormatObj($format); |
|
378 |
if ($this->isError($result)) { |
|
379 |
return $result; |
|
380 |
} |
|
381 |
||
382 |
if (is_object($this->formatObj[$format])) { |
|
383 |
$output .= $this->formatObj[$format]->pre(); |
|
384 |
} |
|
385 |
||
386 |
foreach (array_keys($this->_countRulesTokens) as $rule) { |
|
387 |
$this->loadRenderObj($format, $rule); |
|
388 |
} |
|
389 |
||
390 |
$k = strlen($this->source); |
|
391 |
for ($i = 0; $i < $k; $i++) { |
|
392 |
||
393 |
$char = $this->source{$i}; |
|
394 |
||
395 |
if ($in_delim) { |
|
396 |
||
397 |
if ($char == $this->delim) { |
|
398 |
||
399 |
$key = (int)$key; |
|
400 |
$rule = $this->tokens[$key][0]; |
|
401 |
$opts = $this->tokens[$key][1]; |
|
402 |
$output .= $this->renderObj[$rule]->token($opts); |
|
403 |
$in_delim = false; |
|
404 |
||
405 |
} else { |
|
406 |
||
407 |
$key .= $char; |
|
408 |
||
409 |
} |
|
410 |
||
411 |
} else { |
|
412 |
||
413 |
if ($char == $this->delim) { |
|
414 |
$key = ''; |
|
415 |
$in_delim = true; |
|
416 |
} else { |
|
417 |
$output .= $char; |
|
418 |
} |
|
419 |
} |
|
420 |
} |
|
421 |
||
422 |
if (is_object($this->formatObj[$format])) { |
|
423 |
$output .= $this->formatObj[$format]->post(); |
|
424 |
} |
|
425 |
||
426 |
return $output; |
|
427 |
} |
|
428 |
||
429 |
function getSource() |
|
430 |
{ |
|
431 |
return $this->source; |
|
432 |
} |
|
433 |
||
434 |
function getTokens($rules = null) |
|
435 |
{ |
|
436 |
if (is_null($rules)) { |
|
437 |
return $this->tokens; |
|
438 |
} else { |
|
439 |
settype($rules, 'array'); |
|
440 |
$result = array(); |
|
441 |
foreach ($this->tokens as $key => $val) { |
|
442 |
if (in_array($val[0], $rules)) { |
|
443 |
$result[$key] = $val; |
|
444 |
} |
|
445 |
} |
|
446 |
return $result; |
|
447 |
} |
|
448 |
} |
|
449 |
||
450 |
function addToken($rule, $options = array(), $id_only = false) |
|
451 |
{ |
|
452 |
static $id; |
|
453 |
if (! isset($id)) { |
|
454 |
$id = 0; |
|
455 |
} else { |
|
456 |
$id ++; |
|
457 |
} |
|
458 |
||
459 |
settype($options, 'array'); |
|
460 |
||
461 |
$this->tokens[$id] = array( |
|
462 |
0 => $rule, |
|
463 |
1 => $options |
|
464 |
); |
|
465 |
if (!isset($this->_countRulesTokens[$rule])) { |
|
466 |
$this->_countRulesTokens[$rule] = 1; |
|
467 |
} else { |
|
468 |
++$this->_countRulesTokens[$rule]; |
|
469 |
} |
|
470 |
||
471 |
if ($id_only) { |
|
472 |
return $id; |
|
473 |
} else { |
|
474 |
return $this->delim . $id . $this->delim; |
|
475 |
} |
|
476 |
} |
|
477 |
||
478 |
function setToken($id, $rule, $options = array()) |
|
479 |
{ |
|
480 |
$oldRule = $this->tokens[$id][0]; |
|
481 |
$this->tokens[$id] = array( |
|
482 |
0 => $rule, |
|
483 |
1 => $options |
|
484 |
); |
|
485 |
if ($rule != $oldRule) { |
|
486 |
if (!($this->_countRulesTokens[$oldRule]--)) { |
|
487 |
unset($this->_countRulesTokens[$oldRule]); |
|
488 |
} |
|
489 |
if (!isset($this->_countRulesTokens[$rule])) { |
|
490 |
$this->_countRulesTokens[$rule] = 1; |
|
491 |
} else { |
|
492 |
++$this->_countRulesTokens[$rule]; |
|
493 |
} |
|
494 |
} |
|
495 |
} |
|
496 |
||
497 |
function loadParseObj($rule) |
|
498 |
{ |
|
499 |
$rule = ucwords(strtolower($rule)); |
|
500 |
$file = $rule . '.php'; |
|
501 |
$class = "Text_Wiki_Parse_$rule"; |
|
502 |
||
503 |
if (! class_exists($class)) { |
|
504 |
$loc = $this->findFile('parse', $file); |
|
505 |
if ($loc) { |
|
506 |
include_once $loc; |
|
507 |
} else { |
|
508 |
$this->parseObj[$rule] = null; |
|
509 |
return $this->error( |
|
510 |
"Parse rule '$rule' not found" |
|
511 |
); |
|
512 |
} |
|
513 |
} |
|
514 |
||
515 |
$this->parseObj[$rule] =& new $class($this); |
|
516 |
||
517 |
} |
|
518 |
||
519 |
function loadRenderObj($format, $rule) |
|
520 |
{ |
|
521 |
$format = ucwords(strtolower($format)); |
|
522 |
$rule = ucwords(strtolower($rule)); |
|
523 |
$file = "$format/$rule.php"; |
|
524 |
$class = "Text_Wiki_Render_$format" . "_$rule"; |
|
525 |
||
526 |
if (! class_exists($class)) { |
|
527 |
$loc = $this->findFile('render', $file); |
|
528 |
if ($loc) { |
|
529 |
include_once $loc; |
|
530 |
} else { |
|
531 |
return $this->error( |
|
532 |
"Render rule '$rule' in format '$format' not found" |
|
533 |
); |
|
534 |
} |
|
535 |
} |
|
536 |
||
537 |
$this->renderObj[$rule] =& new $class($this); |
|
538 |
} |
|
539 |
||
540 |
function loadFormatObj($format) |
|
541 |
{ |
|
542 |
$format = ucwords(strtolower($format)); |
|
543 |
$file = $format . '.php'; |
|
544 |
$class = "Text_Wiki_Render_$format"; |
|
545 |
||
546 |
if (! class_exists($class)) { |
|
547 |
$loc = $this->findFile('render', $file); |
|
548 |
if ($loc) { |
|
549 |
include_once $loc; |
|
550 |
} else { |
|
551 |
return $this->error( |
|
552 |
"Rendering format class '$class' not found" |
|
553 |
); |
|
554 |
} |
|
555 |
} |
|
556 |
||
557 |
$this->formatObj[$format] =& new $class($this); |
|
558 |
} |
|
559 |
||
560 |
function addPath($type, $dir) |
|
561 |
{ |
|
562 |
$dir = $this->fixPath($dir); |
|
563 |
if (! isset($this->path[$type])) { |
|
564 |
$this->path[$type] = array($dir); |
|
565 |
} else { |
|
566 |
array_unshift($this->path[$type], $dir); |
|
567 |
} |
|
568 |
} |
|
569 |
||
570 |
function getPath($type = null) |
|
571 |
{ |
|
572 |
if (is_null($type)) { |
|
573 |
return $this->path; |
|
574 |
} elseif (! isset($this->path[$type])) { |
|
575 |
return array(); |
|
576 |
} else { |
|
577 |
return $this->path[$type]; |
|
578 |
} |
|
579 |
} |
|
580 |
||
581 |
function findFile($type, $file) |
|
582 |
{ |
|
583 |
$set = $this->getPath($type); |
|
584 |
||
585 |
foreach ($set as $path) { |
|
586 |
$fullname = $path . $file; |
|
587 |
if (file_exists($fullname) && is_readable($fullname)) { |
|
588 |
return $fullname; |
|
589 |
} |
|
590 |
} |
|
591 |
||
592 |
return false; |
|
593 |
} |
|
594 |
||
595 |
function fixPath($path) |
|
596 |
{ |
|
597 |
$len = strlen($this->_dirSep); |
|
598 |
||
599 |
if (! empty($path) && |
|
600 |
substr($path, -1 * $len, $len) != $this->_dirSep) { |
|
601 |
return $path . $this->_dirSep; |
|
602 |
} else { |
|
603 |
return $path; |
|
604 |
} |
|
605 |
} |
|
606 |
||
607 |
function &error($message) |
|
608 |
{ |
|
609 |
die($message); |
|
610 |
} |
|
611 |
||
612 |
function isError(&$obj) |
|
613 |
{ |
|
614 |
return is_a($obj, 'PEAR_Error'); |
|
615 |
} |
|
616 |
} |
|
617 |
||
618 |
?> |