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\LogUtility;
14use ComboStrap\PluginUtility;
15use ComboStrap\TagAttributes;
16use ComboStrap\Toc;
17
18
19/**
20 * Class syntax_plugin_combo_tov
21 *
22 */
23class syntax_plugin_combo_toc extends DokuWiki_Syntax_Plugin
24{
25
26    const TAG = "toc";
27
28    /**
29     * The attribute to holds the toc data
30     */
31    const TOC_ATTRIBUTE = "toc";
32    const CANONICAL = "toc";
33
34
35    /**
36     * Syntax Type.
37     *
38     * Needs to return one of the mode types defined in $PARSER_MODES in parser.php
39     * @see https://www.dokuwiki.org/devel:syntax_plugins#syntax_types
40     * @see DokuWiki_Syntax_Plugin::getType()
41     */
42    function getType(): string
43    {
44        return 'formatting';
45    }
46
47    /**
48     * How Dokuwiki will add P element
49     *
50     *  * 'normal' - The plugin can be used inside paragraphs (inline)
51     *  * 'block'  - Open paragraphs need to be closed before plugin output - block should not be inside paragraphs
52     *  * 'stack'  - Special case. Plugin wraps other paragraphs. - Stacks can contain paragraphs
53     *
54     * @see DokuWiki_Syntax_Plugin::getPType()
55     * @see https://www.dokuwiki.org/devel:syntax_plugins#ptype
56     */
57    function getPType()
58    {
59        return 'substition';
60    }
61
62    /**
63     * @return array
64     * Allow which kind of plugin inside
65     *
66     * No one of array('baseonly','container', 'formatting', 'substition', 'protected', 'disabled', 'paragraphs')
67     * because we manage self the content and we call self the parser
68     *
69     * Return an array of one or more of the mode types {@link $PARSER_MODES} in Parser.php
70     */
71    function getAllowedTypes(): array
72    {
73        return array();
74    }
75
76    function getSort(): int
77    {
78        return 201;
79    }
80
81
82    function connectTo($mode)
83    {
84
85        $specialPattern = PluginUtility::getEmptyTagPattern(self::TAG);
86        $this->Lexer->addSpecialPattern($specialPattern, $mode, PluginUtility::getModeFromTag($this->getPluginComponent()));
87
88
89    }
90
91    /**
92     *
93     * The handle function goal is to parse the matched syntax through the pattern function
94     * and to return the result for use in the renderer
95     * This result is always cached until the page is modified.
96     * @param string $match
97     * @param int $state
98     * @param int $pos - byte position in the original source file
99     * @param Doku_Handler $handler
100     * @return array
101     * @see DokuWiki_Syntax_Plugin::handle()
102     *
103     */
104    function handle($match, $state, $pos, Doku_Handler $handler): array
105    {
106
107        if ($state == DOKU_LEXER_SPECIAL) {
108            $attributes = PluginUtility::getTagAttributes($match);
109            return array(
110                PluginUtility::STATE => $state,
111                PluginUtility::ATTRIBUTES => $attributes
112            );
113        }
114        return array();
115
116    }
117
118    /**
119     * Render the output
120     * @param string $format
121     * @param Doku_Renderer $renderer
122     * @param array $data - what the function handle() return'ed
123     * @return boolean - rendered correctly? (however, returned value is not used at the moment)
124     * @see DokuWiki_Syntax_Plugin::render()
125     *
126     *
127     */
128    function render($format, Doku_Renderer $renderer, $data): bool
129    {
130
131        if ($format == 'xhtml') {
132
133            /** @var Doku_Renderer_xhtml $renderer */
134            $state = $data[PluginUtility::STATE];
135            /**
136             * Toc rendering
137             */
138            if ($state == DOKU_LEXER_SPECIAL) {
139                $tocNavHtml = TagAttributes::createFromCallStackArray($data[PluginUtility::ATTRIBUTES])
140                ->setLogicalTag(self::TAG)
141                    ->toHtmlEnterTag("nav");
142                $tocHtml = Toc::createForRequestedPage()
143                    ->toXhtml();
144                $renderer->doc .= "{$tocNavHtml}{$tocHtml}</nav>";
145                return true;
146            }
147
148        }
149
150        // unsupported $mode
151        return false;
152    }
153
154}
155
156