1<?php
2
3use ComboStrap\PluginUtility;
4use ComboStrap\TagAttribute\Boldness;
5
6
7/**
8 *
9 * Taking over {@link \dokuwiki\Parsing\ParserMode\Formatting strong}
10 *
11 * * To disallow the preformatted mode
12 * * To add the one line constraint
13 *
14 */
15class syntax_plugin_combo_strong extends DokuWiki_Syntax_Plugin
16{
17
18
19    const TAG = "strong";
20    /**
21     * Explanation
22     *   * `\n?` to take over the listblock {@link \dokuwiki\Parsing\ParserMode\Listblock}
23     *
24     *   * Only on one line
25     *
26     */
27    const ENTRY_PATTERN = "[ \t]*\*\*(?=[^\n]*\*\*)(?!\n)";
28    const EXIT_PATTERN = "\*\*";
29
30    const CANONICAL = Boldness::CANONICAL;
31
32
33    public function getSort(): int
34    {
35        /**
36         * It's 9
37         *
38         * Before the 10 of {@link \dokuwiki\Parsing\ParserMode\Listblock}
39         * Before the original of 70
40         * {@link \dokuwiki\Parsing\ParserMode\Formatting}
41         */
42        return 9;
43    }
44
45    public function getType(): string
46    {
47        return 'formatting';
48    }
49
50
51    /**
52     *
53     * How Dokuwiki will add P element
54     *
55     *  * 'normal' - The plugin can be used inside paragraphs (inline)
56     *  * 'block'  - Open paragraphs need to be closed before plugin output - block should not be inside paragraphs
57     *  * 'stack'  - Special case. Plugin wraps other paragraphs. - Stacks can contain paragraphs
58     *
59     * @see DokuWiki_Syntax_Plugin::getPType()
60     *
61     * This is the equivalent of inline or block for css
62     */
63    public function getPType(): string
64    {
65        return 'normal';
66    }
67
68    /**
69     * @return array
70     * Allow which kind of plugin inside
71     *
72     * No one of array('baseonly','container', 'formatting', 'substition', 'protected', 'disabled', 'paragraphs')
73     * because we manage self the content and we call self the parser
74     *
75     * Return an array of one or more of the mode types {@link $PARSER_MODES} in Parser.php
76     */
77    function getAllowedTypes(): array
78    {
79        return array('formatting', 'substition', 'protected', 'disabled');
80    }
81
82
83    public function connectTo($mode)
84    {
85
86        $this->Lexer->addEntryPattern(self::ENTRY_PATTERN, $mode, PluginUtility::getModeFromTag($this->getPluginComponent()));
87
88
89    }
90
91    public function postConnect()
92    {
93
94        $this->Lexer->addExitPattern(self::EXIT_PATTERN, PluginUtility::getModeFromTag($this->getPluginComponent()));
95
96    }
97
98
99    /**
100     * Handle the syntax
101     *
102     * At the end of the parser, the `section_open` and `section_close` calls
103     * are created in {@link action_plugin_combo_instructionspostprocessing}
104     * and the text inside for the toc is captured
105     *
106     * @param string $match
107     * @param int $state
108     * @param int $pos
109     * @param Doku_Handler $handler
110     * @return array
111     */
112    public function handle($match, $state, $pos, Doku_Handler $handler): array
113    {
114        switch ($state) {
115
116            case DOKU_LEXER_EXIT:
117            case DOKU_LEXER_ENTER:
118                $beforeSpaces = str_replace("**", "", $match);
119                return array(
120                    PluginUtility::STATE => $state,
121                    PluginUtility::PAYLOAD => $beforeSpaces
122                );
123            case DOKU_LEXER_UNMATCHED :
124
125                return PluginUtility::handleAndReturnUnmatchedData(self::TAG, $match, $handler);
126
127        }
128        return array();
129    }
130
131    public function render($format, $renderer, $data): bool
132    {
133
134        switch ($format) {
135            case "xhtml":
136            {
137                /**
138                 * @var Doku_Renderer_xhtml $renderer
139                 */
140                $state = $data[PluginUtility::STATE];
141                switch ($state) {
142
143                    case DOKU_LEXER_ENTER:
144
145                        $renderer->doc .= $data[PluginUtility::PAYLOAD] . "<strong>";
146                        return true;
147                    case DOKU_LEXER_UNMATCHED:
148                        $renderer->doc .= PluginUtility::renderUnmatched($data);
149                        return true;
150                    case DOKU_LEXER_EXIT:
151                        $renderer->doc .= "</strong>";
152                        return true;
153
154                }
155                break;
156            }
157            case "metadata":
158                /**
159                 * For the automatic description, we render
160                 * only the un-match
161                 *
162                 * @var Doku_Renderer_metadata $renderer
163                 */
164                $state = $data[PluginUtility::STATE];
165                if ($state == DOKU_LEXER_UNMATCHED) {
166                    $renderer->doc .= PluginUtility::renderUnmatched($data);
167                }
168                break;
169        }
170
171        return false;
172    }
173
174
175}
176