<commit_before><?php

function js_start()
{
  ob_start('Response::jsOutputCallback');
}

function js_end()
{
  ob_end_flush();
}

class View
{
  const LAYOUT_PARAMS = '_layout_params';

  const WEB_DIR = ROOT_DIR.'/web';
  const SCSS_DIR = self::WEB_DIR.'/scss';
  const CSS_DIR = self::WEB_DIR.'/css';
  const JS_DIR = self::WEB_DIR.'/js';

  public static function render($template, array $vars = [])
  {
    if (!static::exists($template) || substr_count($template, '/') !== 1)
    {
      throw new InvalidArgumentException(sprintf('The template "%s" does not exist or is unreadable.', $template));
    }

    if (static::isMarkdown($template))
    {
      return static::markdownToHtml(static::getFullPath($template));
    }

    list($module, $view) = explode('/', $template);

    $isPartial = $view[0] === '_';
    $actionClass  = ucfirst($module) . 'Actions';
    $method = 'prepare' . ucfirst(ltrim($view, '_')) . ($isPartial ? 'Partial' : '');

    if (method_exists($actionClass, $method))
    {
      $vars = $actionClass::$method($vars);
    }

    if ($vars === null)
    {
      return;
    }

    return static::interpolateTokens(static::getTemplateSafely($template, $vars));
  }

  /**
   * This is its own function because we don't want in-scope variables to leak into the template
   *
   * @param string $___template
   * @param array  $___vars
   *
   * @return string
   * @throws Throwable
   */
  protected static function getTemplateSafely(string $___template, array $___vars): string
  {
    extract($___vars);
    ob_start();
    ob_implicit_flush(0);

    try
    {
      require(static::getFullPath($___template));
      return ob_get_clean();
    }
    catch (Throwable $e)
    {
      // need to end output buffering before throwing the exception
      ob_end_clean();
      throw $e;
    }
  }

  public static function markdownToHtml($path)
  {
    return ParsedownExtra::instance()->text(trim(file_get_contents($path)));
  }

  public static function exists($template): bool
  {
    return is_readable(static::getFullPath($template));
  }

  protected static function isMarkdown($nameOrPath): bool
  {
    return strlen($nameOrPath) > 3 && substr($nameOrPath, -3) == '.md';
  }

  protected static function getFullPath($template): string
  {
    if ($template && $template[0] == '/')
    {
      return $template;
    }

    if (static::isMarkdown($template))
    {
      return ROOT_DIR . '/posts/' . $template;
    }

    return ROOT_DIR . '/view/template/' . $template . '.php';
  }

  public static function imagePath($image): string
  {
    return '/img/' . $image;
  }

  public static function parseMarkdown($template): array
  {
    $path = static::getFullPath($template);
    list($ignored, $frontMatter, $markdown) = explode('---', file_get_contents($path), 3);
    $metadata = Spyc::YAMLLoadString(trim($frontMatter));
    $html = ParsedownExtra::instance()->text(trim($markdown));
    return [$metadata, $html];
  }

  public static function compileCss()
  {
    $scssCompiler = new \Leafo\ScssPhp\Compiler();

    $scssCompiler->setImportPaths([self::SCSS_DIR]);

    $compress = true;
    if ($compress)
    {
      $scssCompiler->setFormatter('Leafo\ScssPhp\Formatter\Crunched');
    }
    else
    {
      $scssCompiler->setFormatter('Leafo\ScssPhp\Formatter\Expanded');
      $scssCompiler->setLineNumberStyle(Leafo\ScssPhp\Compiler::LINE_COMMENTS);
    }

    $css = $scssCompiler->compile(file_get_contents(self::SCSS_DIR.'/all.scss'));
    file_put_contents(self::CSS_DIR.'/all.css', $css);
  }

  public static function gzipAssets()
  {
    foreach([self::CSS_DIR => 'css', self::JS_DIR => 'js'] as $dir => $ext)
    {
      foreach(glob("$dir/*.$ext") as $file)
      {
        file_put_contents($file.'.gz', gzcompress(file_get_contents($file)));
      }
    }
  }

  protected static function interpolateTokens($html)<commit_msg>wrong function<commit_after><?php

function js_start()
{
  ob_start('Response::jsOutputCallback');
}

function js_end()
{
  ob_end_flush();
}

class View
{
  const LAYOUT_PARAMS = '_layout_params';

