xref: /plugin/bpmnio/syntax/bpmnio.php (revision 696812a400f0348b761c78697df1fa24da37e35b)
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">DW2PDF support missing: Help wanted</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                    $class = $this->_startSectionEdit($renderer, $posStart, $type);
104                    $renderer->doc .= <<<HTML
105                        <div class="{$type}_js_canvas {$class}">
106                            <div class="{$type}_js_container"></div>
107                        </div>
108                        HTML;
109                    $this->_finishSectionEdit($renderer, $posEnd);
110                    break;
111
112                case DOKU_LEXER_EXIT:
113                    $renderer->doc .= <<<HTML
114                        </div>
115                        HTML;
116                    break;
117            }
118            return true;
119        }
120        return false;
121    }
122
123    private function _startSectionEdit(Doku_Renderer $renderer, $pos, $type)
124    {
125        $target = "plugin_bpmnio_{$type}";
126        $sectionEditData = ['target' => $target];
127        if (!defined('SEC_EDIT_PATTERN')) {
128            // backwards-compatibility for Frusterick Manners (2017-02-19)
129            $sectionEditData = $target;
130        }
131        return $renderer->startSectionEdit($pos, $sectionEditData);
132    }
133
134    private function _finishSectionEdit(Doku_Renderer $renderer, $pos)
135    {
136        $renderer->finishSectionEdit($pos);
137    }
138}
139