1<?php
2if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
3if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
4require_once(DOKU_PLUGIN.'syntax.php');
5
6class syntax_plugin_bbcodeextensions_classdiv extends DokuWiki_Syntax_Plugin {
7
8    function getType() { return 'container'; }
9    function getPType() { return 'block'; }
10    function getAllowedTypes() { return array('formatting', 'substition', 'protected', 'container'); }
11    function getSort() { return 165; }
12    function connectTo($mode) {
13        $this->Lexer->addEntryPattern('\[cd\=.*?\](?=.*?\x5B/cd\x5D)',$mode,'plugin_bbcodeextensions_classdiv');
14    }
15    function postConnect() { $this->Lexer->addExitPattern('\[/cd\]','plugin_bbcodeextensions_classdiv'); }
16
17    function handle($match, $state, $pos, Doku_Handler $handler) {
18        switch ($state) {
19          case DOKU_LEXER_ENTER :
20            $omatch= $match;
21            $match = explode('"',substr($match, 3, -1));
22            return array($state, $match[1], $omatch);
23          case DOKU_LEXER_UNMATCHED :
24            return array($state, $match);
25          case DOKU_LEXER_EXIT :
26            return array($state, '');
27        }
28        return array();
29    }
30
31    function render($mode, Doku_Renderer $renderer, $data) {
32		if(in_array($mode, ['xhtml', 's5'], true)) {
33			list($state, $match) = $data;
34			$defcl= $this->getConf('class_preffix') ?? 'bbcodeextensions';
35			switch ($state) {
36				case DOKU_LEXER_ENTER :
37                $renderer->doc .= <<<EOF
38<div class="{$defcl} {$match}" >
39EOF;
40					break;
41
42				case DOKU_LEXER_UNMATCHED :
43					$match = $renderer->_xmlEntities($match);
44					$renderer->doc .= $match;
45				break;
46
47				case DOKU_LEXER_EXIT :
48					$renderer->doc .= '</div><!-- bbcodeextensions_div -->';
49				break;
50			}
51			return true;
52		} // if render
53		if(in_array($mode, ['text'], true)) {
54			list($state, $match) = $data;
55			switch ($state) {
56				case DOKU_LEXER_ENTER :
57				break;
58
59				case DOKU_LEXER_UNMATCHED :
60				$renderer->doc .= $match;
61				break;
62
63				case DOKU_LEXER_EXIT :
64				break;
65			}
66			return true;
67		}
68
69        return false;
70    }
71}
72