Exemplo n.º 1
0
 /**
  * Create a tree from the tokenized text
  *
  * @return void
  */
 protected function _createTree()
 {
     $inside = true;
     foreach ($this->_tokens as $key => $token) {
         // first check if the token is a stopper
         if ($this->_isStopper($token, $this->_current)) {
             if ($this->_current->getName() == 'li') {
                 // list items are handled differently
                 if (isset($this->_tokens[$key + 1]) && $this->_tokens[$key + 1]['type'] == Markup\Token::TYPE_TAG && $this->_tokens[$key + 1]['name'] == 'list') {
                     // the next item is a correct tag
                     $this->_current->setStopper($token['tag']);
                     $this->_current = $this->_current->getParent();
                 } else {
                     // close the list
                     $this->_current->setStopper($token['tag']);
                     $this->_current = $this->_current->getParent()->getParent();
                     // go up in the tree until we found the end
                     while ($this->_isStopper($token, $this->_current)) {
                         $this->_current->setStopper($token['tag']);
                         $this->_current = $this->_current->getParent();
                     }
                 }
             } else {
                 // go up in the tree until we found the end of stoppers
                 while ($this->_isStopper($token, $this->_current)) {
                     $this->_current->setStopper($token['tag']);
                     if (!empty($token['attributes'])) {
                         foreach ($token['attributes'] as $name => $value) {
                             $this->_current->addAttribute($name, $value);
                         }
                     }
                     $this->_current = $this->_current->getParent();
                 }
             }
             $inside = true;
         } elseif ($token['type'] == Markup\Token::TYPE_TAG && $inside) {
             if ($token['name'] == 'break') {
                 // add the newline and continue parsing
                 $this->_current->addChild(new Markup\Token($token['tag'], Markup\Token::TYPE_NONE, '', array(), $this->_current));
             } else {
                 // handle a list item
                 if ($token['name'] == 'list') {
                     $attributes = array();
                     if (isset($token['attributes']['list'])) {
                         $attributes['list'] = $token['attributes']['list'];
                         unset($token['attributes']['list']);
                     }
                     if ($this->_current->getName() != 'list') {
                         // the list isn't started yet, create it
                         $child = new Markup\Token('', Markup\Token::TYPE_TAG, 'list', $attributes, $this->_current);
                         $this->_current->addChild($child);
                         $this->_current = $child;
                     }
                     $token['name'] = 'li';
                 } elseif ($token['name'] == 'img' || $token['name'] == 'url') {
                     $inside = false;
                 }
                 // add the token
                 $child = new Markup\Token($token['tag'], Markup\Token::TYPE_TAG, $token['name'], $token['attributes'], $this->_current);
                 $this->_current->addChild($child);
                 $this->_current = $child;
             }
         } else {
             // simply add the token as text
             $this->_current->addChild(new Markup\Token($token['tag'], Markup\Token::TYPE_NONE, '', array(), $this->_current));
         }
     }
 }