<commit_before><?php

namespace Bolt\Extension\cdowdy\ResponsiveImages;

use Bolt;
use Bolt\BaseExtension;
use Bolt\Library as Lib;

/**
 * Class Extension
 * @package Bolt\Extension\cdowdy\ResponsiveImages
 */
class Extension extends BaseExtension
{

    public function initialize()
    {
        $this->addTwigFunction( 'respImg', 'respImg' );

        /**
         * since there is no head function or any reliable way to insert anything in to the head in Bolt we have to
         * hackishly insert picturefill into the head this way.
         *
         * first we assign a variable ($extensionsPath) to the base URL
         * then insert that variable into a heredoc
         */

        $pictureFillJS = $this->getBaseUrl() . 'assets/picturefill.min.js';
        $pictureFill   = <<<PFILL
<script src="{$pictureFillJS}" async defer></script>
PFILL;
        // insert snippet after the last CSS file in the head
        $this->addSnippet( 'aftercss', $pictureFill );


        // for browsers that don't understand <picture> element
        $picElement = <<<PICELEM
<script>document.createElement( "picture" );</script>
PICELEM;
        // insert snippet after the last CSS file in the head
        $this->addSnippet( 'aftercss', $picElement );

    }

    /**
     * @return string
     */
    public function getName()
    {
        return "responsiveimages";
    }


    /**
     * @param string $filename
     * @param string $sizing
     * @param string $altText
     *
     * @return \Twig_Markup
     */
    public function respImg( $filename = '',  $sizing = '', $altText = '' )
    {
        // load up twig template directory
        $this->app['twig.loader.filesystem']->addPath( __DIR__ . "/assets" );


        // set variable for cropping to use in $sizing switch statement
        $thumbconf = $this->config['cropping'];

        // Bolt doesn't have alt text stored or a description for images so let the person using this twig tag to set it
        // if they don't set it strip the file extension and use that.. .even though that isn't a good enough alt text
        // its better than having no alt text.
        if (empty($altText) ) {

            $tempAltText = pathinfo($filename);
            $altText = $tempAltText['filename'];
        }


        // After v1.5.1 we store image data as an array
        if (is_array( $filename )) {
            $filename = isset( $filename['filename'] ) ? $filename['filename'] : $filename['file'];
        }


        //  Get the Sizes for the Images
        // small image
        if (isset( $this->config['sizes']['small'][0] )) {
            $width1 = $this->config['sizes']['small'][0];
        } else {
            $width1 = 320;
        }

        // middle medium image
        if (isset( $this->config['sizes']['mid'] )) {
            $width2 = $this->config['sizes']['mid'];
        } else {
            $width2 = 640;
        }

        // medium image
        if (isset( $this->config['sizes']['medium'] )) {
            $width3 = $this->config['sizes']['medium'];
        } else {
            $width3 = 800;
        }

        // large image
        if (isset( $this->config['sizes']['large'] )) {
            $width4 = $this->config['sizes']['large'];
        } else {
            $width4 = 1100;
        }

        /**
         * switch statement to determine cropping/resizing of image
         */
        switch ($sizing) {
            case 'fit':
            case 'f':
                $scale = 'f';
                break;

            case 'resize':
            case 'r':
                $scale = 'r';
                break;

            case 'borders':
            case 'b':
                $scale = 'b';
                break;

            case 'crop':
            case 'c':
                $scale = 'c';
                break;

            default:
                $scale = ! empty( $thumbconf ) ? $thumbconf : 'c';
        }


        // Test For Small Images
        // Get the image path ( either by filename or record.image )
        // then get the widths, crop
        $smallImg = sprintf(
            '%sthumbs/%sx%s%s/%s',
            $this->app['paths']['root'],
            round( $width1 ),
            round( $height1 ),
            $scale,
            Lib::safeFilename( $filename )
        );

        $midImg = sprintf(
            '%sthumbs/%sx%s%s/%s',
            $this->app['paths']['root'],
            round( $width2 ),
            round( $height2 ),
            $scale,
            Lib::safeFilename( $filename )
        );

        $medImg = sprintf(
            '%sthumbs/%sx%s%s/%s',
            $this->app['paths']['root'],
            round( $width3 ),
            round( $height3 ),
            $scale,
            Lib::safeFilename( $filename )
        );

        $lrgImg = sprintf(
            '%sthumbs/%sx%s%s/%s',
            $this->app['paths']['root'],
            round( $width4 ),
            round( $height4 ),
            $scale,
            Lib::safeFilename( $filename )
        );


        $imgSource = $this->app['render']->render( 'respImg.srcset.html.twig', array(
            'smallImg' => $smallImg,
            'midImg'   => $midImg,
            'medImg'   => $medImg,
            'lrgImg'   => $lrgImg,
            'scale'    => $scale,
            'width1'   => $width1,
            'width2'   => $width2,
            'width3'   => $width3,
            'width4'   => $width4,
            'altText'   => $altText
        ) );

        return new \Twig_Markup( $imgSource, 'UTF-8' );
    }

    public function isSafe()
    {
        return true;
    }

}





<commit_msg>initial template switching code<commit_after><?php

namespace Bolt\Extension\cdowdy\ResponsiveImages;

use Bolt;
use Bolt\BaseExtension;
use Bolt\Library as Lib;

/**
 * Class Extension
 * @package Bolt\Extension\cdowdy\ResponsiveImages
 */
