1<?php
2
3use dokuwiki\Extension\SyntaxPlugin;
4
5/**
6 * DokuWiki Plugin networkgraph (Syntax Component)
7 *
8 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
9 * @author Feralheart <dokuwiki@feralheart.dev>
10 */
11class syntax_plugin_networkgraph_renderer extends SyntaxPlugin
12{
13    /** @inheritDoc */
14    public function getType()
15    {
16        return 'substition';
17    }
18
19    /** @inheritDoc */
20    public function getPType()
21    {
22        return 'normal';
23    }
24
25    /** @inheritDoc */
26    public function getSort()
27    {
28        return 361;
29    }
30
31    /** @inheritDoc */
32    public function connectTo($mode)
33    {
34        $this->Lexer->addSpecialPattern('~~NETWORKGRAPH~~', $mode, 'plugin_networkgraph_renderer');
35    }
36
37    /** @inheritDoc */
38    public function handle($match, $state, $pos, Doku_Handler $handler)
39    {
40        //
41    }
42
43
44    /** @inheritDoc */
45    public function render($mode, Doku_Renderer $renderer, $data)
46    {
47        if ($mode !== 'xhtml') {
48            return false;
49        }
50
51        $radomNumber    = rand(0, getrandmax());
52        $graphId        = "networkgraph_". $radomNumber;
53
54        $file           = metaFN('networkgraph_data', '.json');
55        $json           = file_exists($file) ? file_get_contents($file) : json_encode(['nodes' => [], 'links' => []]);
56        $jsonData       = json_decode($json);
57        $jsGraph        = json_encode($jsonData);
58
59        $renderer->info['cache'] = FALSE;
60        $renderer->doc .= "<div id='" . $graphId . "' class='networkgraph' style=\"width:100%; height:600px; border:1px solid #ddd;\"></div>";
61        $renderer->doc .= "<script>
62            (function(){
63                const graph = " . $json . ";
64                renderGraph('" . $graphId ."', graph);
65            })();
66        </script>";
67
68        return true;
69    }
70}
71