1<?php
2/**
3 * Bootstrap Wrapper Plugin: Alert
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_alert extends syntax_plugin_bootswrapper_bootstrap
11{
12
13    public $p_type         = 'block';
14    public $pattern_start  = '<(?:ALERT|alert).*?>(?=.*?</(?:ALERT|alert)>)';
15    public $pattern_end    = '</(?:ALERT|alert)>';
16    public $tag_name       = 'alert';
17    public $tag_attributes = array(
18
19        'type'    => array(
20            'type'     => 'string',
21            'values'   => array('success', 'info', 'warning', 'danger'),
22            'required' => true,
23            'default'  => 'info'),
24
25        'dismiss' => array(
26            'type'     => 'boolean',
27            'values'   => array(0, 1),
28            'required' => false,
29            'default'  => false),
30
31        'icon'    => array(
32            'type'     => 'string',
33            'values'   => null,
34            'required' => false,
35            'default'  => null),
36    );
37
38    public function render($mode, Doku_Renderer $renderer, $data)
39    {
40
41        if (empty($data)) {
42            return false;
43        }
44
45        if ($mode !== 'xhtml') {
46            return false;
47        }
48
49        /** @var Doku_Renderer_xhtml $renderer */
50        list($state, $match, $pos, $attributes) = $data;
51
52        if ($state == DOKU_LEXER_ENTER) {
53            extract($attributes);
54
55            $html_attributes            = $this->mergeCoreAttributes($attributes);
56            $html_attributes['class'][] = 'bs-wrap alert';
57            $html_attributes['class'][] = "alert-$type";
58            $html_attributes['role']    = 'alert';
59
60            if ($dismiss) {
61                $html_attributes['class'][] = 'alert-dismissible';
62            }
63
64            $markup = '<div ' . $this->buildAttributes($html_attributes) . '>';
65
66            if ($dismiss) {
67                $markup .= '<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>';
68            }
69
70            if ($icon) {
71                $markup .= '<i class="' . $icon . '"></i> ';
72            }
73
74            $renderer->doc .= $markup;
75            return true;
76        }
77
78        if ($state == DOKU_LEXER_EXIT) {
79            $renderer->doc .= '</div>';
80            return true;
81        }
82
83        return true;
84    }
85}
86