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