From b2c464d6e3b4fa91eca3a80985150ee100713b9f Mon Sep 17 00:00:00 2001 From: Kevin Chabowski Date: Sun, 20 Oct 2013 22:14:18 +0200 Subject: tidyup_ast improved --- stupid_template_engine.php | 53 ++++++++++++++++++++++++---------------------- 1 file changed, 28 insertions(+), 25 deletions(-) diff --git a/stupid_template_engine.php b/stupid_template_engine.php index 1cd21fc..386f760 100644 --- a/stupid_template_engine.php +++ b/stupid_template_engine.php @@ -218,37 +218,40 @@ class Parser { $out = array(); $prevtext = NULL; - $first = true; + $wastag = true; foreach($ast as $node) { - if($node instanceof TagNode) { - $node->sub = self::tidyup_ast($node->sub); - } - - if(!($node instanceof TextNode)) { - if($prevtext !== NULL) { - if($prevtext->text != "") { - $out[] = $prevtext; - } - $prevtext = NULL; + if($node instanceof TextNode) { + if($wastag) { + $node->text = ltrim($node->text); } - $out[] = $node; - - $first = false; - continue; - } - - if($first) { - $node->text = ltrim($node->text); - } - - if($prevtext !== NULL) { - $prevtext->text .= $node->text; + if($prevtext === NULL) { + $prevtext = $node; + } else { + $prevtext->text .= $node->text; + } } else { - $prevtext = $node; + if(($prevtext !== NULL) && ($prevtext->text != "")) { + $out[] = $prevtext; + } + $prevtext = NULL; + + if($node instanceof TagNode) { + $node->sub = self::tidyup_ast($node->sub); + foreach($node->params as $k => &$v) { + $v = self::tidyup_ast($v); + } + unset($v); + } else { /* VariableNode */ + foreach($node->arrayfields as &$v) { + $v = self::tidyup_ast($v); + } + unset($v); + } + $out[] = $node; } - $first = false; + $wastag = $node instanceof TagNode; } if(($prevtext !== NULL) && ($prevtext->text != "")) { -- cgit v1.2.3-70-g09d2