1<?php
2/**
3 * Bootstrap Wrapper Plugin: Callout
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_callout extends syntax_plugin_bootswrapper_bootstrap
11{
12
13    public $p_type         = 'block';
14    public $pattern_start  = '<callout.*?>(?=.*?</callout>)';
15    public $pattern_end    = '</callout>';
16    public $tag_name       = 'callout';
17    public $tag_attributes = array(
18
19        'type'  => array(
20            'type'     => 'string',
21            'values'   => array('default', 'primary', 'success', 'info', 'warning', 'danger', 'question', 'tip'),
22            'required' => true,
23            'default'  => 'default'),
24
25        'title' => array(
26            'type'     => 'string',
27            'values'   => null,
28            'required' => false,
29            'default'  => null),
30
31        'color' => array(
32            'type'     => 'string',
33            'values'   => null,
34            'required' => false,
35            'default'  => null),
36
37        'icon'  => array(
38            'type'     => 'string',
39            'values'   => null,
40            'required' => false,
41            'default'  => null),
42
43    );
44
45    public function render($mode, Doku_Renderer $renderer, $data)
46    {
47
48        if (empty($data)) {
49            return false;
50        }
51
52        if ($mode !== 'xhtml') {
53            return false;
54        }
55
56        /** @var Doku_Renderer_xhtml $renderer */
57        list($state, $match, $pos, $attributes) = $data;
58
59        global $icon;
60
61        if ($state == DOKU_LEXER_ENTER) {
62            $type  = $attributes['type'];
63            $icon  = (isset($attributes['icon']) ? $attributes['icon'] : '');
64            $color = (isset($attributes['color']) ? $attributes['color'] : false);
65            $title = (isset($attributes['title']) ? $attributes['title'] : false);
66
67            $icon_class    = '';
68            $text_color    = '';
69            $callout_color = '';
70
71            $html_attributes            = $this->mergeCoreAttributes($attributes);
72            $html_attributes['class'][] = 'bs-wrap bs-callout';
73
74            # Automatic detection of icon
75            if (strtolower($icon) == 'true') {
76
77                switch ($type) {
78                    case 'success':
79                        $icon_class = 'check-circle';
80                        break;
81
82                    case 'info':
83                        $icon_class = 'info-circle';
84                        break;
85
86                    case 'danger':
87                        $icon_class = 'minus-circle';
88                        break;
89
90                    case 'primary':
91                        $icon_class = 'exclamation-circle';
92                        break;
93
94                    case 'warning':
95                        $icon_class = 'exclamation-triangle';
96                        break;
97
98                    // Extra
99                    case 'question':
100                        $type       = 'primary';
101                        $icon_class = 'question-circle';
102                        break;
103
104                    case 'tip':
105                        $type       = 'warning';
106                        $icon_class = 'lightbulb-o';
107                        break;
108
109                    default:
110                        $icon_class = $type;
111                }
112
113                $icon_class = "fa fa-$icon_class";
114            } else {
115                $icon_class = $icon;
116            }
117
118            if ($color) {
119                $html_attributes['style']['border-left-color'] = $color;
120                $text_color                                    = ' style="color:' . $color . '"';
121            }
122
123            $html_attributes['class'][] = "bs-callout-$type";
124
125            $markup = '<div ' . $this->buildAttributes($html_attributes) . '>';
126
127            if ($icon && $icon_class) {
128                $markup .= '<div class="row"><div class="col-xs-1"><i class="bs-callout-icon ' . $icon_class . '"' . $text_color . '></i></div><div class="col-xs-11">';
129            }
130
131            if ($title) {
132                $markup .= '<h4' . $text_color . '>' . $title . '</h4>';
133            }
134
135            $renderer->doc .= $markup;
136            return true;
137        }
138
139        if ($state == DOKU_LEXER_EXIT) {
140            $markup = '</div>';
141
142            if ($icon) {
143                $markup .= '</div></div>';
144            }
145
146            $renderer->doc .= $markup;
147            return true;
148        }
149
150        return true;
151    }
152}
153