/** * Parse the token array into a tree * * @param array $tokens * * @return \Zend\Markup\TokenList */ protected function _createTree($tokens) { // variable initialization for treebuilder $this->_searchedStoppers = array(); $this->_tree = new Markup\TokenList(); $this->_current = new Markup\Token('', Markup\Token::TYPE_NONE, 'Zend_Markup_Root'); $this->_tree->addChild($this->_current); foreach ($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'] == Markup\Token::TYPE_TAG) { if ($token['tag'] == self::NEWLINE) { // this is a newline tag, add it as a token $this->_current->addChild(new Markup\Token("\n", 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 Markup\Token($token['tag'], 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 Markup\Token($token['tag'], Markup\Token::TYPE_NONE, '', array(), $this->_current)); } else { // add the tag $child = new 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 Markup\Token($token['tag'], Markup\Token::TYPE_NONE, '', array(), $this->_current)); } } } return $this->_tree; }
/** * Parse the token array into a tree * * @param array $tokens * * @return \Zend\Markup\TokenList */ protected function _createTree($tokens) { // variable initialization for treebuilder $groupStack = array($this->_group); $this->_searchedStoppers = array(); $this->_tree = new TokenList(); $this->_current = new Token('', Token::TYPE_NONE, 'Zend_Markup_Root'); $this->_tree->addChild($this->_current); foreach ($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(); // use a lower level group $this->_group = array_pop($groupStack); } // we found the stopper, so stop the tag $this->_current->setStopper($token['tag']); $this->_removeFromSearchedStoppers($this->_current); $this->_current = $this->_current->getParent(); $this->_group = array_pop($groupStack); // 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; // re-add the group $groupStack[] = $this->_group; $this->_group = $this->_getGroup($item->getName()); } } } else { if ($token['type'] == Token::TYPE_MARKUP) { if ($token['tag'] == self::NEWLINE) { // this is a newline tag, add it as a token $this->_current->addChild(new Token("\n", 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 Token($token['tag'], Token::TYPE_NONE, '', array(), $this->_current)); } elseif (!$this->_checkTagAllowed($token)) { // TODO: expand this to using groups for the context-awareness $this->_current->addChild(new Token($token['tag'], Token::TYPE_NONE, '', array(), $this->_current)); } else { // add the tag $child = new Token($token['tag'], $token['type'], $token['name'], $token['attributes'], $this->_current); $this->_current->addChild($child); // set the new group $groupStack[] = $this->_group; $this->_group = $this->_getGroup($token['name']); // 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 Token($token['tag'], Token::TYPE_NONE, '', array(), $this->_current)); } } } return $this->_tree; }
/** * 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)); } } }