$ar = preg_split('/(\{[^\}]+})/', $contents, -1, PREG_SPLIT_DELIM_CAPTURE);
//echo '<PRE>' . htmlspecialchars(print_r($ar,true));
- $ret = array();
+ $out= array();
- $ret[] = "var $name = function(t) {\n var ret=[];\n";
+ $head = "var $name = function(t) {\n var ret=[];\n";
$funcs = array();
// do not allow nested functions..?
$fstart = 0;
$indent = 1;
+
+ $ret = &$out;
foreach($ar as $item) {
$in = str_repeat(" ", $indent);
$indent--;
$in = str_repeat(" ", $indent);
$ret[] = $in . "}";
+ if ($fstart == $indent) {
+ $ret = &$out;
+ }
continue;
case (substr($item,1,7) == 'return:'):
continue;
case (substr($item,1,9) == 'function:'):
+ $fstart = $indent;
$indent++;
+ $ret = &$funct;
$def = substr($item,10,-1) ;
list($name,$body) = explode('(', $def, 2);
}
$in = str_repeat(" ", $indent);
$ret[] = $in . "return ret.join('');\n}\n";
- return implode("\n",$ret);
- echo '<PRE>' . htmlspecialchars(implode("\n",$ret));
+ return $head . implode("\n",$funcs) . "\n" .implode("\n",$out) ;
+ //echo '<PRE>' . htmlspecialchars(implode("\n",$ret));