Source for file Wiki.php
Documentation is available at Wiki.php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +----------------------------------------------------------------------+
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2003 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.0 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available through the world-wide-web at |
// | http://www.php.net/license/2_02.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Authors: Paul M. Jones <pmjones@ciaweb.net> |
// +----------------------------------------------------------------------+
// $Id: Wiki.php,v 1.23 2004/03/30 19:38:52 pmjones Exp $
require_once 'Text/Wiki/Rule.php';
* This is the "master" class for handling the management and convenience
* functions to transform Wiki-formatted text.
* @author Paul M. Jones <pmjones@ciaweb.net>
* The array of rules to apply to the source text, in order.
'file' => 'Text/Wiki/Rule/prefilter.php',
'name' => 'Text_Wiki_Rule_prefilter',
'file' => 'Text/Wiki/Rule/delimiter.php',
'name' => 'Text_Wiki_Rule_delimiter',
'file' => 'Text/Wiki/Rule/code.php',
'name' => 'Text_Wiki_Rule_code',
'file' => 'Text/Wiki/Rule/phpcode.php',
'name' => 'Text_Wiki_Rule_phpcode',
'file' => 'Text/Wiki/Rule/html.php',
'name' => 'Text_Wiki_Rule_html',
'file' => 'Text/Wiki/Rule/raw.php',
'name' => 'Text_Wiki_Rule_raw',
'file' => 'Text/Wiki/Rule/include.php',
'name' => 'Text_Wiki_Rule_include',
'base' => '/path/to/scripts/'
'file' => 'Text/Wiki/Rule/heading.php',
'name' => 'Text_Wiki_Rule_heading',
'file' => 'Text/Wiki/Rule/horiz.php',
'name' => 'Text_Wiki_Rule_horiz',
'file' => 'Text/Wiki/Rule/break.php',
'name' => 'Text_Wiki_Rule_break',
'file' => 'Text/Wiki/Rule/blockquote.php',
'name' => 'Text_Wiki_Rule_blockquote',
'file' => 'Text/Wiki/Rule/list.php',
'name' => 'Text_Wiki_Rule_list',
'file' => 'Text/Wiki/Rule/deflist.php',
'name' => 'Text_Wiki_Rule_deflist',
'file' => 'Text/Wiki/Rule/table.php',
'name' => 'Text_Wiki_Rule_table',
'file' => 'Text/Wiki/Rule/embed.php',
'name' => 'Text_Wiki_Rule_embed',
'base' => '/path/to/scripts/'
'file' => 'Text/Wiki/Rule/image.php',
'name' => 'Text_Wiki_Rule_image',
'base' => '/path/to/images/'
'file' => 'Text/Wiki/Rule/phplookup.php',
'name' => 'Text_Wiki_Rule_phplookup',
'file' => 'Text/Wiki/Rule/toc.php',
'name' => 'Text_Wiki_Rule_toc',
'file' => 'Text/Wiki/Rule/tighten.php',
'name' => 'Text_Wiki_Rule_tighten',
'file' => 'Text/Wiki/Rule/newline.php',
'name' => 'Text_Wiki_Rule_newline',
'file' => 'Text/Wiki/Rule/center.php',
'name' => 'Text_Wiki_Rule_center',
'file' => 'Text/Wiki/Rule/paragraph.php',
'name' => 'Text_Wiki_Rule_paragraph',
'file' => 'Text/Wiki/Rule/url.php',
'name' => 'Text_Wiki_Rule_url',
'file' => 'Text/Wiki/Rule/interwiki.php',
'name' => 'Text_Wiki_Rule_interwiki',
'MeatBall' => 'http://www.usemod.com/cgi-bin/mb.pl?',
'Advogato' => 'http://advogato.org/',
'Wiki' => 'http://c2.com/cgi/wiki?'
'file' => 'Text/Wiki/Rule/wikilink.php',
'name' => 'Text_Wiki_Rule_wikilink',
'view_url' => 'http://example.com/index.php?page=',
'new_url' => 'http://example.com/new.php?page=',
'file' => 'Text/Wiki/Rule/colortext.php',
'name' => 'Text_Wiki_Rule_colortext',
'file' => 'Text/Wiki/Rule/strong.php',
'name' => 'Text_Wiki_Rule_strong',
'file' => 'Text/Wiki/Rule/bold.php',
'name' => 'Text_Wiki_Rule_bold',
'file' => 'Text/Wiki/Rule/emphasis.php',
'name' => 'Text_Wiki_Rule_emphasis',
'file' => 'Text/Wiki/Rule/italic.php',
'name' => 'Text_Wiki_Rule_italic',
'file' => 'Text/Wiki/Rule/tt.php',
'name' => 'Text_Wiki_Rule_tt',
'file' => 'Text/Wiki/Rule/superscript.php',
'name' => 'Text_Wiki_Rule_superscript',
'file' => 'Text/Wiki/Rule/revise.php',
'name' => 'Text_Wiki_Rule_revise',
'file' => 'Text/Wiki/Rule/entities.php',
'name' => 'Text_Wiki_Rule_entities',
* The delimiter that surrounds a token number embedded in the source
* An array of tokens generated by rules as the source text is
* 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 seqential; 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
* 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 $_rule_obj = array ();
* Constructor. Loads the rule objects.
* @param array $rules The set of rules to load for this object.
function Text_Wiki ($rules = null )
// set up the list of rules
* Inserts a rule into to the rule set.
* @param string $key The key name for the rule. Should be different from
* all other keys in the rule set.
* @param string $val The rule values; should be an associative array with
* the keys 'file', 'name', 'flag', and 'conf'.
* @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
function insertRule ($key, $val, $tgt = null )
// does the rule key to be inserted already exist?
if (isset ($this->rules[$key])) {
// the target name is not null, not '', but does not exist. this
// means we're trying to insert after a target key, but the
// target key isn't there.
if (! is_null($tgt) && $tgt != '' && ! isset ($this->rules[$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.
$this->rules[$key] = $val;
// 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
$this->rules[$key] = $val;
foreach ($tmp as $k => $v) {
// insert after the named rule
foreach ($tmp as $k => $v) {
$this->rules[$key] = $val;
* Delete (remove or unset) a rule from the $rules property.
* @param string $rule The name of the rule to remove.
function deleteRule ($key)
unset ($this->rules[$key]);
* Sets the value of a rule's configuration keys.
* @param string $rule The name of the rule for which to set
* @param array|string$arg1 If an array, sets the entire 'conf' key
* for the rule; if a string, specifies which 'conf' subkey to set.
* @param mixed $arg2 If $arg1 is a string, the 'conf' subkey
* specified by $arg1 is set to this value.
function setRuleConf ($rule, $arg1, $arg2 = null )
if (! isset ($this->rules[$rule])) {
if (! isset ($this->rules[$rule]['conf'])) {
$this->rules[$rule]['conf'] = array ();
$this->rules[$rule]['conf'] = $arg1;
$this->rules[$rule]['conf'][$arg1] = $arg2;
* Sets the value of a rule's configuration keys.
* @param string $rule The name of the rule from which to get
* @param string $key Which 'conf' subkey to retrieve. If null,
* gets the entire 'conf' key for the rule.
function getRuleConf ($rule, $key = null )
if (! isset ($this->rules[$rule])) {
if (! isset ($this->rules[$rule]['conf'])) {
$this->rules[$rule]['conf'] = array ();
return $this->rules[$rule]['conf'];
if (! isset ($this->rules[$rule]['conf'][$key])) {
return $this->rules[$rule]['conf'][$key];
* Enables a rule so that it is applied when parsing.
* @param string $rule The name of the rule to enable.
function enableRule ($rule)
if (isset ($this->rules[$rule])) {
$this->rules[$rule]['flag'] = true;
* Disables a rule so that it is not applied when parsing.
* @param string $rule The name of the rule to disable.
function disableRule ($rule)
if (isset ($this->rules[$rule])) {
$this->rules[$rule]['flag'] = false;
* 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.
function transform ($text, $format = 'Xhtml')
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 $key => $val) {
// if flag is not set to 'true' (active),
// do not parse under this rule. assume
// that if a rule exists, but has no flag,
// that it wants to be parsed with.
if (! isset ($val['flag']) || $val['flag'] == true ) {
$this->_loadRuleObject ($key);
$this->_rule_obj[$key]->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
$method = " render$format";
// 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
// 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->_rule_obj[$rule]->$method($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
// 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.
function getTokens ($rules = null )
foreach ($this->_tokens as $key => $val) {
* Loads a rule class file and creates an instance of it.
function _loadRuleObject ($key)
$name = $this->rules [$key]['name'];
include_once $this->rules [$key]['file'];
$this->_rule_obj [$key] = & new $name($this, $key);
Documentation generated on Mon, 11 Mar 2019 10:14:15 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|