<commit_before><?php

namespace Knp\IpsumBundle\Twig\Extension;

use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Bundle\TwigBundle\Loader\FilesystemLoader;

class IpsumExtension extends \Twig_Extension
{
    protected $loader;
    protected $controller;

    public function __construct(FilesystemLoader $loader)
    {
        $this->loader = $loader;
    }

    public function setController($controller)
    {
        $this->controller = $controller;
    }

    /**
     * {@inheritdoc}
     */
    public function getFunctions()
    {
        return array(
            'code' => new \Twig_Function_Method($this, 'getCode', array('is_safe' => array('html'))),
        );
    }

    protected function cleanCode($code)
    {
        return rtrim(preg_replace('@^( *\n)*(.*)@s', '$2', htmlspecialchars($code, ENT_QUOTES, 'UTF-8')), "\n\t \r");
    }

    public function getCode($template)
    {
        $controllerPath = $this->getControllerPath();
        $templatePath = $this->getTemplatePath($template);

        $controller = preg_replace('/^\s{4}/m', '', $this->cleanCode($this->getControllerCode()));
        $template = $this->getTemplateCode($template);

        // remove the code block
        $template = str_replace('{% set code = code(_self) %}', '', $template);
        $template = $this->cleanCode($template);

        $featureName = preg_replace('/^.*Controller\/([^\/]+)Controller\.php\:\d+$/', '$1', $controllerPath);
        $featurePath = realpath(__DIR__ . '/../../Features/'.$featureName.'.feature');
        if (file_exists($featurePath)) {
            $feature = file_get_contents($featurePath);
            $featurePath = preg_replace('/^.*(src\/Knp\/IpsumBundle\/Features)/', '$1', $featurePath);
            $featureContent = <<<FEATURE
<h3>User Story</h3>
<span class="code_path">$featurePath</span>
<pre class="code_block"><code>$feature</code></pre>
FEATURE;
        } else {
            $featureContent = '';
        }
<commit_msg>escape example tags in features on printing<commit_after><?php

namespace Knp\IpsumBundle\Twig\Extension;

use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Bundle\TwigBundle\Loader\FilesystemLoader;

class IpsumExtension extends \Twig_Extension
{
    protected $loader;
    protected $controller;

    public function __construct(FilesystemLoader $loader)
    {
        $this->loader = $loader;
    }

    public function setController($controller)
    {
        $this->controller = $controller;
    }

    /**
     * {@inheritdoc}
     */
    public function getFunctions()
    {
        return array(
            'code' => new \Twig_Function_Method($this, 'getCode', array('is_safe' => array('html'))),
        );
    }

    protected function cleanCode($code)
    {
        return rtrim(preg_replace('@^( *\n)*(.*)@s', '$2', htmlspecialchars($code, ENT_QUOTES, 'UTF-8')), "\n\t \r");
    }

    public function getCode($template)
    {
        $controllerPath = $this->getControllerPath();
        $templatePath = $this->getTemplatePath($template);

        $controller = preg_replace('/^\s{4}/m', '', $this->cleanCode($this->getControllerCode()));
        $template = $this->getTemplateCode($template);

        // remove the code block
        $template = str_replace('{% set code = code(_self) %}', '', $template);
        $template = $this->cleanCode($template);

        $featureName = preg_replace('/^.*Controller\/([^\/]+)Controller\.php\:\d+$/', '$1', $controllerPath);
        $featurePath = realpath(__DIR__ . '/../../Features/'.$featureName.'.feature');
        if (file_exists($featurePath)) {
            $feature = htmlspecialchars(file_get_contents($featurePath), ENT_QUOTES, 'UTF-8');
            $featurePath = preg_replace('/^.*(src\/Knp\/IpsumBundle\/Features)/', '$1', $featurePath);
            $featureContent = <<<FEATURE
<h3>User Story</h3>
<span class="code_path">$featurePath</span>
<pre class="code_block"><code>$feature</code></pre>
FEATURE;
        } else {
            $featureContent = '';
        }

{'fraction_non_alphanumeric': 0.17664596273291924, 'fraction_numerical': 0.0037267080745341614, 'mean_word_length': 3.026, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 39, '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': '411600', 'n_tokens_mistral': 1219, 'n_tokens_neox': 1198, 'n_words': 208}