저자: Asmir Mustafic (goetas@gmail.com)
예제 #1
0
파일: EmbedNode.php 프로젝트: goetas/twital
 public function visit(\DOMElement $node, Compiler $context)
 {
     if ($node->hasAttribute("from-exp")) {
         $filename = $node->getAttribute("from-exp");
     } elseif ($node->hasAttribute("from")) {
         $filename = '"' . $node->getAttribute("from") . '"';
     } else {
         throw new Exception("The 'from' or 'from-exp' attribute is required");
     }
     // remove any non-element node
     foreach (iterator_to_array($node->childNodes) as $child) {
         if (!$child instanceof \DOMElement) {
             $child->parentNode->removeChild($child);
         }
     }
     $context->compileChilds($node);
     $code = "embed {$filename}";
     if ($node->hasAttribute("ignore-missing") && $node->hasAttribute("ignore-missing") !== false) {
         $code .= " ignore missing";
     }
     if ($node->hasAttribute("with")) {
         $code .= " with " . $node->getAttribute("with");
     }
     if ($node->hasAttribute("only") && $node->getAttribute("only") !== "false") {
         $code .= " only";
     }
     $ext = $context->createControlNode($code);
     $set = iterator_to_array($node->childNodes);
     $n = $node->ownerDocument->createTextNode("\n");
     array_unshift($set, $n);
     array_unshift($set, $ext);
     $set[] = $context->createControlNode("endembed");
     DOMHelper::replaceWithSet($node, $set);
 }
예제 #2
0
 public function visit(\DOMElement $node, Compiler $context)
 {
     $code = "include ";
     if ($node->hasAttribute("from-exp")) {
         $code .= $node->getAttribute("from-exp");
     } elseif ($node->hasAttribute("from")) {
         $code .= '"' . $node->getAttribute("from") . '"';
     } else {
         throw new Exception("The 'from' or 'from-exp' attribute is required");
     }
     if ($node->hasAttribute("ignore-missing") && $node->getAttribute("ignore-missing") !== "false") {
         $code .= " ignore missing";
     }
     if ($node->hasAttribute("with")) {
         $code .= " with " . $node->getAttribute("with");
     }
     if ($node->hasAttribute("only") && $node->getAttribute("only") !== "false") {
         $code .= " only";
     }
     if ($node->hasAttribute("sandboxed") && $node->getAttribute("sandboxed") !== "false") {
         $code .= " sandboxed = true";
     }
     $pi = $context->createControlNode($code);
     $node->parentNode->replaceChild($pi, $node);
 }
예제 #3
0
 public function visit(\DOMAttr $att, Compiler $context)
 {
     $node = $att->ownerElement;
     DOMHelper::removeChilds($node);
     $pi = $context->createPrintNode(html_entity_decode($att->value));
     $node->appendChild($pi);
     $node->removeAttributeNode($att);
 }
예제 #4
0
 public function visit(\DOMAttr $att, Compiler $context)
 {
     $node = $att->ownerElement;
     $pi = $context->createControlNode("block " . $att->value);
     $node->parentNode->insertBefore($pi, $node);
     $pi = $context->createControlNode("endblock");
     $node->parentNode->insertBefore($pi, $node->nextSibling);
     $node->removeAttributeNode($att);
 }
예제 #5
0
 public function visit(\DOMAttr $att, Compiler $context)
 {
     $node = $att->ownerElement;
     $pi = $context->createControlNode("set " . html_entity_decode($att->value));
     $node->parentNode->insertBefore($pi, $node);
     $pi = $context->createControlNode("endset");
     $node->parentNode->insertBefore($pi, $node->nextSibling);
     // insert after
     $node->removeAttributeNode($att);
 }
예제 #6
0
 public function visit(\DOMAttr $att, Compiler $context)
 {
     $node = $att->ownerElement;
     $sets = ParserHelper::staticSplitExpression(html_entity_decode($att->value), ",");
     foreach ($sets as $set) {
         $pi = $context->createControlNode("set " . $set);
         $node->parentNode->insertBefore($pi, $node);
     }
     $node->removeAttributeNode($att);
 }
예제 #7
0
파일: MacroNode.php 프로젝트: goetas/twital
 public function visit(\DOMElement $node, Compiler $context)
 {
     if (!$node->hasAttribute("name")) {
         throw new Exception("Name attribute is required");
     }
     $context->compileChilds($node);
     $set = iterator_to_array($node->childNodes);
     $start = $context->createControlNode("macro " . $node->getAttribute("name") . "(" . $node->getAttribute("args") . ")");
     array_unshift($set, $start);
     $set[] = $context->createControlNode("endmacro");
     DOMHelper::replaceWithSet($node, $set);
 }
