<commit_before><?php

/**
 * @author <NAME>
 *
 * @copyright 2016 <NAME>
 *
 * @link http://wikirenderer.jelix.org
 *
 * @licence MIT see LICENCE file
 */

namespace WikiRenderer;

/**
 * Base class to parse block elements.
 */
class RendererNG extends Renderer
{
    /**
     * @var \WikiRenderer\Generator\DocumentGeneratorInterface
     */
    protected $documentGenerator;

    public function __construct(\WikiRenderer\Generator\DocumentGeneratorInterface $generator, Config $config)
    {
        $this->documentGenerator = $generator;

        if (is_subclass_of($config, '\WikiRenderer\Config')) {
            $this->config = $config;
        } else {
            throw new \InvalidArgumentException('WikiRenderer: Bad configuration.');
        }

        $this->inlineParser = new InlineParserNG($this->config, $generator);

        foreach ($this->config->blocktags as $name) {
            $this->_blockList[] = new $name($this, $generator);
        }
        if ($this->config->defaultBlock) {
            $name = $this->config->defaultBlock;
            $this->_defaultBlock = new $name($this);
        }
    }

    public function render($text)
    {
        $text = $this->config->onStart($text);

        $lignes = preg_split("/\015\012|\015|\012/", $text); // we split the text at all line feeds

        $this->_newtext = array();
        $this->errors = array();
        $this->_currentBlock = null;
        $this->_previousBloc = null;

        // we loop over all lines
        foreach ($lignes as $num => $ligne) {
            if ($this->_currentBlock) {
                // a block is already opened
                if ($this->_currentBlock->detect($ligne, true)) {
                    // the line is part of the block
                    $this->_currentBlock->validateDetectedLine();
                } else {
                    // the line is not part of the block, we close it.
                    $this->_newtext[] = $this->_currentBlock->close();
                    $this->detectNewBlock($ligne);
                }
            } else {
                // no opened block, we see if the line correspond to a block
                $this->detectNewBlock($ligne);
            }
            if ($this->inlineParser->error) {
                $this->errors[$num + 1] = $ligne;
            }
        }
        if ($this->_currentBlock) {
            $this->_newtext[] = $this->_currentBlock->close();
        }

        $result = '';
        foreach($this->_newtext as $k => $blockGenerator) {
            if ($k > 0) {
                $result .= "\n";
            }
            if (is_string($blockGenerator)) {
                $result .= $blockGenerator;
            }
            else {
                $result .= $blockGenerator->generate();
            }
        }

        if ($this->documentGenerator->getConfig()->generateHeaderFooter) {
            $result = $this->documentGenerator->generateHeader()
                     .$result
                     .$this->documentGenerator->generateFooter();
        }<commit_msg>Renderer: use an iterator on lines<commit_after><?php

/**
 * @author <NAME>
 *
 * @copyright 2016 <NAME>
 *
 * @link http://wikirenderer.jelix.org
 *
 * @licence MIT see LICENCE file
 */

namespace WikiRenderer;

/**
 * Base class to parse block elements.
 */
class RendererNG extends Renderer
{
    /**
     * @var \WikiRenderer\Generator\DocumentGeneratorInterface
     */
    protected $documentGenerator;

    public function __construct(\WikiRenderer\Generator\DocumentGeneratorInterface $generator, Config $config)
    {
        $this->documentGenerator = $generator;

        if (is_subclass_of($config, '\WikiRenderer\Config')) {
            $this->config = $config;
        } else {
            throw new \InvalidArgumentException('WikiRenderer: Bad configuration.');
        }

        $this->inlineParser = new InlineParserNG($this->config, $generator);

        foreach ($this->config->blocktags as $name) {
            $this->_blockList[] = new $name($this, $generator);
        }
        if ($this->config->defaultBlock) {
            $name = $this->config->defaultBlock;
            $this->_defaultBlock = new $name($this);
        }
    }

    public function render($text)
    {
        $text = $this->config->onStart($text);

        $linesIterator = new \ArrayIterator(preg_split("/\015\012|\015|\012/", $text)); // we split the text at all line feeds

        $this->_newtext = array();
        $this->errors = array();
        $this->_currentBlock = null;
        $this->_previousBloc = null;

        // we loop over all lines
        while ($linesIterator->valid()) {
            $line = $linesIterator->current();
            if ($this->_currentBlock) {
                // a block is already opened
                if ($this->_currentBlock->detect($line, true)) {
                    // the line is part of the block
                    $this->_currentBlock->validateDetectedLine();
                } else {
                    // the line is not part of the block, we close it.
                    $this->_newtext[] = $this->_currentBlock->close();
                    $this->detectNewBlock($line);
                }
            } else {
                // no opened block, we see if the line correspond to a block
                $this->detectNewBlock($line);
            }
            if ($this->inlineParser->error) {
                $this->errors[$linesIterator->key() + 1] = $line;
            }
            $linesIterator->next();
        }
        if ($this->_currentBlock) {
            $this->_newtext[] = $this->_currentBlock->close();
        }

        $result = '';
        foreach($this->_newtext as $k => $blockGenerator) {
            if ($k > 0) {
                $result .= "\n";
            }
            if (is_string($blockGenerator)) {
                $result .= $blockGenerator;
            }
            else {
                $result .= $blockGenerator->generate();
            }
        }

        if ($this->documentGenerator->getConfig()->generateHeaderFooter) {
            $result = $this->documentGenerator->generateHeader()
                     .$result
                     .$this->documentGenerator->generateFooter();
        }
{'fraction_non_alphanumeric': 0.13859480269489893, 'fraction_numerical': 0.005774783445620789, 'mean_word_length': 1.7155923344947734, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 104, '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': '4715766', 'n_tokens_mistral': 1689, 'n_tokens_neox': 1593, 'n_words': 366}