xref: /plugin/vegalite/syntax.php (revision e66c86a29c7c323e0ed804145202ccc8d1c27d79)
1<?php
2
3/**
4 * DokuWiki Plugin VegaLite (Syntax Component)
5 *
6 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
7 * @author  Robert Weinmeister <develop@weinmeister.org>
8 */
9
10declare(strict_types=1);
11
12if (!defined('DOKU_INC'))
13{
14    die();
15}
16
17use dokuwiki\Parsing\Parser;
18
19class syntax_plugin_vegalite extends \dokuwiki\Extension\SyntaxPlugin
20{
21    /** @inheritDoc */
22    function getType(): string {
23        return 'container';
24    }
25
26    /** @inheritDoc */
27    function getSort(): int {
28        return 150;
29    }
30
31    /**
32    * Entry pattern for Vega-Lite
33    *
34    * @param string $mode Parser mode
35    * @return void
36    */
37    public function connectTo($mode): void {
38        $this->Lexer->addEntryPattern(
39            '<vegalite.*?>(?=.*?</vegalite>)',
40            $mode,
41            'plugin_vegalite');
42    }
43
44    /**
45     * Exit pattern for Vega-Lite
46     *
47     * @return void
48     */
49    function postConnect(): void {
50        $this->Lexer->addExitPattern(
51            '</vegalite>',
52            'plugin_vegalite'
53        );
54    }
55
56    /**
57     * Handles the matched text based on the lexer state
58     *
59     * @param string $match Matched text from the lexer
60     * @param int $state Current lexer state (DOKU_LEXER_ENTER, DOKU_LEXER_UNMATCHED, DOKU_LEXER_EXIT)
61     * @param Doku_Handler $handler DokuWiki handler instance
62     * @return array Array with the state and processed match
63     */
64    function handle($match, $state, $pos, Doku_Handler $handler): array {
65        return [$state, $match];
66    }
67
68    /**
69    * Render xhtml output or metadata
70    */
71    function render($mode, Doku_Renderer $renderer, $indata)
72    {
73        if($mode == 'xhtml'){
74            list($state, $match) = $indata;
75            switch ($state) {
76                case DOKU_LEXER_ENTER:
77                    $values = explode(" ", $match);
78                    $divwidth = count($values) < 2 ? 'auto' : $values[1];
79                    $divheight = count($values) < 3 ? 'auto' : substr($values[2], 0, -1);
80
81                    $this->vegaliteContent .= '<div id="vegalite" class="vegalite" style="position: relative; width:'.$divwidth.'; height:'.$divheight.'"></div>' . "\r\n";
82                break;
83                case DOKU_LEXER_UNMATCHED:
84                    $this->vegaliteContent .=
85'<script type="text/javascript">
86    var yourVlSpec = ' . $match . ";
87    vegaEmbed('#vegalite', yourVlSpec);
88</script>";
89                break;
90                case DOKU_LEXER_EXIT:
91                    $this->vegaliteContent .= "";
92
93                    $renderer->doc .= $this->vegaliteContent;
94                    $this->vegaliteContent = '';
95                break;
96            }
97            return true;
98        }
99        return false;
100    }
101}