xref: /plugin/localopen/syntax.php (revision 44be7d0e031b0b44685f0acf9e02d51f1885d42d)
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        $renderer->doc .= '<a class="localopen-link" title="' . $title_attr . '" href="' . $href . '" onclick="fetch(this.href,{mode:\'no-cors\'}); return false;">' . $title . '</a>';
68
69        return true;
70    }
71}
72