Text_Wiki
[ class tree: Text_Wiki ] [ index: Text_Wiki ] [ all elements ]

Source for file test_Text_Wiki.php

Documentation is available at test_Text_Wiki.php

  1. <?php
  2. ini_set('display_errors', true);
  3. // needed for error checking
  4. require_once 'PEAR.php';
  5. // base class
  6. require_once 'Text/Wiki.php';
  7. /**
  8.  * Eventually set an include path if all parsers/renderers not installed
  9.  * $Id$
  10.  */
  11. $parser = $render = $source = '';
  12. $plist = array('Default', 'BBCode', 'Cowiki', 'Doku', 'Mediawiki', 'Tiki', 'X-choice', 'Creole');
  13. $rlist = array('Xhtml', 'Plain', 'Latex', 'Cowiki', 'Doku', 'Tiki', 'Ooosxw', 'Pdf', 'Docbook', 'Creole');
  14.  
  15.     echo "\nWARNING: to this script work properly you have to set magic_quotes_gpc = Off on your php.ini file\n";
  16. }
  17.  
  18. /**
  19.  * Here we need to know if we are running from command line or from web
  20.  * That runs anyway: if (isset($_SERVER['SERVER_NAME'])) {
  21.  * but have some o(l|d)d compatibility problem ...
  22.  */
  23. if (in_array(php_sapi_name(), array('cli', 'cgi'))) {
  24.     $html = false;
  25.     $parser = isset($_SERVER['argv'][1]) ? $_SERVER['argv'][1] : 'BBCode';
  26.     $render = isset($_SERVER['argv'][2]) ? $_SERVER['argv'][2] : 'Xhtml';
  27.     if (!isset($_SERVER['argv'][3]) or !is_readable($sou = $_SERVER['argv'][3])) {
  28.         die("Enter a text file to be processed as 3d argument\n First and second are parser and renderer\n");
  29.     }
  30.     $source = file_get_contents ($sou);
  31. } else {
  32.     $html = true;
  33.     $elist = findExamples(dirname(__FILE__));
  34.     if (isset($_REQUEST['example'])
  35.         && in_array($_REQUEST['exchoice'], $elist)) {
  36.         $_REQUEST['source'] = file_get_contents ($_REQUEST['exchoice']);
  37.         if (preg_match('#(\b'.implode('\b|\b', $plist).'\b)#',
  38.                          $_REQUEST['source'], $match)) {
  39.             $_REQUEST['parser'] = $match[1];
  40.         }
  41.         $_REQUEST['translate'] = true;
  42.     }
  43.     foreach (array('parser'=>$plist[0], 'render'=>$rlist[0],
  44.                    'exchoice'=>($elist ? $elist[0] : ''), 'source'=>'')
  45.              as $fld=>$def) {
  46.         if(!isset($_REQUEST[$fld])) {
  47.             $_REQUEST[$fld] = $def;
  48.         }
  49.         $$fld = $_REQUEST[$fld];
  50.     }
  51.     if (!isset($_REQUEST['translate'])) {
  52.         echo bldHtml('', $plist, $rlist, $elist);
  53.         die();
  54.     }
  55. }
  56.  
  57. // instantiate a Text_Wiki object from the given class
  58. $wiki = & Text_Wiki::singleton($parser);
  59.  
  60. // If you want to include rules, use
  61. //$wiki = & Text_Wiki::singleton($parser, $rules);
  62.  
  63. // If you want to get a new copy of the class use factory
  64. //$wiki =& Text_Wiki::factory($parser);
  65.  
  66. //print "<pre>\n";
  67. //print_r($wiki);
  68. //print "</pre>\n";
  69.  
  70. // when rendering XHTML, make sure wiki links point to a
  71. // specific base URL
  72. //$wiki->setRenderConf('xhtml', 'wikilink', 'view_url',
  73. // 'http://example.com/view.php?page=');
  74.  
  75. // set an array of pages that exist in the wiki
  76. // and tell the XHTML renderer about them
  77. //$pages = array('HomePage', 'AnotherPage', 'SomeOtherPage');
  78.  
  79. $wiki->setRenderConf('xhtml', 'code', 'css_filename', 'codefilename');
  80.  
  81. // transform the wiki text into given rendering
  82. $result = $wiki->transform($source, $render);
  83.  
  84. // display the transformed text
  85. if ($html) {
  86.     echo bldHtml($result, $plist, $rlist, $elist);
  87. } else {
  88.     if (PEAR::isError($result)) {
  89.         var_dump($result);
  90.     } else {
  91.         echo $result;
  92.     }
  93. }
  94. function bldOpt($name, $list) {
  95.     $ret = '';
  96.     foreach($list as $opt) {
  97.           $ret .= "<option value='{$opt}'".
  98.             ($opt == $_REQUEST[$name]? " selected" : "").
  99.             ">{$opt}</option>\n";
  100.     }
  101.     return $ret;
  102. }
  103. function bldHtml($result, $plist, $rlist, $elist) {
  104.     $optparser = bldOpt('parser', $plist);
  105.     $optrender = bldOpt('render', $rlist);
  106.     $optexample = bldOpt('exchoice', $elist);
  107.     if (PEAR::isError($result)) {
  108.         $hresult = '<span class="error">' .
  109.             nl2br(htmlentities($result->toString ())) . '</span>';
  110.         $result = '';
  111.     } else {
  112.         $hresult = nl2br(htmlentities($result));
  113.     }
  114.     if ($_REQUEST['render'] != 'Xhtml') {
  115.         $result = '';
  116.     }
  117.     $_REQUEST['source'] = htmlspecialchars($_REQUEST['source']);
  118.     return <<<EOT
  119. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  120. <html>
  121.  
  122. <head>
  123.   <title>PEAR::Text_Wiki Demo</title>
  124.   <meta name="AUTHOR" content="bertrand Gugger / Toggg">
  125.   <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  126.   <meta name="KEYWORDS" content="PEAR, Wiki, Parse, Render, Convert, PHP, BBCode, Xhtml, Plain, Latex">
  127.   <style type="text/css">
  128.     blockquote, pre {
  129.         border: solid;
  130.     }
  131.     .codefilename {
  132.         color: blue;
  133.         background-color:orange;
  134.         text-decoration: underline;
  135.     }
  136.     .error {
  137.         color: red;
  138.     }
  139.   </style>
  140. </head>
  141. <body>
  142. <h3>PEAR::Text_Wiki Demo</h3>
  143. <div style="float: left;">
  144. <FORM method="post">
  145. Translate from
  146. <SELECT name="parser">{$optparser}</SELECT>
  147.  to
  148. <SELECT name="render">{$optrender}</SELECT>
  149.  <INPUT type="submit" name="translate" value="translate" />
  150. <br />
  151. <textarea name="source" cols="60" rows="25">{$_REQUEST['source']}</textarea>
  152. <br />
  153. <h4> Or choose
  154. <SELECT name="exchoice">{$optexample}</SELECT>
  155.  and
  156. <INPUT type="submit" name="example" value="Load example" />
  157. </h4>
  158. </FORM>
  159. </div>
  160. <div style="float: down; font-family: monospace;">
  161. {$hresult}
  162. </div>
  163. <div>
  164. {$result}
  165. </div>
  166. </body>
  167. </html>
  168. EOT;
  169. }
  170. function findExamples($dir=null) {
  171.     $ret = array();
  172.     $dh=opendir($dir? $dir : '.');
  173.     while ($subfil = readdir($dh)) {
  174.         if (!is_dir($subfil) && is_readable($subfil)
  175.             && (substr($subfil, -4) == '.txt')) {
  176.             $ret[] = $subfil;
  177.         }
  178.     }
  179.     closedir($dh);
  180.     return $ret;
  181. }
  182. ?>

Documentation generated on Tue, 12 Mar 2019 21:49:22 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.