<commit_before><?php

namespace SilexMarkdown\Parser;

use Radiant\Parser;

class MarkdownExtraExtendedParser extends MarkdownExtraParser {
    # Tags that are always treated as block tags:
    public $block_tags_re = 'figure|figcaption|p|div|h[1-6]|blockquote|pre|table|dl|ol|ul|address|form|fieldset|iframe|hr|legend';

    public function __construct() {
        $this->block_gamut += array(
            "doFencedFigures" => 7,
        );

        parent::__construct();
    }

    public function transform($text) {
        $text = parent::transform($text);
        return $text;
    }

    public function doHardBreaks($text) {
        # Do hard breaks:
        # EXTENDED: changed to allow breaks without two spaces and just one new line
        # original code /* return preg_replace_callback('/ {2,}\n/', */
        return preg_replace_callback('/ *\n/',
            array(&$this, '_doHardBreaks_callback'), $text);
    }


    public function doBlockQuotes($text) {
        $text = preg_replace_callback('/
			(?>^[ ]*>[ ]?
				(?:\((.+?)\))?
				[ ]*(.+\n(?:.+\n)*)
			)+
			/xm',
            array(&$this, '_doBlockQuotes_callback'), $text);

        return $text;
    }

    public function _doBlockQuotes_callback($matches) {
        $cite = $matches[1];
        $bq = '> ' . $matches[2];
        # trim one level of quoting - trim whitespace-only lines
        $bq = preg_replace('/^[ ]*>[ ]?|^[ ]+$/m', '', $bq);
        $bq = $this->runBlockGamut($bq);		# recurse

        $bq = preg_replace('/^/m', "  ", $bq);
        # These leading spaces cause problem with <pre> content,
        # so we need to fix that:
        $bq = preg_replace_callback('{(\s*<pre>.+?</pre>)}sx',
            array(&$this, '_doBlockQuotes_callback2'), $bq);

        $res = "<blockquote";
        $res .= empty($cite) ? ">" : " cite=\"$cite\">";
        $res .= "\n$bq\n</blockquote>";
        return "\n". $this->hashBlock($res)."\n\n";
    }

