/**
  * Add token with data:
  * * level   - header level (depend on # count)
  * * content - header content
  *
  * @param string                  $content
  * @param int                     $position
  * @param \ViKon\Parser\TokenList $tokenList
  *
  * @return bool
  */
 protected function handleSingleState($content, $position, TokenList $tokenList)
 {
     $content = trim($content);
     preg_match('/^#{1,6}/', $content, $matches);
     $tokenList->addToken($this->name, $position)->set('level', abs(strlen($matches[0])))->set('content', trim($content, "# \t\n\r\v"));
     return true;
 }
 /**
  * @param \ViKon\Parser\Token     $token
  * @param \ViKon\Parser\TokenList $tokenList
  *
  * @return string
  * @throws \ViKon\Parser\LexerException
  */
 public function renderReference(Token $token, TokenList $tokenList)
 {
     $reference = $token->get('reference');
     $label = $token->get('label');
     if ($reference instanceof Token) {
         $referenceToken = $reference;
     } else {
         if (empty($reference) === '') {
             $reference = strtolower($token->get('label'));
         }
         $tokens = $tokenList->getTokensByCallback(function (Token $token) use($reference) {
             return $token->getName() === ReferenceRule::NAME && $token->get('reference', null) === $reference;
         });
         // Get first token (if not found return full match)
         if (($referenceToken = reset($tokens)) === false) {
             return $token->get('match', '');
         }
         $referenceToken->set('used', true);
     }
     $url = $referenceToken->get('url');
     $title = $referenceToken->get('title', '');
     if (empty($title)) {
         return "<a href=\"{$url}\">{$label}</a>";
     }
     return "<a href=\"{$url}\" title=\"{$title}\">{$label}</a>";
 }
 /**
  * Add token with data:
  * * level   - header level (depend on # count)
  * * content - header
  *
  * @param string                  $content
  * @param int                     $position
  * @param \ViKon\Parser\TokenList $tokenList
  *
  * @return bool
  */
 protected function handleSingleState($content, $position, TokenList $tokenList)
 {
     list(, $content, $level) = explode("\n", $content);
     $content = trim($content);
     $level = $level[0] === '=' ? 1 : 2;
     $tokenList->addToken($this->name, $position)->set('level', $level)->set('content', $content);
     return true;
 }
 /**
  * @param string                  $content
  * @param int                     $position
  * @param int                     $state
  * @param \ViKon\Parser\TokenList $tokenList
  *
  * @throws \ViKon\Parser\Rule\RuleException
  */
 public function parseToken($content, $position, $state, TokenList $tokenList)
 {
     switch ($state) {
         case Lexer::STATE_MATCHED:
             $token = $tokenList->addToken($this->name, $position);
             $token->set('content', str_repeat("\n", substr_count($content, "\n")));
             break;
         default:
             parent::parseToken($content, $position, $state, $tokenList);
             break;
     }
 }
 /**
  * Add token with data:
  * * match     - whole matched content
  * * alt       - alt content for image
  * * reference - reference name or object
  *
  * @param string                  $content
  * @param int                     $position
  * @param \ViKon\Parser\TokenList $tokenList
  */
 protected function handleSingleState($content, $position, TokenList $tokenList)
 {
     preg_match('/!\\[((?:\\\\.|[^]\\\\])+)\\][\\t ]*\\[((?:\\\\.|[^]\\\\])*)\\]/', $content, $matches);
     $reference = strtolower(empty($matches[2]) ? $matches[1] : $matches[2]);
     $referenceTokens = $tokenList->getTokensByCallback(function (Token $token) use($reference) {
         return $token->getName() === ReferenceRule::NAME && $token->get('reference', null) === $reference;
     });
     if (($referenceToken = reset($referenceTokens)) !== false) {
         $referenceToken->set('used', true);
         $reference = $referenceToken;
     }
     $tokenList->addToken($this->name, $position)->set('match', $content)->set('alt', $matches[1])->set('reference', $reference);
 }
 /**
  * Add token with data
  * * match     - whole matched content
  * * label     - link content
  * * reference - reference name or object
  *
  * @param string                  $content
  * @param int                     $position
  * @param \ViKon\Parser\TokenList $tokenList
  */
 protected function handleSingleState($content, $position, TokenList $tokenList)
 {
     preg_match('/\\[((?:\\\\.|[^]\\\\])+)\\](?: ?\\[((?:\\\\.|[^]\\\\])+)\\])?/', $content, $matches);
     $reference = strtolower(empty($matches[2]) ? $matches[1] : $matches[2]);
     // List all reference token which match url reference part
     $referenceTokens = $tokenList->getTokensByCallback(function (Token $token) use($reference) {
         return $token->getName() === ReferenceRule::NAME && $token->get('reference', null) === $reference;
     });
     // Get first matching reference token
     if (($referenceToken = reset($referenceTokens)) !== false) {
         $referenceToken->set('used', true);
         $reference = $referenceToken;
     }
     $tokenList->addToken($this->name, $position)->set('match', $content)->set('label', $matches[1])->set('reference', $reference);
 }
 /**
  * @param \ViKon\Parser\Token     $token
  * @param \ViKon\Parser\TokenList $tokenList
  *
  * @return string
  * @throws \ViKon\Parser\LexerException
  */
 public function renderReference(Token $token, TokenList $tokenList)
 {
     $reference = $token->get('reference');
     $alt = $token->get('alt');
     if ($reference instanceof Token) {
         $referenceToken = $reference;
     } else {
         $tokens = $tokenList->getTokensByCallback(function (Token $token) use($reference) {
             return $token->getName() === ReferenceRule::NAME && $token->get('reference', null) === $reference;
         });
         // Get first token (if not found return full match)
         if (($referenceToken = reset($tokens)) === false) {
             return $token->get('match', '');
         }
         $referenceToken->set('used', true);
     }
     $url = $referenceToken->get('url');
     $title = $referenceToken->get('title', '');
     if (empty($title)) {
         return "<img alt=\"{$alt}\" src=\"{$url}\"/>";
     }
     return "<img alt=\"{$alt}\" src=\"{$url}\" title=\"{$title}\" />";
 }