  const WEB_DIR = ROOT_DIR.'/web';
  const SCSS_DIR = self::WEB_DIR.'/scss';
  const CSS_DIR = self::WEB_DIR.'/css';
  const JS_DIR = self::WEB_DIR.'/js';

  public static function render($template, array $vars = [])
  {
    if (!static::exists($template) || substr_count($template, '/') !== 1)
    {
      throw new InvalidArgumentException(sprintf('The template "%s" does not exist or is unreadable.', $template));
    }

    if (static::isMarkdown($template))
    {
      return static::markdownToHtml(static::getFullPath($template));
    }

    list($module, $view) = explode('/', $template);

    $isPartial = $view[0] === '_';
    $actionClass  = ucfirst($module) . 'Actions';
    $method = 'prepare' . ucfirst(ltrim($view, '_')) . ($isPartial ? 'Partial' : '');

    if (method_exists($actionClass, $method))
    {
      $vars = $actionClass::$method($vars);
    }

    if ($vars === null)
    {
      return;
    }

    return static::interpolateTokens(static::getTemplateSafely($template, $vars));
  }

  /**
   * This is its own function because we don't want in-scope variables to leak into the template
   *
   * @param string $___template
   * @param array  $___vars
   *
   * @return string
   * @throws Throwable
   */
  protected static function getTemplateSafely(string $___template, array $___vars): string
  {
    extract($___vars);
    ob_start();
    ob_implicit_flush(0);

    try
    {
      require(static::getFullPath($___template));
      return ob_get_clean();
    }
    catch (Throwable $e)
    {
      // need to end output buffering before throwing the exception
      ob_end_clean();
      throw $e;
    }
  }

  public static function markdownToHtml($path)
  {
    return ParsedownExtra::instance()->text(trim(file_get_contents($path)));
  }

  public static function exists($template): bool
  {
    return is_readable(static::getFullPath($template));
  }

  protected static function isMarkdown($nameOrPath): bool
  {
    return strlen($nameOrPath) > 3 && substr($nameOrPath, -3) == '.md';
  }

  protected static function getFullPath($template): string
  {
    if ($template && $template[0] == '/')
    {
      return $template;
    }

    if (static::isMarkdown($template))
    {
      return ROOT_DIR . '/posts/' . $template;
    }

    return ROOT_DIR . '/view/template/' . $template . '.php';
  }

  public static function imagePath($image): string
  {
    return '/img/' . $image;
  }

  public static function parseMarkdown($template): array
  {
    $path = static::getFullPath($template);
    list($ignored, $frontMatter, $markdown) = explode('---', file_get_contents($path), 3);
    $metadata = Spyc::YAMLLoadString(trim($frontMatter));
    $html = ParsedownExtra::instance()->text(trim($markdown));
    return [$metadata, $html];
  }

  public static function compileCss()
  {
    $scssCompiler = new \Leafo\ScssPhp\Compiler();

    $scssCompiler->setImportPaths([self::SCSS_DIR]);

    $compress = true;
    if ($compress)
    {
      $scssCompiler->setFormatter('Leafo\ScssPhp\Formatter\Crunched');
    }
    else
    {
      $scssCompiler->setFormatter('Leafo\ScssPhp\Formatter\Expanded');
      $scssCompiler->setLineNumberStyle(Leafo\ScssPhp\Compiler::LINE_COMMENTS);
    }

    $css = $scssCompiler->compile(file_get_contents(self::SCSS_DIR.'/all.scss'));
    file_put_contents(self::CSS_DIR.'/all.css', $css);
  }

  public static function gzipAssets()
  {
    foreach([self::CSS_DIR => 'css', self::JS_DIR => 'js'] as $dir => $ext)
    {
      foreach(glob("$dir/*.$ext") as $file)
      {
        file_put_contents($file.'.gz', gzencode(file_get_contents($file)));
      }
    }
  }

  protected static function interpolateTokens($html)
{'fraction_non_alphanumeric': 0.1597427652733119, 'fraction_numerical': 0.0018006430868167203, 'mean_word_length': 3.40317100792752, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 25, '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': '4360116', 'n_tokens_mistral': 2599, 'n_tokens_neox': 2528, 'n_words': 490}