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_anchor extends DokuWiki_Syntax_Plugin {
7
8    function getType() { return 'disabled'; }
9    function getPType() { return 'normal'; }
10    function getAllowedTypes() { return array('disabled'); }
11    function getSort() { return 110; }
12    function connectTo($mode){
13        $this->Lexer->addSpecialPattern('\[a\][0-9a-zA-Z_\-:]+?\[/a\]', $mode, 'plugin_bbcodeextensions_anchor');
14    }
15
16    function handle($match, $state, $pos, Doku_Handler $handler) {
17        switch ($state) {
18          case DOKU_LEXER_ENTER :
19            return array($state, $match);
20
21          case DOKU_LEXER_UNMATCHED :
22            return array($state, $match);
23
24          case DOKU_LEXER_EXIT :
25            return array($state, '');
26
27           case DOKU_LEXER_SPECIAL :
28			return array($state, $match);
29
30        }
31        return array();
32    }
33
34	function render($mode, Doku_Renderer $renderer, $data) {
35		list ($state, $match) = $data;
36		switch ($state) {
37			case DOKU_LEXER_ENTER :
38			break;
39
40			//case DOKU_LEXER_UNMATCHED :
41			case DOKU_LEXER_SPECIAL :
42			if(in_array($mode, ['xhtml', 's5'], true)) {
43				$match= substr($match, 3, -4);
44				$match= $renderer->_xmlEntities($match); // htmlspecialchars($match);
45				$renderer->doc .= '<a id="'. $match. '" name="'. $match. '"></a>';
46			} else
47			if ($mode==='text') {
48				$renderer->doc .= $this->getConf('anchor_symbol');
49				$renderer->doc .= '['. $match. ']';
50			} else {
51				$renderer->doc .= '#['. $match. '] ';
52			}
53
54			break;
55
56			case DOKU_LEXER_EXIT:
57			break;
58
59		}
60        return false;
61    }
62}
63