/** * Prepare the parsing of a Textile string, the real parsing is done in {@link _parse()} * * @param string $value * * @return array */ public function parse($value) { if (!is_string($value)) { throw new Exception('Value to parse should be a string.'); } if (empty($value)) { throw new Exception('Value to parse cannot be left empty.'); } // first make we only have LF newlines, also trim the value $this->_value = str_replace(array("\r\n", "\r"), "\n", $value); $this->_value = trim($this->_value); // initialize variables and tokenize $this->_valueLen = iconv_strlen($this->_value, 'UTF-8'); $this->_pointer = 0; $this->_buffer = ''; $this->_temp = array(); $this->_tokens = array(); $this->_tokenize(); // create the tree $this->_tree = new Markup\TokenList(); $this->_current = new Markup\Token('', Markup\Token::TYPE_NONE, 'Zend_Markup_Root'); $this->_tree->addChild($this->_current); $this->_createTree(); 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 $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; }
/** * Test filtering * * @return void */ public function testFiltering() { // create a token list $tokenList = new TokenList(); // first create a root token $root = new Token('', Token::TYPE_MARKUP, 'Zend_Markup_Root'); $tokenList->addChild($root); // now add the actual markups to the root token $root->addChild(new Token('baz ', Token::TYPE_NONE, '', array(), $root)); $testMarkup = new Token('foohooo', Token::TYPE_MARKUP, 'test', array(), $root); $testMarkup->setStopper('bazzaa'); $root->addChild($testMarkup); // now add the content for the test markup $testMarkup->addChild(new Token('booh', Token::TYPE_NONE, '', array(), $testMarkup)); // add a filter to the test markup $this->_renderer->getMarkup('test')->addFilter(new StringToUpperFilter(), -1); $this->assertEquals('baz fooBOOHbar', $this->_renderer->render($tokenList)); }
/** * Parse a string * * @param string $value * @return Zend_Markup_TokenList */ public function parse($value) { if (!is_string($value)) { /** * @see Zend_Markup_Parser_Exception */ throw new Parser\Exception('Value to parse should be a string.'); } if (empty($value)) { /** * @see Zend_Markup_Parser_Exception */ throw new Parser\Exception('Value to parse cannot be left empty.'); } // initialize variables $tree = new TokenList(); $current = new Token('', Token::TYPE_NONE, 'Zend_Markup_Root'); $tree->addChild($current); $token = new Token($value, Token::TYPE_NONE, '', array(), $current); $current->addChild($token); return $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; }