예제 #8
0
 public function visit(\DOMAttr $att, Compiler $context)
 {
     $node = $att->ownerElement;
     if (!($prev = IfAttribute::findPrevElement($node))) {
         throw new Exception("The attribute 'elseif' must be the very next sibling of an 'if' of 'elseif' attribute");
     }
     $pi = $context->createControlNode("else");
     $node->parentNode->insertBefore($pi, $node);
     $pi = $context->createControlNode("endif");
     $node->parentNode->insertBefore($pi, $node->nextSibling);
     $node->removeAttributeNode($att);
 }
예제 #9
0
 public function visit(\DOMAttr $att, Compiler $context)
 {
     $node = $att->ownerElement;
     $pi = $context->createControlNode("block " . $att->value);
     if ($node->firstChild) {
         $node->insertBefore($pi, $node->firstChild);
     } else {
         $node->appendChild($pi);
     }
     $pi = $context->createControlNode("endblock");
     $node->appendChild($pi);
     $node->removeAttributeNode($att);
 }
예제 #10
0
파일: UseNode.php 프로젝트: goetas/twital
 public function visit(\DOMElement $node, Compiler $context)
 {
     $code = "use ";
     if ($node->hasAttribute("from")) {
         $code .= '"' . $node->getAttribute("from") . '"';
     } else {
         throw new Exception("The 'from' attribute is required");
     }
     if ($node->hasAttribute("with")) {
         $code .= " with " . $node->getAttribute("with");
     }
     $pi = $context->createControlNode($code);
     $node->parentNode->replaceChild($pi, $node);
 }
예제 #11
0
 public function visit(\DOMAttr $att, Compiler $context)
 {
     $node = $att->ownerElement;
     $pi = $context->createControlNode("if " . html_entity_decode($att->value));
     $node->parentNode->insertBefore($pi, $node);
     if (!($nextElement = self::findNextElement($node)) || !$nextElement->hasAttributeNS(Twital::NS, 'elseif') && !$nextElement->hasAttributeNS(Twital::NS, 'else')) {
         $pi = $context->createControlNode("endif");
         $node->parentNode->insertBefore($pi, $node->nextSibling);
         // insert after
     } else {
         self::removeWhitespace($node);
     }
     $node->removeAttributeNode($att);
 }
예제 #12
0
 public function visit(\DOMAttr $att, Compiler $context)
 {
     $node = $att->ownerElement;
     if (!($prev = IfAttribute::findPrevElement($node))) {
         throw new Exception("The attribute 'elseif' must be the very next sibling of an 'if' of 'elseif' attribute");
     }
     $pi = $context->createControlNode("elseif " . html_entity_decode($att->value));
     $node->parentNode->insertBefore($pi, $node);
     if (!($nextElement = IfAttribute::findNextElement($node)) || !$nextElement->hasAttributeNS(Twital::NS, 'elseif') && !$nextElement->hasAttributeNS(Twital::NS, 'else')) {
         $pi = $context->createControlNode("endif");
         $node->parentNode->insertBefore($pi, $node->nextSibling);
         // insert after
     } else {
         IfAttribute::removeWhitespace($node);
     }
     $node->removeAttributeNode($att);
 }
예제 #13
0
 public function visit(DOMAttr $att, Compiler $context)
 {
     $node = $att->ownerElement;
     $expessions = ParserHelper::staticSplitExpression(html_entity_decode($att->value), ",");
     $params = 'trans';
     if (isset($expessions[0]) && trim($expessions[0])) {
         $params .= " with " . $expessions[0];
     }
     if (isset($expessions[1]) && strlen($expessions[1])) {
         $params .= " from " . $expessions[1];
     }
     $start = $context->createControlNode($params);
     $end = $context->createControlNode("endtrans");
     $node->insertBefore($start, $node->firstChild);
     $node->appendChild($end);
     $node->removeAttributeNode($att);
 }
예제 #14
0
파일: BlockNode.php 프로젝트: goetas/twital
 public function visit(\DOMElement $node, Compiler $context)
 {
     if (!$node->hasAttribute("name")) {
         throw new Exception("Name attribute is required");
     }
     $sandbox = $node->ownerDocument->createElementNS(Twital::NS, "sandbox");
     $node->parentNode->insertBefore($sandbox, $node);
     $node->parentNode->removeChild($node);
     $sandbox->appendChild($node);
     $context->compileAttributes($node);
     $context->compileChilds($node);
     $start = $context->createControlNode("block " . $node->getAttribute("name"));
     $end = $context->createControlNode("endblock");
     $sandbox->insertBefore($start, $sandbox->firstChild);
     $sandbox->appendChild($end);
     DOMHelper::replaceWithSet($sandbox, iterator_to_array($sandbox->childNodes));
     DOMHelper::replaceWithSet($node, iterator_to_array($node->childNodes));
 }
