xref: /plugin/combo/syntax/brand.php (revision 32b85071e019dd3646a67c17fac4051338e495eb)
1<?php
2
3
4// must be run within Dokuwiki
5use ComboStrap\PluginUtility;
6
7if (!defined('DOKU_INC')) die();
8
9
10class syntax_plugin_combo_brand extends DokuWiki_Syntax_Plugin
11{
12
13    const TAG = "brand";
14
15    /**
16     * Syntax Type.
17     *
18     * Needs to return one of the mode types defined in $PARSER_MODES in parser.php
19     * @see DokuWiki_Syntax_Plugin::getType()
20     */
21    function getType()
22    {
23        return 'formatting';
24    }
25
26    /**
27     * How Dokuwiki will add P element
28     *
29     *  * 'normal' - The plugin can be used inside paragraphs
30     *  * 'block'  - Open paragraphs need to be closed before plugin output - block should not be inside paragraphs
31     *  * 'stack'  - Special case. Plugin wraps other paragraphs. - Stacks can contain paragraphs
32     *
33     * @see DokuWiki_Syntax_Plugin::getPType()
34     */
35    function getPType()
36    {
37        return 'normal';
38    }
39
40    /**
41     * @return array
42     * Allow which kind of plugin inside
43     *
44     * array('container', 'baseonly', 'formatting', 'substition', 'protected', 'disabled', 'paragraphs')
45     *
46     */
47    function getAllowedTypes()
48    {
49        return array('container', 'baseonly', 'formatting', 'substition', 'protected', 'disabled');
50    }
51
52    function getSort()
53    {
54        return 201;
55    }
56
57
58    /**
59     * Create a pattern that will called this plugin
60     *
61     * @param string $mode
62     * @see Doku_Parser_Mode::connectTo()
63     */
64    function connectTo($mode)
65    {
66
67        $pattern = PluginUtility::getContainerTagPattern(self::getTag());
68        $this->Lexer->addEntryPattern($pattern, $mode, 'plugin_' . PluginUtility::PLUGIN_BASE_NAME . '_' . $this->getPluginComponent());
69
70    }
71
72    function postConnect()
73    {
74
75        $this->Lexer->addExitPattern('</' . self::getTag() . '>', 'plugin_' . PluginUtility::PLUGIN_BASE_NAME . '_' . $this->getPluginComponent());
76
77    }
78
79    function handle($match, $state, $pos, Doku_Handler $handler)
80    {
81
82        switch ($state) {
83
84            case DOKU_LEXER_ENTER :
85                $parameters = PluginUtility::getTagAttributes($match);
86                return array(
87                    PluginUtility::STATE => $state,
88                    PluginUtility::ATTRIBUTES => $parameters
89                );
90
91            case DOKU_LEXER_UNMATCHED :
92                return PluginUtility::handleAndReturnUnmatchedData(self::TAG, $match, $handler);
93
94            case DOKU_LEXER_EXIT :
95
96                // Important otherwise we don't get an exit in the render
97                return array(
98                    PluginUtility::STATE => $state
99                );
100
101
102        }
103        return array();
104
105    }
106
107    /**
108     * Render the output
109     * @param string $format
110     * @param Doku_Renderer $renderer
111     * @param array $data - what the function handle() return'ed
112     * @return boolean - rendered correctly? (however, returned value is not used at the moment)
113     * @see DokuWiki_Syntax_Plugin::render()
114     *
115     *
116     */
117    function render($format, Doku_Renderer $renderer, $data)
118    {
119
120        if ($format == 'xhtml') {
121
122            /** @var Doku_Renderer_xhtml $renderer */
123            $state = $data[PluginUtility::STATE];
124            switch ($state) {
125                case DOKU_LEXER_ENTER :
126                    $renderer->doc .= '<a href="' . wl() . '" accesskey="h"';
127
128                    $parameters = $data[PluginUtility::ATTRIBUTES];
129                    $title = ' title="';
130                    if (array_key_exists("title", $parameters)) {
131                        $title .= $parameters["title"];
132                    } else {
133                        global $conf;
134                        $title .= $conf['title'];
135                    }
136                    $title .= '"';
137                    $renderer->doc .= $title;
138
139                    $renderer->doc .= ' class="navbar-brand';
140                    if (array_key_exists("class", $parameters)) {
141                        $renderer->doc .= ' ' . hsc($parameters["class"]);
142                    }
143                    $renderer->doc .= '"';
144                    if (array_key_exists("style", $parameters)) {
145                        $renderer->doc .= ' style="' . hsc($parameters["style"]) . '"';
146                    }
147                    $renderer->doc .= '>';
148                    break;
149
150                case DOKU_LEXER_UNMATCHED :
151                    // What about:
152                    //   * the title of the website ? $conf['title']
153                    //   * the logo ? $logo = tpl_getMediaFile(array(':wiki:logo.png', ':logo.png', 'images/logo.png'), false, $logoSize);
154                    $renderer->doc .= PluginUtility::renderUnmatched($data);
155                    break;
156
157                case DOKU_LEXER_EXIT :
158                    $renderer->doc .= '</a>';
159                    break;
160            }
161            return true;
162        }
163
164        // unsupported $mode
165        return false;
166    }
167
168    public static function getTag()
169    {
170        list(/* $t */, /* $p */, /* $n */, $c) = explode('_', get_called_class(), 4);
171        return (isset($c) ? $c : '');
172    }
173
174}
175
176