/** * @param ContextInterface $context * @param InlineParserContext $inlineContext * * @return bool */ public function parse(ContextInterface $context, InlineParserContext $inlineContext) { $cursor = $inlineContext->getCursor(); $startPos = $cursor->getPosition(); $previousState = $cursor->saveState(); // Look through stack of delimiters for a [ or ! $opener = $inlineContext->getDelimiterStack()->searchByCharacter(['[', '!']); if ($opener === null) { return false; } if (!$opener->isActive()) { // no matched opener; remove from emphasis stack $inlineContext->getDelimiterStack()->removeDelimiter($opener); return false; } $isImage = $opener->getChar() === '!'; // Instead of copying a slice, we null out the parts of inlines that don't correspond to linkText; later, we'll // collapse them. This is awkward, and could be simplified if we made inlines a linked list instead $inlines = $inlineContext->getInlines(); $labelInlines = new ArrayCollection($inlines->toArray()); $this->nullify($labelInlines, 0, $opener->getPos() + 1); $cursor->advance(); // Check to see if we have a link/image if (!($link = $this->tryParseLink($cursor, $context->getDocument()->getReferenceMap(), $opener, $startPos))) { // No match $inlineContext->getDelimiterStack()->removeDelimiter($opener); // Remove this opener from stack $cursor->restoreState($previousState); return false; } $delimiterStack = $inlineContext->getDelimiterStack(); $stackBottom = $opener->getPrevious(); foreach ($this->environment->getInlineProcessors() as $inlineProcessor) { $inlineProcessor->processInlines($labelInlines, $delimiterStack, $stackBottom); } if ($delimiterStack instanceof DelimiterStack) { $delimiterStack->removeAll($stackBottom); } // Remove the part of inlines that become link_text $this->nullify($inlines, $opener->getPos(), $inlines->count()); // processEmphasis will remove this and later delimiters. // Now, for a link, we also remove earlier link openers (no links in links) if (!$isImage) { $inlineContext->getDelimiterStack()->removeEarlierMatches('['); } $inlines->add($this->createInline($link['url'], $labelInlines, $link['title'], $isImage)); return true; }
/** * @param InlineParserContext $inlineContext * * @return bool */ public function parse(InlineParserContext $inlineContext) { $character = $inlineContext->getCursor()->getCharacter(); if (in_array($character, $this->double)) { $character = '“'; } elseif (in_array($character, $this->single)) { $character = '’'; } else { return false; } $cursor = $inlineContext->getCursor(); $charBefore = $cursor->peek(-1); if ($charBefore === null) { $charBefore = "\n"; } $cursor->advance(); $charAfter = $cursor->getCharacter(); if ($charAfter === null) { $charAfter = "\n"; } $afterIsWhitespace = preg_match('/\\pZ|\\s/u', $charAfter); $afterIsPunctuation = preg_match(RegexHelper::REGEX_PUNCTUATION, $charAfter); $beforeIsWhitespace = preg_match('/\\pZ|\\s/u', $charBefore); $beforeIsPunctuation = preg_match(RegexHelper::REGEX_PUNCTUATION, $charBefore); $leftFlanking = !$afterIsWhitespace && !($afterIsPunctuation && !$beforeIsWhitespace && !$beforeIsPunctuation); $rightFlanking = !$beforeIsWhitespace && !($beforeIsPunctuation && !$afterIsWhitespace && !$afterIsPunctuation); $canOpen = $leftFlanking && !$rightFlanking; $canClose = $rightFlanking; $node = new Text($character, ['delim' => true]); $inlineContext->getContainer()->appendChild($node); // Add entry to stack to this opener $inlineContext->getDelimiterStack()->push(new Delimiter($character, 1, $node, $canOpen, $canClose)); return true; }
/** * @param ContextInterface $context * @param InlineParserContext $inlineContext * * @return bool */ public function parse(ContextInterface $context, InlineParserContext $inlineContext) { $character = $inlineContext->getCursor()->getCharacter(); if (!in_array($character, $this->getCharacters())) { return false; } $numDelims = 0; $cursor = $inlineContext->getCursor(); $charBefore = $cursor->peek(-1); if ($charBefore === null) { $charBefore = "\n"; } while ($cursor->peek($numDelims) === $character) { ++$numDelims; } $cursor->advanceBy($numDelims); $charAfter = $cursor->getCharacter(); if ($charAfter === null) { $charAfter = "\n"; } $leftFlanking = $numDelims > 0 && !preg_match('/\\pZ|\\s/u', $charAfter) && !(preg_match(RegexHelper::REGEX_PUNCTUATION, $charAfter) && !preg_match('/\\pZ|\\s/u', $charBefore) && !preg_match(RegexHelper::REGEX_PUNCTUATION, $charBefore)); $rightFlanking = $numDelims > 0 && !preg_match('/\\pZ|\\s/u', $charBefore) && !(preg_match(RegexHelper::REGEX_PUNCTUATION, $charBefore) && !preg_match('/\\pZ|\\s/u', $charAfter) && !preg_match(RegexHelper::REGEX_PUNCTUATION, $charAfter)); if ($character === '_') { $canOpen = $leftFlanking && (!$rightFlanking || preg_match(RegexHelper::REGEX_PUNCTUATION, $charBefore)); $canClose = $rightFlanking && (!$leftFlanking || preg_match(RegexHelper::REGEX_PUNCTUATION, $charAfter)); } else { $canOpen = $leftFlanking; $canClose = $rightFlanking; } $inlineContext->getInlines()->add(new Text($cursor->getPreviousText(), array('delim' => true))); // Add entry to stack to this opener $delimiter = new Delimiter($character, $numDelims, $inlineContext->getInlines()->count() - 1, $canOpen, $canClose); $inlineContext->getDelimiterStack()->push($delimiter); return true; }
/** * @param InlineParserContext $inlineContext * * @return bool */ public function parse(InlineParserContext $inlineContext) { $cursor = $inlineContext->getCursor(); $startPos = $cursor->getPosition(); $previousState = $cursor->saveState(); // Look through stack of delimiters for a [ or ! $opener = $inlineContext->getDelimiterStack()->searchByCharacter(['[', '!']); if ($opener === null) { return false; } if (!$opener->isActive()) { // no matched opener; remove from emphasis stack $inlineContext->getDelimiterStack()->removeDelimiter($opener); return false; } $isImage = $opener->getChar() === '!'; $cursor->advance(); // Check to see if we have a link/image if (!($link = $this->tryParseLink($cursor, $inlineContext->getReferenceMap(), $opener, $startPos))) { // No match $inlineContext->getDelimiterStack()->removeDelimiter($opener); // Remove this opener from stack $cursor->restoreState($previousState); return false; } $inline = $this->createInline($link['url'], $link['title'], $isImage); $opener->getInlineNode()->replaceWith($inline); while (($label = $inline->next()) !== null) { $inline->appendChild($label); } $delimiterStack = $inlineContext->getDelimiterStack(); $stackBottom = $opener->getPrevious(); foreach ($this->environment->getInlineProcessors() as $inlineProcessor) { $inlineProcessor->processInlines($delimiterStack, $stackBottom); } if ($delimiterStack instanceof DelimiterStack) { $delimiterStack->removeAll($stackBottom); } // processEmphasis will remove this and later delimiters. // Now, for a link, we also remove earlier link openers (no links in links) if (!$isImage) { $inlineContext->getDelimiterStack()->removeEarlierMatches('['); } return true; }
/** * @param InlineParserContext $inlineParserContext */ protected function processInlines(InlineParserContext $inlineParserContext) { $delimiterStack = $inlineParserContext->getDelimiterStack(); foreach ($this->environment->getInlineProcessors() as $inlineProcessor) { $inlineProcessor->processInlines($delimiterStack); } // Remove all delimiters $delimiterStack->removeAll(); }
/** * @param ContextInterface $context * @param InlineParserContext $inlineContext * * @return bool */ public function parse(ContextInterface $context, InlineParserContext $inlineContext) { if ($inlineContext->getCursor()->getCharacter() !== '[') { return false; } $inlineContext->getCursor()->advance(); $inlineContext->getInlines()->add(new Text('[', ['delim' => true])); // Add entry to stack for this opener $delimiter = new Delimiter('[', 1, $inlineContext->getInlines()->count() - 1, true, false, $inlineContext->getCursor()->getPosition()); $inlineContext->getDelimiterStack()->push($delimiter); return true; }
/** * @param ContextInterface $context * @param InlineParserContext $inlineContext * * @return bool */ public function parse(ContextInterface $context, InlineParserContext $inlineContext) { $cursor = $inlineContext->getCursor(); if ($cursor->peek() === '[') { $cursor->advanceBy(2); $inlineContext->getInlines()->add(new Text('![', array('delim' => true))); // Add entry to stack for this opener $delimiter = new Delimiter('!', 1, $inlineContext->getInlines()->count() - 1, true, false, $cursor->getPosition()); $inlineContext->getDelimiterStack()->push($delimiter); return true; } return false; }
/** * @param InlineParserContext $inlineContext * * @return bool */ public function parse(InlineParserContext $inlineContext) { if ($inlineContext->getCursor()->getCharacter() !== '[') { return false; } $inlineContext->getCursor()->advance(); $node = new Text('[', ['delim' => true]); $inlineContext->getContainer()->appendChild($node); // Add entry to stack for this opener $delimiter = new Delimiter('[', 1, $node, true, false, $inlineContext->getCursor()->getPosition()); $inlineContext->getDelimiterStack()->push($delimiter); return true; }
/** * @param InlineParserContext $inlineContext * * @return bool */ public function parse(InlineParserContext $inlineContext) { $cursor = $inlineContext->getCursor(); $delimiterStack = $inlineContext->getDelimiterStack(); if ($cursor->peek(-1) !== '[') { return false; } if ($cursor->peek(-2) == '!' || $cursor->peek(-1) !== '[' || $cursor->peek(1) !== ']' || $cursor->peek(2) !== ' ') { return false; } $status = $cursor->peek(0) == 'x'; $cursor->advanceBy(2); // Add entry to stack for this opener $delimiter = new Delimiter('[', 1, $inlineContext->getContainer()->firstChild(), true, false, 0); $delimiterStack->push($delimiter); $opener = $delimiterStack->searchByCharacter(['[']); $opener->getInlineNode()->replaceWith(new TaskListsCheckbox($status)); $delimiterStack->removeDelimiter($opener); return true; }
/** * @param InlineParserContext $inlineContext * * @return bool */ public function parse(InlineParserContext $inlineContext) { $character = $inlineContext->getCursor()->getCharacter(); if (!in_array($character, $this->getCharacters())) { return false; } $numDelims = 0; $cursor = $inlineContext->getCursor(); $charBefore = $cursor->peek(-1); if ($charBefore === null) { $charBefore = "\n"; } while ($cursor->peek($numDelims) === $character) { ++$numDelims; } $cursor->advanceBy($numDelims); $charAfter = $cursor->getCharacter(); if ($charAfter === null) { $charAfter = "\n"; } $afterIsWhitespace = preg_match('/\\pZ|\\s/u', $charAfter); $afterIsPunctuation = preg_match(RegexHelper::REGEX_PUNCTUATION, $charAfter); $beforeIsWhitespace = preg_match('/\\pZ|\\s/u', $charBefore); $beforeIsPunctuation = preg_match(RegexHelper::REGEX_PUNCTUATION, $charBefore); $leftFlanking = $numDelims > 0 && !$afterIsWhitespace && !($afterIsPunctuation && !$beforeIsWhitespace && !$beforeIsPunctuation); $rightFlanking = $numDelims > 0 && !$beforeIsWhitespace && !($beforeIsPunctuation && !$afterIsWhitespace && !$afterIsPunctuation); if ($character === '_') { $canOpen = $leftFlanking && (!$rightFlanking || $beforeIsPunctuation); $canClose = $rightFlanking && (!$leftFlanking || $afterIsPunctuation); } else { $canOpen = $leftFlanking; $canClose = $rightFlanking; } $node = new Text($cursor->getPreviousText(), ['delim' => true]); $inlineContext->getContainer()->appendChild($node); // Add entry to stack to this opener $delimiter = new Delimiter($character, $numDelims, $node, $canOpen, $canClose); $inlineContext->getDelimiterStack()->push($delimiter); return true; }
public function parse(InlineParserContext $inlineContext) { $cursor = $inlineContext->getCursor(); if ($cursor->getFirstNonSpaceCharacter() !== '{') { return false; } $char = $cursor->getCharacter(); if ('{' === $char) { $char = (string) $cursor->getCharacter($cursor->getPosition() - 1); } $attributes = AttributesUtils::parse($cursor); if (empty($attributes)) { return false; } if ('' === $char) { $cursor->advanceToFirstNonSpace(); } $node = new InlineAttributes($attributes, ' ' === $char || '' === $char); $inlineContext->getContainer()->appendChild($node); $inlineContext->getDelimiterStack()->push(new Delimiter('attributes', 1, $node, false, false)); return true; }
/** * @param InlineParserContext $inlineContext * * @return bool */ public function parse(InlineParserContext $inlineContext) { $cursor = $inlineContext->getCursor(); $character = $this->getCharacterType($cursor->getCharacter()); $charBefore = $cursor->peek(-1); if ($charBefore === null) { $charBefore = "\n"; } $cursor->advance(); $charAfter = $cursor->getCharacter(); if ($charAfter === null) { $charAfter = "\n"; } list($leftFlanking, $rightFlanking) = $this->determineFlanking($charBefore, $charAfter); $canOpen = $leftFlanking && !$rightFlanking; $canClose = $rightFlanking; $node = new Text($character, ['delim' => true]); $inlineContext->getContainer()->appendChild($node); // Add entry to stack to this opener $inlineContext->getDelimiterStack()->push(new Delimiter($character, 1, $node, $canOpen, $canClose)); return true; }
/** * @param InlineParserContext $inlineContext * * @return bool */ public function parse(InlineParserContext $inlineContext) { $character = $inlineContext->getCursor()->getCharacter(); if (!in_array($character, $this->getCharacters())) { return false; } $numDelims = 0; $cursor = $inlineContext->getCursor(); $charBefore = $cursor->peek(-1); if ($charBefore === null) { $charBefore = "\n"; } while ($cursor->peek($numDelims) === $character) { ++$numDelims; } if ($numDelims === 0) { return false; } // Skip single delims if emphasis is disabled if ($numDelims === 1 && !$this->config->getConfig('enable_em')) { return false; } $cursor->advanceBy($numDelims); $charAfter = $cursor->getCharacter(); if ($charAfter === null) { $charAfter = "\n"; } list($canOpen, $canClose) = $this->determineCanOpenOrClose($charBefore, $charAfter, $character); $node = new Text($cursor->getPreviousText(), ['delim' => true, 'emphasis_config' => $this->config]); $inlineContext->getContainer()->appendChild($node); // Add entry to stack to this opener $delimiter = new Delimiter($character, $numDelims, $node, $canOpen, $canClose); $inlineContext->getDelimiterStack()->push($delimiter); return true; }
/** * @param InlineParserContext $inlineParserContext */ protected function processInlines(InlineParserContext $inlineParserContext) { foreach ($this->environment->getInlineProcessors() as $inlineProcessor) { $inlineProcessor->processInlines($inlineParserContext->getInlines(), $inlineParserContext->getDelimiterStack()); } }