1<?php
2
3
4use ComboStrap\BlockquoteTag;
5use ComboStrap\CallStack;
6use ComboStrap\CardTag;
7use ComboStrap\PluginUtility;
8use ComboStrap\TagAttributes;
9use ComboStrap\XmlTagProcessing;
10
11
12/**
13 * An header may be:
14 *   * A outline section header
15 *   * The header of a card ...
16 */
17class syntax_plugin_combo_header extends DokuWiki_Syntax_Plugin
18{
19
20
21    const TAG = "header";
22
23    function getType(): string
24    {
25        return 'container';
26    }
27
28    /**
29     * How Dokuwiki will add P element
30     *
31     *  * 'normal' - The plugin can be used inside paragraphs (inline)
32     *  * 'block'  - Open paragraphs need to be closed before plugin output - block should not be inside paragraphs
33     *  * 'stack'  - Special case. Plugin wraps other paragraphs. - Stacks can contain paragraphs
34     *
35     * @see DokuWiki_Syntax_Plugin::getPType()
36     */
37    function getPType(): string
38    {
39        // block because we don't want any `p` created inside by dokuwiki, otherwise the card-header is not happy
40        return 'block';
41    }
42
43    function getAllowedTypes(): array
44    {
45        return array('substition', 'formatting', 'disabled');
46    }
47
48    function getSort()
49    {
50        return 201;
51    }
52
53
54    function connectTo($mode)
55    {
56
57        $this->Lexer->addEntryPattern(XmlTagProcessing::getContainerTagPattern(self::TAG), $mode, PluginUtility::getModeFromTag($this->getPluginComponent()));
58    }
59
60    public function postConnect()
61    {
62        $this->Lexer->addExitPattern('</' . self::TAG . '>', PluginUtility::getModeFromTag($this->getPluginComponent()));
63    }
64
65    function handle($match, $state, $pos, Doku_Handler $handler): array
66    {
67
68        switch ($state) {
69
70            case DOKU_LEXER_ENTER:
71                $tagAttributes = PluginUtility::getTagAttributes($match);
72                $callStack = CallStack::createFromHandler($handler);
73                $parent = $callStack->moveToParent();
74                $parentName = "";
75                if ($parent !== false) {
76                    $parentName = $parent->getTagName();
77                }
78                return array(
79                    PluginUtility::STATE => $state,
80                    PluginUtility::ATTRIBUTES => $tagAttributes,
81                    PluginUtility::CONTEXT => $parentName
82                );
83
84            case DOKU_LEXER_UNMATCHED :
85                return array(
86                    PluginUtility::STATE => $state,
87                    PluginUtility::PAYLOAD => $match);
88
89            case DOKU_LEXER_EXIT :
90                $callStack = CallStack::createFromHandler($handler);
91                $openingCall = $callStack->moveToPreviousCorrespondingOpeningCall();
92                return array(
93                    PluginUtility::STATE => $state,
94                    PluginUtility::CONTEXT => $openingCall->getContext()
95                );
96
97
98        }
99        return array();
100
101    }
102
103    /**
104     * Render the output
105     * @param string $format
106     * @param Doku_Renderer $renderer
107     * @param array $data - what the function handle() return'ed
108     * @return boolean - rendered correctly? (however, returned value is not used at the moment)
109     * @see DokuWiki_Syntax_Plugin::render()
110     *
111     *
112     */
113    function render($format, Doku_Renderer $renderer, $data): bool
114    {
115
116        if ($format === 'xhtml') {
117
118            /** @var Doku_Renderer_xhtml $renderer */
119            $state = $data[PluginUtility::STATE];
120            switch ($state) {
121
122                case DOKU_LEXER_ENTER:
123                    $parent = $data[PluginUtility::CONTEXT];
124                    $tagAttributes = TagAttributes::createFromCallStackArray($data[PluginUtility::ATTRIBUTES]);
125                    switch ($parent) {
126                        case BlockquoteTag::TAG:
127                        case CardTag::CARD_TAG:
128                            $tagAttributes->addClassName("card-header");
129                            $renderer->doc .= $tagAttributes->toHtmlEnterTag("div");
130                            break;
131                        default:
132                            $renderer->doc .= $tagAttributes
133                                ->setLogicalTag(self::TAG)
134                                ->toHtmlEnterTag("header");
135                            break;
136                    }
137                    break;
138
139                case DOKU_LEXER_UNMATCHED :
140                    $renderer->doc .= PluginUtility::renderUnmatched($data);
141                    break;
142
143                case DOKU_LEXER_EXIT:
144                    $parent = $data[PluginUtility::CONTEXT];
145                    switch ($parent) {
146                        case BlockquoteTag::TAG:
147                        case CardTag::CARD_TAG:
148                            $renderer->doc .= "</div>";
149                            break;
150                        default:
151                            $renderer->doc .= "</header>";
152                            break;
153                    }
154                    break;
155
156
157            }
158        }
159        // unsupported $mode
160        return false;
161    }
162
163
164}
165
166