cf54c63d6928fde6ce11384ef014378923238467
5 * Parse structured wiki text and render into arbitrary formats such as XHTML.
11 * @author Paul M. Jones <pmjones@php.net>
15 * @version $Id: Wiki.php,v 1.29 2005/02/24 17:26:29 pmjones Exp $
20 * The baseline abstract parser class.
23 require_once 'Text/Wiki/Parse.php';
26 * The baseline abstract render class.
29 require_once 'Text/Wiki/Render.php';
34 * Parse structured wiki text and render into arbitrary formats such as XHTML.
36 * This is the "master" class for handling the management and convenience
37 * functions to transform Wiki-formatted text.
48 * @author Paul M. Jones <pmjones@php.net>
50 * @version @package_version@
59 * The default list of rules, in order, to apply to the source text.
109 * The list of rules to not-apply to the source text.
117 var $disable = array(
126 * Custom configuration for rules at the parsing stage.
128 * In this array, the key is the parsing rule name, and the value is
129 * an array of key-value configuration pairs corresponding to the $conf
130 * property in the target parsing rule.
135 * $parseConf = array(
136 * 'Include' => array(
137 * 'base' => '/path/to/scripts/'
142 * Note that most default rules do not need any parsing configuration.
150 var $parseConf = array();
155 * Custom configuration for rules at the rendering stage.
157 * Because rendering may be different for each target format, the
158 * first-level element in this array is always a format name (e.g.,
161 * Within that first level element, the subsequent elements match the
162 * $parseConf format. That is, the sub-key is the rendering rule name,
163 * and the sub-value is an array of key-value configuration pairs
164 * corresponding to the $conf property in the target rendering rule.
172 var $renderConf = array(
173 'Docbook' => array(),
184 * Custom configuration for the output format itself.
186 * Even though Text_Wiki will render the tokens from parsed text,
187 * the format itself may require some configuration. For example,
188 * RTF needs to know font names and sizes, PDF requires page layout
189 * information, and DocBook needs a section hierarchy. This array
190 * matches the $conf property of the the format-level renderer
191 * (e.g., Text_Wiki_Render_Xhtml).
193 * In this array, the key is the rendering format name, and the value is
194 * an array of key-value configuration pairs corresponding to the $conf
195 * property in the rendering format rule.
203 var $formatConf = array(
204 'Docbook' => array(),
215 * The delimiter for token numbers of parsed elements in source text.
228 * The tokens generated by rules as the source text is parsed.
230 * As Text_Wiki applies rule classes to the source text, it will
231 * replace portions of the text with a delimited token number. This
232 * is the array of those tokens, representing the replaced text and
233 * any options set by the parser for that replaced text.
235 * The tokens array is sequential; each element is itself a sequential
236 * array where element 0 is the name of the rule that generated the
237 * token, and element 1 is an associative array where the key is an
238 * option name and the value is an option value.
246 var $tokens = array();
251 * The source text to which rules will be applied.
253 * This text will be transformed in-place, which means that it will
254 * change as the rules are applied.
267 * Array of rule parsers.
269 * Text_Wiki creates one instance of every rule that is applied to
270 * the source text; this array holds those instances. The array key
271 * is the rule name, and the array value is an instance of the rule
280 var $parseObj = array();
285 * Array of rule renderers.
287 * Text_Wiki creates one instance of every rule that is applied to
288 * the source text; this array holds those instances. The array key
289 * is the rule name, and the array value is an instance of the rule
298 var $renderObj = array();
303 * Array of format renderers.
311 var $formatObj = array();
316 * Array of paths to search, in order, for parsing and rendering rules.
333 * The directory separator character.
341 var $_dirSep = DIRECTORY_SEPARATOR
;
350 * @param array $rules The set of rules to load for this object.
354 function Text_Wiki($rules = null
)
356 if (is_array($rules)) {
357 $this->rules
= $rules;
362 $this->fixPath(dirname(__FILE__
)) . 'Wiki/Parse/Default/'
367 $this->fixPath(dirname(__FILE__
)) . 'Wiki/Render/'
375 * Set parser configuration for a specific rule and key.
379 * @param string $rule The parse rule to set config for.
381 * @param array|string $arg1 The full config array to use for the
382 * parse rule, or a conf key in that array.
384 * @param string $arg2 The config value for the key.
390 function setParseConf($rule, $arg1, $arg2 = null
)
392 $rule = ucwords(strtolower($rule));
394 if (! isset($this->parseConf
[$rule])) {
395 $this->parseConf
[$rule] = array();
398 // if first arg is an array, use it as the entire
399 // conf array for the rule. otherwise, treat arg1
400 // as a key and arg2 as a value for the rule conf.
401 if (is_array($arg1)) {
402 $this->parseConf
[$rule] = $arg1;
404 $this->parseConf
[$rule][$arg1] = $arg2;
411 * Get parser configuration for a specific rule and key.
415 * @param string $rule The parse rule to get config for.
417 * @param string $key A key in the conf array; if null,
418 * returns the entire conf array.
420 * @return mixed The whole conf array if no key is specified,
421 * or the specific conf key value.
425 function getParseConf($rule, $key = null
)
427 $rule = ucwords(strtolower($rule));
429 // the rule does not exist
430 if (! isset($this->parseConf
[$rule])) {
434 // no key requested, return the whole array
436 return $this->parseConf
[$rule];
439 // does the requested key exist?
440 if (isset($this->parseConf
[$rule][$key])) {
441 // yes, return that value
442 return $this->parseConf
[$rule][$key];
452 * Set renderer configuration for a specific format, rule, and key.
456 * @param string $format The render format to set config for.
458 * @param string $rule The render rule to set config for in the format.
460 * @param array|string $arg1 The config array, or the config key
461 * within the render rule.
463 * @param string $arg2 The config value for the key.
469 function setRenderConf($format, $rule, $arg1, $arg2 = null
)
471 $format = ucwords(strtolower($format));
472 $rule = ucwords(strtolower($rule));
474 if (! isset($this->renderConf
[$format])) {
475 $this->renderConf
[$format] = array();
478 if (! isset($this->renderConf
[$format][$rule])) {
479 $this->renderConf
[$format][$rule] = array();
482 // if first arg is an array, use it as the entire
483 // conf array for the render rule. otherwise, treat arg1
484 // as a key and arg2 as a value for the render rule conf.
485 if (is_array($arg1)) {
486 $this->renderConf
[$format][$rule] = $arg1;
488 $this->renderConf
[$format][$rule][$arg1] = $arg2;
495 * Get renderer configuration for a specific format, rule, and key.
499 * @param string $format The render format to get config for.
501 * @param string $rule The render format rule to get config for.
503 * @param string $key A key in the conf array; if null,
504 * returns the entire conf array.
506 * @return mixed The whole conf array if no key is specified,
507 * or the specific conf key value.
511 function getRenderConf($format, $rule, $key = null
)
513 $format = ucwords(strtolower($format));
514 $rule = ucwords(strtolower($rule));
516 if (! isset($this->renderConf
[$format]) ||
517 ! isset($this->renderConf
[$format][$rule])) {
521 // no key requested, return the whole array
523 return $this->renderConf
[$format][$rule];
526 // does the requested key exist?
527 if (isset($this->renderConf
[$format][$rule][$key])) {
528 // yes, return that value
529 return $this->renderConf
[$format][$rule][$key];
539 * Set format configuration for a specific rule and key.
543 * @param string $format The format to set config for.
545 * @param string $key The config key within the format.
547 * @param string $val The config value for the key.
553 function setFormatConf($format, $arg1, $arg2 = null
)
555 if (! is_array($this->formatConf
[$format])) {
556 $this->formatConf
[$format] = array();
559 // if first arg is an array, use it as the entire
560 // conf array for the format. otherwise, treat arg1
561 // as a key and arg2 as a value for the format conf.
562 if (is_array($arg1)) {
563 $this->formatConf
[$format] = $arg1;
565 $this->formatConf
[$format][$arg1] = $arg2;
573 * Get configuration for a specific format and key.
577 * @param string $format The format to get config for.
579 * @param mixed $key A key in the conf array; if null,
580 * returns the entire conf array.
582 * @return mixed The whole conf array if no key is specified,
583 * or the specific conf key value.
587 function getFormatConf($format, $key = null
)
589 // the format does not exist
590 if (! isset($this->formatConf
[$format])) {
594 // no key requested, return the whole array
596 return $this->formatConf
[$format];
599 // does the requested key exist?
600 if (isset($this->formatConf
[$format][$key])) {
601 // yes, return that value
602 return $this->formatConf
[$format][$key];
612 * Inserts a rule into to the rule set.
616 * @param string $name The name of the rule. Should be different from
617 * all other keys in the rule set.
619 * @param string $tgt The rule after which to insert this new rule. By
620 * default (null) the rule is inserted at the end; if set to '', inserts
627 function insertRule($name, $tgt = null
)
629 $name = ucwords(strtolower($name));
630 if (! is_null($tgt)) {
631 $tgt = ucwords(strtolower($tgt));
634 // does the rule name to be inserted already exist?
635 if (in_array($name, $this->rules
)) {
640 // the target name is not null, and not '', but does not exist
641 // in the list of rules. this means we're trying to insert after
642 // a target key, but the target key isn't there.
643 if (! is_null($tgt) && $tgt != '' &&
644 ! in_array($tgt, $this->rules
)) {
648 // if $tgt is null, insert at the end. We know this is at the
649 // end (instead of resetting an existing rule) becuase we exited
650 // at the top of this method if the rule was already in place.
652 $this->rules
[] = $name;
656 // save a copy of the current rules, then reset the rule set
657 // so we can insert in the proper place later.
658 // where to insert the rule?
660 // insert at the beginning
661 array_unshift($this->rules
, $name);
665 // insert after the named rule
667 $this->rules
= array();
669 foreach ($tmp as $val) {
670 $this->rules
[] = $val;
672 $this->rules
[] = $name;
683 * Delete (remove or unset) a rule from the $rules property.
687 * @param string $rule The name of the rule to remove.
693 function deleteRule($name)
695 $name = ucwords(strtolower($name));
696 $key = array_search($name, $this->rules
);
697 if ($key !== false
) {
698 unset($this->rules
[$key]);
705 * Change from one rule to another in-place.
709 * @param string $old The name of the rule to change from.
711 * @param string $new The name of the rule to change to.
717 function changeRule($old, $new)
719 $old = ucwords(strtolower($old));
720 $new = ucwords(strtolower($new));
721 $key = array_search($old, $this->rules
);
722 if ($key !== false
) {
723 $this->rules
[$old] = $new;
730 * Enables a rule so that it is applied when parsing.
734 * @param string $rule The name of the rule to enable.
740 function enableRule($name)
742 $name = ucwords(strtolower($name));
743 $key = array_search($name, $this->disable
);
744 if ($key !== false
) {
745 unset($this->disable
[$key]);
752 * Disables a rule so that it is not applied when parsing.
756 * @param string $rule The name of the rule to disable.
762 function disableRule($name)
764 $name = ucwords(strtolower($name));
765 $key = array_search($name, $this->disable
);
766 if ($key === false
) {
767 $this->disable
[] = $name;
774 * Parses and renders the text passed to it, and returns the results.
776 * First, the method parses the source text, applying rules to the
777 * text as it goes. These rules will modify the source text
778 * in-place, replacing some text with delimited tokens (and
779 * populating the $this->tokens array as it goes).
781 * Next, the method renders the in-place tokens into the requested
784 * Finally, the method returns the transformed text. Note that the
785 * source text is transformed in place; once it is transformed, it is
786 * no longer the same as the original source text.
790 * @param string $text The source text to which wiki rules should be
791 * applied, both for parsing and for rendering.
793 * @param string $format The target output format, typically 'xhtml'.
794 * If a rule does not support a given format, the output from that
795 * rule is rule-specific.
797 * @return string The transformed wiki text.
801 function transform($text, $format = 'Xhtml')
804 return $this->render($format);
810 * Sets the $_source text property, then parses it in place and
811 * retains tokens in the $_tokens array property.
815 * @param string $text The source text to which wiki rules should be
816 * applied, both for parsing and for rendering.
822 function parse($text)
824 // set the object property for the source text
825 $this->source
= $text;
828 $this->tokens
= array();
830 // apply the parse() method of each requested rule to the source
832 foreach ($this->rules
as $name) {
833 // do not parse the rules listed in $disable
834 if (! in_array($name, $this->disable
)) {
836 // load the parsing object
837 $this->loadParseObj($name);
839 // load may have failed; only parse if
840 // an object is in the array now
841 if (is_object($this->parseObj
[$name])) {
842 $this->parseObj
[$name]->parse();
851 * Renders tokens back into the source text, based on the requested format.
855 * @param string $format The target output format, typically 'xhtml'.
856 * If a rule does not support a given format, the output from that
857 * rule is rule-specific.
859 * @return string The transformed wiki text.
863 function render($format = 'Xhtml')
865 // the rendering method we're going to use from each rule
866 $format = ucwords(strtolower($format));
868 // the eventual output text
871 // when passing through the parsed source text, keep track of when
872 // we are in a delimited section
875 // when in a delimited section, capture the token key number
878 // load the format object
879 $this->loadFormatObj($format);
881 // pre-rendering activity
882 if (is_object($this->formatObj
[$format])) {
883 $output .= $this->formatObj
[$format]->pre();
886 // load the render objects
887 foreach (array_keys($this->parseObj
) as $rule) {
888 $this->loadRenderObj($format, $rule);
891 // pass through the parsed source text character by character
892 $k = strlen($this->source
);
893 for ($i = 0; $i < $k; $i++
) {
895 // the current character
896 $char = $this->source
{$i};
898 // are alredy in a delimited section?
901 // yes; are we ending the section?
902 if ($char == $this->delim
) {
904 // yes, get the replacement text for the delimited
905 // token number and unset the flag.
907 $rule = $this->tokens
[$key][0];
908 $opts = $this->tokens
[$key][1];
909 $output .= $this->renderObj
[$rule]->token($opts);
914 // no, add to the dlimited token key number
921 // not currently in a delimited section.
922 // are we starting into a delimited section?
923 if ($char == $this->delim
) {
924 // yes, reset the previous key and
929 // no, add to the output as-is
935 // post-rendering activity
936 if (is_object($this->formatObj
[$format])) {
937 $output .= $this->formatObj
[$format]->post();
940 // return the rendered source text.
947 * Returns the parsed source text with delimited token placeholders.
951 * @return string The parsed source text.
957 return $this->source
;
963 * Returns tokens that have been parsed out of the source text.
967 * @param array $rules If an array of rule names is passed, only return
968 * tokens matching these rule names. If no array is passed, return all
971 * @return array An array of tokens.
975 function getTokens($rules = null
)
977 if (is_null($rules)) {
978 return $this->tokens
;
980 settype($rules, 'array');
982 foreach ($this->tokens
as $key => $val) {
983 if (in_array($val[0], $rules)) {
994 * Add a token to the Text_Wiki tokens array, and return a delimited
999 * @param array $options An associative array of options for the new
1000 * token array element. The keys and values are specific to the
1001 * rule, and may or may not be common to other rule options. Typical
1002 * options keys are 'text' and 'type' but may include others.
1004 * @param boolean $id_only If true, return only the token number, not
1005 * a delimited token string.
1007 * @return string|int By default, return the number of the
1008 * newly-created token array element with a delimiter prefix and
1009 * suffix; however, if $id_only is set to true, return only the token
1010 * number (no delimiters).
1014 function addToken($rule, $options = array(), $id_only = false
)
1016 // increment the token ID number. note that if you parse
1017 // multiple times with the same Text_Wiki object, the ID number
1018 // will not reset to zero.
1026 // force the options to be an array
1027 settype($options, 'array');
1030 $this->tokens
[$id] = array(
1037 // return the last token number
1040 // return the token number with delimiters
1041 return $this->delim
. $id . $this->delim
;
1048 * Set or re-set a token with specific information, overwriting any
1049 * previous rule name and rule options.
1053 * @param int $id The token number to reset.
1055 * @param int $rule The rule name to use.
1057 * @param array $options An associative array of options for the
1058 * token array element. The keys and values are specific to the
1059 * rule, and may or may not be common to other rule options. Typical
1060 * options keys are 'text' and 'type' but may include others.
1066 function setToken($id, $rule, $options = array())
1069 $this->tokens
[$id] = array(
1078 * Load a rule parser class file.
1082 * @return bool True if loaded, false if not.
1086 function loadParseObj($rule)
1088 $rule = ucwords(strtolower($rule));
1089 $file = $rule . '.php';
1090 $class = "Text_Wiki_Parse_$rule";
1092 if (! class_exists($class)) {
1093 $loc = $this->findFile('parse', $file);
1098 // can't find the class
1099 $this->parseObj
[$rule] = null
;
1104 $this->parseObj
[$rule] =& new $class($this);
1111 * Load a rule-render class file.
1115 * @return bool True if loaded, false if not.
1119 function loadRenderObj($format, $rule)
1121 $format = ucwords(strtolower($format));
1122 $rule = ucwords(strtolower($rule));
1123 $file = "$format/$rule.php";
1124 $class = "Text_Wiki_Render_$format" . "_$rule";
1126 if (! class_exists($class)) {
1128 $loc = $this->findFile('render', $file);
1133 // can't find the class
1138 $this->renderObj
[$rule] =& new $class($this);
1144 * Load a format-render class file.
1148 * @return bool True if loaded, false if not.
1152 function loadFormatObj($format)
1154 $format = ucwords(strtolower($format));
1155 $file = $format . '.php';
1156 $class = "Text_Wiki_Render_$format";
1158 if (! class_exists($class)) {
1159 $loc = $this->findFile('render', $file);
1164 // can't find the class
1169 $this->formatObj
[$format] =& new $class($this);
1175 * Add a path to a path array.
1179 * @param string $type The path-type to add (parse or render).
1181 * @param string $dir The directory to add to the path-type.
1187 function addPath($type, $dir)
1189 $dir = $this->fixPath($dir);
1190 if (! isset($this->path
[$type])) {
1191 $this->path
[$type] = array($dir);
1193 array_unshift($this->path
[$type], $dir);
1200 * Get the current path array for a path-type.
1204 * @param string $type The path-type to look up (plugin, filter, or
1205 * template). If not set, returns all path types.
1207 * @return array The array of paths for the requested type.
1211 function getPath($type = null
)
1213 if (is_null($type)) {
1215 } elseif (! isset($this->path
[$type])) {
1218 return $this->path
[$type];
1225 * Searches a series of paths for a given file.
1227 * @param array $type The type of paths to search (template, plugin,
1230 * @param string $file The file name to look for.
1232 * @return string|bool The full path and file name for the target file,
1233 * or boolean false if the file is not found in any of the paths.
1237 function findFile($type, $file)
1239 // get the set of paths
1240 $set = $this->getPath($type);
1242 // start looping through them
1243 foreach ($set as $path) {
1244 $fullname = $path . $file;
1245 if (file_exists($fullname) && is_readable($fullname)) {
1250 // could not find the file in the set of paths
1257 * Append a trailing '/' to paths, unless the path is empty.
1261 * @param string $path The file path to fix
1263 * @return string The fixed file path
1267 function fixPath($path)
1269 $len = strlen($this->_dirSep
);
1271 if (! empty($path) &&
1272 substr($path, -1 * $len, $len) != $this->_dirSep
) {
1273 return $path . $this->_dirSep
;