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 BBCodeExt_simpletag_plugin_base extends DokuWiki_Syntax_Plugin {
7
8	public $T= ''; // tag name, used for the wrapper in render stage
9	public $textwrap= array('', ''); // symbols used to wrap in text mode
10
11    function getType() { return 'formatting'; }
12    function getPType() { return 'normal'; }
13    function getAllowedTypes() { return array('formatting', 'substition', 'disabled'); }
14    function getSort() { return 140; }
15    function makepatterns($bbtag) {
16		return [ <<<EOT
17\[${bbtag}\](?=.*?\x5B/${bbtag}\x5D)
18EOT
19, "\[/${bbtag}\]" ] ;
20	}
21
22	function setHTMLtag ($t= null) {
23		$this->T= $t;
24	}
25
26	function settextwraps ($open, $close) {
27		$this->textwrap= [ $open, $close ];
28	}
29
30    function handle($match, $state, $pos, Doku_Handler $handler) {
31        switch ($state) {
32          case DOKU_LEXER_ENTER :
33            return array($state, [$this->T, $match]);
34
35          case DOKU_LEXER_UNMATCHED :
36            return array($state, [$this->T, $match]);
37
38          case DOKU_LEXER_EXIT :
39            return array($state, [$this->T, $match]);
40
41           case DOKU_LEXER_SPECIAL :
42			return array($state, [$this->T, $match]);
43
44        }
45        return array();
46    }
47
48	function render($mode, Doku_Renderer $renderer, $data) {
49		list ($state, $match) = $data;
50		list ($tag, $text) = $match;
51		if (in_array($mode, ['xhtml', 's5'], true) ) {
52			switch ($state) {
53				case DOKU_LEXER_ENTER :  {
54					$renderer->doc.= "<$tag>";
55					break;
56				}
57				case DOKU_LEXER_UNMATCHED : {
58					$text= $renderer->_xmlEntities($text); // htmlspecialchars($match);
59					$renderer->doc .= $text;
60					break;
61				}
62				case DOKU_LEXER_EXIT: {
63					$renderer->doc.= "</${tag}>";
64					break;
65				}
66			} // end switch
67			return true;
68		} else if (in_array($mode, ['text'], true) ) {
69			switch ($state) {
70				case DOKU_LEXER_ENTER: {
71					$renderer->doc.= $this->textwrap[0];
72					break;
73				}
74				case DOKU_LEXER_UNMATCHED: {
75					$text= $renderer->_xmlEntities($text);
76					$renderer->doc .= $text;
77					break;
78				}
79				case DOKU_LEXER_EXIT : {
80					$renderer->doc.= $this->textwrap[1];
81					break;
82				}
83			}
84			return true;
85		}
86
87        return false;
88    }
89}
90
91?>
92