Source for file Wiki.php
Documentation is available at Wiki.php
* The baseline abstract parser class.
require_once 'Text/Wiki/Parse.php';
* The baseline abstract render class.
require_once 'Text/Wiki/Render.php';
* Parse structured wiki text and render into arbitrary formats such as XHTML.
* This is the "master" class for handling the management and convenience
* functions to transform Wiki-formatted text.
* $Id: Wiki.php,v 1.25 2004/10/08 17:51:16 pmjones Exp $
* @author Paul M. Jones <pmjones@ciaweb.net>
* The default list of rules, in order, to apply to the source text.
* The list of rules to not-apply to the source text.
* Custom configuration for rules at the parsing stage.
* In this array, the key is the parsing rule name, and the value is
* an array of key-value configuration pairs corresponding to the $conf
* property in the target parsing rule.
* 'base' => '/path/to/scripts/'
* Note that most default rules do not need any parsing configuration.
* Custom configuration for rules at the rendering stage.
* Because rendering may be different for each target format, the
* first-level element in this array is always a format name (e.g.,
* Within that first level element, the subsequent elements match the
* $parseConf format. That is, the sub-key is the rendering rule name,
* and the sub-value is an array of key-value configuration pairs
* corresponding to the $conf property in the target rendering rule.
* Custom configuration for the output format itself.
* Even though Text_Wiki will render the tokens from parsed text,
* the format itself may require some configuration. For example,
* RTF needs to know font names and sizes, PDF requires page layout
* information, and DocBook needs a section hierarchy. This array
* matches the $conf property of the the format-level renderer
* (e.g., Text_Wiki_Render_Xhtml).
* In this array, the key is the rendering format name, and the value is
* an array of key-value configuration pairs corresponding to the $conf
* property in the rendering format rule.
* The delimiter for token numbers of parsed elements in source text.
* The tokens generated by rules as the source text is parsed.
* As Text_Wiki applies rule classes to the source text, it will
* replace portions of the text with a delimited token number. This
* is the array of those tokens, representing the replaced text and
* any options set by the parser for that replaced text.
* The tokens array is sequential; each element is itself a sequential
* array where element 0 is the name of the rule that generated the
* token, and element 1 is an associative array where the key is an
* option name and the value is an option value.
* The source text to which rules will be applied.
* This text will be transformed in-place, which means that it will
* change as the rules are applied.
* Text_Wiki creates one instance of every rule that is applied to
* the source text; this array holds those instances. The array key
* is the rule name, and the array value is an instance of the rule
* Array of rule renderers.
* Text_Wiki creates one instance of every rule that is applied to
* the source text; this array holds those instances. The array key
* is the rule name, and the array value is an instance of the rule
var $renderObj = array ();
* Array of format renderers.
var $formatObj = array ();
* Array of paths to search, in order, for parsing and rendering rules.
* The directory separator character.
var $_dirSep = DIRECTORY_SEPARATOR;
* @param array $rules The set of rules to load for this object.
$this->fixPath (dirname(__FILE__ )) . 'Wiki/Parse/'
$this->fixPath (dirname(__FILE__ )) . 'Wiki/Render/'
* Set parser configuration for a specific rule and key.
* @param string $rule The parse rule to set config for.
* @param array|string$arg1 The full config array to use for the
* parse rule, or a conf key in that array.
* @param string $arg2 The config value for the key.
// if first arg is an array, use it as the entire
// conf array for the rule. otherwise, treat arg1
// as a key and arg2 as a value for the rule conf.
* Get parser configuration for a specific rule and key.
* @param string $rule The parse rule to get config for.
* @param string $key A key in the conf array; if null,
* returns the entire conf array.
* @return mixed The whole conf array if no key is specified,
* or the specific conf key value.
// the rule does not exist
// no key requested, return the whole array
// does the requested key exist?
// yes, return that value
* Set renderer configuration for a specific format, rule, and key.
* @param string $format The render format to set config for.
* @param string $rule The render rule to set config for in the format.
* @param array|string$arg1 The config array, or the config key
* within the render rule.
* @param string $arg2 The config value for the key.
// if first arg is an array, use it as the entire
// conf array for the render rule. otherwise, treat arg1
// as a key and arg2 as a value for the render rule conf.
* Get renderer configuration for a specific format, rule, and key.
* @param string $format The render format to get config for.
* @param string $rule The render format rule to get config for.
* @param string $key A key in the conf array; if null,
* returns the entire conf array.
* @return mixed The whole conf array if no key is specified,
* or the specific conf key value.
// no key requested, return the whole array
// does the requested key exist?
if (isset ($this->renderConf[$format][$rule][$key])) {
// yes, return that value
* Set format configuration for a specific rule and key.
* @param string $format The format to set config for.
* @param string $key The config key within the format.
* @param string $val The config value for the key.
// if first arg is an array, use it as the entire
// conf array for the format. otherwise, treat arg1
// as a key and arg2 as a value for the format conf.
* Get configuration for a specific format and key.
* @param string $format The format to get config for.
* @param mixed $key A key in the conf array; if null,
* returns the entire conf array.
* @return mixed The whole conf array if no key is specified,
* or the specific conf key value.
// the format does not exist
// no key requested, return the whole array
// does the requested key exist?
// yes, return that value
* Inserts a rule into to the rule set.
* @param string $name The name of the rule. Should be different from
* all other keys in the rule set.
* @param string $tgt The rule after which to insert this new rule. By
* default (null) the rule is inserted at the end; if set to '', inserts
// does the rule name to be inserted already exist?
// the target name is not null, and not '', but does not exist
// in the list of rules. this means we're trying to insert after
// a target key, but the target key isn't there.
if (! is_null($tgt) && $tgt != '' &&
// if $tgt is null, insert at the end. We know this is at the
// end (instead of resetting an existing rule) becuase we exited
// at the top of this method if the rule was already in place.
// save a copy of the current rules, then reset the rule set
// so we can insert in the proper place later.
// where to insert the rule?
// insert at the beginning
// insert after the named rule
* Delete (remove or unset) a rule from the $rules property.
* @param string $rule The name of the rule to remove.
unset ($this->rules[$key]);
* Change from one rule to another in-place.
* @param string $old The name of the rule to change from.
* @param string $new The name of the rule to change to.
$this->rules[$old] = $new;
* Enables a rule so that it is applied when parsing.
* @param string $rule The name of the rule to enable.
* Disables a rule so that it is not applied when parsing.
* @param string $rule The name of the rule to disable.
* Parses and renders the text passed to it, and returns the results.
* First, the method parses the source text, applying rules to the
* text as it goes. These rules will modify the source text
* in-place, replacing some text with delimited tokens (and
* populating the $this->tokens array as it goes).
* Next, the method renders the in-place tokens into the requested
* Finally, the method returns the transformed text. Note that the
* source text is transformed in place; once it is transformed, it is
* no longer the same as the original source text.
* @param string $text The source text to which wiki rules should be
* applied, both for parsing and for rendering.
* @param string $format The target output format, typically 'xhtml'.
* If a rule does not support a given format, the output from that
* @return string The transformed wiki text.
return $this->render($format);
* Sets the $_source text property, then parses it in place and
* retains tokens in the $_tokens array property.
* @param string $text The source text to which wiki rules should be
* applied, both for parsing and for rendering.
// set the object property for the source text
// apply the parse() method of each requested rule to the source
foreach ($this->rules as $name) {
// do not parse the rules listed in $disable
// load the parsing object
// load may have failed; only parse if
// an object is in the array now
$this->parseObj[$name]->parse ();
* Renders tokens back into the source text, based on the requested format.
* @param string $format The target output format, typically 'xhtml'.
* If a rule does not support a given format, the output from that
* @return string The transformed wiki text.
function render($format = 'Xhtml')
// the rendering method we're going to use from each rule
// the eventual output text
// when passing through the parsed source text, keep track of when
// we are in a delimited section
// when in a delimited section, capture the token key number
// load the format object
// pre-rendering activity
$output .= $this->formatObj[$format]->pre ();
// load the render objects
// pass through the parsed source text character by character
for ($i = 0; $i < $k; $i++ ) {
$char = $this->source{$i};
// are alredy in a delimited section?
// yes; are we ending the section?
if ($char == $this->delim) {
// yes, get the replacement text for the delimited
// token number and unset the flag.
$rule = $this->tokens[$key][0 ];
$opts = $this->tokens[$key][1 ];
$output .= $this->renderObj[$rule]->token ($opts);
// no, add to the dlimited token key number
// not currently in a delimited section.
// are we starting into a delimited section?
if ($char == $this->delim) {
// yes, reset the previous key and
// no, add to the output as-is
// post-rendering activity
if (is_object ($this->formatObj [$format])) {
$output .= $this->formatObj [$format]->post ();
// return the rendered source text.
* Returns the parsed source text with delimited token placeholders.
* @return string The parsed source text.
* Returns tokens that have been parsed out of the source text.
* @param array $rules If an array of rule names is passed, only return
* tokens matching these rule names. If no array is passed, return all
* @return array An array of tokens.
foreach ($this->tokens as $key => $val) {
* Add a token to the Text_Wiki tokens array, and return a delimited
* @param array $options An associative array of options for the new
* token array element. The keys and values are specific to the
* rule, and may or may not be common to other rule options. Typical
* options keys are 'text' and 'type' but may include others.
* @param boolean $id_only If true, return only the token number, not
* a delimited token string.
* @return string|intBy default, return the number of the
* newly-created token array element with a delimiter prefix and
* suffix; however, if $id_only is set to true, return only the token
* number (no delimiters).
function addToken($rule, $options = array (), $id_only = false )
// increment the token ID number. note that if you parse
// multiple times with the same Text_Wiki object, the ID number
// will not reset to zero.
// force the options to be an array
$this->tokens [$id] = array (
// return the last token number
// return the token number with delimiters
* Set or re-set a token with specific information, overwriting any
* previous rule name and rule options.
* @param int $id The token number to reset.
* @param int $rule The rule name to use.
* @param array $options An associative array of options for the
* token array element. The keys and values are specific to the
* rule, and may or may not be common to other rule options. Typical
* options keys are 'text' and 'type' but may include others.
function setToken($id, $rule, $options = array ())
$this->tokens [$id] = array (
* Load a rule parser class file.
* @return bool True if loaded, false if not.
$class = " Text_Wiki_Parse_$rule";
$this->parseObj [$rule] = null;
$this->parseObj [$rule] = & new $class($this);
* Load a rule-render class file.
* @return bool True if loaded, false if not.
$file = " $format/$rule.php";
$class = " Text_Wiki_Render_$format" . " _$rule";
$loc = $this->findFile('render', $file);
$this->renderObj [$rule] = & new $class($this);
* Load a format-render class file.
* @return bool True if loaded, false if not.
$file = $format . '.php';
$class = " Text_Wiki_Render_$format";
$loc = $this->findFile('render', $file);
$this->formatObj [$format] = & new $class($this);
* Add a path to a path array.
* @param string $type The path-type to add (parse or render).
* @param string $dir The directory to add to the path-type.
$dir = $this->fixPath ($dir);
if (! isset ($this->path [$type])) {
$this->path [$type] = array ($dir);
* Get the current path array for a path-type.
* @param string $type The path-type to look up (plugin, filter, or
* template). If not set, returns all path types.
* @return array The array of paths for the requested type.
} elseif (! isset ($this->path [$type])) {
return $this->path [$type];
* Searches a series of paths for a given file.
* @param array $type The type of paths to search (template, plugin,
* @param string $file The file name to look for.
* @return string|boolThe full path and file name for the target file,
* or boolean false if the file is not found in any of the paths.
// start looping through them
foreach ($set as $path) {
$fullname = $path . $file;
// could not find the file in the set of paths
* Append a trailing '/' to paths, unless the path is empty.
* @param string $path The file path to fix
* @return string The fixed file path
$len = strlen($this->_dirSep );
substr($path, -1 * $len, $len) != $this->_dirSep ) {
return $path . $this->_dirSep;
Documentation generated on Mon, 11 Mar 2019 14:22:34 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|