1<?php
2/**
3 * Bootstrap Wrapper Plugin: Button
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Giuseppe Di Terlizzi <giuseppe.diterlizzi@gmail.com>
7 * @copyright  (C) 2015-2020, Giuseppe Di Terlizzi
8 */
9
10class syntax_plugin_bootswrapper_button extends syntax_plugin_bootswrapper_bootstrap
11{
12
13    public $p_type         = 'normal';
14    public $pattern_start  = '<(?:btn|button).*?>(?=.*?</(?:btn|button)>)';
15    public $pattern_end    = '</(?:btn|button)>';
16    public $tag_name       = 'button';
17    public $tag_attributes = array(
18
19        'type'     => array(
20            'type'     => 'string',
21            'values'   => array('default', 'primary', 'success', 'info', 'warning', 'danger', 'link'),
22            'required' => true,
23            'default'  => 'default'),
24
25        'size'     => array(
26            'type'     => 'string',
27            'values'   => array('lg', 'sm', 'xs'),
28            'required' => false,
29            'default'  => null),
30
31        'icon'     => array(
32            'type'     => 'string',
33            'values'   => null,
34            'required' => false,
35            'default'  => null),
36
37        'collapse' => array(
38            'type'     => 'string',
39            'values'   => null,
40            'required' => false,
41            'default'  => null),
42
43        'modal'    => array(
44            'type'     => 'string',
45            'values'   => null,
46            'required' => false,
47            'default'  => null),
48
49        'block'    => array(
50            'type'     => 'boolean',
51            'values'   => array(0, 1),
52            'required' => false,
53            'default'  => null),
54
55        'disabled' => array(
56            'type'     => 'boolean',
57            'values'   => array(0, 1),
58            'required' => false,
59            'default'  => null),
60
61    );
62
63    public function render($mode, Doku_Renderer $renderer, $data)
64    {
65
66        if (empty($data)) {
67            return false;
68        }
69
70        if ($mode !== 'xhtml') {
71            return false;
72        }
73
74        /** @var Doku_Renderer_xhtml $renderer */
75        list($state, $match, $pos, $attributes) = $data;
76
77        if ($state == DOKU_LEXER_ENTER) {
78            $html_attributes            = $this->mergeCoreAttributes($attributes);
79            $html_attributes['class'][] = 'bs-wrap bs-wrap-button';
80
81            foreach (array_keys($this->tag_attributes) as $attribute) {
82                if (isset($attributes[$attribute])) {
83                    $html_attributes["data-btn-$attribute"] = $attributes[$attribute];
84                }
85            }
86
87            $markup = '<span ' . $this->buildAttributes($html_attributes) . '>';
88
89            $renderer->doc .= $markup;
90            return true;
91        }
92
93        if ($state == DOKU_LEXER_EXIT) {
94            $renderer->doc .= "</span>";
95            return true;
96        }
97
98        return true;
99    }
100}
101