xref: /plugin/bpmnio/syntax/bpmnio.php (revision e9e1a493e68dc5cb4119e6c779e79064beae5117)
1<?php
2
3/**
4 * @license    See LICENSE file
5 * @author     Jaap de Haan <jaap.dehaan@color-of-code.de>
6 */
7
8// must be run within DokuWiki
9if (!defined('DOKU_INC')) {
10    die();
11}
12
13// See help: https://www.dokuwiki.org/devel:syntax_plugins
14
15class syntax_plugin_bpmnio_bpmnio extends DokuWiki_Syntax_Plugin
16{
17
18    public function getPType()
19    {
20        return 'block';
21    }
22
23    public function getType()
24    {
25        return 'protected';
26    }
27
28    public function getSort()
29    {
30        return 0;
31    }
32
33    public function connectTo($mode)
34    {
35        $this->Lexer->addEntryPattern('<bpmnio.*?>(?=.*?</bpmnio>)', $mode, 'plugin_bpmnio_bpmnio');
36    }
37
38    public function postConnect()
39    {
40        $this->Lexer->addExitPattern('</bpmnio>', 'plugin_bpmnio_bpmnio');
41    }
42
43    public function handle($match, $state, $pos, Doku_Handler $handler)
44    {
45        if ($state == DOKU_LEXER_UNMATCHED) {
46            $match = base64_encode($match);
47        }
48        return array($match, $state, $pos);
49    }
50
51    public function render($mode, Doku_Renderer $renderer, $data)
52    {
53        // $data is returned by handle()
54        if ($mode == 'xhtml' || $mode == 'odt') {
55            list($match, $state, $pos) = $data;
56            switch ($state) {
57                case DOKU_LEXER_ENTER:
58                    preg_match('/<bpmnio type="(\w+)">/', $match, $type);
59                    $type = $type[1] ?? 'bpmn';
60                    $bpmnid = uniqid('__' . $type . '_js_');
61                    $renderer->doc .= '<div class="plugin-bpmnio" id="' . $bpmnid . '">';
62                    $renderer->doc .= '<textarea class="bpmn_js_data" style="visibility:hidden;">';
63                    break;
64
65                case DOKU_LEXER_UNMATCHED:
66                    $renderer->doc .= trim($match);
67                    break;
68                case DOKU_LEXER_EXIT:
69                    $renderer->doc .= '</textarea>';
70                    $renderer->doc .= '<div class="bpmn_js_container">';
71                    $renderer->doc .= '</div>';
72                    $renderer->doc .= '</div>';
73                    break;
74            }
75            return true;
76        }
77        return false;
78    }
79}
80