<commit_before><?php
/*
 * Copyright (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace PHPoole\Renderer;

use PHPoole\Renderer\Twig\Extension as TwigExtension;
use Symfony\Component\Filesystem\Filesystem;

/**
 * Class Twig.
 */
class Twig implements RendererInterface
{
    /**
     * @var \Twig_Environment
     */
    protected $twig;
    /**
     * @var string
     */
    protected $templatesDir;
    /**
     * @var string
     */
    protected $rendered;
    /**
     * @var Filesystem
     */
    protected $fs;
    /**
     * @var bool
     */
    protected $twigStrict = true;
    /**
     * @var bool
     */
    protected $twigDebug = true;
    /*
     * @var string|bool
     */
    protected $twigCache = false;

    /**
     * {@inheritdoc}
     */
    public function __construct($templatesPath, $config)
    {
        // internal layouts
        $internalLoader = [];
        if ($internalLayouts = $config->get('layouts.internal')) {
            foreach ($internalLayouts as $layout => $path) {
                $internalLoader[sprintf('%s%s.twig', (($path) ? $path : ''), $layout)] = file_get_contents(sprintf(__DIR__.'/../../res/layouts/%s.twig', $layout));
            }
        }
        $loaderArray = new \Twig_Loader_Array($internalLoader);
        // project layouts
        $loaderFS = new \Twig_Loader_Filesystem($templatesPath);
        // load layouts
        $loader = new \Twig_Loader_Chain([$loaderFS, $loaderArray]);
        $this->twig = new \Twig_Environment($loader,
            [
                'autoescape'       => false,
                'strict_variables' => $this->twigStrict,
                'debug'            => $this->twigDebug,
                'cache'            => $this->twigCache,
            ]
        );
        // add extensions
        $this->twig->addExtension(new \Twig_Extension_Debug());
        $this->twig->addExtension(new TwigExtension($config->getOutputPath()));
        $this->twig->getExtension('core')->setDateFormat($config->get('site.date.format'));
        $this->twig->getExtension('core')->setTimezone($config->get('site.date.timezone'));

        $this->fs = new Filesystem();
    }

    /**
     * {@inheritdoc}
     */
    public function addGlobal($name, $value)
    {
        $this->twig->addGlobal($name, $value);
    }

    /**
     * {@inheritdoc}
     */
    public function render($template, $variables)
    {
        $this->rendered = $this->twig->render($template, $variables);

        // add generator meta
        if (!preg_match('/<meta name="generator".*/i', $this->rendered)) {
            $meta = '<meta name="generator" content="PHPoole" />';
            $this->rendered = preg_replace('/(<head.*>)/i', '$1'."\n\t".$meta, $this->rendered);<commit_msg>Enhance regex that detects <head>.<commit_after><?php
/*
 * Copyright (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace PHPoole\Renderer;

use PHPoole\Renderer\Twig\Extension as TwigExtension;
use Symfony\Component\Filesystem\Filesystem;

/**
 * Class Twig.
 */
class Twig implements RendererInterface
{
    /**
     * @var \Twig_Environment
     */
    protected $twig;
    /**
     * @var string
     */
    protected $templatesDir;
    /**
     * @var string
     */
    protected $rendered;
    /**
     * @var Filesystem
     */
    protected $fs;
    /**
     * @var bool
     */
    protected $twigStrict = true;
    /**
     * @var bool
     */
    protected $twigDebug = true;
    /*
     * @var string|bool
     */
    protected $twigCache = false;

    /**
     * {@inheritdoc}
     */
    public function __construct($templatesPath, $config)
    {
        // internal layouts
        $internalLoader = [];
        if ($internalLayouts = $config->get('layouts.internal')) {
            foreach ($internalLayouts as $layout => $path) {
                $internalLoader[sprintf('%s%s.twig', (($path) ? $path : ''), $layout)] = file_get_contents(sprintf(__DIR__.'/../../res/layouts/%s.twig', $layout));
            }
        }
        $loaderArray = new \Twig_Loader_Array($internalLoader);
        // project layouts
        $loaderFS = new \Twig_Loader_Filesystem($templatesPath);
        // load layouts
        $loader = new \Twig_Loader_Chain([$loaderFS, $loaderArray]);
        $this->twig = new \Twig_Environment($loader,
            [
                'autoescape'       => false,
                'strict_variables' => $this->twigStrict,
                'debug'            => $this->twigDebug,
                'cache'            => $this->twigCache,
            ]
        );
        // add extensions
        $this->twig->addExtension(new \Twig_Extension_Debug());
        $this->twig->addExtension(new TwigExtension($config->getOutputPath()));
        $this->twig->getExtension('core')->setDateFormat($config->get('site.date.format'));
        $this->twig->getExtension('core')->setTimezone($config->get('site.date.timezone'));

        $this->fs = new Filesystem();
    }

    /**
     * {@inheritdoc}
     */
    public function addGlobal($name, $value)
    {
        $this->twig->addGlobal($name, $value);
    }

    /**
     * {@inheritdoc}
     */
    public function render($template, $variables)
    {
        $this->rendered = $this->twig->render($template, $variables);

        // add generator meta
        if (!preg_match('/<meta name="generator".*/i', $this->rendered)) {
            $meta = '<meta name="generator" content="PHPoole" />';
            $this->rendered = preg_replace('/(<head>|<head[[:space:]]+.*>)/i', '$1'."\n\t".$meta, $this->rendered);
{'fraction_non_alphanumeric': 0.1673036831132731, 'fraction_numerical': 0.00034746351633078526, 'mean_word_length': 2.1701541850220263, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 79, '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': '2810637', 'n_tokens_mistral': 1742, 'n_tokens_neox': 1715, 'n_words': 317}