xref: /plugin/localopen/syntax.php (revision 39a9131bf0b2ea0cb8ee8c6e93be37441eba14b1)
1<?php
2/**
3 * DokuWiki Plugin localopen (Syntax Component)
4 *
5 * @license MIT
6 * @author  Leonard Heyman
7 */
8
9class syntax_plugin_localopen extends \dokuwiki\Extension\SyntaxPlugin
10{
11    public function getType()
12    {
13        return 'substition';
14    }
15
16    public function getPType()
17    {
18        return 'normal';
19    }
20
21    public function getSort()
22    {
23        return 299;
24    }
25
26    public function connectTo($mode)
27    {
28        $tag = preg_quote($this->getConf('tag'), '/');
29
30        $this->Lexer->addSpecialPattern(
31            '\[\[' . $tag . '>[^|\]]+\|[^]]+\]\]',
32            $mode,
33            'plugin_localopen'
34        );
35    }
36
37    public function handle($match, $state, $pos, Doku_Handler $handler)
38    {
39        $tag = preg_quote($this->getConf('tag'), '/');
40
41        preg_match('/\[\[' . $tag . '>([^|]+)\|([^]]+)\]\]/i', $match, $matches);
42
43        $path = str_replace('"', '', $matches[1]);
44        $title = $matches[2];
45
46        return [
47            'path'  => $path,
48            'title' => $title,
49        ];
50    }
51
52    public function render($mode, Doku_Renderer $renderer, $data)
53    {
54        if ($mode !== 'xhtml') return false;
55
56        $path  = $data['path'];
57        $title = hsc($data['title']);
58
59        $token = $this->getConf('token');
60        $port  = $this->getConf('port');
61
62        $url = 'http://127.0.0.1:' . $port . '/open?path=' . rawurlencode($path) . '&token=' . rawurlencode($token);
63
64        $href = hsc($url);
65        $title_attr = hsc($path);
66
67        $icon = DOKU_BASE . 'lib/plugins/localopen/images/lopen.svg';
68
69        $renderer->doc .=
70            '<a class="localopen-link" title="' . $title_attr . '" href="' . $href . '" onclick="fetch(this.href,{mode:\'no-cors\'}); return false;">' .
71            '<img src="' . hsc($icon) . '" alt="" class="localopen-icon" /> ' .
72            $title .
73            '</a>';
74
75        return true;
76    }
77}
78