    public function doFencedCodeBlocks($text) {
        $less_than_tab = $this->tab_width;

        $text = preg_replace_callback('{
				(?:\n|\A)
				# 1: Opening marker
				(
					~{3,}|`{3,} # Marker: three tilde or more.
				)

				[ ]?(\w+)?(?:,[ ]?(\d+))?[ ]* \n # Whitespace and newline following marker.

				# 3: Content
				(
					(?>
						(?!\1 [ ]* \n)	# Not a closing marker.
						.*\n+
					)+
				)

				# Closing marker.
				\1 [ ]* \n
			}xm',
            array(&$this, '_doFencedCodeBlocks_callback'), $text);

        return $text;
    }

    public function _doFencedCodeBlocks_callback($matches) {
        $codeblock = $matches[4];
        $codeblock = htmlspecialchars($codeblock, ENT_NOQUOTES);
        $codeblock = preg_replace_callback('/^\n+/',
            array(&$this, '_doFencedCodeBlocks_newlines'), $codeblock);

        $cb = empty($matches[3]) ? '<pre class="radiant"><code' : "<pre class=\"linenums:$matches[3]\"><code";
        $cb .= empty($matches[2]) ? ">" : " class=\"language-$matches[2]\">";

        if($matches[2] == 'html') {
            $key = trim($codeblock);
            $codeblock = $this->html_hashes[$key];
        }

        $cb .= Parser::transform($matches[2], $codeblock)."</code></pre>";

        return "\n\n".$this->hashBlock($cb)."\n\n";
    }

    public function doFencedFigures($text){
        $text = preg_replace_callback('{
			(?:\n|\A)
			# 1: Opening marker
			(
				={3,} # Marker: equal sign.
			)

			[ ]?(?:\[([^\]]+)\])?[ ]* \n # Whitespace and newline following marker.

			# 3: Content
			(
				(?>
					(?!\1 [ ]?(?:\[([^\]]+)\])?[ ]* \n)	# Not a closing marker.
					.*\n+
				)+
			)<commit_msg>bugfix<commit_after><?php

namespace SilexMarkdown\Parser;

use Radiant\Parser;

class MarkdownExtraExtendedParser extends MarkdownExtraParser {
    # Tags that are always treated as block tags:
    public $block_tags_re = 'figure|figcaption|p|div|h[1-6]|blockquote|pre|table|dl|ol|ul|address|form|fieldset|iframe|hr|legend';

    public function __construct() {
        $this->block_gamut += array(
            "doFencedFigures" => 7,
        );

        parent::__construct();
    }

    public function transform($text) {
        $text = parent::transform($text);
        return $text;
    }

    public function doHardBreaks($text) {
        # Do hard breaks:
        # EXTENDED: changed to allow breaks without two spaces and just one new line
        # original code /* return preg_replace_callback('/ {2,}\n/', */
        return preg_replace_callback('/ *\n/',
            array(&$this, '_doHardBreaks_callback'), $text);
    }


    public function doBlockQuotes($text) {
        $text = preg_replace_callback('/
			(?>^[ ]*>[ ]?
				(?:\((.+?)\))?
				[ ]*(.+\n(?:.+\n)*)
			)+
			/xm',
            array(&$this, '_doBlockQuotes_callback'), $text);

        return $text;
    }

    public function _doBlockQuotes_callback($matches) {
        $cite = $matches[1];
        $bq = '> ' . $matches[2];
        # trim one level of quoting - trim whitespace-only lines
        $bq = preg_replace('/^[ ]*>[ ]?|^[ ]+$/m', '', $bq);
        $bq = $this->runBlockGamut($bq);		# recurse

        $bq = preg_replace('/^/m', "  ", $bq);
        # These leading spaces cause problem with <pre> content,
        # so we need to fix that:
        $bq = preg_replace_callback('{(\s*<pre>.+?</pre>)}sx',
            array(&$this, '_doBlockQuotes_callback2'), $bq);

        $res = "<blockquote";
        $res .= empty($cite) ? ">" : " cite=\"$cite\">";
        $res .= "\n$bq\n</blockquote>";
        return "\n". $this->hashBlock($res)."\n\n";
    }

    public function doFencedCodeBlocks($text) {
        $less_than_tab = $this->tab_width;

        $text = preg_replace_callback('{
				(?:\n|\A)
				# 1: Opening marker
				(
					~{3,}|`{3,} # Marker: three tilde or more.
				)

				[ ]?(\w+)?(?:,[ ]?(\d+))?[ ]* \n # Whitespace and newline following marker.

				# 3: Content
				(
					(?>
						(?!\1 [ ]* \n)	# Not a closing marker.
						.*\n+
					)+
				)

				# Closing marker.
				\1 [ ]* \n
			}xm',
            array(&$this, '_doFencedCodeBlocks_callback'), $text);

        return $text;
    }

    public function _doFencedCodeBlocks_callback($matches) {
        $codeblock = $matches[4];
        $codeblock = htmlspecialchars($codeblock, ENT_NOQUOTES);
        $codeblock = preg_replace_callback('/^\n+/',
            array(&$this, '_doFencedCodeBlocks_newlines'), $codeblock);

        $cb = empty($matches[3]) ? '<pre class="radiant"><code' : "<pre class=\"linenums:$matches[3]\"><code";
        $cb .= empty($matches[2]) ? ">" : " class=\"language-$matches[2]\">";

        if($matches[2] == 'html') {
            $key = trim($codeblock);

            if(isset($this->html_hashes[$key])) {
                $codeblock = $this->html_hashes[$key];
            }
        }

        $cb .= Parser::transform($matches[2], $codeblock)."</code></pre>";

        return "\n\n".$this->hashBlock($cb)."\n\n";
    }

    public function doFencedFigures($text){
        $text = preg_replace_callback('{
			(?:\n|\A)
			# 1: Opening marker
			(
				={3,} # Marker: equal sign.
			)

			[ ]?(?:\[([^\]]+)\])?[ ]* \n # Whitespace and newline following marker.

			# 3: Content
			(
				(?>
					(?!\1 [ ]?(?:\[([^\]]+)\])?[ ]* \n)	# Not a closing marker.
					.*\n+
				)+
			)
{'fraction_non_alphanumeric': 0.2012077957727148, 'fraction_numerical': 0.006587976942080703, 'mean_word_length': 2.3549723756906076, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 54, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1788286', 'n_tokens_mistral': 2765, 'n_tokens_neox': 2457, 'n_words': 473}