/** * Parse the token array into a tree * * @param array $tokens * * @return void */ public function _createTree() { foreach ($this->_tokens as $token) { // first we want to know if this tag is a stopper, or at least a searched one if ($this->_isStopper($token['tag'])) { // find the stopper $oldItems = array(); while (!in_array($token['tag'], $this->_tags[$this->_current->getName()]['stoppers'])) { $oldItems[] = clone $this->_current; $this->_current = $this->_current->getParent(); } // we found the stopper, so stop the tag $this->_current->setStopper($token['tag']); $this->_removeFromSearchedStoppers($this->_current); $this->_current = $this->_current->getParent(); // add the old items again if there are any if (!empty($oldItems)) { foreach (array_reverse($oldItems) as $item) { /* @var $token Zend_Markup_Token */ $this->_current->addChild($item); $item->setParent($this->_current); $this->_current = $item; } } } else { if ($token['type'] == Zend_Markup_Token::TYPE_TAG) { if ($token['tag'] == self::NEWLINE) { // this is a newline tag, add it as a token $this->_current->addChild(new Zend_Markup_Token("\n", Zend_Markup_Token::TYPE_NONE, '', array(), $this->_current)); } elseif (isset($token['name']) && $token['name'][0] == '/') { // this is a stopper, add it as a empty token $this->_current->addChild(new Zend_Markup_Token($token['tag'], Zend_Markup_Token::TYPE_NONE, '', array(), $this->_current)); } elseif (isset($this->_tags[$this->_current->getName()]['parse_inside']) && !$this->_tags[$this->_current->getName()]['parse_inside']) { $this->_current->addChild(new Zend_Markup_Token($token['tag'], Zend_Markup_Token::TYPE_NONE, '', array(), $this->_current)); } else { // add the tag $child = new Zend_Markup_Token($token['tag'], $token['type'], $token['name'], $token['attributes'], $this->_current); $this->_current->addChild($child); // add stoppers for this tag, if its has stoppers if ($this->_getType($token['name']) == self::TYPE_DEFAULT) { $this->_current = $child; $this->_addToSearchedStoppers($this->_current); } } } else { // no tag, just add it as a simple token $this->_current->addChild(new Zend_Markup_Token($token['tag'], Zend_Markup_Token::TYPE_NONE, '', array(), $this->_current)); } } } }
/** * 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'] == Zend_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'] == Zend_Markup_Token::TYPE_TAG && $inside) { if ($token['name'] == 'break') { // add the newline and continue parsing $this->_current->addChild(new Zend_Markup_Token($token['tag'], Zend_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 Zend_Markup_Token('', Zend_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 Zend_Markup_Token($token['tag'], Zend_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 Zend_Markup_Token($token['tag'], Zend_Markup_Token::TYPE_NONE, '', array(), $this->_current)); } } }