1<?php
2/*
3 * Reference links, i.e.
4 *  ... [name][id] ...
5 *  ... [id][] ...
6 *  ...
7 *  [id]: http://example.com (handled by markdowku_references)
8 */
9
10if(!defined('DOKU_INC')) die();
11if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
12require_once(DOKU_PLUGIN.'syntax.php');
13
14class syntax_plugin_markdowku_anchorsreference extends DokuWiki_Syntax_Plugin {
15
16    function getType()  { return 'substition'; }
17    function getPType() { return 'normal'; }
18    function getSort()  { return 102; }
19
20    function connectTo($mode) {
21        $this->nested_brackets_re =
22            str_repeat('(?>[^\[\]]+|\[', 3).
23            str_repeat('\])*', 3);
24        $this->Lexer->addSpecialPattern(
25            '\['.$this->nested_brackets_re.'\][ ]?(?:\n[ ]*)?\[[^\[\]\n]*?\]',
26            $mode,
27            'plugin_markdowku_anchorsreference');
28    }
29
30    function handle($match, $state, $pos, Doku_Handler $handler) {
31        return array($state, $match);
32    }
33
34    function render($mode, Doku_Renderer$renderer, $data) {
35        global $ID;
36        preg_match(
37            '/^\[('.$this->nested_brackets_re.')\][ ]?(?:\n[ ]*)?\[(.*?)\]$/',
38            $data[1],
39            $matches);
40
41        $title = $matches[1];
42
43        if ($matches[2] == '')
44            $rid = $matches[1];
45        else
46            $rid = $matches[2];
47
48        $rid = preg_replace("/ /", ".", $rid);
49        $target = p_get_metadata($ID, 'markdowku_references_'.$rid, METADATA_RENDER_USING_CACHE);
50        if ($target == '') {
51            $renderer->cdata($data[1]);
52        } else if (preg_match('/^mailto:/', $target) or
53            preg_match('<'.PREG_PATTERN_VALID_EMAIL.'>', $target)) {
54                $target = preg_replace('/^mailto:/', '', $target);
55                $renderer->emaillink($target, $title);
56        } else {
57                $renderer->externallink($target, $title);
58        }
59        return true;
60    }
61}
62//Setup VIM: ex: et ts=4 enc=utf-8 :
63