<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\Step;

use PHPoole\Collection\Page\Page;
use PHPoole\Exception\Exception;
use PHPoole\Renderer\Layout;
use PHPoole\Renderer\Twig as Twig;
use PHPoole\Util;

/**
 * Pages rendering:
 * 1. Iterates Pages collection
 * 2. Applies Twig templates
 * 3. Saves rendered files.
 */
class RenderPages extends AbstractStep
{
    /**
     * {@inheritdoc}
     *
     * @throws Exception
     */
    public function init()
    {
        // checks layouts dir
        if (!is_dir($this->phpoole->getConfig()->getLayoutsPath()) && !$this->phpoole->getConfig()->hasTheme()) {
            throw new Exception(sprintf(
                "'%s' is not a valid layouts directory", $this->phpoole->getConfig()->getLayoutsPath()
            ));
        }
        $this->process = true;
    }

    /**
     * {@inheritdoc}
     *
     * @throws Exception
     */
    public function internalProcess()
    {
        $paths = [];

        // prepares renderer
        if (is_dir($this->phpoole->getConfig()->getLayoutsPath())) {
            $paths[] = $this->phpoole->getConfig()->getLayoutsPath();
        }
        if ($this->phpoole->getConfig()->hasTheme()) {
            $paths[] = $this->phpoole->getConfig()->getThemePath($this->phpoole->getConfig()->get('theme'));
        }
        $this->phpoole->setRenderer(new Twig($paths, $this->phpoole->getConfig()));

        // adds global variables
        $this->phpoole->getRenderer()->addGlobal('site', array_merge(
            $this->phpoole->getConfig()->get('site'),
            ['menus' => $this->phpoole->getMenus()],
            ['pages' => $this->phpoole->getPages()]
        ));
        $this->phpoole->getRenderer()->addGlobal('phpoole', [
            'url'       => 'http://phpoole.org/#v'.$this->phpoole->getVersion(),
            'version'   => $this->phpoole->getVersion(),
            'poweredby' => 'PHPoole-library v'.$this->phpoole->getVersion(),
        ]);

        // start rendering
        Util::getFS()->mkdir($this->phpoole->getConfig()->getOutputPath());
        call_user_func_array($this->phpoole->getMessageCb(), ['RENDER', 'Rendering pages']);
        $max = count($this->phpoole->getPages());
        $count = 0;
        /* @var $page Page */
        foreach ($this->phpoole->getPages() as $page) {
            if ($page->getVariable('published') !== false) {
                $count++;
                $pathname = $this->renderPage($page, $this->phpoole->getConfig()->getOutputPath());
                $message = substr($pathname, strlen($this->phpoole->getConfig()->getDestinationDir()) + 1);
                call_user_func_array($this->phpoole->getMessageCb(), ['RENDER_PROGRESS', $message, $count, $max]);<commit_msg>Filters not published pages.<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\Step;

use PHPoole\Collection\Page\Page;
use PHPoole\Exception\Exception;
use PHPoole\Renderer\Layout;
use PHPoole\Renderer\Twig as Twig;
use PHPoole\Util;

/**
 * Pages rendering:
 * 1. Iterates Pages collection
 * 2. Applies Twig templates
 * 3. Saves rendered files.
 */
class RenderPages extends AbstractStep
{
    /**
     * {@inheritdoc}
     *
     * @throws Exception
     */
    public function init()
    {
        // checks layouts dir
        if (!is_dir($this->phpoole->getConfig()->getLayoutsPath()) && !$this->phpoole->getConfig()->hasTheme()) {
            throw new Exception(sprintf(
                "'%s' is not a valid layouts directory", $this->phpoole->getConfig()->getLayoutsPath()
            ));
        }
        $this->process = true;
    }

    /**
     * {@inheritdoc}
     *
     * @throws Exception
     */
    public function internalProcess()
    {
        $paths = [];

        // prepares renderer
        if (is_dir($this->phpoole->getConfig()->getLayoutsPath())) {
            $paths[] = $this->phpoole->getConfig()->getLayoutsPath();
        }
        if ($this->phpoole->getConfig()->hasTheme()) {
            $paths[] = $this->phpoole->getConfig()->getThemePath($this->phpoole->getConfig()->get('theme'));
        }
        $this->phpoole->setRenderer(new Twig($paths, $this->phpoole->getConfig()));

        // adds global variables
        $this->phpoole->getRenderer()->addGlobal('site', array_merge(
            $this->phpoole->getConfig()->get('site'),
            ['menus' => $this->phpoole->getMenus()],
            ['pages' => $this->phpoole->getPages()->filter(function (Page $page) {
                return $page->getVariable('published');
            })]
        ));
        $this->phpoole->getRenderer()->addGlobal('phpoole', [
            'url'       => 'http://phpoole.org/#v'.$this->phpoole->getVersion(),
            'version'   => $this->phpoole->getVersion(),
            'poweredby' => 'PHPoole-library v'.$this->phpoole->getVersion(),
        ]);

        // start rendering
        Util::getFS()->mkdir($this->phpoole->getConfig()->getOutputPath());
        call_user_func_array($this->phpoole->getMessageCb(), ['RENDER', 'Rendering pages']);
        $max = count($this->phpoole->getPages());
        $count = 0;
        /* @var $page Page */
        foreach ($this->phpoole->getPages() as $page) {
            if ($page->getVariable('published') !== false) {
                $count++;
                $pathname = $this->renderPage($page, $this->phpoole->getConfig()->getOutputPath());
                $message = substr($pathname, strlen($this->phpoole->getConfig()->getDestinationDir()) + 1);
                call_user_func_array($this->phpoole->getMessageCb(), ['RENDER_PROGRESS', $message, $count, $max]);
{'fraction_non_alphanumeric': 0.182157394843962, 'fraction_numerical': 0.0016960651289009499, 'mean_word_length': 2.546001202645821, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 157, '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': '5685883', 'n_tokens_mistral': 1781, 'n_tokens_neox': 1759, 'n_words': 312}