1<?php
2
3use ComboStrap\PluginUtility;
4use ComboStrap\Site;
5
6
7/**
8 *
9 * Known as code
10 * https://spec.commonmark.org/0.30/#code-spans
11 *
12 * note supported but specific highlight is done with two `==`
13 * in some processor
14 * https://www.markdownguide.org/extended-syntax/#highlight
15 *
16 */
17class syntax_plugin_combo_highlightmd extends DokuWiki_Syntax_Plugin
18{
19
20
21    const TAG = "highlightmd";
22    // Only on one line
23
24    /**
25     * Only on one line, otherwise
26     * if it's not closed, it will eat all other syntaqx
27     */
28    const ENTRY_PATTERN = "`[^`\n]*(?=`)(?!\n)";
29
30    const EXIT_PATTERN = "`";
31    const CANONICAL = self::TAG;
32
33    public function getSort(): int
34    {
35
36        return 200;
37    }
38
39    public function getType(): string
40    {
41        return 'formatting';
42    }
43
44
45    /**
46     *
47     * How Dokuwiki will add P element
48     *
49     *  * 'normal' - The plugin can be used inside paragraphs (inline)
50     *  * 'block'  - Open paragraphs need to be closed before plugin output - block should not be inside paragraphs
51     *  * 'stack'  - Special case. Plugin wraps other paragraphs. - Stacks can contain paragraphs
52     *
53     * @see DokuWiki_Syntax_Plugin::getPType()
54     *
55     * This is the equivalent of inline or block for css
56     */
57    public function getPType(): string
58    {
59        return 'normal';
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('formatting', 'substition', 'protected', 'disabled');
74    }
75
76
77    public function connectTo($mode)
78    {
79
80        $this->Lexer->addEntryPattern(self::ENTRY_PATTERN, $mode, PluginUtility::getModeFromTag($this->getPluginComponent()));
81
82
83    }
84
85    public function postConnect()
86    {
87
88        $this->Lexer->addExitPattern(self::EXIT_PATTERN, PluginUtility::getModeFromTag($this->getPluginComponent()));
89
90    }
91
92
93    /**
94     * Handle the syntax
95     *
96     * At the end of the parser, the `section_open` and `section_close` calls
97     * are created in {@link action_plugin_combo_instructionspostprocessing}
98     * and the text inside for the toc is captured
99     *
100     * @param string $match
101     * @param int $state
102     * @param int $pos
103     * @param Doku_Handler $handler
104     * @return array
105     */
106    public function handle($match, $state, $pos, Doku_Handler $handler): array
107    {
108        switch ($state) {
109
110            case DOKU_LEXER_EXIT:
111            case DOKU_LEXER_ENTER:
112
113                $content = substr($match, 1);
114                return array(
115                    PluginUtility::STATE => $state,
116                    PluginUtility::PAYLOAD => $content,
117                );
118            case DOKU_LEXER_UNMATCHED :
119
120                return PluginUtility::handleAndReturnUnmatchedData(self::TAG, $match, $handler);
121
122        }
123        return array();
124    }
125
126    public function render($format, Doku_Renderer $renderer, $data): bool
127    {
128
129        switch ($format) {
130            case "xhtml":
131                /**
132                 * @var Doku_Renderer_xhtml $renderer
133                 */
134                $state = $data[PluginUtility::STATE];
135                switch ($state) {
136
137                    case DOKU_LEXER_ENTER:
138                        $renderer->doc .= syntax_plugin_combo_highlightwiki::getOpenTagHighlight(self::TAG) . $data[PluginUtility::PAYLOAD];
139                        return true;
140                    case DOKU_LEXER_UNMATCHED:
141                        $renderer->doc .= PluginUtility::renderUnmatched($data);
142                        return true;
143                    case DOKU_LEXER_EXIT:
144                        $renderer->doc .= "</mark>";
145                        return true;
146
147                }
148                break;
149            case "metadata":
150                /**
151                 * @var Doku_Renderer_metadata $renderer
152                 */
153                $state = $data[PluginUtility::STATE];
154                switch ($state) {
155                    case DOKU_LEXER_ENTER:
156                        $renderer->doc .= $data[PluginUtility::PAYLOAD];
157                        return true;
158                    case DOKU_LEXER_UNMATCHED:
159                        $renderer->doc .= PluginUtility::renderUnmatched($data);
160                }
161                break;
162        }
163
164        return false;
165    }
166
167
168}
169