<commit_before><?php

/* Copyright 2015 <NAME>*/

/**
 * SectionWidget is a Grav plugin
 *
 * This plugin splits, at special markers, large content in sections. Only one section is visible at a time.
 *
 * Licensed under MIT, see LICENSE.
 *
 * @package     SectionWidget
 * @version     0.1.0
 * @link        <https://github.com/aptly-io/grav-plugin-sectionwidget>
 * @author      <NAME> <https://aptly.io/contact>
 * @copyright   2015, <NAME>
 * @license     MIT <http://opensource.org/licenses/MIT>
 *
 * @todo have a page's active section in a cookie (to later open with the last read section)
 * @todo sectionwidget.html.twig could create controls according to the order in sectionwidget's header
 */

namespace Grav\Plugin;     // use this namespace to avoids bin/gpm fails

use Grav\Common\Grav;
use Grav\Common\Plugin;
use Grav\Common\Page\Page;
use RocketTheme\Toolbox\Event\Event;
use Grav\Common\Markdown\Parsedown;
use Grav\Common\Markdown\ParsedownExtra;


class SectionWidgetPlugin extends Plugin
{
    /** Enable all controls by default*/
    const CONTROLS_YAML = 'controls';
    const DEFAULT_CONTROLS = ['start', 'prev', 'next', 'last', 'fullmenu']; // full: show full whole page control

    /** Sections are by default recognized by this separator*/
    const SEPARATOR_YAML = 'sep';
    const DEFAULT_SEPARATOR   = '======';

    /** The section to show by default (0-based index)*/
    const INITIAL_SECTION_YAML = 'initial_section';
    const DEFAULT_INITIAL_SECTION = 0;               // 'all' also supported
    const INITIAL_ALL_SECTIONS = 'all';

    /** The prefix for the id of a div element wrapping each section*/
    const ID_SECTION_PREFIX = 'sw_section';

    /** The class of a div element wrapping each section*/
    const CLASS_SECTION_HIDEABLE = 'sw_hideable';

    const PREV_IDX_ID = -10;
    const NEXT_IDX_ID = -20;
    const FULL_IDX_ID = -30;


    /** Return a list of subscribed events*/
    public static function getSubscribedEvents()
    {
        return [
            'onPluginsInitialized' => ['onPluginsInitialized', 0]
        ];
    }


    /** Initialize the plug-in*/
    public function onPluginsInitialized()
    {
        /* Sommerregen explains this checks if the admin user is active.
         * If so, this plug-in disables itself.
         * rhukster mentions this is for speedup purposes related to the admin plugin
         */
        if ($this->isAdmin()) {
            $this->active = false;

        } else {

            if ($this->config->get('plugins.sectionwidget.enabled')) {
                // if the plugin is activated, then subscribe to these additional events
                $this->enable([
                    'onTwigTemplatePaths'    => ['onTwigTemplatePaths', 0],
                    'onTwigSiteVariables'    => ['onTwigSiteVariables', 0]
                ]);
            }
        }
    }


    /** Register the enabled plugin's template PATH*/
    public function onTwigTemplatePaths()
    {
        $this->grav['twig']->twig_paths[] = __DIR__ . '/templates';
    }


    /** Setup the necessary assets and variables to build the widget*/
    public function onTwigSiteVariables()
    {
        $page = $this->grav['page'];

        $config = $this->mergeConfig($page);
        if ($config->get('enabled', false)) {

            $assets = $this->grav['assets'];
            $assets->addJs('plugin://sectionwidget/assets/js/sectionwidget.js');
            if ($config->get('built_in_css', false)) {
                $assets->addCss('plugin://sectionwidget/assets/css/sectionwidget.css');
            }

            $this->enable([
                'onPageContentRaw'       => ['onPageContentRaw', 0],
            ]);
        }
    }


    /**
     * Process the page's Markdown content
     *
     * By converting the markdown to HTML first makes searching the markers easier
     *
     * @note inspirated by grav/system/src/Grav/Common/Page/Page.php
     */
    protected function processMarkdown($page)
    {
        $config = $this->grav['config'];

        $defaults = (array) $config->get('system.pages.markdown');
        if (isset($page->header()->markdown)) {
            $defaults = array_merge($defaults, $page->header()->markdown);
        }

        // pages.markdown_extra is deprecated, but still check it...
        if (isset($page->markdown_extra) || $config->get('system.pages.markdown_extra') !== null) {
            $defaults['extra'] = $this->markdown_extra ?: $config->get('system.pages.markdown_extra');
        }

        // Initialize the preferred variant of Parsedown
        if ($defaults['extra']) {
            $parsedown = new ParsedownExtra($page, $defaults);
        } else {
            $parsedown = new Parsedown($page, $defaults);
        }

        return $parsedown->text($page->getRawContent());
    }


