/** * Parse the given content. * * Any newly created nodes should be appended to the given target. Any remaining content should be passed to the * next parser in the chain. * * @param Text $content * @param InlineNodeAcceptorInterface $target * @return void */ public function parseInline(Text $content, InlineNodeAcceptorInterface $target) { $content->handle('{ (?<!\\\\) ( \\<[A-Z][A-Z0-9]* # an opening HTML tag with (?: # multiple attributes... \\s+ [A-Z_:][A-Z0-9_:.\\-]* (?: \\s*=\\s* (?: [^ "\'=<>`]+| # unquoted attribute values \'[^\']*\'| # single-quoted attribute values "[^"]*" # double-quoted attribute values ) )? )* \\s*/?\\>| # ...or \\</[A-Z][A-Z0-9]*\\s*\\>| # a closing HTML tag, or \\<!--(-(?!-)|[^\\-])*?--\\>| # a HTML comment, or \\<\\?.*?\\?\\>| # a processing instruction, or \\<![A-Z]+\\s+[^>]+\\>| # an element type declaration, or \\<!\\[CDATA\\[.*?\\]\\]\\> # a CDATA section ) }isx', function (Text $content) use($target) { $target->addInline(new RawHTML($content)); }, function (Text $part) use($target) { $this->next->parseInline($part, $target); }); }
protected function parseEmail(Text $content, InlineNodeAcceptorInterface $target) { if ($content->contains('@')) { $content->handle('{ < (?:mailto:)? ( [a-zA-Z0-9.!#$%&\'*+/=?^_`{|}~-]+ \\@ [a-zA-Z0-9] (?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])? (?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)* ) > }ix', function (Text $w, Text $email) use($target) { $text = $email->copy(); $link = new Link($email->prepend('mailto:')); $link->addInline(new String($text)); $target->addInline($link); }, function (Text $part) use($target) { $this->next->parseInline($part, $target); }); } else { $this->next->parseInline($content, $target); } }
/** * Parse the given content. * * Any newly created nodes should be appended to the given target. Any remaining content should be passed to the * next parser in the chain. * * @param Text $content * @param InlineNodeAcceptorInterface $target * @return void */ public function parseInline(Text $content, InlineNodeAcceptorInterface $target) { $this->handleBackslashes($content); $content->decodeEntities(); // Remove spaces at the end of each line $content->replace('/[ ]+(?=\\n)/', ''); $target->addInline(new String($content)); }
/** * Parse the given content. * * Any newly created nodes should be appended to the given target. Any remaining content should be passed to the * next parser in the chain. * * @param Text $content * @param InlineNodeAcceptorInterface $target * @return void */ public function parseInline(Text $content, InlineNodeAcceptorInterface $target) { $content->handle('/(\\\\\\n)|( {2,}\\n)/', function () use($target) { $target->addInline(new HardBreak()); }, function (Text $part) use($target) { $this->next->parseInline($part, $target); }); }
protected function parseUnderscores(Text $content, InlineNodeAcceptorInterface $target) { $content->handle('{ (?<![A-Za-z0-9]) (__) (?![\\s_]) (.+) (?<![\\s_]) \\1 (?![A-Za-z0-9]) }sx', function (Text $w, Text $a, Text $inner) use($target) { $emphasis = new StrongEmphasis($inner); $this->context->queue($emphasis->getContent(), $emphasis); $target->addInline($emphasis); }, function (Text $part) use($target) { $this->next->parseInline($part, $target); }); }
protected function parseCodeSpan(Text $content, InlineNodeAcceptorInterface $target) { $content->handle('{ (?<![`\\\\]) (`+) # $1 = Opening run of ` (?!`) (.+?) # $2 = The code block (?<!`) \\1 # Matching closer (?!`) }sx', function (Text $whole, Text $b, Text $code) use($target) { // Replace multiple whitespace characters in a row with a single space $code->trim()->replaceString("\n", ' ')->replace('/[\\s]{2,}/', ' '); $target->addInline(new Code($code->escapeHtml())); }, function (Text $part) use($target) { $this->next->parseInline($part, $target); }); }
protected function parseReferenceLink(Text $content, InlineNodeAcceptorInterface $target) { $references = implode('|', array_map(function ($reference) { return str_replace(' ', '[ ]', preg_quote($reference)); }, $this->context->getReferences())); $content->handle('/ (?<!\\\\) (?| () \\[ (' . $references . ') \\] [\\n\\ ]* \\[\\] | (?: \\[ (' . $this->getNestedBrackets() . ') # link text = $1 \\] [ \\t\\n]* )? \\[ (' . $references . ') # label = $2 \\] ) /iux', function (Text $whole, Text $linkText, Text $label) use($target) { $reference = $label->copy()->lower(); $url = $this->context->getReferenceUrl($reference); $title = $this->context->getReferenceTitle($reference); if ($linkText->isEmpty()) { $linkText = $label; } $link = new Link($url, $linkText, $title); $target->addInline($link); $this->context->queue($link->getContent(), $link); }, function (Text $part) use($target) { $this->next->parseInline($part, $target); }); }
protected function parseReferenceImage(Text $content, InlineNodeAcceptorInterface $target) { $references = implode('|', array_map(function ($reference) { return str_replace(' ', '[ ]', preg_quote($reference)); }, $this->context->getReferences())); $content->handle('{ (?<!\\\\)! (?| () \\[ (' . $references . ') \\] [\\n\\ ]* \\[\\] | (?: \\[ (.*?) # alt text = $1 \\] [ \\t\\n]* )? \\[ (' . $references . ') \\] ) }iux', function (Text $whole, Text $alt, Text $label) use($target) { $reference = $label->copy()->lower(); $url = $this->context->getReferenceUrl($reference); $title = $this->context->getReferenceTitle($reference); if ($alt->isEmpty()) { $alt = $label; } $image = new Image($url, $alt, $title); $target->addInline($image); }, function (Text $part) use($target) { $this->next->parseInline($part, $target); }); }