Пример #8
0
 /**
  * @param string                  $col col content
  * @param \ViKon\Parser\TokenList $tokenList
  */
 private function parseCol($col, TokenList $tokenList)
 {
     $startRule = $this->set->getStartRule();
     $colTokenList = $this->parseContent($col);
     for ($i = 0; $i < count($colTokenList); $i++) {
         if ($colTokenList->getTokenAt($i)->getName() === $this->name) {
             $token = $colTokenList->getTokenAt($i);
             $colTokenList->removeTokenAt($i);
             $colTokenList->insertTokenAt($startRule->getName(), $token->getPosition(), $i)->set('content', $token->get('content', ''));
         }
     }
     $tokenList->merge($colTokenList);
 }
 /**
  * Parse list content and
  *
  * @param \ViKon\Parser\TokenList $tokenList
  */
 protected function parseListContent(TokenList $tokenList)
 {
     $lastToken = $tokenList->last();
     if ($lastToken->getName() === $this->name) {
         $tokenList->removeTokenAt(-1);
         // Remove last LIST_BLOCK (content)
         $content = $lastToken->get('content', '');
         $containsParagraphs = $tokenList->last()->get('forceParagraph', false);
         if (preg_match('/(?:\\n(?:[ \\t]*(?=\\n))?){2,}/i', $content) === 1) {
             $containsParagraphs = true;
             // Remove starting spaces or tab
             $content = explode("\n", $content);
             $pad = null;
             foreach ($content as $index => &$item) {
                 // Find out lin starting space length up to 4 characters
                 if ($pad === null && $index > 0 && preg_match('/^[ \\t]*$/i', $item) !== 1) {
                     if (preg_match('/^[ ]{0,4}/', $item, $matches) === 1) {
                         $pad = strlen($matches[0]);
                     } else {
                         $pad = 4;
                     }
                 }
                 // Remove line starting space or tab characters
                 if ($pad !== null) {
                     $item = preg_replace('/^(?: {' . $pad . '}|\\t)/', '', $item);
                 }
             }
             $content = implode("\n", $content);
         }
         $content = "\n" . $content . "\n";
         $contentTokenList = $this->parseContent($content, null, true);
         // Remove beginning and ending new line feed
         if ($contentTokenList->first()->getName() === EolRule::NAME && $contentTokenList->last()->getName() === EolRule::NAME) {
             $contentTokenList->removeTokenAt(0);
             // Remove first EOL
             $contentTokenList->removeTokenAt(-1);
             // Remove last EOL
         }
         // Remove paragraphs
         if (!$containsParagraphs && $contentTokenList->first()->getName() === PRule::NAME . AbstractBlockRule::OPEN && $contentTokenList->last()->getName() === PRule::NAME . AbstractBlockRule::CLOSE) {
             $contentTokenList->removeTokenAt(0);
             // Remove first P
             $contentTokenList->removeTokenAt(-1);
             // Remove last P
         }
         // Add new line indicator for paragraphs
         for ($i = 0; $i < count($contentTokenList); $i++) {
             $token = $contentTokenList->getTokenAt($i);
             if ($token->getName() === EolRule::NAME) {
                 $contentTokenList->removeTokenAt($i);
                 $contentTokenList->insertTokenAt($this->name . '_ITEM_EOL', $token->getPosition(), $i);
             }
         }
         // Change LIST_BLOCK to BASE token
         $startRule = $this->set->getStartRule();
         for ($i = 0; $i < count($contentTokenList); $i++) {
             if ($contentTokenList->getTokenAt($i)->getName() === $this->name) {
                 $token = $contentTokenList->getTokenAt($i);
                 $contentTokenList->removeTokenAt($i);
                 $contentTokenList->insertTokenAt($startRule->getName(), $token->getPosition(), $i)->set('content', $token->get('content', ''));
             }
         }
         $tokenList->merge($contentTokenList);
     }
 }
 /**
  * Add token with data
  * * match     - whole matched content
  * * reference - reference identifier
  * * url       - reference url
  * * title     - reference title
  * * used      - indicates if reference is used by link or image
  *
  * @param string                  $content
  * @param int                     $position
  * @param \ViKon\Parser\TokenList $tokenList
  */
 protected function handleSingleState($content, $position, TokenList $tokenList)
 {
     preg_match('/\\[((?:\\\\.|[^]\\\\])*)\\]:[ \\t]*([^ \\t\\n]+)[ \\t]*\\n?[ \\t]*(?:["\'\\(]((?:\\\\.|[^"\\\\])+)["\'\\)])?/', $content, $matches);
     $tokenList->addToken($this->name, $position)->set('match', $content)->set('reference', strtolower($matches[1]))->set('url', $matches[2])->set('title', isset($matches[3]) ? $matches[3] : null)->set('used', false);
 }
 /**
  * Handle lexers entry state
  *
  * @param string                  $content
  * @param int                     $position
  * @param \ViKon\Parser\TokenList $tokenList
  */
 protected function handleEntryState($content, $position, TokenList $tokenList)
 {
     $lang = trim($content, " `\t\n\r\v");
     $tokenList->addToken($this->name . self::OPEN, $position)->set('lang', $lang);
 }
 /**
  * @param string                  $content
  * @param int                     $position
  * @param \ViKon\Parser\TokenList $tokenList
  */
 protected function handleSingleState($content, $position, TokenList $tokenList)
 {
     $tokenList->addToken($this->name, $position)->set('url', $content);
 }
 /**
  * Add token with data
  * * label - link content
  * * url   - link url address
  * * title - link title (optional)
  *
  * @param string                  $content
  * @param int                     $position
  * @param \ViKon\Parser\TokenList $tokenList
  */
 protected function handleSingleState($content, $position, TokenList $tokenList)
 {
     preg_match('/\\[((?:\\\\.|[^]\\\\])+)\\][\\t ]*\\([\\t ]*((?:\\\\.|[^\\)\\\\ ])+)[\\t ]*(?:"((?:\\\\.|[^"\\\\])+)")?\\)/', $content, $matches);
     $tokenList->addToken($this->name, $position)->set('label', $matches[1])->set('url', $matches[2])->set('title', isset($matches[3]) ? $matches[3] : null);
 }
Пример #14
0
 /**
  * @param string                  $content
  * @param int                     $position
  * @param \ViKon\Parser\TokenList $tokenList
  */
 protected function handleUnmatchedState($content, $position, TokenList $tokenList)
 {
     $tokenList->addToken($this->name, $position)->set('expression', $content);
 }
Пример #15
0
 protected function handleSingleState($content, $position, TokenList $tokenList)
 {
     preg_match('/(\\\\\\\\|\\\\`|\\\\\\*|\\\\\\_|\\\\\\{|\\\\\\}|\\\\\\[|\\\\\\]|\\\\\\(|\\\\\\)|\\\\\\#|\\\\\\+|\\\\\\-|\\\\\\.|\\\\\\!)/', $content, $matches);
     $tokenList->addToken($this->name, $position)->set('char', $matches[1][1]);
 }
Пример #16
0
 /**
  * Accept all matches
  *
  * @param string                  $content
  * @param int                     $position
  * @param int                     $state
  * @param \ViKon\Parser\TokenList $tokenList
  */
 public function parseToken($content, $position, $state, TokenList $tokenList)
 {
     if (!empty($content)) {
         $tokenList->addToken($this->name, $position)->set('content', $content);
     }
 }