From 80c76caffa50ce7c9a520ff2773d1c98b9100875 Mon Sep 17 00:00:00 2001 From: Kevin Chabowski Date: Fri, 25 Oct 2013 00:17:53 +0200 Subject: Better handling of newslines and whitespaces --- stupid_template_engine.php | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/stupid_template_engine.php b/stupid_template_engine.php index 58b327d..0eef489 100644 --- a/stupid_template_engine.php +++ b/stupid_template_engine.php @@ -243,23 +243,26 @@ class Parser { $out = array(); $prevtext = NULL; - $wastag = true; + $first = true; + foreach($ast as $node) { if($node instanceof TextNode) { - if($wastag) { - $node->text = ltrim($node->text); - } - if($prevtext === NULL) { $prevtext = $node; } else { $prevtext->text .= $node->text; } } else { - if(($prevtext !== NULL) && ($prevtext->text != "")) { - $out[] = $prevtext; + if($prevtext !== NULL) { + if($first) { + $prevtext->text = ltrim($prevtext->text); + } + if($prevtext->text != "") { + $out[] = $prevtext; + } } $prevtext = NULL; + $first = false; if($node instanceof TagNode) { $node->sub = self::tidyup_ast($node->sub); @@ -273,15 +276,20 @@ class Parser { } unset($v); } + $out[] = $node; } - - $wastag = $node instanceof TagNode; } - if(($prevtext !== NULL) && ($prevtext->text != "")) { - $out[] = $prevtext; + if($prevtext !== NULL) { + if($first) { + $prevtext->text = ltrim($prevtext->text); + } + if($prevtext->text != "") { + $out[] = $prevtext; + } } + return $out; } -- cgit v1.2.3-70-g09d2