xref: /plugin/bpmnio/syntax/bpmnio.php (revision a494ddcc62e5b9ad640d30acec0360d3b24cbd8a)
1<?php
2
3use dokuwiki\Extension\SyntaxPlugin;
4
5/**
6 * @license    See LICENSE file
7 */
8// See help: https://www.dokuwiki.org/devel:syntax_plugins
9// The HTML structure generated by this syntax plugin is:
10//
11// <div class="plugin-bpmnio" id="__(bpmn|dmn)_js_<hash>">
12//   <div class="bpmn_js_data">
13//      ... base64 encoded xml
14//   </div>
15//   <div class="bpmn_js_canvas {$class}">
16//     <div class="bpmn_js_container">... rendered herein</div>
17//   </div>
18// </div>
19class syntax_plugin_bpmnio_bpmnio extends SyntaxPlugin
20{
21    public string $type = ''; // 'bpmn' or 'dmn'
22
23    public function getPType()
24    {
25        return 'block';
26    }
27
28    public function getType()
29    {
30        return 'protected';
31    }
32
33    public function getSort()
34    {
35        return 0;
36    }
37
38    public function connectTo($mode)
39    {
40        $this->Lexer->addEntryPattern('<bpmnio.*?>(?=.*?</bpmnio>)', $mode, 'plugin_bpmnio_bpmnio');
41    }
42
43    public function postConnect()
44    {
45        $this->Lexer->addExitPattern('</bpmnio>', 'plugin_bpmnio_bpmnio');
46    }
47
48    public function handle($match, $state, $pos, Doku_Handler $handler)
49    {
50        switch ($state) {
51            case DOKU_LEXER_ENTER:
52                $matched = '';
53                preg_match('/<bpmnio type="(\w+)">/', $match, $matched);
54                $this->type = $matched[1] ?? 'bpmn';
55                return [$state, $this->type, '', $pos, ''];
56
57            case DOKU_LEXER_UNMATCHED:
58                $posStart = $pos;
59                $posEnd = $pos + strlen($match);
60                $match = base64_encode($match);
61                return [$state, $this->type, $match, $posStart, $posEnd];
62
63            case DOKU_LEXER_EXIT:
64                $this->type = '';
65                return [$state, '', '', '', ''];
66        }
67        return [];
68    }
69
70    public function render($mode, Doku_Renderer $renderer, $data)
71    {
72        [$state, $type, $match, $posStart, $posEnd] = $data;
73
74        if (is_a($renderer, 'renderer_plugin_dw2pdf')) {
75            if ($state == DOKU_LEXER_EXIT) {
76                $renderer->doc .= <<<HTML
77                    <div class="plugin-bpmnio">
78                        <a href="https://github.com/Color-Of-Code/dokuwiki-plugin-bpmnio/issues/4">
79                            DW2PDF support missing: Help wanted
80                        </a>
81                    </div>
82                    HTML;
83            }
84            return true;
85        }
86
87        if ($mode == 'xhtml' || $mode == 'odt') {
88            switch ($state) {
89                case DOKU_LEXER_ENTER:
90                    $bpmnid = "__{$type}_js_{$posStart}";
91                    $renderer->doc .= <<<HTML
92                        <div class="plugin-bpmnio" id="{$bpmnid}">
93                        HTML;
94                    break;
95
96                case DOKU_LEXER_UNMATCHED:
97                    $renderer->doc .= <<<HTML
98                        <div class="{$type}_js_data">
99                            {$match}
100                        </div>
101                        HTML;
102
103                    $target = "plugin_bpmnio_{$type}";
104                    $sectionEditData = ['target' => $target];
105                    $class = $renderer->startSectionEdit($posStart, $sectionEditData);
106                    $renderer->doc .= <<<HTML
107                        <div class="{$type}_js_canvas {$class}">
108                            <div class="{$type}_js_container"></div>
109                        </div>
110                        HTML;
111                    $renderer->finishSectionEdit($posEnd);
112                    break;
113
114                case DOKU_LEXER_EXIT:
115                    $renderer->doc .= <<<HTML
116                        </div>
117                        HTML;
118                    break;
119            }
120            return true;
121        }
122        return false;
123    }
124}
125