예제 #15
0
 public function visit(\DOMElement $node, Compiler $context)
 {
     if ($node->hasAttribute("from-exp")) {
         $filename = $node->getAttribute("from-exp");
     } elseif ($node->hasAttribute("from")) {
         $filename = '"' . $node->getAttribute("from") . '"';
     } else {
         throw new Exception("The 'from' or 'from-exp' attribute is required");
     }
     $context->compileChilds($node);
     $ext = $context->createControlNode("extends {$filename}");
     $set = iterator_to_array($node->childNodes);
     if (count($set)) {
         $n = $node->ownerDocument->createTextNode("\n");
         array_unshift($set, $n);
     }
     array_unshift($set, $ext);
     DOMHelper::replaceWithSet($node, $set);
 }
예제 #16
0
 public function visit(\DOMElement $node, Compiler $context)
 {
     if ($node->hasAttribute("from-exp")) {
         $filename = $node->getAttribute("from-exp");
     } elseif ($node->hasAttribute("from")) {
         $filename = '"' . $node->getAttribute("from") . '"';
     } else {
         throw new Exception("The 'from' or 'from-exp' attribute is required");
     }
     if ($node->hasAttribute("as")) {
         $code = "import {$filename} as " . $node->getAttribute("as");
         $context->createControlNode("import " . ($node->getAttribute("fro-exp") ? $node->getAttribute("name-exp") : "'" . $node->getAttribute("name") . "'") . " as " . $node->getAttribute("as"));
     } elseif ($node->hasAttribute("aliases")) {
         $code = "from {$filename} import " . $node->getAttribute("aliases");
     } else {
         throw new Exception("As or Alias attribute is required");
     }
     $pi = $context->createControlNode($code);
     $node->parentNode->replaceChild($pi, $node);
 }
예제 #17
0
 public function visit(DOMAttr $att, Compiler $context)
 {
     $node = $att->ownerElement;
     $expessions = ParserHelper::staticSplitExpression($att->value, ",");
     if (!isset($expessions[0]) || !strlen($expessions[0])) {
         throw new \Exception("The count for trans-n is required");
     }
     $with = '{\'%count%\':' . $expessions[0] . '}';
     if (isset($expessions[1]) && strlen($expessions[1])) {
         $with = "{$with}|merge(" . $expessions[1] . ')';
     }
     $from = '';
     if (isset($expessions[2]) && strlen($expessions[2])) {
         $from = " from {$expessions['2']}";
     }
     $start = $context->createControlNode("transchoice " . $expessions[0] . " with {$with}" . $from);
     $end = $context->createControlNode("endtranschoice");
     $node->insertBefore($start, $node->firstChild);
     $node->appendChild($end);
     $node->removeAttributeNode($att);
 }
예제 #18
0
 public function visit(\DOMAttr $att, Compiler $context)
 {
     $node = $att->ownerElement;
     $pi = $context->createControlNode("set __tmp_omit = " . html_entity_decode($att->value));
     $node->parentNode->insertBefore($pi, $node);
     $pi = $context->createControlNode("if not __tmp_omit");
     $node->parentNode->insertBefore($pi, $node);
     $pi = $context->createControlNode("endif");
     if ($node->firstChild) {
         $node->insertBefore($pi, $node->firstChild);
     } else {
         $node->appendChild($pi);
     }
     $pi = $context->createControlNode("if not __tmp_omit");
     $node->appendChild($pi);
     $pi = $context->createControlNode("endif");
     if ($node->parentNode->nextSibling) {
         $node->parentNode->insertBefore($pi, $node->parentNode->nextSibling);
     } else {
         $node->parentNode->appendChild($pi);
     }
     $node->removeAttributeNode($att);
     if ($att->value == "true" || $att->value == "1") {
         foreach (iterator_to_array($node->attributes) as $att) {
             $node->removeAttributeNode($att);
         }
     }
     return Attribute::STOP_ATTRIBUTE;
 }
 public function visit(DOMAttr $att, Compiler $context)
 {
     $node = $att->ownerElement;
     $expressions = ParserHelper::staticSplitExpression($att->value, ",");
     $varName = self::getVarname($node);
     $parts = array();
     foreach ($expressions as $expression) {
         $attrExpr = ParserHelper::staticSplitExpression($expression, ":");
         if (!$node->hasAttribute($attrExpr[0])) {
             throw new \Exception("Can't find the attribute named '" . $attrExpr[0] . "'");
         }
         $attNode = $node->getAttributeNode($attrExpr[0]);
         $transParams = isset($attrExpr[1]) ? ParserHelper::staticSplitExpression(trim($attrExpr[1], "[]\n\r\t"), ",") : array();
         $parts[$attrExpr[0]] = "['" . addcslashes($attNode->value, "'") . "'|trans(" . (isset($transParams[0]) ? $transParams[0] : '') . (isset($transParams[1]) ? ", {$transParams['1']}" : "") . ")]";
         $node->removeAttributeNode($attNode);
     }
     $code = "set {$varName} = {$varName}|default({})|merge({" . ParserHelper::implodeKeyed(",", $parts, true) . "})";
     $node->setAttribute("__attr__", $varName);
     $pi = $context->createControlNode($code);
     $node->parentNode->insertBefore($pi, $node);
     $node->removeAttributeNode($att);
 }
