xref: /plugin/combo/syntax/header.php (revision 5f891b7e09648e05e78f5882f3fdde1e9df9b0f1)
1<?php
2
3// implementation of
4// https://developer.mozilla.org/en-US/docs/Web/HTML/Element/cite
5
6// must be run within Dokuwiki
7use ComboStrap\HeaderUtility;
8use ComboStrap\TitleUtility;
9use ComboStrap\PluginUtility;
10use ComboStrap\StringUtility;
11use ComboStrap\Tag;
12
13require_once(__DIR__ . '/../class/HeaderUtility.php');
14
15if (!defined('DOKU_INC')) die();
16
17
18class syntax_plugin_combo_header extends DokuWiki_Syntax_Plugin
19{
20
21
22    function getType()
23    {
24        return 'formatting';
25    }
26
27    /**
28     * How Dokuwiki will add P element
29     *
30     *  * 'normal' - The plugin can be used inside paragraphs (inline)
31     *  * 'block'  - Open paragraphs need to be closed before plugin output - block should not be inside paragraphs
32     *  * 'stack'  - Special case. Plugin wraps other paragraphs. - Stacks can contain paragraphs
33     *
34     * @see DokuWiki_Syntax_Plugin::getPType()
35     */
36    function getPType()
37    {
38        return 'normal';
39    }
40
41    function getAllowedTypes()
42    {
43        return array('substition', 'formatting', 'disabled');
44    }
45
46    function getSort()
47    {
48        return 201;
49    }
50
51
52    function connectTo($mode)
53    {
54
55        $this->Lexer->addEntryPattern(PluginUtility::getContainerTagPattern(HeaderUtility::HEADER), $mode, PluginUtility::getModeForComponent($this->getPluginComponent()));
56    }
57
58    public function postConnect()
59    {
60        $this->Lexer->addExitPattern('</' . HeaderUtility::HEADER . '>', PluginUtility::getModeForComponent($this->getPluginComponent()));
61    }
62
63    function handle($match, $state, $pos, Doku_Handler $handler)
64    {
65
66        switch ($state) {
67
68            case DOKU_LEXER_ENTER:
69                $tagAttributes = PluginUtility::getTagAttributes($match);
70                $htmlAttributes = $tagAttributes;
71                $tag = new Tag(HeaderUtility::HEADER, $tagAttributes, $state, $handler);
72                $parent = $tag->getParent();
73                $parentName = "";
74                $html = "";
75                if ($parent != null) {
76                    $parentName = $parent->getName();
77                    switch ($parentName) {
78                        case syntax_plugin_combo_blockquote::TAG:
79                        case syntax_plugin_combo_card::TAG:
80                            PluginUtility::addClass2Attributes("card-header", $htmlAttributes);
81                            $inlineAttributes = PluginUtility::array2HTMLAttributes($htmlAttributes);
82                            $html = "<div {$inlineAttributes}>" . DOKU_LF;
83                            break;
84                    }
85                }
86                return array(
87                    PluginUtility::STATE => $state,
88                    PluginUtility::ATTRIBUTES => $tagAttributes,
89                    PluginUtility::PAYLOAD => $html,
90                    PluginUtility::CONTEXT => $parentName
91                );
92
93            case DOKU_LEXER_UNMATCHED :
94                return array(
95                    PluginUtility::STATE => $state,
96                    PluginUtility::PAYLOAD => $match);
97
98            case DOKU_LEXER_EXIT :
99                $html = "</div>";
100                $tag = new Tag(HeaderUtility::HEADER, array(), $state, $handler);
101                $parent = $tag->getParent();
102                if ($parent != null) {
103                    switch ($parent->getName()) {
104                        case syntax_plugin_combo_blockquote::TAG:
105                            $html .= syntax_plugin_combo_blockquote::CARD_BODY_BLOCKQUOTE_OPEN_TAG;
106                            break;
107                        case syntax_plugin_combo_card::TAG:
108                            $html .= syntax_plugin_combo_card::CARD_BODY;
109                            break;
110                    }
111                }
112                return array(
113                    PluginUtility::STATE => $state,
114                    PluginUtility::PAYLOAD => $html
115                );
116
117
118        }
119        return array();
120
121    }
122
123    /**
124     * Render the output
125     * @param string $format
126     * @param Doku_Renderer $renderer
127     * @param array $data - what the function handle() return'ed
128     * @return boolean - rendered correctly? (however, returned value is not used at the moment)
129     * @see DokuWiki_Syntax_Plugin::render()
130     *
131     *
132     */
133    function render($format, Doku_Renderer $renderer, $data)
134    {
135
136        if ($format == 'xhtml') {
137
138            /** @var Doku_Renderer_xhtml $renderer */
139            $state = $data[PluginUtility::STATE];
140            switch ($state) {
141
142                case DOKU_LEXER_ENTER:
143                    $parent = $data[PluginUtility::CONTEXT];
144                    switch ($parent) {
145                        case syntax_plugin_combo_blockquote::TAG:
146                            StringUtility::rtrim($renderer->doc, syntax_plugin_combo_blockquote::CARD_BODY_BLOCKQUOTE_OPEN_TAG);
147                            break;
148                        case syntax_plugin_combo_card::TAG:
149                            StringUtility::rtrim($renderer->doc, syntax_plugin_combo_card::CARD_BODY);
150                            break;
151                    }
152                    $renderer->doc .= $data[PluginUtility::PAYLOAD];
153                    break;
154
155                case DOKU_LEXER_UNMATCHED :
156                    $renderer->doc .= PluginUtility::escape($data[PluginUtility::PAYLOAD]);
157                    break;
158
159                case DOKU_LEXER_EXIT:
160                    $renderer->doc .= $data[PluginUtility::PAYLOAD];
161                    break;
162
163
164            }
165        }
166        // unsupported $mode
167        return false;
168    }
169
170
171}
172
173