1<?php
2/**
3 * Bootnote Syntax Plugin: display note with different level of importance.
4 *
5 * @author Algorys
6 */
7
8if (!defined('DOKU_INC')) die();
9
10class syntax_plugin_bootnote extends DokuWiki_Syntax_Plugin {
11
12    public function getType() {
13        return 'container';
14    }
15
16    /**
17    * @return string Paragraph type
18    **/
19
20    public function getPType() {
21        return 'normal';
22    }
23
24    // Keep syntax inside plugin
25    function getAllowedTypes() {
26	return array('container', 'baseonly', 'substition','protected','disabled','formatting','paragraphs');
27    }
28
29    public function getSort() {
30        return 192;
31    }
32
33    function connectTo($mode) {
34        if($this->getConf('bootnote.note') == 'note') {
35            $this->Lexer->addEntryPattern('<note[^>]*>(?=.*</note>)', $mode,'plugin_bootnote');
36        } else {
37            $this->Lexer->addEntryPattern('<bootnote[^>]*>(?=.*</bootnote>)', $mode,'plugin_bootnote');
38        }
39    }
40
41    function postConnect() {
42        if($this->getConf('bootnote.note') == 'note') {
43            $this->Lexer->addExitPattern("</note>", 'plugin_bootnote');
44        } else {
45            $this->Lexer->addExitPattern("</bootnote>", 'plugin_bootnote');
46        }
47    }
48
49    /**
50    * Do the regexp
51    **/
52    function handle($match, $state, $pos, Doku_Handler $handler) {
53        switch($state){
54            case DOKU_LEXER_ENTER :
55                $data = array(
56                        'state'=>$state,
57                        'lvl'=> "",
58                    );
59                // Looking for id
60                if($this->getConf('bootnote.note') == 'note') {
61                    $note = 'note';
62                } else {
63                    $note = 'bootnote';
64                }
65                preg_match("/$note (\\w*)/", $match, $lvl);
66                if( count($lvl) != 0 ) {
67                    $data['lvl'] = $lvl[1];
68                }
69                return $data;
70            case DOKU_LEXER_UNMATCHED :
71                return array('state'=>$state, 'text'=>$match);
72            default:
73                return array('state'=>$state, 'bytepos_end' => $pos + strlen($match));
74         }
75    }
76
77    // Dokuwiki Renderer
78    function render($mode, Doku_Renderer $renderer, $data) {
79        if($mode != 'xhtml') return false;
80
81        if($data['error']) {
82            $renderer->doc .= $data['text'];
83            return true;
84        }
85        $renderer->info['cache'] = false;
86        switch($data['state']) {
87            case DOKU_LEXER_ENTER :
88                $this->_define_note($renderer, $data);
89                break;
90            case DOKU_LEXER_EXIT:
91                if ($this->getConf('bootnote.theme') == 'oldtheme') {
92                    $renderer->doc .= '</div>';// /.note
93                    $renderer->doc .= '<div class="triangle"></div>';
94                    $renderer->doc .= '</div>';// /.note-container
95                    $renderer->doc .= '</div>';// /Global
96                } else {
97                    $renderer->doc .= '</p></div>';
98                }
99
100            case DOKU_LEXER_UNMATCHED :
101                $renderer->doc .= $renderer->_xmlEntities($data['text']);
102                break;
103        }
104        return true;
105    }
106
107    // Define note before render
108    function _define_note($renderer, $data) {
109        $glyphs = Array(
110            'web' => 'globe',
111            'question' => 'question-sign',
112            'learn' => 'education',
113            'tip' => 'education',
114            'warning' => 'alert',
115            'critical' => 'fire',
116            'important' => 'fire',
117            '' => 'info-sign'
118        );
119
120	    $this->_render_note($renderer, $glyphs[$data['lvl']]);
121    }
122
123    // Render Note
124    function _render_note($renderer, $glyph) {
125        if ($this->getConf('bootnote.theme') == 'oldtheme') {
126            $renderer->doc .= '<div style="clear: both;">'; // Global
127            $renderer->doc .= '<div class="sign-container">';
128            $renderer->doc .= '  <span class="sign">';
129            $renderer->doc .= '    <span class="glyphicon glyphicon-'.$glyph.' s-'.$glyph.' glyph" aria-hidden="true"></span>';
130            $renderer->doc .= '  </span>'; // /.sign
131            $renderer->doc .= '</div>'; // /.sign-container
132            $renderer->doc .= '<div class="note-container s-'.$glyph.'">';
133            $renderer->doc .= '<div class="note">';
134        } else {
135            $renderer->doc .= '<div class="w3-panel n-'.$glyph.'"><p>';
136            $renderer->doc .= '<span class="glyphicon glyphicon-'.$glyph.' blackglyph" aria-hidden="true"></span>';
137            $renderer->doc .= '<span class="n-title">'.$this->getTitle($glyph).':</span>';
138            if ($this->getConf('bootnote.position') == 'below') {
139                $renderer->doc .= '</p>';
140                $renderer->doc .= '<p>';
141            } else {
142                $renderer->doc .= ' '; // Need a space to separate title from text
143            }
144        }
145    }
146
147    function getTitle($glyph) {
148        $titles = Array(
149            'info-sign'     => $this->getLang('bootnote.info'),
150            'education'     => $this->getLang('bootnote.learn'),
151            'question-sign' => $this->getLang('bootnote.question'),
152            'alert'         => $this->getLang('bootnote.alert'),
153            'fire'          => $this->getLang('bootnote.fire'),
154            'globe'         => $this->getLang('bootnote.link')
155        );
156
157        return $titles[$glyph];
158    }
159}
160
161
162
163