Attempt to parse link destination
public static parseLinkDestination ( |
||
$cursor | ||
리턴 | null | string | The string, or null if no match |
/** * @param string $mediaType * @return mixed */ protected function getElementParts($mediaType) { $label = trim($this->cursor->match('/^\\[(?:[^\\\\\\[\\]]|\\\\[\\[\\]]){0,750}\\]/'), '[]'); $url = trim(LinkParserHelper::parseLinkDestination($this->cursor), '()'); if ($label === null || $url === null) { return $this->cancel(); } return $this->getElement($mediaType, $label, $url); }
/** * Attempt to parse a link reference, modifying the refmap. * * @param Cursor $cursor * * @return bool */ public function parse(Cursor $cursor) { if ($cursor->isAtEnd()) { return false; } $initialState = $cursor->saveState(); $matchChars = LinkParserHelper::parseLinkLabel($cursor); if ($matchChars === 0) { $cursor->restoreState($initialState); return false; } // We need to trim the opening and closing brackets from the previously-matched text $label = substr($cursor->getPreviousText(), 1, -1); if (preg_match('/[^\\s]/', $label) === 0) { $cursor->restoreState($initialState); return false; } if ($cursor->getCharacter() !== ':') { $cursor->restoreState($initialState); return false; } // Advance past the colon $cursor->advance(); // Link URL $cursor->advanceToFirstNonSpace(); $destination = LinkParserHelper::parseLinkDestination($cursor); if (empty($destination)) { $cursor->restoreState($initialState); return false; } $previousState = $cursor->saveState(); $cursor->advanceToFirstNonSpace(); $title = LinkParserHelper::parseLinkTitle($cursor); if ($title === null) { $title = ''; $cursor->restoreState($previousState); } // Make sure we're at line end: if ($cursor->match('/^ *(?:\\n|$)/') === null) { $cursor->restoreState($initialState); return false; } if (!$this->referenceMap->contains($label)) { $reference = new Reference($label, $destination, $title); $this->referenceMap->addReference($reference); } return true; }
/** * @param Cursor $cursor * * @return array|bool */ protected function tryParseInlineLinkAndTitle(Cursor $cursor) { $cursor->advance(); $cursor->advanceToFirstNonSpace(); if (($dest = LinkParserHelper::parseLinkDestination($cursor)) === null) { return false; } $cursor->advanceToFirstNonSpace(); $title = null; // make sure there's a space before the title: if (preg_match('/^\\s/', $cursor->peek(-1))) { $title = LinkParserHelper::parseLinkTitle($cursor) ?: ''; } $cursor->advanceToFirstNonSpace(); if (!$cursor->match('/^\\)/')) { return false; } return ['url' => $dest, 'title' => $title]; }
/** * Attempt to parse a link reference, modifying the refmap. * * @param Cursor $cursor * * @return bool */ public function parse(Cursor $cursor) { if ($cursor->isAtEnd()) { return false; } $initialState = $cursor->saveState(); $matchChars = LinkParserHelper::parseLinkLabel($cursor); if ($matchChars === 0) { $cursor->restoreState($initialState); return false; } // We need to trim the opening and closing brackets from the previously-matched text $label = substr($cursor->getPreviousText(), 1, -1); if (preg_match('/[^\\s]/', $label) === 0) { $cursor->restoreState($initialState); return false; } if ($cursor->getCharacter() !== ':') { $cursor->restoreState($initialState); return false; } // Advance past the colon $cursor->advance(); // Link URL $cursor->advanceToFirstNonSpace(); $destination = LinkParserHelper::parseLinkDestination($cursor); if (empty($destination)) { $cursor->restoreState($initialState); return false; } $previousState = $cursor->saveState(); $cursor->advanceToFirstNonSpace(); $title = LinkParserHelper::parseLinkTitle($cursor); if ($title === null) { $title = ''; $cursor->restoreState($previousState); } // Make sure we're at line end: $atLineEnd = true; if ($cursor->match('/^ *(?:\\n|$)/') === null) { if ($title === '') { $atLineEnd = false; } else { // The potential title we found is not at the line end, // but it could still be a legal link reference if we // discard the title $title = ''; // rewind before spaces $cursor->restoreState($previousState); // and instead check if the link URL is at the line end $atLineEnd = $cursor->match('/^ *(?:\\n|$)/') !== null; } } if (!$atLineEnd) { $cursor->restoreState($initialState); return false; } if (!$this->referenceMap->contains($label)) { $reference = new Reference($label, $destination, $title); $this->referenceMap->addReference($reference); } return true; }
/** * @param Cursor $cursor * * @return array|bool */ protected function tryParseInlineLinkAndTitle(Cursor $cursor) { if ($cursor->getCharacter() !== '(') { return false; } $previousState = $cursor->saveState(); $cursor->advance(); $cursor->advanceToFirstNonSpace(); if (($dest = LinkParserHelper::parseLinkDestination($cursor)) === null) { $cursor->restoreState($previousState); return false; } $cursor->advanceToFirstNonSpace(); $title = null; // make sure there's a space before the title: if (preg_match(RegexHelper::REGEX_WHITESPACE_CHAR, $cursor->peek(-1))) { $title = LinkParserHelper::parseLinkTitle($cursor) ?: ''; } $cursor->advanceToFirstNonSpace(); if ($cursor->match('/^\\)/') === null) { $cursor->restoreState($previousState); return false; } return ['url' => $dest, 'title' => $title]; }