xref: /plugin/combo/syntax/iterator.php (revision 1fa8c418ed5809db58049141be41b7738471dd32)
137748cd8SNickeau<?php
237748cd8SNickeau
337748cd8SNickeau
437748cd8SNickeauuse ComboStrap\CallStack;
537748cd8SNickeauuse ComboStrap\PluginUtility;
637748cd8SNickeauuse ComboStrap\TagAttributes;
737748cd8SNickeau
837748cd8SNickeaurequire_once(__DIR__ . '/../ComboStrap/PluginUtility.php');
937748cd8SNickeau
1037748cd8SNickeau
1137748cd8SNickeau/**
1237748cd8SNickeau *
1337748cd8SNickeau * An iterator to iterate over templates.
1437748cd8SNickeau *
1537748cd8SNickeau * *******************
1637748cd8SNickeau * Iteration driver
1737748cd8SNickeau * *******************
1837748cd8SNickeau * The end tag of the template node is driving the iteration.
1937748cd8SNickeau * This way, the tags just after the template
2037748cd8SNickeau * sees them in the {@link CallStack} and can change their context
2137748cd8SNickeau *
2237748cd8SNickeau * For instance, a {@link syntax_plugin_combo_masonry}
2337748cd8SNickeau * component will change the context of all card inside it.
2437748cd8SNickeau *
2537748cd8SNickeau * ********************
2637748cd8SNickeau * Header and footer delimitation
2737748cd8SNickeau * ********************
2837748cd8SNickeau * The iterator delimits also the header and footer.
2937748cd8SNickeau * Some component needs the header to be generate completely.
3037748cd8SNickeau * This is the case of a complex markup such as a table
3137748cd8SNickeau *
3237748cd8SNickeau * ******************************
3337748cd8SNickeau * Delete if no data
3437748cd8SNickeau * ******************************
3537748cd8SNickeau * It gives also the possibility to {@link syntax_plugin_combo_iterator::EMPTY_ROWS_COUNT_ATTRIBUTE
3637748cd8SNickeau * delete the whole block}
3737748cd8SNickeau * (header and footer also) if there is no data
3837748cd8SNickeau *
3937748cd8SNickeau * *****************************
4037748cd8SNickeau * Always Contextual
4137748cd8SNickeau * *****************************
4237748cd8SNickeau * We don't capture the text markup such as in a {@link syntax_plugin_combo_code}
4337748cd8SNickeau * in order to loop because you can't pass the actual handler (ie callstack)
4437748cd8SNickeau * when you {@link p_get_instructions() parse again} a markup.
4537748cd8SNickeau *
4637748cd8SNickeau * The markup is then seen as a new single page without any context.
4737748cd8SNickeau * That may lead to problems.
4837748cd8SNickeau * Example: `heading` may then think that they are `outline heading` ...
4937748cd8SNickeau *
5037748cd8SNickeau */
5137748cd8SNickeauclass syntax_plugin_combo_iterator extends DokuWiki_Syntax_Plugin
5237748cd8SNickeau{
5337748cd8SNickeau
5437748cd8SNickeau    /**
5537748cd8SNickeau     * Tag in Dokuwiki cannot have a `-`
5637748cd8SNickeau     * This is the last part of the class
5737748cd8SNickeau     */
5837748cd8SNickeau    const TAG = "iterator";
5937748cd8SNickeau
6037748cd8SNickeau    /**
6137748cd8SNickeau     * Page canonical and tag pattern
6237748cd8SNickeau     */
6337748cd8SNickeau    const CANONICAL = "iterator";
6437748cd8SNickeau
6537748cd8SNickeau    /**
6637748cd8SNickeau     * An attribute that is set back
6737748cd8SNickeau     * by the {@link DOKU_LEXER_EXIT} state in {@link syntax_plugin_combo_template::handle()}
6837748cd8SNickeau     * in order to delete the whole iterator content (ie header, footer)
6937748cd8SNickeau     * at the {@link DOKU_LEXER_EXIT} state of {@link syntax_plugin_combo_iterator::handle()}
7037748cd8SNickeau     * if there is no rows to iterate
7137748cd8SNickeau     */
7237748cd8SNickeau    const EMPTY_ROWS_COUNT_ATTRIBUTE = "emptyRowCount";
7337748cd8SNickeau
7437748cd8SNickeau
7537748cd8SNickeau    /**
7637748cd8SNickeau     * Syntax Type.
7737748cd8SNickeau     *
7837748cd8SNickeau     * Needs to return one of the mode types defined in $PARSER_MODES in parser.php
7937748cd8SNickeau     * @see https://www.dokuwiki.org/devel:syntax_plugins#syntax_types
8037748cd8SNickeau     * @see DokuWiki_Syntax_Plugin::getType()
8137748cd8SNickeau     */
8237748cd8SNickeau    function getType()
8337748cd8SNickeau    {
8437748cd8SNickeau        return 'container';
8537748cd8SNickeau    }
8637748cd8SNickeau
8737748cd8SNickeau    /**
8837748cd8SNickeau     * How Dokuwiki will add P element
8937748cd8SNickeau     *
9037748cd8SNickeau     *  * 'normal' - The plugin can be used inside paragraphs (inline or inside)
9137748cd8SNickeau     *  * 'block'  - Open paragraphs need to be closed before plugin output (box) - block should not be inside paragraphs
9237748cd8SNickeau     *  * 'stack'  - Special case. Plugin wraps other paragraphs. - Stacks can contain paragraphs
9337748cd8SNickeau     *
9437748cd8SNickeau     * @see DokuWiki_Syntax_Plugin::getPType()
9537748cd8SNickeau     * @see https://www.dokuwiki.org/devel:syntax_plugins#ptype
9637748cd8SNickeau     */
9737748cd8SNickeau    function getPType()
9837748cd8SNickeau    {
9937748cd8SNickeau        return 'block';
10037748cd8SNickeau    }
10137748cd8SNickeau
10237748cd8SNickeau    /**
10337748cd8SNickeau     * @return array
10437748cd8SNickeau     * Allow which kind of plugin inside
10537748cd8SNickeau     *
10637748cd8SNickeau     * No one of array('baseonly','container', 'formatting', 'substition', 'protected', 'disabled', 'paragraphs')
10737748cd8SNickeau     * because we manage self the content and we call self the parser
10837748cd8SNickeau     *
10937748cd8SNickeau     * Return an array of one or more of the mode types {@link $PARSER_MODES} in Parser.php
11037748cd8SNickeau     */
11137748cd8SNickeau    function getAllowedTypes()
11237748cd8SNickeau    {
11337748cd8SNickeau        return array('container', 'formatting', 'substition', 'protected', 'disabled', 'paragraphs');
11437748cd8SNickeau    }
11537748cd8SNickeau
11637748cd8SNickeau    function getSort()
11737748cd8SNickeau    {
11837748cd8SNickeau        return 201;
11937748cd8SNickeau    }
12037748cd8SNickeau
12137748cd8SNickeau    public function accepts($mode)
12237748cd8SNickeau    {
12337748cd8SNickeau        return syntax_plugin_combo_preformatted::disablePreformatted($mode);
12437748cd8SNickeau    }
12537748cd8SNickeau
12637748cd8SNickeau
12737748cd8SNickeau    function connectTo($mode)
12837748cd8SNickeau    {
12937748cd8SNickeau
13037748cd8SNickeau
13137748cd8SNickeau        $pattern = PluginUtility::getContainerTagPattern(self::TAG);
13237748cd8SNickeau        $this->Lexer->addEntryPattern($pattern, $mode, PluginUtility::getModeFromTag($this->getPluginComponent()));
13337748cd8SNickeau
13437748cd8SNickeau
13537748cd8SNickeau    }
13637748cd8SNickeau
13737748cd8SNickeau
13837748cd8SNickeau    public function postConnect()
13937748cd8SNickeau    {
14037748cd8SNickeau
14137748cd8SNickeau        $this->Lexer->addExitPattern('</' . self::TAG . '>', PluginUtility::getModeFromTag($this->getPluginComponent()));
14237748cd8SNickeau
14337748cd8SNickeau
14437748cd8SNickeau    }
14537748cd8SNickeau
14637748cd8SNickeau
14737748cd8SNickeau    /**
14837748cd8SNickeau     *
14937748cd8SNickeau     * The handle function goal is to parse the matched syntax through the pattern function
15037748cd8SNickeau     * and to return the result for use in the renderer
15137748cd8SNickeau     * This result is always cached until the page is modified.
15237748cd8SNickeau     * @param string $match
15337748cd8SNickeau     * @param int $state
15437748cd8SNickeau     * @param int $pos - byte position in the original source file
15537748cd8SNickeau     * @param Doku_Handler $handler
15637748cd8SNickeau     * @return array|bool
15737748cd8SNickeau     * @throws Exception
15837748cd8SNickeau     * @see DokuWiki_Syntax_Plugin::handle()
15937748cd8SNickeau     *
16037748cd8SNickeau     */
16137748cd8SNickeau    function handle($match, $state, $pos, Doku_Handler $handler)
16237748cd8SNickeau    {
16337748cd8SNickeau
16437748cd8SNickeau        switch ($state) {
16537748cd8SNickeau
16637748cd8SNickeau            case DOKU_LEXER_ENTER :
16737748cd8SNickeau
16837748cd8SNickeau                $tagAttributes = TagAttributes::createFromTagMatch($match);
16937748cd8SNickeau                $callStackArray = $tagAttributes->toCallStackArray();
17037748cd8SNickeau                return array(
17137748cd8SNickeau                    PluginUtility::STATE => $state,
17237748cd8SNickeau                    PluginUtility::ATTRIBUTES => $callStackArray
17337748cd8SNickeau                );
17437748cd8SNickeau
17537748cd8SNickeau            case DOKU_LEXER_UNMATCHED :
17637748cd8SNickeau
17737748cd8SNickeau                // We should not ever come here but a user does not not known that
17837748cd8SNickeau                return PluginUtility::handleAndReturnUnmatchedData(self::TAG, $match, $handler);
17937748cd8SNickeau
18037748cd8SNickeau
18137748cd8SNickeau            case DOKU_LEXER_EXIT :
18237748cd8SNickeau
183*1fa8c418SNickeau                /**
184*1fa8c418SNickeau                 * Do we need to delete all call because the data returns no rows
185*1fa8c418SNickeau                 */
186*1fa8c418SNickeau                $callStack = CallStack::createFromHandler($handler);
187*1fa8c418SNickeau                $openingCall = $callStack->moveToPreviousCorrespondingOpeningCall();
188*1fa8c418SNickeau                if($openingCall->getAttribute(self::EMPTY_ROWS_COUNT_ATTRIBUTE,false)){
189*1fa8c418SNickeau                    $callStack->deleteAllCallsAfter($openingCall);
190*1fa8c418SNickeau                }
19137748cd8SNickeau                return array(PluginUtility::STATE => $state);
19237748cd8SNickeau
19337748cd8SNickeau        }
19437748cd8SNickeau        return array();
19537748cd8SNickeau
19637748cd8SNickeau    }
19737748cd8SNickeau
19837748cd8SNickeau    /**
19937748cd8SNickeau     * Render the output
20037748cd8SNickeau     * @param string $format
20137748cd8SNickeau     * @param Doku_Renderer $renderer
20237748cd8SNickeau     * @param array $data - what the function handle() return'ed
20337748cd8SNickeau     * @return boolean - rendered correctly? (however, returned value is not used at the moment)
20437748cd8SNickeau     * @see DokuWiki_Syntax_Plugin::render()
20537748cd8SNickeau     *
20637748cd8SNickeau     *
20737748cd8SNickeau     */
20837748cd8SNickeau    function render($format, Doku_Renderer $renderer, $data)
20937748cd8SNickeau    {
21037748cd8SNickeau        // unsupported $mode
21137748cd8SNickeau        return false;
21237748cd8SNickeau    }
21337748cd8SNickeau
21437748cd8SNickeau
21537748cd8SNickeau}
21637748cd8SNickeau
217