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