xref: /plugin/bpmnio/syntax/bpmnio.php (revision 8428abe89edc955ce9c825bc3a1c9fd774300019)
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);
49    }
50
51    public function render($mode, Doku_Renderer $renderer, $data)
52    {
53        if ($mode == 'xhtml' || $mode == 'odt') {
54            list($match, $state) = $data;
55            switch ($state) {
56                case DOKU_LEXER_ENTER:
57                    preg_match('/<bpmnio type="(\w+)">/', $match, $type);
58                    $type = $type[1] ?? 'bpmn';
59                    $bpmnid = uniqid('__' . $type . '_js_');
60                    $renderer->doc .= <<<HTML
61                        <div class="plugin-bpmnio" id="{$bpmnid}">
62                            <textarea class="bpmn_js_data">
63                        HTML;
64                    break;
65
66                case DOKU_LEXER_UNMATCHED:
67                    $renderer->doc .= trim($match);
68                    break;
69                case DOKU_LEXER_EXIT:
70                    $renderer->doc .= <<<HTML
71                            </textarea>
72                            <div class="bpmn_js_container"></div>
73                        </div>
74                        HTML;
75                    break;
76            }
77            return true;
78        }
79        return false;
80    }
81}
82