예제 #20
0
 public function visit(\DOMAttr $att, Compiler $context)
 {
     $node = $att->ownerElement;
     $expressions = ParserHelper::staticSplitExpression($att->value, ",");
     $attributes = array();
     foreach ($expressions as $k => $expression) {
         $expressions[$k] = $attrExpr = self::splitAttrExpression($expression);
         $attNode = null;
         if (!isset($attributes[$attrExpr['name']])) {
             $attributes[$attrExpr['name']] = array();
         }
         if ($node->hasAttribute($attrExpr['name'])) {
             $attNode = $node->getAttributeNode($attrExpr['name']);
             $node->removeAttributeNode($attNode);
             $attributes[$attrExpr['name']][] = "'" . addcslashes($attNode->value, "'") . "'";
         }
         if ($attrExpr['test'] === "true" || $attrExpr['test'] === "1") {
             unset($expressions[$k]);
             $attributes[$attrExpr['name']][] = $attrExpr['expr'];
         }
     }
     $code = array();
     $varName = self::getVarname($node);
     $code[] = $context->createControlNode("if {$varName} is not defined");
     $code[] = $context->createControlNode("set {$varName} = {" . ParserHelper::implodeKeyedDouble(",", $attributes, true) . " }");
     $code[] = $context->createControlNode("else");
     $code[] = $context->createControlNode("set {$varName} = {$varName}|merge({" . ParserHelper::implodeKeyedDouble(",", $attributes, true) . "})");
     $code[] = $context->createControlNode("endif");
     foreach ($expressions as $attrExpr) {
         $code[] = $context->createControlNode("if {$attrExpr['test']}");
         $code[] = $context->createControlNode("set {$varName} = {$varName}|merge({ '{$attrExpr['name']}':{$varName}.{$attrExpr['name']}|merge([{$attrExpr['expr']}]) })");
         $code[] = $context->createControlNode("endif");
     }
     $this->addSpecialAttr($node, $varName, $code);
     $node->removeAttributeNode($att);
 }
예제 #21
0
파일: Twital.php 프로젝트: goetas/twital
 /**
  *
  * @param SourceAdapter $adapter
  * @param string $source
  * @return string
  */
 public function compile(SourceAdapter $adapter, $source)
 {
     $this->initExtensions();
     $sourceEvent = new SourceEvent($this, $source);
     $this->dispatcher->dispatch('compiler.pre_load', $sourceEvent);
     $template = $adapter->load($sourceEvent->getTemplate());
     $templateEvent = new TemplateEvent($this, $template);
     $this->dispatcher->dispatch('compiler.post_load', $templateEvent);
     $compiler = new Compiler($this, isset($this->options['lexer']) ? $this->options['lexer'] : array());
     $compiler->compile($templateEvent->getTemplate()->getDocument());
     $templateEvent = new TemplateEvent($this, $templateEvent->getTemplate());
     $this->dispatcher->dispatch('compiler.pre_dump', $templateEvent);
     $source = $adapter->dump($templateEvent->getTemplate());
     $sourceEvent = new SourceEvent($this, $source);
     $this->dispatcher->dispatch('compiler.post_dump', $sourceEvent);
     return $sourceEvent->getTemplate();
 }
예제 #22
0
파일: OmitNode.php 프로젝트: goetas/twital
 public function visit(\DOMElement $node, Compiler $context)
 {
     $context->compileAttributes($node);
     $context->compileChilds($node);
     DOMHelper::replaceWithSet($node, iterator_to_array($node->childNodes));
 }