-
Notifications
You must be signed in to change notification settings - Fork 5
/
syntax.php
388 lines (335 loc) · 12 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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
<?php
/**
* Ditaa-Plugin: Converts Ascii-Flowcharts into a png-File
*
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
* @author Dennis Ploeger <develop [at] dieploegers [dot] de>
* @author Christoph Mertins <c [dot] mertins [at] gmail [dot] com>
* @author Gerry Weißbach / i-net software <tools [at] inetsoftware [dot] de>
* @author Christian Marg <marg@rz.tu-clausthal.de>
* @author Andreas Gohr <andi@splitbrain.org>
*/
if(!defined('DOKU_INC')) define('DOKU_INC', realpath(dirname(__FILE__) . '/../../') . '/');
if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC . 'lib/plugins/');
require_once(DOKU_PLUGIN . 'syntax.php');
/**
* Class syntax_plugin_ditaa
*/
class syntax_plugin_ditaa extends DokuWiki_Syntax_Plugin {
/**
* What about paragraphs?
*/
public function getPType() {
return 'normal';
}
/**
* What kind of syntax are we?
*/
public function getType() {
return 'substition';
}
/**
* Where to sort in?
*/
public function getSort() {
return 200;
}
/**
* Connect pattern to lexer
*
* @param string $mode
*/
public function connectTo($mode) {
$this->Lexer->addSpecialPattern('<ditaa.*?>\n.*?\n</ditaa>', $mode, 'plugin_ditaa');
}
/**
* Stores all infor about the diagram in two files. One is the actual ditaa data, the other
* contains the options.
*
* @param string $match The text matched by the patterns
* @param int $state The lexer state for the match
* @param int $pos The character position of the matched text
* @param Doku_Handler $handler The Doku_Handler object
* @return bool|array Return an array with all data you want to use in render, false don't add an instruction
*/
public function handle($match, $state, $pos, Doku_Handler $handler) {
$info = $this->getInfo();
// prepare default data
$return = array(
'width' => 0,
'height' => 0,
'antialias' => true,
'edgesep' => true,
'round' => false,
'shadow' => true,
'scale' => 1,
'align' => '',
'version' => $info['date'],
'now' => time()
);
// prepare input
$lines = explode("\n", $match);
$conf = array_shift($lines);
array_pop($lines);
// match config options
if(preg_match('/\b(left|center|right)\b/i', $conf, $match)) $return['align'] = $match[1];
if(preg_match('/\b(\d+)x(\d+)\b/', $conf, $match)) {
$return['width'] = $match[1];
$return['height'] = $match[2];
}
if(preg_match('/\b(\d+(\.\d+)?)X\b/', $conf, $match)) $return['scale'] = $match[1];
if(preg_match('/\bwidth=([0-9]+)\b/i', $conf, $match)) $return['width'] = $match[1];
if(preg_match('/\bheight=([0-9]+)\b/i', $conf, $match)) $return['height'] = $match[1];
// match boolean toggles
if(preg_match_all('/\b(no)?(antialias|edgesep|round|shadow)\b/i', $conf, $matches, PREG_SET_ORDER)) {
foreach($matches as $match) {
$return[$match[2]] = !$match[1];
}
}
$input = join("\n", $lines);
$return['md5'] = md5($input.$this->_prepareData($return)); // we only pass a hash around
// store input for later use in _imagefile()
io_saveFile(getCacheName($return['md5'], '.ditaa.txt'), $input);
io_saveFile(getCacheName($return['md5'], '.ditaa.cfg'), serialize($return));
return $return;
}
/**
* Output the image
*
* @param string $format output format being rendered
* @param Doku_Renderer $R the current renderer object
* @param array $data data created by handler()
* @return boolean rendered correctly?
*/
public function render($format, Doku_Renderer $R, $data) {
global $ID;
if($format == 'xhtml') {
// Only use the md5 key
$img = ml($ID, array('ditaa' => $data['md5'], 't' => $data['now']));
$R->doc .= '<img src="' . $img . '" class="media' . $data['align'] . '" alt=""';
if($data['width']) $R->doc .= ' width="' . $data['width'] . '"';
if($data['height']) $R->doc .= ' height="' . $data['height'] . '"';
if($data['align'] == 'right') $R->doc .= ' align="right"';
if($data['align'] == 'left') $R->doc .= ' align="left"';
$R->doc .= '/>';
return true;
} else if($format == 'odt') {
$src = $this->_imgfile($data['md5']);
/** @var renderer_plugin_odt $R */
$R->_odtAddImage($src, $data['width'], $data['height'], $data['align']);
return true;
}
return false;
}
/**
* Prepares the Data that is used for the cache name
* Width, height and scale are left out.
* Ensures sanity.
*/
protected function _prepareData($input) {
$output = array();
foreach($input as $key => $value) {
switch($key) {
case 'scale':
case 'antialias':
case 'edgesep':
case 'round':
case 'shadow':
case 'version':
$output[$key] = $value;
};
}
ksort($output);
return $output;
}
/**
* Return path to the rendered image on our local system
*
* @param string $md5 MD5 of the input data, used to identify the cache files
* @return false|string path to file or fals on error
*/
public function _imgfile($md5) {
$file_cfg = getCacheName($md5, '.ditaa.cfg'); // configs
$file_txt = getCacheName($md5, '.ditaa.txt'); // input
$file_png = getCacheName($md5, '.ditaa.png'); // ouput
if(!file_exists($file_cfg) || !file_exists($file_txt)) {
return false;
}
$data = unserialize(io_readFile($file_cfg, false));
// file does not exist or is outdated
if(@filemtime($file_png) < filemtime($file_cfg)) {
if($this->getConf('java')) {
$ok = $this->_runJava($data, $file_txt, $file_png);
} else {
$ok = $this->_runGo($data, $file_txt, $file_png);
#$ok = $this->_remote($data, $in, $cache);
}
if(!$ok) return false;
clearstatcache($file_png);
}
// resized version
if($data['width']) {
$file_png = media_resize_image($file_png, 'png', $data['width'], $data['height']);
}
// something went wrong, we're missing the file
if(!file_exists($file_png)) return false;
return $file_png;
}
/**
* Render the output remotely at ditaa.org
*
* @deprecated ditaa.org is no longer available, so this defunct
* @param array $data The config settings
* @param string $in Path to the ditaa input file (txt)
* @param string $out Path to the output file (PNG)
* @return bool true if the image was created, false otherwise
*/
protected function _remote($data, $in, $out) {
global $conf;
if(!file_exists($in)) {
if($conf['debug']) {
dbglog($in, 'no such ditaa input file');
}
return false;
}
$http = new DokuHTTPClient();
$http->timeout = 30;
$pass = array();
$pass['scale'] = $data['scale'];
$pass['timeout'] = 25;
$pass['grid'] = io_readFile($in);
if(!$data['antialias']) $pass['A'] = 'on';
if(!$data['shadow']) $pass['S'] = 'on';
if($data['round']) $pass['r'] = 'on';
if(!$data['edgesep']) $pass['E'] = 'on';
$img = $http->post('http://ditaa.org/ditaa/render', $pass);
if(!$img) return false;
return io_saveFile($out, $img);
}
/**
* Run the ditaa Java program
*
* @param array $data The config settings
* @param string $in Path to the ditaa input file (txt)
* @param string $out Path to the output file (PNG)
* @return bool true if the image was created, false otherwise
*/
protected function _runJava($data, $in, $out) {
global $conf;
if(!file_exists($in)) {
if($conf['debug']) {
dbglog($in, 'no such ditaa input file');
}
return false;
}
$cmd = $this->getConf('java');
$cmd .= ' -Djava.awt.headless=true -Dfile.encoding=UTF-8 -jar';
$cmd .= ' ' . escapeshellarg(__DIR__ . '/ditaa/ditaa.jar');
$cmd .= ' --encoding UTF-8';
$cmd .= ' ' . escapeshellarg($in); //input
$cmd .= ' ' . escapeshellarg($out); //output
$cmd .= ' -s ' . escapeshellarg($data['scale']);
if(!$data['antialias']) $cmd .= ' -A';
if(!$data['shadow']) $cmd .= ' -S';
if($data['round']) $cmd .= ' -r';
if(!$data['edgesep']) $cmd .= ' -E';
exec($cmd, $output, $error);
if($error != 0) {
if($conf['debug']) {
dbglog(join("\n", $output), 'ditaa command failed: ' . $cmd);
}
return false;
}
return true;
}
/**
* Run the ditaa Go program
*
* @param array $data The config settings - currently not used because the Go relase supports no options
* @param string $in Path to the ditaa input file (txt)
* @param string $out Path to the output file (PNG)
* @return bool true if the image was created, false otherwise
*/
protected function _runGo($data, $in, $out) {
global $conf;
if(!file_exists($in)) {
if($conf['debug']) {
dbglog($in, 'no such ditaa input file');
}
return false;
}
$cmd = $this->getLocalBinary();
if(!$cmd) return false;
$cmd .= ' ' . escapeshellarg($in); //input
$cmd .= ' ' . escapeshellarg($out); //output
exec($cmd, $output, $error);
if($error != 0) {
if($conf['debug']) {
dbglog(join("\n", $output), 'ditaa command failed: ' . $cmd);
}
return false;
}
return true;
}
/**
* Detects the platform of the PHP host and constructs the appropriate binary name
*
* @return false|string
*/
protected function getBinaryName() {
$ext = '';
$os = php_uname('s');
if(preg_match('/darwin/i', $os)) {
$os = 'darwin';
} elseif(preg_match('/win/i', $os)) {
$os = 'windows';
$ext = '.exe';
} elseif(preg_match('/linux/i', $os)) {
$os = 'linux';
} elseif(preg_match('/freebsd/i', $os)) {
$os = 'freebsd';
} elseif(preg_match('/openbsd/i', $os)) {
$os = 'openbsd';
} elseif(preg_match('/netbsd/i', $os)) {
$os = 'netbsd';
} elseif(preg_match('/(solaris|netbsd)/i', $os)) {
$os = 'freebsd';
} else {
return false;
}
$arch = php_uname('m');
if($arch == 'x86_64') {
$arch = 'amd64';
} elseif(preg_match('/arm/i', $arch)) {
$arch = 'amd';
} else {
$arch = '386';
}
return "ditaa-$os-$arch$ext";
}
/**
* Returns the local binary to use
*
* Downloads it if necessary
*
* @return bool|string
*/
protected function getLocalBinary() {
global $conf;
$bin = $this->getBinaryName();
if(!$bin) return false;
// check distributed files first
if(file_exists(__DIR__ . '/ditaa/' . $bin)) {
return __DIR__ . '/ditaa/' . $bin;
}
$info = $this->getInfo();
$cache = getCacheName($info['date'], ".$bin");
if(file_exists($cache)) return $cache;
$url = 'https://github.com/akavel/ditaa/releases/download/g1.0.0/' . $bin;
if(io_download($url, $cache, false, '', 0)) {
@chmod($cache, $conf['dmode']);
return $cache;
}
return false;
}
}