A node has children and always tries to reference its parents It also has some utility methods to work with those nodes
Author: Torben Koehn (torben@talesoft.codes)
Author: Talesoft (info@talesoft.codes)
Exemplo n.º 1
0
 /**
  * Compiles a while-loop into PHTML.
  *
  * Notice that if it has no children, we assume it's a do/while loop
  * and don't print brackets
  *
  * @param Node $node the while-node to compile
  *
  * @return string The compiled PHTML
  */
 protected function compileWhile(Node $node)
 {
     $subject = $node->subject;
     if ($this->isVariable($subject)) {
         $subject = "isset({$subject}) ? {$subject} : null";
     }
     $hasChildren = count($node->children) > 0;
     $isDoWhile = $node->prev() && $node->prev()->type === 'do';
     if (!$hasChildren && !$isDoWhile) {
         $this->throwException('A while-loop without children you loop through is not valid if' . ' there\'s no do-statement before it.', $node);
     } else {
         if ($isDoWhile && $hasChildren) {
             $this->throwException('In a do-while statement the while-part shouldn\'t have any children', $node);
         }
     }
     $phtml = $this->createCode("while ({$subject})" . ($hasChildren ? ' {' : ''), $isDoWhile ? ' ' : '<?php ') . $this->newLine();
     if ($hasChildren) {
         $phtml .= $this->compileChildren($node->children) . $this->newLine();
         $phtml .= $this->indent() . $this->createCode('}') . $this->newLine();
     }
     return $phtml;
 }
Exemplo n.º 2
0
 /**
  * Handles a <text>-token and parses it into a text-node.
  *
  * If there's a $_current element, we append it to that element,
  * if not, it becomes the $_current element
  *
  * @param array $token the <text>-token
  */
 protected function handleText(array $token)
 {
     $node = $this->createNode('text', $token);
     $node->value = $token['value'];
     $node->level = $token['level'];
     $node->escaped = $token['escaped'];
     if ($this->current) {
         $this->current->append($node);
     } else {
         $this->current = $node;
     }
 }
Exemplo n.º 3
0
 /**
  * A plain-text filter that just corrects indentation and new-lines.
  *
  * @param Node   $node    the node to be wrapped
  * @param string $indent  the indentation to use on each child
  * @param string $newLine the new-line to append after each line
  *
  * @return string the wrapped PTHML-string
  */
 public static function filterPlain(Node $node, $indent, $newLine)
 {
     $text = trim($node->text());
     //Normalize newlines to $newLine and append our indent
     $i = 0;
     return implode($newLine, array_map(function ($value) use($indent, $newLine, &$i) {
         if (strlen($indent) < 1 && $i++ !== 0 && strlen($value) > 0) {
             //Make sure we separate with at least one white-space
             $indent = ' ';
         }
         return $indent . trim($value);
     }, explode("\n", $text)));
 }
Exemplo n.º 4
0
 /**
  * Compiles the SASS content to CSS
  *
  * @param Node   $node    the node to be compiled
  * @param string $indent  the indentation to use on each child
  * @param string $newLine the new-line to append after each line
  *
  * @return string the wrapped SASS-CSS-string
  * @throws Compiler\Exception when the Stylus package is not installed
  */
 public static function filterSass(Node $node, $indent, $newLine)
 {
     if (!class_exists('Leafo\\ScssPhp\\Compiler')) {
         throw new Compiler\Exception("Failed to compile SASS: " . "Please install the leafo/scssphp composer package");
     }
     $sass = new \Leafo\ScssPhp\Compiler();
     $css = $sass->compile($node->text());
     return '<style>' . $newLine . $indent . $css . $newLine . $indent . '</style>';
 }
Exemplo n.º 5
0
 /**
  * Compiles a conditional, either if, elseif, else if or else into PHTML.
  *
  * @param Node $node the conditional node to compile
  *
  * @return string The compiled PHTML
  */
 protected function compileConditional(Node $node)
 {
     $type = $node->conditionType;
     $subject = $node->subject;
     if ($subject === 'block') {
         $subject = '$__block';
     }
     if ($this->isVariable($subject)) {
         $subject = "isset({$subject}) ? {$subject} : false";
     }
     if ($type === 'unless') {
         $type = 'if';
         $subject = "!({$subject})";
     }
     $isPrevConditional = $node->prev() && $node->prev()->type === 'conditional';
     $isNextConditional = $node->next() && $node->next()->type === 'conditional' && $node->next()->conditionType !== 'if';
     $prefix = $isPrevConditional ? '' : '<?php ';
     $suffix = $isNextConditional ? '' : '?>';
     $phtml = $type === 'else' ? $this->createCode(' else {', $prefix) : $this->createCode("{$type} ({$subject}) {", $prefix);
     $phtml .= $this->compileChildren($node->children);
     $phtml .= $this->newLine() . $this->indent() . $this->createCode("}", '<?php ', $suffix);
     return $phtml;
 }