1<?php
2
3
4// must be run within Dokuwiki
5use ComboStrap\PluginUtility;
6use ComboStrap\TagAttributes;
7use ComboStrap\XmlTagProcessing;
8
9if (!defined('DOKU_INC')) die();
10
11/**
12 * Class syntax_plugin_combo_itext
13 * Setting text attributes on words
14 *
15 */
16class syntax_plugin_combo_itext extends DokuWiki_Syntax_Plugin
17{
18
19    const TAG = "itext";
20
21    /**
22     * Syntax Type.
23     *
24     * Needs to return one of the mode types defined in {@link $PARSER_MODES} in parser.php
25     * @see DokuWiki_Syntax_Plugin::getType()
26     */
27    function getType()
28    {
29        return 'formatting';
30    }
31
32    /**
33     * How Dokuwiki will add P element
34     *
35     *  * 'normal' - The plugin can be used inside paragraphs
36     *  * 'block'  - Open paragraphs need to be closed before plugin output - block should not be inside paragraphs
37     *  * 'stack'  - Special case. Plugin wraps other paragraphs. - Stacks can contain paragraphs
38     *
39     * @see DokuWiki_Syntax_Plugin::getPType()
40     */
41    function getPType()
42    {
43        return 'normal';
44    }
45
46    /**
47     * @return array
48     * Allow which kind of plugin inside
49     *
50     * No one of array('baseonly','container', 'formatting', 'substition', 'protected', 'disabled', 'paragraphs')
51     * because we manage self the content and we call self the parser
52     *
53     * Return an array of one or more of the mode types {@link $PARSER_MODES} in Parser.php
54     */
55    function getAllowedTypes()
56    {
57        return array('formatting', 'substition', 'protected');
58    }
59
60
61    function getSort()
62    {
63        return 201;
64    }
65
66
67    function connectTo($mode)
68    {
69
70
71        $pattern = XmlTagProcessing::getContainerTagPattern(self::TAG);
72        $this->Lexer->addEntryPattern($pattern, $mode, PluginUtility::getModeFromTag($this->getPluginComponent()));
73
74    }
75
76
77    function postConnect()
78    {
79
80        $this->Lexer->addExitPattern('</' . self::TAG . '>', PluginUtility::getModeFromTag($this->getPluginComponent()));
81
82
83    }
84
85    function handle($match, $state, $pos, Doku_Handler $handler)
86    {
87
88        switch ($state) {
89
90            case DOKU_LEXER_ENTER :
91                $knownTypes = [];
92                $defaultAttributes = [];
93                $attributes = TagAttributes::createFromTagMatch($match, $defaultAttributes, $knownTypes);
94
95                $callStackArray = $attributes->toCallStackArray();
96
97                return array(
98                    PluginUtility::STATE => $state,
99                    PluginUtility::ATTRIBUTES => $callStackArray
100                );
101
102            case DOKU_LEXER_UNMATCHED :
103                return PluginUtility::handleAndReturnUnmatchedData(self::TAG, $match, $handler);
104
105            case DOKU_LEXER_EXIT :
106
107                return array(PluginUtility::STATE => $state);
108
109
110        }
111        return array();
112
113    }
114
115    /**
116     * Render the output
117     * @param string $format
118     * @param Doku_Renderer $renderer
119     * @param array $data - what the function handle() return'ed
120     * @return boolean - rendered correctly? (however, returned value is not used at the moment)
121     * @see DokuWiki_Syntax_Plugin::render()
122     *
123     *
124     */
125    function render($format, Doku_Renderer $renderer, $data): bool
126    {
127        if ($format == 'xhtml') {
128
129            /** @var Doku_Renderer_xhtml $renderer */
130            $state = $data[PluginUtility::STATE];
131            switch ($state) {
132                case DOKU_LEXER_ENTER :
133                    $tagAttributes = TagAttributes::createFromCallStackArray($data[PluginUtility::ATTRIBUTES]);
134                    $toHtmlEnterTag = $tagAttributes->toHtmlEnterTag("span");
135                    $renderer->doc .= $toHtmlEnterTag;
136                    break;
137                case DOKU_LEXER_UNMATCHED :
138                    $renderer->doc .= PluginUtility::renderUnmatched($data);
139                    break;
140                case DOKU_LEXER_EXIT :
141                    $renderer->doc .= "</span>";
142                    break;
143            }
144            return true;
145        }
146
147        // unsupported $mode
148        return false;
149    }
150
151
152}
153
154