cvs: pear /Text_Wiki/doc test_Text_Wiki.php

From: Date: Tue, 19 Jul 2005 20:27:19 +0000
Subject: cvs: pear /Text_Wiki/doc test_Text_Wiki.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-33159@lists.php.net to get a copy of this message
toggg Tue Jul 19 16:27:19 2005 EDT Modified files: /pear/Text_Wiki/doc test_Text_Wiki.php Log: No more change include path, check running on command line if 'cli' ... and more also, but it's minor, the test interface is finished and I'm happy with it http://cvs.php.net/diff.php/pear/Text_Wiki/doc/test_Text_Wiki.php?r1=1.3&r2=1.4&ty=u Index: pear/Text_Wiki/doc/test_Text_Wiki.php diff -u pear/Text_Wiki/doc/test_Text_Wiki.php:1.3 pear/Text_Wiki/doc/test_Text_Wiki.php:1.4 --- pear/Text_Wiki/doc/test_Text_Wiki.php:1.3 Mon Jul 18 12:35:50 2005 +++ pear/Text_Wiki/doc/test_Text_Wiki.php Tue Jul 19 16:27:18 2005 @@ -1,10 +1,28 @@ <?php -set_include_path( realpath(dirname(__FILE__).'/../'). PATH_SEPARATOR . get_include_path() ); +/** + * Eventually set an include path if all parsers/renderers not installed + * $Id: test_Text_Wiki.php,v 1.4 2005/07/19 20:27:18 toggg Exp $ + */ $parser = $render = $source = ''; $plist = array('Default', 'BBCode'); -$rlist = array('Xhtml', 'Plain', 'Latex'); -if ($_SERVER['PHP_SELF']) { +$rlist = array('Xhtml', 'Plain', 'Latex');php_sapi_name(); + +/** + * Here we need to know if we are running from command line or from web + * That runs anyway: if (isset($_SERVER['SERVER_NAME'])) { + * but have some o(l|d)d compatibility problem ... + */ +if (in_array(php_sapi_name(), array('cli', 'cgi'))) { + $html = false; + $parser = isset($_SERVER['argv'][1]) ? $_SERVER['argv'][1] : 'BBCode'; + $render = isset($_SERVER['argv'][2]) ? $_SERVER['argv'][2] : 'Xhtml'; + if (!isset($_SERVER['argv'][3]) or !is_readable($sou = $_SERVER['argv'][3])) { + die("Enter a text file to be processed as 3d argument\n First and second are parser and renderer\n"); + } + $source = file_get_contents ($sou); +} else { $html = true; + $elist = findExamples(dirname(__FILE__)); if (isset($_REQUEST['example'])) { $_REQUEST['source'] = file_get_contents ($_REQUEST['exchoice']); if (preg_match('#(\b'.implode('\b|\b', $plist).'\b)#i', @@ -13,21 +31,18 @@ } $_REQUEST['translate'] = true; } - $parser = isset($_REQUEST['parser']) ? $_REQUEST['parser'] : 'BBCode'; - $render = isset($_REQUEST['render']) ? $_REQUEST['render'] : 'Xhtml'; - $source = isset($_REQUEST['source']) ? $_REQUEST['source'] : ''; + foreach (array('parser'=>$plist[0], 'render'=>$rlist[0], + 'exchoice'=>($elist ? $elist[0] : ''), 'source'=>'') + as $fld=>$def) { + if(!isset($_REQUEST[$fld])) { + $_REQUEST[$fld] = $def; + } + $$fld = $_REQUEST[$fld]; + } if (!isset($_REQUEST['translate'])) { - echo bldHtml($parser, $render, $source, '', $plist, $rlist); + echo bldHtml('', $plist, $rlist, $elist); die(); } -} else { - $html = false; - $parser = isset($_SERVER['argv'][1]) ? $_SERVER['argv'][1] : 'BBCode'; - $render = isset($_SERVER['argv'][2]) ? $_SERVER['argv'][2] : 'Xhtml'; - if (!isset($_SERVER['argv'][3]) or !is_readable($sou = $_SERVER['argv'][3])) { - die("Enter a text file to be processed as 3d argument\n First and second are parser and renderer\n"); - } - $source = file_get_contents ($sou); } // load the class file if ($parser != 'Default') { @@ -57,24 +72,25 @@ // display the transformed text if ($html) { - echo bldHtml($parser, $render, $source, $result, $plist, $rlist); + echo bldHtml($result, $plist, $rlist, $elist); } else { echo $result; } -function bldHtml($parser, $render, $source, $result, $plist, $rlist) { - $optparser = $optrender = $optexample = ''; - foreach($plist as $opt) { - $optparser .= "<option value='{$opt}'". - ($opt == $parser? " selected" : ""). +function bldOpt($name, $list) { + $ret = ''; + foreach($list as $opt) { + $ret .= "<option value='{$opt}'". + ($opt == $_REQUEST[$name]? " selected" : ""). ">{$opt}</option>\n"; } - foreach($rlist as $opt) { - $optrender .= "<option value='{$opt}'". - ($opt == $render? " selected" : ""). - ">{$opt}</option>\n"; - } - $hresult = htmlentities($result); - if ($render != 'Xhtml') { + return $ret; +} +function bldHtml($result, $plist, $rlist, $elist) { + $optparser = bldOpt('parser', $plist); + $optrender = bldOpt('render', $rlist); + $optexample = bldOpt('exchoice', $elist); + $hresult = nl2br(htmlentities($result)); + if ($_REQUEST['render'] != 'Xhtml') { $result = ''; } return <<<EOT @@ -104,7 +120,7 @@ to <SELECT name="render">{$optrender}</SELECT> <br /> -<textarea name="source" cols="80" rows="25">{$source}</textarea> +<textarea name="source" cols="80" rows="25">{$_REQUEST['source']}</textarea> <br /> <INPUT type="submit" name="translate" value="translate"> or choose <SELECT name="exchoice">{$optexample}</SELECT> @@ -122,4 +138,15 @@ </html> EOT; } +function findExamples($dir=null) { + $ret = array(); + $dh=opendir($dir? $dir : '.'); + while ($subfil = readdir($dh)) { + if (!is_dir($subfil) && (substr($subfil, -4) == '.txt')) { + $ret[] = $subfil; + } + } + closedir($dh); + return $ret; +} ?>

« previous php.pear.cvs (#33159) next »