+ Text_Wiki

%%TOC%%

++ Overview and Background

We assume you know what [http://c2.com/cgi/wiki Wiki] is.  The Wiki Way entails a lot more than just rules for structured-text markup, but that's the only part of the Wiki Way that this package, {{Text_Wiki}}, is concerned with.

The {{Text_Wiki}} package allows you to transform text structured using Wiki rules into any defined target output format. For example, you can convert...

* Tavi-style Wiki text into XHTML
* Original C2-style Wiki text into XML
* !MeatBall style Wiki text into !LaTeX
* Any other Wiki markup rules into any other format (provided it's programmed properly ;-)

{{Text_Wiki}} achieves this level of flexibility by using one class per transformation rule, instead of using a monolithic set of functions.  Each Text_Wiki rule has these methods:

* A constructor that links back to the "master" Text_Wiki class, which the source text as well as any optional confugration parameters
* A parse() method that looks through the source text for strings that match a regular expression
* A process() method that takes the parsed regular expression matches and converts them to an intermediate "meta-format" (generally a delimited token that is re-inserted into the source text, along with a set of options for that specific token)
* A render() method that replaces a saved token with output for a specific target format (e.g., XHTML or !LaTeX).

{{Text_Wiki}} draws its inspiration from a number of codebases, most notably !WikkiTikkiTavi and coWiki; while no code has been directly copied from those codebases, they were indispensible in learning how to process Wiki text.

Also see these pages for more information:

* SamplePage -- for a full list of default markup rules
* TokenRuleKeys -- for a list of the tokens keys generated by different rules
* TextWikiProposal -- a modified version of the initial Text_Wiki proposal to PEAR

++ What Text_Wiki Does Not Do

The {{Text_Wiki}} package does not implement storage, saving, editing, diffs, page counts, and so on.  Those functions are more properly the domain of a full Wiki application and environment, which is outside the stated {{Text_Wiki}} goals (i.e., to transform Wiki text according to defined rules for a target output format).

++ Using Text_Wiki

Here is a quick example of how to use {{Text_Wiki}} to parse from the default markup style (combined from [http://tavi.sourceforge.net/ Tavi] and [http://develnet.org/ coWiki]), do some basic filtering, and render to XHTML.

<php>
	
	// require the Text_Wiki class and create an instance of a new wiki
	// parse-and-render object
	require_once 'Text/Wiki.php';
	$wiki = new Text_Wiki();
	
	// load up some source text
	$text = file_get_contents('sample.wiki.txt');
	
	// transform the text using the default rules for parsing and rendering.
	// the extra \n newlines seem to help with some parsing rules.
	echo $wiki->transform("\n" . $text . "\n");
	
</php>



++ Text_Wiki Options

When you create an instance of Text_Wiki, you can pass a set of options as an associative array. 

The array keys for the options are:

|| '''option key''' || '''variable type''' || '''description''' ||
|| {{delim}}        || string              || the source text delimiter for tokens ||
|| {{interwiki}}    || array               || interwiki site names and URLs ||
|| {{new_text}}     || string              || link-text for new pages ||
|| {{new_url}}      || string              || URL for new pages ||
|| {{pages}}        || array               || list of pages existing in the wiki ||
|| {{rules}}        || array               || the list of rules to be applied to source text ||
|| {{view_url}}     || string              || URL for viewing pages in the wiki ||

For example:

<php>
	// the list of rules, and in which order, to use when
	// transforming structured text; the key is the name to use for
	// identifying tokens for the rule, and the value is the file
	// name of the rule class.
	$rules = array(
        'prefilter'   => 'Text/Wiki/Rule/prefilter.php',
        'delimiter'   => 'Text/Wiki/Rule/delimiter.php',
        'code'        => 'Text/Wiki/Rule/code.php',
        'phpcode'     => 'Text/Wiki/Rule/phpcode.php',
        'html'        => 'Text/Wiki/Rule/html.php',
        'heading'     => 'Text/Wiki/Rule/heading.php',
        'horiz'       => 'Text/Wiki/Rule/horiz.php',
        'blockquote'  => 'Text/Wiki/Rule/blockquote.php',
        'deflist'     => 'Text/Wiki/Rule/deflist.php',
        'table'       => 'Text/Wiki/Rule/table.php',
        'list'        => 'Text/Wiki/Rule/list.php',
        'toc'         => 'Text/Wiki/Rule/toc.php',
        'paragraph'   => 'Text/Wiki/Rule/paragraph.php',
        'raw'         => 'Text/Wiki/Rule/raw.php',
        'phplookup'   => 'Text/Wiki/Rule/phplookup.php',
        'url'         => 'Text/Wiki/Rule/url.php',
        'interwiki'   => 'Text/Wiki/Rule/interwiki.php',
        'freelink'    => 'Text/Wiki/Rule/freelink.php',
        'wikilink'    => 'Text/Wiki/Rule/wikilink.php',
        'strong'      => 'Text/Wiki/Rule/strong.php',
        'bold'        => 'Text/Wiki/Rule/bold.php',
        'emphasis'    => 'Text/Wiki/Rule/emphasis.php',
        'italic'      => 'Text/Wiki/Rule/italic.php',
        'tt'          => 'Text/Wiki/Rule/tt.php',
        'superscript' => 'Text/Wiki/Rule/superscript.php',
        'revise'      => 'Text/Wiki/Rule/revise.php',
        'entities'    => 'Text/Wiki/Rule/entities.php',
        'tighten'     => 'Text/Wiki/Rule/tighten.php'
	);
	
	// the list of InterWiki names and URLs
	$interwiki = array(
		'MeatBall' => 'http://www.usemod.com/cgi-bin/mb.pl?',
		'Advogato' => 'http://advogato.org/',
		'Wiki'     => 'http://c2.com/cgi/wiki?'
	);
	
	// the URL used to view local WikiWeb pages
	$view_url = "http://example.com/wiki.php?page=";
	
	// create a Text_Wiki options array
	$options = array(
		'rules'     => $rules,
		'interwiki' => $interwiki,
		'view_url'  => $view_url
	);
	
	// instantiate a Text_Wiki object with the options
	$wiki =& new Text_Wiki($options);
</php>



+++ {{delim}}

The {{delim}} option is a string, and sets the value of the delimiter character used to mark a token number in the Wiki source text.  When a rule needs to set a token, it replaces the source text with a token number surrounded by this delimiter.

The default delmiter is {{```\xFF```}}, but you can change it to anything you like.  Be aware that the token you choose may be subject to Wiki rules, so be sure to choose something that the rules don't affect.

For example:

<php>
	// use a nullchar delimiter
	$options['delim'] = '\x00');
</php>



+++ {{interwiki}}

The {{interwiki}} option is an associative array where the key is an !InterWiki site name, and the value is the URL for pages at that site.  For example, if you want to refer to the !MeatBall site in your pages using the !InterWiki rule, you need to pass this option.

<php>
	// name and link to the MeatBall Wiki
	$options['interwiki'] = array(
		'MeatBall' => 'http://www.usemod.com/cgi-bin/mb.pl?'
	);
</php>




+++ {{new_text}}

The {{new_text}} option is a string.  If your wiki source text refers to a page that does not exist (as provided by the {{pages}} option) then it is considered to be a "new" page; thus, the Text_Wiki will link to a web page for creating new pages.

The {{new_text}} option specifies the literal linked text to be appended to the page name on display.  It can be text proper, or an HTML image tag, or anything else.

See also the {{new_url}} option.

<php>
	// use a question mark and a space
	$options['new_text'] = '? ';
	
	// use an image
	$options['new_text'] = '<img src="http://example.com/newpage.gif />';
</php>



+++ {{new_url}}

The {{new_url}} option is a string.  If your wiki source text refers to a page that does not exist (as provided by the {{pages}} option) then it is considered to be a "new" page; thus, Text_Wiki will link to a web page for creating new pages.

The {{new_url}} specifies the URL to the application-specific page creation utility.

See also the {{new_text}} option.

<php>
	// link to the new-page script
	$options['new_url']  = 'http://example.com/newpage.php?page=';
</php>


+++ {{pages}}

The {{pages}} option is a sequential array where each element is the name of page in the Wiki.  See also the {{view_url}} option.

<php>
	// only three pages in this wiki
	$options['pages'] = array(
		'HomePage',
		'SamplePage',
		'TokenRuleKeys'
	)
</php>


+++ {{rules}}

This option is a associative array; it specifies the rule names to apply to wiki source text.  The key is the token name to be used when idenitfying text marked by that rule, and the value is the file name of the rule class.  (Previously, rule class names and rule file names had to adhere to a specific convention; that is no longer necessary.)

<php>
	$options['rules'] = array(
        'prefilter'   => 'Text/Wiki/Rule/prefilter.php',
        'delimiter'   => 'Text/Wiki/Rule/delimiter.php',
        'code'        => 'Text/Wiki/Rule/code.php',
        'phpcode'     => 'Text/Wiki/Rule/phpcode.php',
        'html'        => 'Text/Wiki/Rule/html.php',
        'heading'     => 'Text/Wiki/Rule/heading.php',
        'horiz'       => 'Text/Wiki/Rule/horiz.php',
        'blockquote'  => 'Text/Wiki/Rule/blockquote.php',
        'deflist'     => 'Text/Wiki/Rule/deflist.php',
        'table'       => 'Text/Wiki/Rule/table.php',
        'list'        => 'Text/Wiki/Rule/list.php',
        'toc'         => 'Text/Wiki/Rule/toc.php',
        'paragraph'   => 'Text/Wiki/Rule/paragraph.php',
        'raw'         => 'Text/Wiki/Rule/raw.php',
        'phplookup'   => 'Text/Wiki/Rule/phplookup.php',
        'url'         => 'Text/Wiki/Rule/url.php',
        'interwiki'   => 'Text/Wiki/Rule/interwiki.php',
        'freelink'    => 'Text/Wiki/Rule/freelink.php',
        'wikilink'    => 'Text/Wiki/Rule/wikilink.php',
        'strong'      => 'Text/Wiki/Rule/strong.php',
        'bold'        => 'Text/Wiki/Rule/bold.php',
        'emphasis'    => 'Text/Wiki/Rule/emphasis.php',
        'italic'      => 'Text/Wiki/Rule/italic.php',
        'tt'          => 'Text/Wiki/Rule/tt.php',
        'superscript' => 'Text/Wiki/Rule/superscript.php',
        'revise'      => 'Text/Wiki/Rule/revise.php',
        'entities'    => 'Text/Wiki/Rule/entities.php',
        'tighten'     => 'Text/Wiki/Rule/tighten.php'
	);
</php>

The above is the default list of rules.  If you want to use your own (for example) {{wikilink}} rule to override the default rule, you can replace the array element with the path to your modified rule:

<php>
	// [snip] set up $options
	$wiki =& new Text_Wiki($options);
	
	// replace the 'wikilink' default rule
	$wiki->rules['wikilink'] = '/path/to/my/rules/my_wikilink.php';
</php>


+++ {{view_url}}

The {{view_url}} option is a string that specifies the URL to the application-specific page viewing utility.  If your wiki source text refers to a page listed in the {{pages}} option array, the Text_Wiki rules for 'wiki' and 'wikifreelink' will create a link to that page using this URL.

<php>
	// link to the page-viewing script
	$options['view_url'] = 'http://example.com/wiki.php?page=';
</php>
