1<?php
2/**
3 * Plugin Alertbox: Use bootstrap-style alerts.
4 *
5 * @author     Steve Levine (sjlevine29@gmail.com)
6 */
7
8// must be run within DokuWiki
9if(!defined('DOKU_INC')) die();
10if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
11require_once(DOKU_PLUGIN.'syntax.php');
12
13class syntax_plugin_alertbox extends DokuWiki_Syntax_Plugin {
14
15    function getType(){ return 'container'; }
16    function getAllowedTypes() { return array('formatting', 'substition', 'disabled'); }
17    function getSort(){ return 200; }
18    function connectTo($mode) { $this->Lexer->addEntryPattern('<alert.*?>(?=.*?</alert>)',$mode,'plugin_alertbox'); }
19    function postConnect() { $this->Lexer->addExitPattern('</alert>','plugin_alertbox'); }
20
21
22    function handle($match, $state, $pos, &$handler){
23        switch ($state) {
24          case DOKU_LEXER_ENTER :
25                // Default
26                $alerttype = "alert-info";
27                if (strpos($match, 'warning') != false) {
28                    $alerttype = "alert-warning";
29                } elseif (strpos($match, 'info') != false) {
30                    $alerttype = "alert-info";
31                } elseif (strpos($match, 'danger') != false) {
32                    $alerttype = "alert-danger";
33                } elseif (strpos($match, 'success') != false) {
34                    $alerttype = "alert-success";
35                }
36                return array($state, $alerttype);
37
38          case DOKU_LEXER_UNMATCHED :  return array($state, $match);
39          case DOKU_LEXER_EXIT :       return array($state, '');
40        }
41        return array();
42    }
43
44
45    function render($mode, &$renderer, $data) {
46        if($mode == 'xhtml'){
47            list($state, $match) = $data;
48            switch ($state) {
49              case DOKU_LEXER_ENTER :
50                $renderer->doc .= "<div class='alert $match'>";
51                break;
52
53              case DOKU_LEXER_UNMATCHED :  $renderer->doc .= $renderer->_xmlEntities($match); break;
54              case DOKU_LEXER_EXIT :       $renderer->doc .= "</div>"; break;
55            }
56            return true;
57        }
58        return false;
59    }
60
61}
62
63?>
64