Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion plugin.info.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
base bpmnio
author Jaap de Haan
email ColorOfCode@googlemail.com
date 2025-03-21
date 2025-03-28
name bpmnio
desc Renders BPMN or DMN xml using the bpmn.io js library
url http://www.dokuwiki.org/plugin:bpmnio
6 changes: 6 additions & 0 deletions script/bpmnio_render.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,12 @@ function safeRender(tag, type, fn) {
const data = root.find(dataId)[0];
const xml = extractXml(data.textContent);

if (xml.startsWith("Error:")) {
container.textContent = xml;
container.style.color = 'red';
return;
}

fn(xml, container);
} catch (err) {
console.warn(err.message);
Expand Down
75 changes: 61 additions & 14 deletions syntax/bpmnio.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use dokuwiki\Extension\SyntaxPlugin;
use dokuwiki\File\MediaResolver;

/**
* @license See LICENSE file
Expand All @@ -18,7 +19,8 @@
// </div>
class syntax_plugin_bpmnio_bpmnio extends SyntaxPlugin
{
public string $type = ''; // 'bpmn' or 'dmn'
protected string $type = ''; // 'bpmn' or 'dmn'
protected string $src = ''; // media file

public function getPType()
{
Expand Down Expand Up @@ -49,27 +51,67 @@ public function handle($match, $state, $pos, Doku_Handler $handler)
{
switch ($state) {
case DOKU_LEXER_ENTER:
$matched = '';
preg_match('/<bpmnio type="(\w+)">/', $match, $matched);
$this->type = $matched[1] ?? 'bpmn';
return [$state, $this->type, '', $pos, ''];
$matched = [];
preg_match('/<bpmnio\s+([^>]+)>/', $match, $matched);

$attrs = [];
if (!empty($matched[1])) {
$attrs = $this->buildAttributes($matched[1]);
}

$this->type = $attrs['type'] ?? 'bpmn';
$this->src = $attrs['src'] ?? '';

return [$state, $this->type, '', $pos, '', false];

case DOKU_LEXER_UNMATCHED:
$posStart = $pos;
$posEnd = $pos + strlen($match);
$match = base64_encode($match);
return [$state, $this->type, $match, $posStart, $posEnd];

$inline = empty($this->src);
if (!$inline) {
$match = $this->getMedia($this->src);
}
return [$state, $this->type, base64_encode($match), $posStart, $posEnd, $inline];

case DOKU_LEXER_EXIT:
$this->type = '';
return [$state, '', '', '', ''];
$this->src = '';
return [$state, '', '', '', '', '', false];
}
return [];
}

private function buildAttributes($string)
{
$attrs = [];
preg_match_all('/(\w+)=["\'](.*?)["\']/', $string, $matches, PREG_SET_ORDER);
foreach ($matches as $match) {
$attrs[$match[1]] = $match[2];
}
return $attrs;
}

private function getMedia($src)
{
global $ID;

$id = (new MediaResolver($ID))->resolveId($src);
if (auth_quickaclcheck($id) < AUTH_READ) {
return "Error: Access denied for file $src";
}

$file = mediaFN($id);
if (!file_exists($file) || !is_readable($file)) {
return "Error: Cannot load file $src";
}

return file_get_contents($file);
}

public function render($mode, Doku_Renderer $renderer, $data)
{
[$state, $type, $match, $posStart, $posEnd] = $data;
[$state, $type, $match, $posStart, $posEnd, $inline] = $data;

if (is_a($renderer, 'renderer_plugin_dw2pdf')) {
if ($state == DOKU_LEXER_EXIT) {
Expand Down Expand Up @@ -99,16 +141,21 @@ public function render($mode, Doku_Renderer $renderer, $data)
{$match}
</div>
HTML;

$target = "plugin_bpmnio_{$type}";
$sectionEditData = ['target' => $target];
$class = $renderer->startSectionEdit($posStart, $sectionEditData);
if ($inline) {
$target = "plugin_bpmnio_{$type}";
$sectionEditData = ['target' => $target];
$class = $renderer->startSectionEdit($posStart, $sectionEditData);
} else {
$class = '';
}
$renderer->doc .= <<<HTML
<div class="{$type}_js_canvas {$class}">
<div class="{$type}_js_container"></div>
</div>
HTML;
$renderer->finishSectionEdit($posEnd);
if ($inline) {
$renderer->finishSectionEdit($posEnd);
}
break;

case DOKU_LEXER_EXIT:
Expand Down