Skip to content

Commit

Permalink
No more change include path, check running on command line if 'cli' .…
Browse files Browse the repository at this point in the history
….. and more

also, but it's minor, the test interface is finished and I'm happy with it


git-svn-id: https://svn.php.net/repository/pear/packages/Text_Wiki/trunk@191036 c90b9560-bf6c-de11-be94-00142212c4b1
  • Loading branch information
bertrand Gugger committed Jul 19, 2005
1 parent c18de9e commit 4afd501
Showing 1 changed file with 56 additions and 29 deletions.
85 changes: 56 additions & 29 deletions doc/test_Text_Wiki.php
@@ -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$
*/
$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',
Expand All @@ -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') {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -104,7 +120,7 @@ function bldHtml($parser, $render, $source, $result, $plist, $rlist) {
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>
Expand All @@ -122,4 +138,15 @@ function bldHtml($parser, $render, $source, $result, $plist, $rlist) {
</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;
}
?>

0 comments on commit 4afd501

Please sign in to comment.