xref: /plugin/combo/syntax/toc.php (revision 21913ab3235d516e2fa19c7e3929b555b3a2bda1)
1<?php
2/**
3 * Copyright (c) 2020. ComboStrap, Inc. and its affiliates. All Rights Reserved.
4 *
5 * This source code is licensed under the GPL license found in the
6 * COPYING  file in the root directory of this source tree.
7 *
8 * @license  GPL 3 (https://www.gnu.org/licenses/gpl-3.0.en.html)
9 * @author   ComboStrap <support@combostrap.com>
10 *
11 */
12
13use ComboStrap\PluginUtility;
14
15if (!defined('DOKU_INC')) die();
16
17/**
18 * Class syntax_plugin_combo_tov
19 *
20 */
21class syntax_plugin_combo_toc extends DokuWiki_Syntax_Plugin
22{
23
24    const TAG = "toc";
25
26
27    /**
28     * Syntax Type.
29     *
30     * Needs to return one of the mode types defined in $PARSER_MODES in parser.php
31     * @see https://www.dokuwiki.org/devel:syntax_plugins#syntax_types
32     * @see DokuWiki_Syntax_Plugin::getType()
33     */
34    function getType()
35    {
36        return 'formatting';
37    }
38
39    /**
40     * How Dokuwiki will add P element
41     *
42     *  * 'normal' - The plugin can be used inside paragraphs (inline)
43     *  * 'block'  - Open paragraphs need to be closed before plugin output - block should not be inside paragraphs
44     *  * 'stack'  - Special case. Plugin wraps other paragraphs. - Stacks can contain paragraphs
45     *
46     * @see DokuWiki_Syntax_Plugin::getPType()
47     * @see https://www.dokuwiki.org/devel:syntax_plugins#ptype
48     */
49    function getPType()
50    {
51        return 'substition';
52    }
53
54    /**
55     * @return array
56     * Allow which kind of plugin inside
57     *
58     * No one of array('baseonly','container', 'formatting', 'substition', 'protected', 'disabled', 'paragraphs')
59     * because we manage self the content and we call self the parser
60     *
61     * Return an array of one or more of the mode types {@link $PARSER_MODES} in Parser.php
62     */
63    function getAllowedTypes()
64    {
65        return array();
66    }
67
68    function getSort()
69    {
70        return 201;
71    }
72
73
74    function connectTo($mode)
75    {
76
77        $pattern = PluginUtility::getContainerTagPattern(self::TAG);
78        $this->Lexer->addEntryPattern($pattern, $mode, PluginUtility::getModeForComponent($this->getPluginComponent()));
79
80    }
81
82    function postConnect()
83    {
84
85        $this->Lexer->addExitPattern('</' . self::TAG . '>', PluginUtility::getModeForComponent($this->getPluginComponent()));
86
87    }
88
89    /**
90     *
91     * The handle function goal is to parse the matched syntax through the pattern function
92     * and to return the result for use in the renderer
93     * This result is always cached until the page is modified.
94     * @param string $match
95     * @param int $state
96     * @param int $pos - byte position in the original source file
97     * @param Doku_Handler $handler
98     * @return array|bool
99     * @see DokuWiki_Syntax_Plugin::handle()
100     *
101     */
102    function handle($match, $state, $pos, Doku_Handler $handler)
103    {
104
105        switch ($state) {
106
107            case DOKU_LEXER_ENTER :
108                $attributes = PluginUtility::getTagAttributes($match);
109                return array($state, $attributes);
110
111            case DOKU_LEXER_UNMATCHED :
112                return PluginUtility::handleAndReturnUnmatchedData(self::TAG,$match,$handler);
113
114            case DOKU_LEXER_EXIT :
115
116                // Important otherwise we don't get an exit in the render
117                return array($state, '');
118
119
120        }
121        return array();
122
123    }
124
125    /**
126     * Render the output
127     * @param string $format
128     * @param Doku_Renderer $renderer
129     * @param array $data - what the function handle() return'ed
130     * @return boolean - rendered correctly? (however, returned value is not used at the moment)
131     * @see DokuWiki_Syntax_Plugin::render()
132     *
133     *
134     */
135    function render($format, Doku_Renderer $renderer, $data)
136    {
137        if ($format == 'xhtml') {
138
139            /** @var Doku_Renderer_xhtml $renderer */
140            $state = $data[PluginUtility::STATE];
141            switch ($state) {
142                case DOKU_LEXER_ENTER :
143
144
145                    $renderer->doc = "toc";
146                    break;
147
148                case DOKU_LEXER_UNMATCHED :
149                    $renderer->doc .= PluginUtility::renderUnmatched($data);
150                    break;
151
152                case DOKU_LEXER_EXIT :
153                    $renderer->doc .= '';
154                    break;
155            }
156            return true;
157        }
158
159        // unsupported $mode
160        return false;
161    }
162
163}
164
165