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