xref: /plugin/localopen/syntax.php (revision 08265a4674d5d9c75d62ecc3574e7c9fcdedb378)
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 = trim($this->getConf('tag') ?: 'lopen');
29		$tag = preg_quote($tag, '/');
30
31		$this->Lexer->addSpecialPattern(
32			'\[\[' . $tag . '>[^\|\]]+(?:\|[^\]]+)?\]\]',
33			$mode,
34			'plugin_localopen'
35		);
36	}
37
38	public function handle($match, $state, $pos, Doku_Handler $handler)
39	{
40		$tag = trim($this->getConf('tag') ?: 'lopen');
41		$tag = preg_quote($tag, '/');
42
43		preg_match('/\[\[' . $tag . '>([^\|\]]+)(?:\|([^\]]+))?\]\]/i', $match, $matches);
44
45		$path = str_replace('"', '', $matches[1]);
46		$title = isset($matches[2]) && $matches[2] !== '' ? $matches[2] : $path;
47
48		return [
49			'path'  => $path,
50			'title' => $title,
51		];
52	}
53
54    public function render($mode, Doku_Renderer $renderer, $data)
55    {
56        if ($mode !== 'xhtml') return false;
57
58        $path  = $data['path'];
59        $title = hsc($data['title']);
60
61        $token = $this->getConf('token');
62        $port  = $this->getConf('port');
63
64        $url = 'http://127.0.0.1:' . $port . '/open?path=' . rawurlencode($path) . '&token=' . rawurlencode($token);
65
66        $href = hsc($url);
67        $title_attr = hsc($path);
68
69        $icon = DOKU_BASE . 'lib/plugins/localopen/images/lopen.svg';
70
71        $renderer->doc .=
72            '<a class="localopen-link" title="' . $title_attr . '" href="' . $href . '" onclick="fetch(this.href,{mode:\'no-cors\'}); return false;">' .
73            '<img src="' . hsc($icon) . '" alt="" class="localopen-icon" /> ' .
74            $title .
75            '</a>';
76
77        return true;
78    }
79}
80