<commit_before><?php

namespace Phpillip\Service;

use RuntimeException;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Process\Process;

/**
 * Pygments code highlight
 */
class Pygments
{
    /**
     * File system
     *
     * @var FileSystem
     */
    protected $files;

    /**
     * Temporary directory path
     *
     * @var string
     */
    protected $tmp;

    /**
     * Constructor
     *
     * @param string $tmp
     */
    public function __construct($tmp = null)
    {
        $this->tmp   = $tmp ?: sys_get_temp_dir();
        $this->files = new Filesystem();
    }

    /**
     * Highlight a portion of code with pygmentize
     *
     * @param string $value
     * @param string $language
     *
     * @return string
     */
    public function highlight($value, $language)
    {
        $path = tempnam($this->tmp, 'pyg');

        if ($language === 'php' && substr($value, 0, 5) !== '<?php') {
            $value = '<?php ' . PHP_EOL . $value;
        }

        $this->files->dumpFile($path, $value);

        $value = $this->pygmentize($path, $language);

        unlink($path);

        if (preg_match('#^<div class="highlight"><pre>((.|[\r\n])+)</pre></div>$#', $value, $matches)) {
            return $matches[1];
        }

        return $value;
    }

    /**
     * Run 'pygmentize' command on the given file
     *
     * @param string $path
     * @param string $language
     *
     * @return string
     */
    public function pygmentize($path, $language)
    {
        $process = new Process(sprintf('pygmentize -f html -l %s %s', $language, $path));

        $process->run();

        if (!$process->isSuccessful()) {
            throw new RuntimeException($process->getErrorOutput());
        }

        return $process->getOutput();
    }

    /**
     * Is pygmentize available?
     *
     * @return boolean
     */
    public static function isAvailable()
    {
        $process = new Process('pygmentize -V');

        $process->run();<commit_msg>Fixed Pygments escape<commit_after><?php

namespace Phpillip\Service;

use RuntimeException;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Process\Process;

/**
 * Pygments code highlight
 */
class Pygments
{
    /**
     * File system
     *
     * @var FileSystem
     */
    protected $files;

    /**
     * Temporary directory path
     *
     * @var string
     */
    protected $tmp;

    /**
     * Constructor
     *
     * @param string $tmp
     */
    public function __construct($tmp = null)
    {
        $this->tmp   = $tmp ?: sys_get_temp_dir();
        $this->files = new Filesystem();
    }

    /**
     * Highlight a portion of code with pygmentize
     *
     * @param string $value
     * @param string $language
     *
     * @return string
     */
    public function highlight($value, $language)
    {
        $path = tempnam($this->tmp, 'pyg');

        if ($language === 'php' && substr($value, 0, 5) !== '<?php') {
            $value = '<?php ' . PHP_EOL . $value;
        }

        $this->files->dumpFile($path, $value);

        $value = $this->pygmentize($path, $language);

        unlink($path);

        if (preg_match('#^<div class="highlight"><pre>#', $value) && preg_match('#</pre></div>$#', $value)) {
            return substr($value, 28, strlen($value) - 40);
        }

        return $value;
    }

    /**
     * Run 'pygmentize' command on the given file
     *
     * @param string $path
     * @param string $language
     *
     * @return string
     */
    public function pygmentize($path, $language)
    {
        $process = new Process(sprintf('pygmentize -f html -l %s %s', $language, $path));

        $process->run();

        if (!$process->isSuccessful()) {
            throw new RuntimeException($process->getErrorOutput());
        }

        return trim($process->getOutput());
    }

    /**
     * Is pygmentize available?
     *
     * @return boolean
     */
    public static function isAvailable()
    {
        $process = new Process('pygmentize -V');

        $process->run();
{'fraction_non_alphanumeric': 0.1565025716385011, 'fraction_numerical': 0.002204261572373255, 'mean_word_length': 2.000734753857458, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 33, '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': '3361701', 'n_tokens_mistral': 1288, 'n_tokens_neox': 1231, 'n_words': 285}