forked from splitbrain/dokuwiki-plugin-graphviz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
syntax.php
357 lines (305 loc) · 13.4 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
<?php
/**
* graphviz-Plugin: Parses graphviz-blocks
*
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
* @author Matteo Nastasi <nastasi@alternativeoutput.it>
* @author Carl-Christian Salvesen <calle@ioslo.net>
* @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_graphviz extends DokuWiki_Syntax_Plugin {
/**
* What about paragraphs?
*/
function getPType(){
return 'normal';
}
/**
* 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('<graphviz.*?>\n.*?\n</graphviz>',$mode,'plugin_graphviz');
}
/**
* Handle the match
*/
function handle($match, $state, $pos, &$handler) {
$info = $this->getInfo();
// prepare default data
$return = array(
'width' => 0,
'height' => 0,
'layout' => 'dot',
'align' => '',
'dpi' => 0,
'slicespace'=> 0,
'version' => $info['date'], //force rebuild of images on update
);
// 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(dot|neato|twopi|circo|fdp)\b/i',$conf,$match)){
$return['layout'] = strtolower($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];
if(preg_match('/\bdpi=([0-9]+)\b/i', $conf,$match)) $return['dpi'] = $match[1];
if(in_array ('imagick', get_loaded_extensions()) &&
preg_match('/\bslicespace=([0-9]+)\b/i', $conf,$match)) $return['slicespace'] = $match[1];
$input = join("\n",$lines);
$return['md5'] = md5($input); // we only pass a hash around
// store input for later use
io_saveFile($this->_cachename($return,'txt'),$input);
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['layer_n']);
unset($data['layer_cur']);
return getcachename(join('x',array_values($data)),'.graphviz.'.$ext);
}
/**
* Create output
*/
/* -- code example --
<div style="background-color: lightblue;">
<script type="text/javascript"><!--
function $(id) { return document.getElementById(id); }
function showhide(obj)
{
// alert("NAME: ["+obj.name+"] value: ["+(obj.checked == true ? "visible" : "hidden")+"]");
$(obj.name).style.visibility = (obj.checked == true ? "visible" : "hidden");
}
//-->
</script>
<div style="background-color: yellow; position: relative; height: 32px;">
<!-- <img style="height: 32px;" src="onepixel.png"> -->
<img id="one" style="position: absolute; left: 0px; top: 0px; height: 32px;" src="one.png">
<img id="two" style="position: absolute; left: 0px; top: 0px;" src="two.png">
I need: the height and the width of the image to dimension the external div tag
*/
function _render_layered_xhtml($format, &$R, $data, $layers, $in) {
$prefix = basename($in, ".graphviz.txt");
$gvpath = dirname($this->getConf('path'));
$layers_n = count($layers);
$data['layer_n'] = $layers_n;
/* get selected layers */
unset($out);
exec(sprintf("%s/gvpr -f %s %s", escapeshellarg($gvpath),
escapeshellarg(DOKU_PLUGIN.'graphviz/get-layerselect-list.gvp'),
escapeshellarg($in)), $out, $retval);
$selayers = $out;
/* FIXME: if dimension is explicitly added use it instead of calculated */
/* get image dimensions */
$cache = $this->_imgfile($data);
$size = getimagesize($cache);
$width = ($data['width'] > 0 ? $data['width'] : $size[0]);
$height = ($data['height'] > 0 ? $data['height'] : $size[1]);
$R->doc .= sprintf('<div class="media%s" style="width: %dpx; %s /*background-color: lightblue;*/">
<script type="text/javascript"><!--
function $(id) { return document.getElementById(id); }
function showhide(obj)
{
// alert("NAME: ["+obj.name+"] value: ["+(obj.checked == true ? "visible" : "hidden")+"]");
$(obj.name).style.visibility = (obj.checked == true ? "visible" : "hidden");
}
//-->
</script>
<div style="/* background-color: yellow; */position: relative; width: %dpx; height: %dpx;">'."\n",
$data['align'], $width, ($data['align'] == '' ? '' : "align: ".$data['align']."; "),
$width, $height + (isset($data['slicespace']) && $data['slicespace'] > 0 ? $data['slicespace'] * ($layers_n - 1) : 0));
$img = DOKU_BASE.'lib/plugins/graphviz/img.php?'.buildURLparams($data);
// $in = $this->_cachename($data,'txt');
/*
$R->doc .= '<img src="'.$img.'" class="media'.$data['align'].'" alt=""';
// $R->doc .= '<!-- zorro2 --><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 .= '/>';
*/
for ($i = 0 ; $i < $layers_n ; $i++) {
$force_view = ($i == 0 ? TRUE : FALSE);
$data['layer_cur'] = $i;
$img = DOKU_BASE.'lib/plugins/graphviz/img.php?'.buildURLparams($data);
$R->doc .= sprintf('<img id="%s_%d" style="position: absolute; visibility: %s; left: 0px; top: %dpx;" src="%s">'."\n",
$prefix, $i,
(array_search($layers[$i], $selayers) === FALSE && !$force_view ? "hidden" : "visible"),
(isset($data['slicespace']) && $data['slicespace'] > 0 ? $data['slicespace'] * ($layers_n - $i -1) : 0),
$img);
}
$R->doc .= sprintf('</div><form>');
for ($first = true, $i = $layers_n - 1 ; $i >= 0 ; $i--) {
// for ($i = 0, $first = true ; $i < $layers_n ; $i++) {
if ($i == 0 && $layers[$i] == '_background_')
continue;
$R->doc .= sprintf('%s<input type="checkbox"%s name="%s_%d" onclick=\'showhide(this);\'> %s '."\n",
($first != true ? ": " : ""), (array_search($layers[$i], $selayers) === FALSE ? "" : " checked"),
$prefix, $i, htmlentities($layers[$i]));
$first = false;
}
$R->doc .= sprintf('</form></div>');
return true;
}
function render($format, &$R, $data) {
if($format == 'xhtml'){
$in = $this->_cachename($data,'txt');
$gvpath = dirname($this->getConf('path'));
$script = 'BEG_G { char* larr[int]; int i; if (!isAttr($,"G","layers")) return; if (isAttr($,"G","layersep")) tokens($.layers,larr,$.layersep); else tokens($.layers,larr," :\t"); for (larr[i]) { printf("%s\n",larr[i]); } }';
exec(sprintf("%s/gvpr %s %s", escapeshellarg($gvpath), escapeshellarg($script), escapeshellarg($in)),
$out, $retval);
if ($out[0] != "") {
return $this->_render_layered_xhtml($format, &$R, $data, $out, $in);
}
$cache = $this->_imgfile($data);
$size = getimagesize($cache);
$img = DOKU_BASE.'lib/plugins/graphviz/img.php?'.buildURLparams($data);
$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;
}elseif($format == 'odt'){
$src = $this->_imgfile($data);
$R->_odtAddImage($src,$data['width'],$data['height'],$data['align']);
return true;
}
return false;
}
/**
* Return path to the rendered image on our local system
*/
function _imgfile($data){
$txt_data = $data;
unset($txt_data['layer_n']);
unset($txt_data['layer_cur']);
if (isset($data['layer_n']) && isset($data['layer_cur']) &&
$data['layer_cur'] >= 0 && $data['layer_cur'] < $data['layer_n']) {
$cache = $this->_cachename($data,sprintf("%d.png", $data['layer_cur']));
}
else {
$cache = $this->_cachename($data,'png');
}
// create the file if needed
if(!file_exists($cache)){
$in = $this->_cachename($txt_data,'txt');
if($this->getConf('path')){
$ok = $this->_run($data,$in,$cache);
}else{
$ok = $this->_remote($data,$in,$cache);
}
if(!$ok) return false;
clearstatcache();
}
// resized version
if($data['width']){
$cache = media_resize_image($cache,'png',$data['width'],$data['height']);
}
// something went wrong, we're missing the file
if(!file_exists($cache)) return false;
return $cache;
}
/**
* Render the output remotely at google
*/
function _remote($data,$in,$out){
if(!file_exists($in)){
if($conf['debug']){
dbglog($in,'no such graphviz input file');
}
return false;
}
$http = new DokuHTTPClient();
$http->timeout=30;
$pass = array();
$pass['cht'] = 'gv:'.$data['layout'];
$pass['chl'] = io_readFile($in);
$img = $http->post('http://chart.apis.google.com/chart',$pass,'&');
if(!$img) return false;
return io_saveFile($out,$img);
}
/**
* Run the graphviz program
*/
function _run($data,$in,$out) {
global $conf;
if(!file_exists($in)){
if($conf['debug']){
dbglog($in,'no such graphviz input file');
}
return false;
}
$cmd = $this->getConf('path');
if (isset($data['layer_n']) && isset($data['layer_cur'])) {
$gvpath = dirname($this->getConf('path'));
$script = 'BEG_G { char* larr[int]; int i; if (!isAttr($,"G","layers")) return; if (isAttr($,"G","layersep")) tokens($.layers,larr,$.layersep); else tokens($.layers,larr," :\t"); for (larr[i]) { printf("%s\n",larr[i]); } }';
exec(sprintf("%s/gvpr %s %s", escapeshellarg($gvpath), escapeshellarg($script), escapeshellarg($in)),
$exout, $retval);
$cmd .= sprintf(" '-Glayerselect=%s'%s", $exout[$data['layer_cur']], ((int)$data['layer_cur'] > 0 ? ' -Gbgcolor=#00000000' : ''));
}
$cmd .= ' -Tpng';
$cmd .= ' -K'.$data['layout'];
$cmd .= ' -o'.escapeshellarg($out); //output
$cmd .= ' '.escapeshellarg($in); //input
if (isset($data['dpi']) && $data['dpi'] > 0) {
$cmd .= sprintf(" -Gdpi=%d", $data['dpi']);
}
$cmd .= ' 2>/dev/null';
exec($cmd, $output, $error);
if (isset($data['slicespace']) && $data['slicespace'] > 0) {
$image = new Imagick($out);
$trans = new ImagickPixel('transparent');
$imageprops = $image->getImageGeometry();
$w = $imageprops['width'];
$h = $imageprops['height'];
$draw = new ImagickDraw();
$draw->setFillColor($trans); // Set up some colors to use for fill and outline
$draw->setStrokeColor( new ImagickPixel( 'rgb(40,40,40)' ) );
$draw->rectangle( 0, 0, $w-1, $h-1 ); // Draw the rectangle
$image->drawImage( $draw );
$image->resizeImage( $w*2, $h, imagick::FILTER_LANCZOS, 1.0);
$image->shearImage($trans, 45 , 0);
$imageprops = $image->getImageGeometry();
$w = $imageprops['width'];
$image->resizeImage( $w/2, $h/2, imagick::FILTER_LANCZOS, 1.0);
$image->writeImage($out);
}
if ($error != 0){
if($conf['debug']){
dbglog(join("\n",$output),'graphviz command failed: '.$cmd);
}
return false;
}
return true;
}
}