<commit_before><?php

namespace monolyth\render;
use monolyth\core\Parser;
use Adapter_Access;
use monolyth\adapter\sql\NoResults_Exception;
use monolyth\Media_Model;

class Media_Parser extends Parser
{
    use Adapter_Access;

    public function __invoke($html)
    {
        if (!preg_match_all(
            "@
                (<|&lt;)img.*?src=(\"|&quot;)
                    {media:(\d+|[0-9a-f]{32}):(jpe?g|png|gif)}
                (\"|&quot;).*?(>|&gt;)
            @mx",
            $html,
            $matches,
            PREG_SET_ORDER
        )) {
            return $html;
        }
        $ids = [];
        foreach ($matches as $match) {
            $ids[] = $match[3];
        }
        try {
            $imgs = [];
            foreach (self::adapter()->rows(
                'monolyth_media',
                '*',
                [['id' => ['IN' => $ids], 'md5' => ['IN' => $ids]]]
            ) as $row) {
                $imgs[$row['id']] = $imgs[$row['md5']] = $row;
            }
        } catch (NoResults_Exception $e) {
            // None found; just strip everything.
            $replace = [];
            foreach ($matches as $match) {
                $replace[] = $match[0];
            }
            return str_replace($replace, '', $html);
        }
        $new = $old = [];
        $media = new Media_Model;
        foreach ($matches as $match) {
            $old[] = $match[0];
            if (!isset($imgs[$match[3]])) {
                $new[] = '';
                continue;
            }
            $ext = $match[4];
            if ($ext == 'jpeg') {
                $ext = 'jpg';
            }
            $width = $height = 0;
            if ($sizes = preg_match_all(
                '@(width|height)=("|&quot;)(\d+)("|&quot;)@m',
                $match[0],
                $m
            )) {
                foreach ($m[1] as $i => $ma) {
                    $$ma = $m[3][$i];
                }
            } elseif (preg_match(
                    '@style="(.*?)"@',
                    $match[0],
                    $tmp
                )
                && preg_match_all(
                    '@(width|height):\s+(\d+)px@m',
                    $tmp[1],
                    $m
                )
            ) {
                foreach ($m[1] as $i => $ma) {
                    $$ma = $m[2][$i];
                }
            }
            $new[] = str_replace(
                "src={$match[2]}{media:{$match[3]}:{$match[4]}}{$match[2]}",
                sprintf(
                    'src="%s"',
                    $media->http(
                        $imgs[$match[3]],
                        compact('width', 'height')
                    )
                ),
                $match[0]
            );
        }
        return str_replace($old, $new, $html);
    }
}
<commit_msg>not happy with this, but works for now<commit_after><?php

namespace monolyth\render;
use monolyth\core\Parser;
use Adapter_Access;
use monolyth\adapter\sql\NoResults_Exception;
use monolyth\Media_Model;

class Media_Parser extends Parser
{
    use Adapter_Access;

    public function __invoke($html)
    {
        if (!preg_match_all(
            "@
                (<|&lt;)img.*?src=(\"|&quot;)
                    {media:(\d+|[0-9a-f]{32}):(jpe?g|png|gif)}
                (\"|&quot;).*?(>|&gt;)
            @mx",
            $html,
            $matches,
            PREG_SET_ORDER
        )) {
            return $html;
        }
        $ids = [];
        foreach ($matches as $match) {
            $ids[] = $match[3];
        }
        try {
            $imgs = [];
            foreach (self::adapter()->rows(
                'monolyth_media',
                '*',
                [['id' => ['IN' => $ids], 'md5' => ['IN' => $ids]]]
            ) as $row) {
                $imgs[$row['id']] = $imgs[$row['md5']] = $row;
            }
        } catch (NoResults_Exception $e) {
            // None found; just strip everything.
            $replace = [];
            foreach ($matches as $match) {
                $replace[] = $match[0];
            }
            return str_replace($replace, '', $html);
        }
        $new = $old = [];
        $media = new Media_Model;
        foreach ($matches as $match) {
            $old[] = $match[0];
            if (!isset($imgs[$match[3]])) {
                $new[] = '';
                continue;
            }
            $ext = $match[4];
            if ($ext == 'jpeg') {
                $ext = 'jpg';
            }
            $width = $height = 0;
            if ($sizes = preg_match_all(
                '@(width|height)=("|&quot;)(\d+)("|&quot;)@m',
                $match[0],
                $m
            )) {
                foreach ($m[1] as $i => $ma) {
                    $$ma = $m[3][$i];
                }
            } elseif (preg_match(
                    '@style="(.*?)"@',
                    $match[0],
                    $tmp
                )
                && preg_match_all(
                    '@(width|height):\s+(\d+)px@m',
                    $tmp[1],
                    $m
                )
            ) {
                foreach ($m[1] as $i => $ma) {
                    $$ma = $m[2][$i];
                }
            }
            $new[] = str_replace(
                "src={$match[2]}{media:{$match[3]}:{$match[4]}}{$match[2]}",
                sprintf(
                    'src="%s"',
                    $media->media->http(
                        $imgs[$match[3]],
                        compact('width', 'height')
                    )
                ),
                $match[0]
            );
        }
        return str_replace($old, $new, $html);
    }
}

{'fraction_non_alphanumeric': 0.1757373595505618, 'fraction_numerical': 0.00877808988764045, 'mean_word_length': 1.0252399573409172, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 22, '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': '3479587', 'n_tokens_mistral': 1775, 'n_tokens_neox': 1690, 'n_words': 287}