xref: /plugin/combo/syntax/headingwiki.php (revision 531e725cdb5a652164f2d97f556304e31f720033)
1<?php
2
3use ComboStrap\CallStack;
4use ComboStrap\LogUtility;
5use ComboStrap\PluginUtility;
6use ComboStrap\TagAttributes;
7
8
9/**
10 * Class headingwiki
11 * Taking over {@link \dokuwiki\Parsing\ParserMode\Header}
12 */
13class syntax_plugin_combo_headingwiki extends DokuWiki_Syntax_Plugin
14{
15
16    /**
17     * Header pattern
18     *   * Dokuwiki does not made a space mandatory after and before the opening an closing `=` character
19     *   * No line break in the look ahead
20     *   * The capture of the first spaces should be optional otherwise the {@link \dokuwiki\Parsing\ParserMode\Header} is taking over
21     *
22     * See also for information,
23     * the original heading pattern of Dokuwiki {@link \dokuwiki\Parsing\ParserMode\Header}
24     */
25    const ENTRY_PATTERN = '[ \t]*={1,6}(?=[^\n]*={1,6}\s*\r??\n)';
26    const EXIT_PATTERN = '={1,6}\s*(?=\r??\n)';
27    const TAG = "headingwiki";
28
29    const CONF_WIKI_HEADING_ENABLE = "headingWikiEnable";
30    const CONF_DEFAULT_WIKI_ENABLE_VALUE = 1;
31
32    public function getSort()
33    {
34        /**
35         * It's 49 (on less than the original heading)
36         * {@link \dokuwiki\Parsing\ParserMode\Header::getSort()}
37         */
38        return 49;
39    }
40
41    public function getType()
42    {
43        return syntax_plugin_combo_heading::SYNTAX_TYPE;
44    }
45
46
47    /**
48     *
49     * How Dokuwiki will add P element
50     *
51     *  * 'normal' - The plugin can be used inside paragraphs (inline)
52     *  * 'block'  - Open paragraphs need to be closed before plugin output - block should not be inside paragraphs
53     *  * 'stack'  - Special case. Plugin wraps other paragraphs. - Stacks can contain paragraphs
54     *
55     * @see DokuWiki_Syntax_Plugin::getPType()
56     *
57     * This is the equivalent of inline or block for css
58     */
59    public function getPType()
60    {
61        return syntax_plugin_combo_heading::SYNTAX_PTYPE;
62    }
63
64    /**
65     * @return array
66     * Allow which kind of plugin inside
67     *
68     * No one of array('baseonly','container', 'formatting', 'substition', 'protected', 'disabled', 'paragraphs')
69     * because we manage self the content and we call self the parser
70     *
71     * Return an array of one or more of the mode types {@link $PARSER_MODES} in Parser.php
72     */
73    function getAllowedTypes()
74    {
75        return array('formatting', 'substition', 'protected', 'disabled');
76    }
77
78
79    public function connectTo($mode)
80    {
81        if ($this->enableWikiHeading($mode)) {
82            $this->Lexer->addEntryPattern(self::ENTRY_PATTERN, $mode, PluginUtility::getModeForComponent($this->getPluginComponent()));
83        }
84    }
85
86    public function postConnect()
87    {
88
89        $this->Lexer->addExitPattern(self::EXIT_PATTERN, PluginUtility::getModeForComponent($this->getPluginComponent()));
90
91    }
92
93
94    /**
95     * Handle the syntax
96     *
97     * At the end of the parser, the `section_open` and `section_close` calls
98     * are created in {@link action_plugin_combo_headingpostprocessing}
99     * and the text inside for the toc is captured
100     *
101     * @param string $match
102     * @param int $state
103     * @param int $pos
104     * @param Doku_Handler $handler
105     * @return array
106     */
107    public function handle($match, $state, $pos, Doku_Handler $handler)
108    {
109        switch ($state) {
110
111            case DOKU_LEXER_ENTER:
112                /**
113                 * Title regexp
114                 */
115                $attributes[syntax_plugin_combo_heading::LEVEL] = $this->getLevelFromMatch($match);
116                $callStack = CallStack::createFromHandler($handler);
117
118                $parentTag = $callStack->moveToParent();
119                $context = syntax_plugin_combo_heading::getContext($parentTag);
120
121                return array(
122                    PluginUtility::STATE => $state,
123                    PluginUtility::ATTRIBUTES => $attributes,
124                    PluginUtility::CONTEXT => $context,
125                    PluginUtility::POSITION => $pos
126                );
127            case DOKU_LEXER_UNMATCHED :
128
129                return PluginUtility::handleAndReturnUnmatchedData(self::TAG, $match, $handler);
130
131            case DOKU_LEXER_EXIT :
132
133                $callStack = CallStack::createFromHandler($handler);
134
135                $returnedData = syntax_plugin_combo_heading::handleExit($callStack);
136
137
138                /**
139                 * Control of the Number of `=` before and after
140                 */
141                $callStack->moveToEnd();
142                $openingTag = $callStack->moveToPreviousCorrespondingOpeningCall();
143                $levelFromMatch = $this->getLevelFromMatch($match);
144                $levelFromStartTag = $openingTag->getAttribute(syntax_plugin_combo_heading::LEVEL);
145                if ($levelFromMatch != $levelFromStartTag) {
146                    $content = "";
147                    while ($actualCall = $callStack->next()) {
148                        $content .= $actualCall->getCapturedContent();
149                    }
150                    LogUtility::msg("The number of `=` character for a wiki heading is not the same before ($levelFromStartTag) and after ($levelFromMatch) the content ($content).", LogUtility::LVL_MSG_WARNING, syntax_plugin_combo_heading::CANONICAL);
151                }
152
153                return $returnedData;
154
155        }
156        return array();
157    }
158
159    public function render($format, Doku_Renderer $renderer, $data)
160    {
161
162        if ($format == "xhtml") {
163            /**
164             * @var Doku_Renderer_xhtml $renderer
165             */
166            $state = $data[PluginUtility::STATE];
167            switch ($state) {
168
169                case DOKU_LEXER_ENTER:
170                    $callStackArray = $data[PluginUtility::ATTRIBUTES];
171                    $tagAttributes = TagAttributes::createFromCallStackArray($callStackArray, syntax_plugin_combo_heading::TAG);
172                    $context = $data[PluginUtility::CONTEXT];
173                    $pos = $data[PluginUtility::POSITION];
174                    syntax_plugin_combo_heading::renderOpeningTag($context, $tagAttributes, $renderer, $pos);
175                    return true;
176                case DOKU_LEXER_UNMATCHED:
177                    $renderer->doc .= PluginUtility::renderUnmatched($data);
178                    return true;
179                case DOKU_LEXER_EXIT:
180                    $callStackArray = $data[PluginUtility::ATTRIBUTES];
181                    $tagAttributes = TagAttributes::createFromCallStackArray($callStackArray);
182                    $renderer->doc .= syntax_plugin_combo_heading::renderClosingTag($tagAttributes);
183                    return true;
184
185            }
186        } else if ($format == renderer_plugin_combo_analytics::RENDERER_FORMAT) {
187
188            /**
189             * @var renderer_plugin_combo_analytics $renderer
190             */
191            syntax_plugin_combo_heading::processMetadataAnalytics($data, $renderer);
192
193        } else if ($format == "metadata") {
194
195            /**
196             * @var Doku_Renderer_metadata $renderer
197             */
198            syntax_plugin_combo_heading::processHeadingMetadata($data, $renderer);
199
200        }
201
202        return false;
203    }
204
205    /**
206     * @param $match
207     * @return int
208     */
209    public
210    function getLevelFromMatch($match)
211    {
212        return 7 - strlen(trim($match));
213    }
214
215
216    private
217    function enableWikiHeading($mode)
218    {
219
220
221        /**
222         * Basically all mode that are not `base`
223         * To not take the dokuwiki heading
224         */
225        if (!(in_array($mode, ['base', 'header', 'table']))) {
226            return true;
227        } else {
228            return PluginUtility::getConfValue(self::CONF_WIKI_HEADING_ENABLE, self::CONF_DEFAULT_WIKI_ENABLE_VALUE);
229        }
230
231
232    }
233
234
235}
236