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