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_alink 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 link.+?\[/a\]',$mode,'plugin_bbcodeextensions_alink');
14	}
15
16    function handle($match, $state, $pos, Doku_Handler $handler) {
17		$match = substr($match, 8, -4);
18		if (preg_match('/^".+?"$/',$match)) $match = substr($match, 1, -1);
19		$match = preg_split('/\]/u',$match,2);
20
21		if ( !isset($match[0]) ) {
22			$url   = $match[1];
23			$title = $match[1];
24		} else {
25			$url   = $match[0];
26			$title = $match[1];
27		}
28		$match= [ $url, $url ];
29
30		switch ($state) {
31		  case DOKU_LEXER_ENTER :
32			return array($state, $match);
33
34		  case DOKU_LEXER_UNMATCHED :
35			return array($state, $match);
36
37		  case DOKU_LEXER_EXIT :
38			return array($state, '');
39
40		   case DOKU_LEXER_SPECIAL :
41			return array($state, $match);
42
43		}
44        return array();
45    }
46
47	function render($mode, Doku_Renderer $renderer, $data) {
48		list ($state, $match) = $data;
49		switch ($state) {
50			case DOKU_LEXER_ENTER :
51			break;
52
53			//case DOKU_LEXER_UNMATCHED :
54			case DOKU_LEXER_SPECIAL :
55			if(in_array($mode, ['xhtml', 's5'], true)) {
56				//$match= substr($match, 3, -4);
57				$ref= $match[0];
58				$title= $match[1];
59				// renderer for some weird reason converts anchors to lowercase
60				//$ref= $renderer->_xmlEntities($ref); // htmlspecialchars($match);
61				//$title= htmlspecialchars($title); // $renderer->_xmlEntities($title);
62				$renderer->doc .= <<<EOF
63<a href="#{$ref}" >{$title}</a>
64EOF;
65			} else
66			if ($mode==='text') {
67				$renderer->doc .= $this->getConf('anchor_symbol');
68				$renderer->doc .= '['. $ref. ']';
69			} else {
70				$renderer->doc .= '#['. $ref. '] ';
71			}
72
73			break;
74
75			case DOKU_LEXER_EXIT:
76			break;
77
78		}
79        return false;
80    }
81}
82