diff options
author | Kevin Chabowski <kevin@kch42.de> | 2013-10-20 16:21:03 +0200 |
---|---|---|
committer | Kevin Chabowski <kevin@kch42.de> | 2013-10-20 16:21:03 +0200 |
commit | f18070853c4c82247ec8e153dbf2a4a1b43e730d (patch) | |
tree | c9245a4c867c8a431166233029c8cfc065805545 /stupid_template_engine.php | |
parent | 6ca501f5992f4ce11fb0e6dc61769d2e9614232c (diff) | |
download | ste-f18070853c4c82247ec8e153dbf2a4a1b43e730d.tar.gz ste-f18070853c4c82247ec8e153dbf2a4a1b43e730d.tar.bz2 ste-f18070853c4c82247ec8e153dbf2a4a1b43e730d.zip |
Added function to tidy up the AST
Diffstat (limited to 'stupid_template_engine.php')
-rw-r--r-- | stupid_template_engine.php | 45 |
1 files changed, 44 insertions, 1 deletions
diff --git a/stupid_template_engine.php b/stupid_template_engine.php index c67b1fd..840e0ce 100644 --- a/stupid_template_engine.php +++ b/stupid_template_engine.php @@ -211,7 +211,50 @@ class Parser { self::ESCAPES_DEFAULT, /* Escapes */ self::PARSE_SHORT | self::PARSE_TAG /* Flags */ ); - return $res[0]; + return self::tidyup_ast($res[0]); + } + + private static function tidyup_ast($ast) { + $out = array(); + + $prevtext = NULL; + $first = 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; + } + + $out[] = $node; + + $first = false; + continue; + } + + if($first) { + $node->text = ltrim($node->text); + } + + if($prevtext !== NULL) { + $prevtext->text .= $node->text; + } else { + $prevtext = $node; + } + + $first = false; + } + + if(($prevtext !== NULL) && ($prevtext->text != "")) { + $out[] = $prevtext; + } + return $out; } private function parse_text($escapes, $flags, $breakon = NULL, $separator = NULL, $nullaction = NULL, $opentag = NULL, $openedat = -1) { |