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

Documentation generated on Mon, 11 Mar 2019 14:37:33 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.