class Extension extends BaseExtension
{

    public function initialize()
    {
        $this->addTwigFunction( 'respImg', 'respImg' );

        /**
         * since there is no head function or any reliable way to insert anything in to the head in Bolt we have to
         * hackishly insert picturefill into the head this way.
         *
         * first we assign a variable ($extensionsPath) to the base URL
         * then insert that variable into a heredoc
         */

        $pictureFillJS = $this->getBaseUrl() . 'assets/picturefill.min.js';
        $pictureFill   = <<<PFILL
<script src="{$pictureFillJS}" async defer></script>
PFILL;
        // insert snippet after the last CSS file in the head
        $this->addSnippet( 'aftercss', $pictureFill );


        // for browsers that don't understand <picture> element
        $picElement = <<<PICELEM
<script>document.createElement( "picture" );</script>
PICELEM;
        // insert snippet after the last CSS file in the head
        $this->addSnippet( 'aftercss', $picElement );

    }

    /**
     * @return string
     */
    public function getName()
    {
        return "responsiveimages";
    }


    /**
     * @param string $filename
     * @param string $sizing
     * @param string $altText
     * @param string $tag
     *
     * @return \Twig_Markup
     */
    public function respImg( $filename = '',  $sizing = '', $altText = '', $tag = '' )
    {
        // load up twig template directory
        $this->app['twig.loader.filesystem']->addPath( __DIR__ . "/assets" );


        // set variable for cropping to use in $sizing switch statement
        $thumbconf = $this->config['cropping'];

        // Bolt doesn't have alt text stored or a description for images so let the person using this twig tag to set it
        // if they don't set it strip the file extension and use that.. .even though that isn't a good enough alt text
        // its better than having no alt text.
        if ( empty( $altText ) ) {

            $tempAltText    = pathinfo($filename);
            $altText        = $tempAltText['filename'];
        }


        // which responsive solution to use
        switch( $tag ) {
            case 'img':
                $template = 'respImg.srcset.html.twig';
                break;

            case 'picture':
                $template = 'respImg.picture.html.twig';
                break;

            default:
                $template = 'respImg.srcset.html.twig';
        }


        // After v1.5.1 we store image data as an array
        if (is_array( $filename )) {
            $filename = isset( $filename['filename'] ) ? $filename['filename'] : $filename['file'];
        }


        //  Get the Sizes for the Images
        // small image
        if (isset( $this->config['sizes']['small'][0] )) {
            $width1 = $this->config['sizes']['small'][0];
        } else {
            $width1 = 320;
        }

        // middle medium image
        if (isset( $this->config['sizes']['mid'] )) {
            $width2 = $this->config['sizes']['mid'];
        } else {
            $width2 = 640;
        }

        // medium image
        if (isset( $this->config['sizes']['medium'] )) {
            $width3 = $this->config['sizes']['medium'];
        } else {
            $width3 = 800;
        }

        // large image
        if (isset( $this->config['sizes']['large'] )) {
            $width4 = $this->config['sizes']['large'];
        } else {
            $width4 = 1100;
        }

        /**
         * switch statement to determine cropping/resizing of image
         */
        switch ($sizing) {
            case 'fit':
            case 'f':
                $scale = 'f';
                break;

            case 'resize':
            case 'r':
                $scale = 'r';
                break;

            case 'borders':
            case 'b':
                $scale = 'b';
                break;

            case 'crop':
            case 'c':
                $scale = 'c';
                break;

            default:
                $scale = ! empty( $thumbconf ) ? $thumbconf : 'c';
        }


        // Test For Small Images
        // Get the image path ( either by filename or record.image )
        // then get the widths, crop
        $smallImg = sprintf(
            '%sthumbs/%sx%s%s/%s',
            $this->app['paths']['root'],
            round( $width1 ),
            round( $height1 ),
            $scale,
            Lib::safeFilename( $filename )
        );

        $midImg = sprintf(
            '%sthumbs/%sx%s%s/%s',
            $this->app['paths']['root'],
            round( $width2 ),
            round( $height2 ),
            $scale,
            Lib::safeFilename( $filename )
        );

        $medImg = sprintf(
            '%sthumbs/%sx%s%s/%s',
            $this->app['paths']['root'],
            round( $width3 ),
            round( $height3 ),
            $scale,
            Lib::safeFilename( $filename )
        );

        $lrgImg = sprintf(
            '%sthumbs/%sx%s%s/%s',
            $this->app['paths']['root'],
            round( $width4 ),
            round( $height4 ),
            $scale,
            Lib::safeFilename( $filename )
        );


        $imgSource = $this->app['render']->render( $template, array(
            'smallImg' => $smallImg,
            'midImg'   => $midImg,
            'medImg'   => $medImg,
            'lrgImg'   => $lrgImg,
            'scale'    => $scale,
            'width1'   => $width1,
            'width2'   => $width2,
            'width3'   => $width3,
            'width4'   => $width4,
            'altText'   => $altText
        ) );

        return new \Twig_Markup( $imgSource, 'UTF-8' );
    }

    public function isSafe()
    {
        return true;
    }

}






{'fraction_non_alphanumeric': 0.13893182208483598, 'fraction_numerical': 0.007384509702902284, 'mean_word_length': 1.5508103372755146, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 75, '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': '7077410', 'n_tokens_mistral': 3419, 'n_tokens_neox': 3286, 'n_words': 876}