xref: /plugin/combo/syntax/text.php (revision 531e725cdb5a652164f2d97f556304e31f720033)
1<?php
2
3
4// must be run within Dokuwiki
5use ComboStrap\CallStack;
6use ComboStrap\PluginUtility;
7use ComboStrap\TagAttributes;
8use ComboStrap\TextAlign;
9
10if (!defined('DOKU_INC')) die();
11
12/**
13 * Class syntax_plugin_combo_text
14 * A text block that permits to style
15 * paragraph at once
16 *
17 * The output will be a series of {@link syntax_plugin_combo_para paragraph}
18 * with the same properties
19 *
20 * It permits to have several paragraph
21 */
22class syntax_plugin_combo_text extends DokuWiki_Syntax_Plugin
23{
24
25    const TAG = "text";
26    const TAGS = ["typo", self::TAG];
27
28    /**
29     * Syntax Type.
30     *
31     * Needs to return one of the mode types defined in {@link $PARSER_MODES} in parser.php
32     * @see DokuWiki_Syntax_Plugin::getType()
33     */
34    function getType()
35    {
36        return 'paragraphs';
37    }
38
39    /**
40     * How Dokuwiki will add P element
41     *
42     *  * 'normal' - The plugin can be used inside paragraphs
43     *  * 'block'  - Open paragraphs need to be closed before plugin output - block should not be inside paragraphs
44     *  * 'stack'  - Special case. Plugin wraps other paragraphs. - Stacks can contain paragraphs
45     *
46     * @see DokuWiki_Syntax_Plugin::getPType()
47     */
48    function getPType()
49    {
50        return 'stack';
51    }
52
53    /**
54     * @return array
55     * Allow which kind of plugin inside
56     *
57     * No one of array('baseonly','container', 'formatting', 'substition', 'protected', 'disabled', 'paragraphs')
58     * because we manage self the content and we call self the parser
59     *
60     * Return an array of one or more of the mode types {@link $PARSER_MODES} in Parser.php
61     */
62    function getAllowedTypes()
63    {
64        return array('formatting', 'substition', 'paragraphs');
65    }
66
67    public function accepts($mode)
68    {
69
70        return syntax_plugin_combo_preformatted::disablePreformatted($mode);
71
72    }
73
74
75    function getSort()
76    {
77        return 201;
78    }
79
80
81    function connectTo($mode)
82    {
83
84        foreach (self::TAGS as $tag) {
85            $pattern = PluginUtility::getContainerTagPattern($tag);
86            $this->Lexer->addEntryPattern($pattern, $mode, PluginUtility::getModeForComponent($this->getPluginComponent()));
87        }
88    }
89
90
91    function postConnect()
92    {
93        foreach (self::TAGS as $tag) {
94            $this->Lexer->addExitPattern('</' . $tag . '>', PluginUtility::getModeForComponent($this->getPluginComponent()));
95        }
96
97    }
98
99    function handle($match, $state, $pos, Doku_Handler $handler)
100    {
101
102        switch ($state) {
103
104            case DOKU_LEXER_ENTER :
105                $attributes = TagAttributes::createFromTagMatch($match);
106                $callStackArray = $attributes->toCallStackArray();
107
108                return array(
109                    PluginUtility::STATE => $state,
110                    PluginUtility::ATTRIBUTES => $callStackArray
111                );
112
113            case DOKU_LEXER_UNMATCHED :
114                return PluginUtility::handleAndReturnUnmatchedData(self::TAG, $match, $handler);
115
116            case DOKU_LEXER_EXIT :
117                /**
118                 * Transform all paragraphs
119                 * with the type as class
120                 */
121                $callStack = CallStack::createFromHandler($handler);
122                $openingCall = $callStack->moveToPreviousCorrespondingOpeningCall();
123                $attributes = $openingCall->getAttributes();
124                // if there is no EOL, we add one to create at minimal a paragraph
125                $callStack->insertEolIfNextCallIsNotEolOrBlock();
126                $callStack->processEolToEndStack($attributes);
127                return array(PluginUtility::STATE => $state);
128
129
130        }
131        return array();
132
133    }
134
135    /**
136     * Render the output
137     * @param string $format
138     * @param Doku_Renderer $renderer
139     * @param array $data - what the function handle() return'ed
140     * @return boolean - rendered correctly? (however, returned value is not used at the moment)
141     * @see DokuWiki_Syntax_Plugin::render()
142     *
143     *
144     */
145    function render($format, Doku_Renderer $renderer, $data)
146    {
147        if ($format == 'xhtml') {
148
149            /** @var Doku_Renderer_xhtml $renderer */
150            $state = $data[PluginUtility::STATE];
151            switch ($state) {
152                case DOKU_LEXER_EXIT :
153                case DOKU_LEXER_ENTER :
154                    /**
155                     * The {@link DOKU_LEXER_EXIT} of the {@link syntax_plugin_combo_text::handle()}
156                     * has already created in the callstack the {@link syntax_plugin_combo_para} call
157                     */
158                    $renderer->doc .= "";
159                    break;
160                case DOKU_LEXER_UNMATCHED :
161                    $renderer->doc .= PluginUtility::renderUnmatched($data);
162                    break;
163            }
164            return true;
165        }
166
167        // unsupported $mode
168        return false;
169    }
170
171
172}
173
174