<commit_before><?php

namespace Mos\TextFilter;

/**
 * Shortcode to format when working with text.
 *
 */
trait TShortcode
{
    /**
     * Shortcode to quicker format text as HTML.
     *
     * @param string $text text to be converted.
     *
     * @return string the formatted text.
     */
    public function shortCode($text)
    {
        /* Needs PHP 7
        $patternsAndCallbacks = [
            "/\[(FIGURE)[\s+](.+)\]/" => function ($match) {
                return self::ShortCodeFigure($matches[2]);
            },
            "/(```([\w]*))\n([^`]*)```[\n]{1}/s" => function ($match) {
                return $this->syntaxHighlightGeSHi($matches[3], $matches[2]);
            },
        ];

        return preg_replace_callback_array($patternsAndCallbacks, $text);
        */

        $patterns = [
            "/\[(FIGURE)[\s+](.+)\]/",
            //'/\[(YOUTUBE) src=(.+) width=(.+) caption=(.+)\]/',
            "/\[(YOUTUBE)[\s+](.+)\]/",
            "/\[(ASCIINEMA)[\s+](.+)\]/",
            "/\[(BOOK)[\s+](.+)\]/",
            "/(```)([\w]*)\n([^`]*)```[\n]{1}/s",
            '/\[(INFO)\]/',
            '/\[(\/INFO)\]/',
        ];

        return preg_replace_callback(
            $patterns,
            function ($matches) {
                switch ($matches[1]) {
                    case "FIGURE":
                        return self::shortCodeFigure($matches[2]);
                    break;

                    case "YOUTUBE":
                        return self::shortCodeYoutube($matches[2]);
                    break;

                    case "ASCIINEMA":
                        return self::shortCodeAsciinema($matches[2]);
                    break;

                    case "BOOK":
                        return self::shortCodeBook($matches[2]);
                    break;

                    case "```":
                        //return $this->syntaxHighlightGeSHi($matches[3], $matches[2]);
                        return $this->syntaxHighlightJs($matches[3], $matches[2]);
                    break;

                    case 'INFO':
                        return <<<EOD
<div class="info">
    <span class="icon fa-stack fa-lg">
        <i class="fa fa-circle fa-stack-2x"></i>
        <i class="fa fa-info fa-stack-1x fa-inverse" aria-hidden="true"></i>
    </span>
    <div markdown=1>
EOD;
                        break;

                    case '/INFO':
                        return "</div></div>";
                        break;

                    default:
                        return "{$matches[1]} is unknown shortcode.";
                }
            },
            $text
        );
    }



    /**
     * Init shortcode handling by preparing the option list to an array,
     * for those using arguments.
     *
     * @param string $options for the shortcode.
     *
     * @return array with all the options.
     */
    public static function shortCodeInit($options)
    {
        preg_match_all('/[a-zA-Z0-9]+="[^"]+"|\S+/', $options, $matches);

        $res = array();
        foreach ($matches[0] as $match) {
            $pos = strpos($match, '=');
            if ($pos === false) {
                $res[$match] = true;
            } else {
                $key = substr($match, 0, $pos);
                $val = trim(substr($match, $pos+1), '"');
                $res[$key] = $val;
            }
        }

        return $res;
    }



    /**
     * Shortcode for [YOUTUBE].
     *
     * Usage example: [YOUTUBE src=id-for-the-tube width=630 caption=""]
     *
     * @param string $options for the shortcode.
     *
     * @return array with all the options.
     */
    public static function shortCodeYoutube($options)
    {
        $options= array_merge(
            [
                "src" => null,
                "width" => null,
                "caption" => null,
                "ratio" => 16/9
            ],
            self::ShortCodeInit($options)
        );
        extract($options, EXTR_SKIP);

        $caption = t("Figure: !CAPTION", ["!CAPTION" => $caption]);
        $height = ceil($width / $ratio);

        // @codingStandardsIgnoreStart
        $html = <<<EOD
<figure>
<iframe width="$width" height="$height" src="http://www.youtube.com/embed/$src" frameborder="0" allowfullscreen></iframe>
<figcaption markdown=1>{$caption}</figcaption>
</figure>
EOD;
        // @codingStandardsIgnoreEnd

        return $html;
    }



