<commit_before><?php

namespace FluxBB\CommonMark\Parser\Inline;

use FluxBB\CommonMark\Common\Text;
use FluxBB\CommonMark\Node\InlineNodeAcceptorInterface;
use FluxBB\CommonMark\Node\Link;
use FluxBB\CommonMark\Parser\AbstractInlineParser;

class LinkParser extends AbstractInlineParser
{

    /**
     * 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)
    {
        if ($content->contains('[')) {
            $this->parseInlineLink($content, $target);
        } else {
            $this->next->parseInline($content, $target);
        }
    }

    protected function parseInlineLink(Text $content, InlineNodeAcceptorInterface $target)
    {
        $content->handle(
            '{
                \[
                    (' . $this->getNestedBrackets() . ')    # link text = $1
                \]
                \(                        # literal paren
                    [ \t\n]*
                    (?|                   # href = $2
                        ([^\s<>]*)
                        |
                        <([^<>\\n]*)>
                    )
                    (?:
                        [ \t\n]+
                        (?|
                            ([\'"])       # quote char = $3
                            (.*)          # title = $4
                            \3            # matching quote
                            |
                            \(            # opening paren
                            ()            # empty quote = $3
                            (.*)          # title = $4
                            \)            # closing paren
                        )
                        [ \t\n]*
                    )?                    # title is optional
                \)
            }x',
            function (Text $whole, Text $linkText, Text $url, Text $q = null, Text $title = null) use ($target) {
                // Replace special characters in the URL
                $url->replaceString(' ', '%20');

                $url->decodeEntities();

                if ($title) {
                    $title->decodeEntities();
                }

                $target->addInline(new Link($url, $linkText, $title));
            },
            function (Text $part) use ($target) {
                $this->parseReferenceLink($part, $target);
            }
        );
    }

    protected function parseReferenceLink(Text $content, InlineNodeAcceptorInterface $target)
    {
        $references = implode('|', array_map(function ($reference) {
            return preg_quote($reference);
        }, $this->context->getReferences()));

        $content->handle(
            '/
                (?:
                    \[
                        (' . $this->getNestedBrackets() . ')    # link text = $1
                    \]
                    [ \t\n]*
                )?<commit_msg>Do not parse backslash-escaped links.<commit_after><?php

namespace FluxBB\CommonMark\Parser\Inline;

use FluxBB\CommonMark\Common\Text;
use FluxBB\CommonMark\Node\InlineNodeAcceptorInterface;
use FluxBB\CommonMark\Node\Link;
use FluxBB\CommonMark\Parser\AbstractInlineParser;

class LinkParser extends AbstractInlineParser
{

    /**
     * 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)
    {
        if ($content->contains('[')) {
            $this->parseInlineLink($content, $target);
        } else {
            $this->next->parseInline($content, $target);
        }
    }

    protected function parseInlineLink(Text $content, InlineNodeAcceptorInterface $target)
    {
        $content->handle(
            '{
                (<!=\\\\)
                \[
                    (' . $this->getNestedBrackets() . ')    # link text = $1
                \]
                \(                        # literal paren
                    [ \t\n]*
                    (?|                   # href = $2
                        ([^\s<>]*)
                        |
                        <([^<>\\n]*)>
                    )
                    (?:
                        [ \t\n]+
                        (?|
                            ([\'"])       # quote char = $3
                            (.*)          # title = $4
                            \3            # matching quote
                            |
                            \(            # opening paren
                            ()            # empty quote = $3
                            (.*)          # title = $4
                            \)            # closing paren
                        )
                        [ \t\n]*
                    )?                    # title is optional
                \)
            }x',
            function (Text $whole, Text $linkText, Text $url, Text $q = null, Text $title = null) use ($target) {
                // Replace special characters in the URL
                $url->replaceString(' ', '%20');

                $url->decodeEntities();

                if ($title) {
                    $title->decodeEntities();
                }

                $target->addInline(new Link($url, $linkText, $title));
            },
            function (Text $part) use ($target) {
                $this->parseReferenceLink($part, $target);
            }
        );
    }

    protected function parseReferenceLink(Text $content, InlineNodeAcceptorInterface $target)
    {
        $references = implode('|', array_map(function ($reference) {
            return preg_quote($reference);
        }, $this->context->getReferences()));

        $content->handle(
            '/
                (<!=\\\\)
                (?:
                    \[
                        (' . $this->getNestedBrackets() . ')    # link text = $1
                    \]
                    [ \t\n]*
                )?
{'fraction_non_alphanumeric': 0.11854863934939006, 'fraction_numerical': 0.0031279324366593683, 'mean_word_length': 1.0333863275039745, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 28, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6591337', 'n_tokens_mistral': 1616, 'n_tokens_neox': 1516, 'n_words': 328}