Hack52.Support Wiki Text


Hack 52. Support Wiki Text

Make it easier for your customers to enter styled text into your application by supporting the Wiki syntax.

A new form of content management system for the Web, Wikis are a collection of pages, each titled with a WikiWord, which is a set of two or more capitalized words joined together without spaces. The ease with which you can install and update Wikis has made them extremely popular both on intranets and on the Internet. Perhaps the most famous Wiki is Wikipedia (http://www.wikipedia.org/). This is an encyclopedia on the Web that anyone can contribute content to by using just their web browser.

Another reason wikis are so popular is that formatting a Wiki page is a lot easier than writing the equivalent HTML code. For example, you specify a paragraph break by just typing two returnsthere is no need to add p tags. In fact, most of the time tags aren't used at all. For example, you create a bulleted list by putting an asterisk at the start of each line; this is far easier than using the equivalent ul and li tags. This hack demonstrates using the wiki-formatting PEAR module in a PHP application.

6.3.1. The Code

Save the code in Example 6-3 as index.php.

Example 6-3. The page that allows you to edit Wiki text
 <html> <body> <form method="post" action="render.php"> <textarea name="text" cols="80" rows="20"> + Header Level 1 Here's a paragraph and a link to AnotherPage.</para> * list item 1 * list item 2 Link to a NewPage like this. </textarea><br/> <input type="submit" /> </form> </body> </html> 

Then save the code in Example 6-4 as render.php.

Example 6-4. The PHP that renders the Wiki text
 <html> <body> <?php // Include the Wiki Text Pear library require_once( "Text/Wiki.php" ); // Create the Wiki object $wiki = new Text_Wiki(); // Render the text field sent to us in the form echo( $wiki->transform( $_POST["text" ], 'Xhtml' ) ); ?> </body> </html> 

6.3.2. Running the Hack

This code requires the Text_Wiki PEAR module [Hack #2]. After installing the module and creating the PHP files, navigate the browser to the index.php page shown in Figure 6-3.

Type some Wiki text into the form and click the Submit button. With the text shown in the example, the output looks like Figure 6-4.

Figure 6-3. The text input page for the Wiki text renderer


Figure 6-4. The rendered Wiki text


A complete list of the wiki formatting rules is on the Text_Wiki PEAR component home page at http://wiki.ciaweb.net/yawiki/index.php?area=Text_Wiki&page=WikiRules.


6.3.3. Hacking the Hack

The Text_Wiki module is very well architected and written. New text formatting rules can be added, and the default formatting rules can be enabled and disabled to suit the application. The enableRule() and disableRule() methods enable and disable the built-in text formatting rules. The addRule() method adds new rules to the formatting engine.



PHP Hacks
PHP Hacks: Tips & Tools For Creating Dynamic Websites
ISBN: 0596101392
EAN: 2147483647
Year: 2006
Pages: 163

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net