    /**
     * Shortcode for <figure>.
     *
     * Usage example: [FIGURE src="img/home/me.jpg" caption="Me" alt="Bild på mig" nolink="nolink"]
     *
     * @param string $options for the shortcode.
     *
     * @return array with all the options.<commit_msg>Use https for youtube videos<commit_after><?php

namespace Mos\TextFilter;

/**
 * Shortcode to format when working with text.
 *
 */
trait TShortcode
{
    /**
     * Shortcode to quicker format text as HTML.
     *
     * @param string $text text to be converted.
     *
     * @return string the formatted text.
     */
    public function shortCode($text)
    {
        /* Needs PHP 7
        $patternsAndCallbacks = [
            "/\[(FIGURE)[\s+](.+)\]/" => function ($match) {
                return self::ShortCodeFigure($matches[2]);
            },
            "/(```([\w]*))\n([^`]*)```[\n]{1}/s" => function ($match) {
                return $this->syntaxHighlightGeSHi($matches[3], $matches[2]);
            },
        ];

        return preg_replace_callback_array($patternsAndCallbacks, $text);
        */

        $patterns = [
            "/\[(FIGURE)[\s+](.+)\]/",
            //'/\[(YOUTUBE) src=(.+) width=(.+) caption=(.+)\]/',
            "/\[(YOUTUBE)[\s+](.+)\]/",
            "/\[(ASCIINEMA)[\s+](.+)\]/",
            "/\[(BOOK)[\s+](.+)\]/",
            "/(```)([\w]*)\n([^`]*)```[\n]{1}/s",
            '/\[(INFO)\]/',
            '/\[(\/INFO)\]/',
            '/\[(WARNING)\]/',
            '/\[(\/WARNING)\]/',
        ];

        return preg_replace_callback(
            $patterns,
            function ($matches) {
                switch ($matches[1]) {
                    case "FIGURE":
                        return self::shortCodeFigure($matches[2]);
                    break;

                    case "YOUTUBE":
                        return self::shortCodeYoutube($matches[2]);
                    break;

                    case "ASCIINEMA":
                        return self::shortCodeAsciinema($matches[2]);
                    break;

                    case "BOOK":
                        return self::shortCodeBook($matches[2]);
                    break;

                    case "```":
                        //return $this->syntaxHighlightGeSHi($matches[3], $matches[2]);
                        return $this->syntaxHighlightJs($matches[3], $matches[2]);
                    break;

                    case 'INFO':
                        return <<<EOD
<div class="info">
    <span class="icon fa-stack fa-lg">
        <i class="fa fa-circle fa-stack-2x"></i>
        <i class="fa fa-info fa-stack-1x fa-inverse" aria-hidden="true"></i>
    </span>
    <div markdown=1>
EOD;
                        break;

                    case 'WARNING':
                        return <<<EOD
<div class="warning">
    <span class="icon fa-stack fa-lg">
        <i class="fa fa-circle fa-stack-2x"></i>
        <i class="fa fa-exclamation-triangle fa-stack-1x fa-inverse" aria-hidden="true"></i>
    </span>
    <div markdown=1>
EOD;
                        break;

                    case '/INFO':
                    case '/WARNING':
                        return "</div></div>";
                        break;

                    default:
                        return "{$matches[1]} is unknown shortcode.";
                }
            },
            $text
        );
    }



    /**
     * Init shortcode handling by preparing the option list to an array,
     * for those using arguments.
     *
     * @param string $options for the shortcode.
     *
     * @return array with all the options.
     */
    public static function shortCodeInit($options)
    {
        preg_match_all('/[a-zA-Z0-9]+="[^"]+"|\S+/', $options, $matches);

        $res = array();
        foreach ($matches[0] as $match) {
            $pos = strpos($match, '=');
            if ($pos === false) {
                $res[$match] = true;
            } else {
                $key = substr($match, 0, $pos);
                $val = trim(substr($match, $pos+1), '"');
                $res[$key] = $val;
            }
        }

        return $res;
    }



    /**
     * Shortcode for [YOUTUBE].
     *
     * Usage example: [YOUTUBE src=id-for-the-tube width=630 caption=""]
     *
     * @param string $options for the shortcode.
     *
     * @return array with all the options.
     */
    public static function shortCodeYoutube($options)
    {
        $options= array_merge(
            [
                "src" => null,
                "width" => null,
                "caption" => null,
                "ratio" => 16/9
            ],
            self::ShortCodeInit($options)
        );
        extract($options, EXTR_SKIP);

        $caption = t("Figure: !CAPTION", ["!CAPTION" => $caption]);
        $height = ceil($width / $ratio);

        // @codingStandardsIgnoreStart
        $html = <<<EOD
<figure>
<iframe width="$width" height="$height" src="https://www.youtube.com/embed/$src" frameborder="0" allowfullscreen></iframe>
<figcaption markdown=1>{$caption}</figcaption>
</figure>
EOD;
        // @codingStandardsIgnoreEnd

        return $html;
    }



    /**
     * Shortcode for <figure>.
     *
     * Usage example: [FIGURE src="img/home/me.jpg" caption="Me" alt="Bild på mig" nolink="nolink"]
     *
     * @param string $options for the shortcode.
     *
     * @return array with all the options.
{'fraction_non_alphanumeric': 0.17019113460756405, 'fraction_numerical': 0.0068117120780805205, 'mean_word_length': 1.4217134416543575, 'pattern_counts': {'":': 10, '<': 62, '<?xml version=': 0, '>': 65, 'https://': 1, 'lorem ipsum': 0, 'www.': 2, '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': '218308', 'n_tokens_mistral': 3005, 'n_tokens_neox': 2802, 'n_words': 596}