forked from Andreone/dokuwiki_plantuml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
syntax.php
281 lines (242 loc) · 8.9 KB
/
syntax.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
<?php
/**
* PlantUML-Plugin: Parses plantuml blocks to render images and html
*
* @license GPL v2 (http://www.gnu.org/licenses/gpl.html)
* @author Andreone
* @author Willi Schönborn <[email protected]>
*/
if (!defined('DOKU_INC')) define('DOKU_INC', realpath(dirname(__FILE__) . '/../../') . '/');
require_once(DOKU_INC . 'inc/init.php');
if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC . 'lib/plugins/');
require_once(DOKU_PLUGIN . 'syntax.php');
class syntax_plugin_plantuml extends DokuWiki_Syntax_Plugin {
/**
* What kind of syntax are we?
*/
function getType() {
return 'substition';
}
/**
* Where to sort in?
*/
function getSort() {
return 200;
}
/**
* Connect pattern to lexer
*/
function connectTo($mode) {
$this->Lexer->addSpecialPattern('<uml.*?>\n.*?\n</uml>', $mode, 'plugin_plantuml');
}
/**
* Handle the match
*/
function handle($match, $state, $pos, &$handler) {
// echo "handle: state=$state<br>";
// echo "handle: match=$match<br>";
// echo "handle: pos=$pos<br>";
$info = $this->getInfo();
// prepare default data
$return = array(
'width' => 0,
'height' => 0,
'title' => 'PlantUML Graph',
'align' => '',
'version' => $info['date'],
);
// prepare input
$lines = explode("\n", $match);
$conf = array_shift($lines);
array_pop($lines);
// alignment
if (preg_match('/\b(left|center|right)\b/i', $conf, $matches)) {
$return['align'] = $matches[1];
}
// size
if (preg_match('/\b(\d+)x(\d+)\b/', $conf, $matches)) {
$return['width'] = $matches[1];
$return['height'] = $matches[2];
} else {
if (preg_match('/\b(?:width|w)=([0-9]+)(%?)/i', $conf, $matches)) {
$return['width'] = $matches[1];
$return['percent'] = $matches[2];
}
if (preg_match('/\b(?:height|h)=([0-9]+)\b/i', $conf, $matches)) {
$return['height'] = $matches[1];
}
}
// title
if (preg_match('/\b(?:title|t)=(\w+)\b/i', $conf, $matches)) {
// single word titles
$return['title'] = $matches[1];
} else if (preg_match('/(?:title|t)="([\w+\s+]+)"/i', $conf, $matches)) {
// multi word titles
$return['title'] = $matches[1];
}
$input = join("\n", $lines);
$return['md5'] = md5($input);
io_saveFile($this->_cachename($return, 'txt'), "@startuml\n$input\n@enduml");
return $return;
}
/**
* Cache file is based on parameters that influence the result image
*/
function _cachename($data, $ext){
unset($data['width']);
unset($data['height']);
unset($data['align']);
unset($data['title']);
return getcachename(join('x', array_values($data)), ".plantuml.$ext");
}
/**
* Create output
*/
function render($mode, &$renderer, $data) {
if ($mode == 'xhtml') {
$img = DOKU_BASE . 'lib/plugins/plantuml/img.php?' . buildURLParams($data);
if($data['width']) {
$temp = $data['width'];
$data['width'] = 0;
$img_unresized = DOKU_BASE . 'lib/plugins/plantuml/img.php?' . buildURLParams($data);
$data['width'] = $temp;
} else {
$img_unresized = $img;
}
$renderer->doc .= '<a title="' . $data['title'] . '" class="media" href="' . $img_unresized . '">';
$renderer->doc .= '<img src="' . $img . '" class="media' . $data['align'] . '" title="' . $data['title'] . '" alt="' . $data['title'] . '"';
if ($data['width']) {
$renderer->doc .= ' width="' . $data['width'] . $data['percent'] . '"';
}
if ($data['height']) {
$renderer->doc .= ' height="' . $data['height'] . '"';
}
if ($data['align'] == 'left') {
$renderer-> doc .= ' align="left"';
}
if ($data['align'] == 'right') {
$renderer->doc .= ' align="right"';
}
$renderer->doc .= '/></a>';
return true;
} else if ($mode == 'odt') {
$src = $this->_imgfile($data);
$renderer->_odtAddImage($src, $data['width'], $data['height'], $data['align']);
return true;
} else {
return false;
}
}
/**
* Return path to the rendered image on our local system
* Note this is also called by img.php
*/
function _imgfile($data) {
$cache = $this->_cachename($data, 'png');
// create the file if needed
if (!file_exists($cache)) {
$in = $this->_cachename($data, 'txt');
if ($this->getConf('render_local') == '0' && $this->getConf('remote_url')) {
$ok = $this->_remote($data, $in, $cache);
} else if ($this->getConf('render_local') == '1' && $this->getConf('java')) {
$ok = $this->_local($data, $in, $cache);
} else {
return false;
}
if (!$ok) return false;
clearstatcache();
}
if ($data['width'] && $data['percent'] != '%') {
$cache = media_resize_image($cache, 'png', $data['width'], $data['height']);
}
return file_exists($cache) ? $cache : false;
}
/**
* Render the output remotely at plantuml.no-ip.org
*/
function _remote($data, $in, $out) {
if (!file_exists($in)) {
dbglog($in, 'No such plantuml input file');
return false;
}
$http = new DokuHTTPClient();
$http->timeout = 30;
$remote_url = $this->getConf('remote_url');
// strip trailing "/" if present
$base_url = preg_replace('/(.+?)\/$/', '$1', $remote_url);
$java = $this->getConf('java');
if ($java) {
// use url compression if java is available
$jar = $this->getConf('jar');
$jar = realpath($jar);
$jar = escapeshellarg($jar);
$command = $java;
$command .= ' -Djava.awt.headless=true';
$command .= ' -Dfile.encoding=UTF-8';
$command .= " -jar $jar";
$command .= ' -charset UTF-8';
$command .= ' -encodeurl';
$command .= ' ' . escapeshellarg($in);
$command .= ' 2>&1';
$encoded = exec($command, $output, $return_value);
if ($return_value == 0) {
$url = "$base_url/image/$encoded";
} else {
dbglog(join("\n", $output), "Encoding url failed: $command");
return false;
}
} else {
$uml = io_readFile($in);
// remove @startuml and @enduml, as they are not required by the webservice
$uml = str_replace("@startuml\n", '', $uml);
$uml = str_replace("\n@enduml", '', $uml);
$uml = str_replace("\n", '/', $uml);
$uml = urlencode($uml);
// decode encoded slashes (or plantuml server won't understand)
$uml = str_replace('%2F', '/', $uml);
$url = "$base_url/startuml/$uml";
}
$img = $http->get($url);
return $img ? io_saveFile($out, $img) : false;
}
/**
* Render the output locally using the plantuml.jar
*/
function _local($data, $in, $out) {
if (!file_exists($in)) {
dbglog($in, 'No such plantuml input file');
return false;
}
$java = $this->getConf('java');
$jar = $this->getConf('jar');
$jar = realpath($jar);
$jar = escapeshellarg($jar);
// we are not specifying the output here, because plantuml will generate a file with the same
// name as the input but with .png extension, which is exactly what we want
$command = $java;
$command .= ' -Djava.awt.headless=true';
$command .= ' -Dfile.encoding=UTF-8';
$command .= " -jar $jar";
$command .= ' -charset UTF-8';
$command .= ' ' . escapeshellarg($in);
$command .= ' 2>&1';
exec($command, $output, $return_value);
if ($return_value == 0) {
return true;
} else {
dbglog(join("\n", $output), "PlantUML execution failed: $command");
return false;
}
}
/**
* Dumps a message in a log file (named dokuwiki_plantuml.log and located in the Dokuwidi's cache directory)
*/
function _log($text) {
global $conf;
$hFile = fopen($conf['cachedir'].'/dokuwiki_plantuml.log', a);
if(hFile) {
fwrite($hFile, $text . "\r\n");
fclose($hFile);
}
}
}