    /** Find all sections based on the section separator token*/
    private function findSections($content, $config)
    {
        $sep = $config->get(SectionWidgetPlugin::SEPARATOR_YAML,
            SectionWidgetPlugin::DEFAULT_SEPARATOR);
        $sep_regex = '~(<p>)?\s*' . $sep . '\s*(?P<title>.*)(?(1)</p>)~i';

        $sections = array();<commit_msg>Drop some checking for markdown extra<commit_after><?php

/* Copyright 2015 <NAME>*/

/**
 * SectionWidget is a Grav plugin
 *
 * This plugin splits, at special markers, large content in sections. Only one section is visible at a time.
 *
 * Licensed under MIT, see LICENSE.
 *
 * @package     SectionWidget
 * @version     0.1.0
 * @link        <https://github.com/aptly-io/grav-plugin-sectionwidget>
 * @author      <NAME> <https://aptly.io/contact>
 * @copyright   2015, <NAME>
 * @license     MIT <http://opensource.org/licenses/MIT>
 *
 * @todo have a page's active section in a cookie (to later open with the last read section)
 * @todo sectionwidget.html.twig could create controls according to the order in sectionwidget's header
 */

namespace Grav\Plugin;     // use this namespace to avoids bin/gpm fails

use Grav\Common\Grav;
use Grav\Common\Plugin;
use Grav\Common\Page\Page;
use RocketTheme\Toolbox\Event\Event;
use Grav\Common\Markdown\Parsedown;
use Grav\Common\Markdown\ParsedownExtra;


class SectionWidgetPlugin extends Plugin
{
    /** Enable all controls by default*/
    const CONTROLS_YAML = 'controls';
    const DEFAULT_CONTROLS = ['start', 'prev', 'next', 'last', 'fullmenu']; // full: show full whole page control

    /** Sections are by default recognized by this separator*/
    const SEPARATOR_YAML = 'sep';
    const DEFAULT_SEPARATOR   = '======';

    /** The section to show by default (0-based index)*/
    const INITIAL_SECTION_YAML = 'initial_section';
    const DEFAULT_INITIAL_SECTION = 0;               // 'all' also supported
    const INITIAL_ALL_SECTIONS = 'all';

    /** The prefix for the id of a div element wrapping each section*/
    const ID_SECTION_PREFIX = 'sw_section';

    /** The class of a div element wrapping each section*/
    const CLASS_SECTION_HIDEABLE = 'sw_hideable';

    const PREV_IDX_ID = -10;
    const NEXT_IDX_ID = -20;
    const FULL_IDX_ID = -30;


    /** Return a list of subscribed events*/
    public static function getSubscribedEvents()
    {
        return [
            'onPluginsInitialized' => ['onPluginsInitialized', 0]
        ];
    }


    /** Initialize the plug-in*/
    public function onPluginsInitialized()
    {
        /* Sommerregen explains this checks if the admin user is active.
         * If so, this plug-in disables itself.
         * rhukster mentions this is for speedup purposes related to the admin plugin
         */
        if ($this->isAdmin()) {
            $this->active = false;

        } else {

            if ($this->config->get('plugins.sectionwidget.enabled')) {
                // if the plugin is activated, then subscribe to these additional events
                $this->enable([
                    'onTwigTemplatePaths'    => ['onTwigTemplatePaths', 0],
                    'onTwigSiteVariables'    => ['onTwigSiteVariables', 0]
                ]);
            }
        }
    }


    /** Register the enabled plugin's template PATH*/
    public function onTwigTemplatePaths()
    {
        $this->grav['twig']->twig_paths[] = __DIR__ . '/templates';
    }


    /** Setup the necessary assets and variables to build the widget*/
    public function onTwigSiteVariables()
    {
        $page = $this->grav['page'];

        $config = $this->mergeConfig($page);
        if ($config->get('enabled', false)) {

            $assets = $this->grav['assets'];
            $assets->addJs('plugin://sectionwidget/assets/js/sectionwidget.js');
            if ($config->get('built_in_css', false)) {
                $assets->addCss('plugin://sectionwidget/assets/css/sectionwidget.css');
            }

            $this->enable([
                'onPageContentRaw'       => ['onPageContentRaw', 0],
            ]);
        }
    }


    /**
     * Process the page's Markdown content
     *
     * By converting the markdown to HTML first makes searching the markers easier
     *
     * @note inspirated by grav/system/src/Grav/Common/Page/Page.php
     */
    protected function processMarkdown($page)
    {
        $config = $this->grav['config'];

        $defaults = (array) $config->get('system.pages.markdown');
        if (isset($page->header()->markdown)) {
            $defaults = array_merge($defaults, $page->header()->markdown);
        }

        if ($defaults['extra']) {
            $parsedown = new ParsedownExtra($page, $defaults);
        } else {
            $parsedown = new Parsedown($page, $defaults);
        }

        return $parsedown->text($page->getRawContent());
    }


    /** Find all sections based on the section separator token*/
    private function findSections($content, $config)
    {
        $sep = $config->get(SectionWidgetPlugin::SEPARATOR_YAML,
            SectionWidgetPlugin::DEFAULT_SEPARATOR);
        $sep_regex = '~(<p>)?\s*' . $sep . '\s*(?P<title>.*)(?(1)</p>)~i';

        $sections = array();
{'fraction_non_alphanumeric': 0.13010153294843718, 'fraction_numerical': 0.004778021102926538, 'mean_word_length': 2.4442920809050395, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 81, 'https://': 4, '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': '3577641', 'n_tokens_mistral': 2981, 'n_tokens_neox': 2811, 'n_words': 776}