1
1
<?php
2
-
3
2
/**
4
3
* Ditaa-Plugin: Converts Ascii-Flowcharts into a png-File
5
4
*
15
14
16
15
class syntax_plugin_ditaa extends DokuWiki_Syntax_Plugin {
17
16
18
- var $ ditaa_name = '' ;
19
-
20
- var $ ditaa_width = -1 ;
21
-
22
- var $ ditaa_height = -1 ;
23
-
24
- var $ ditaa_data = '' ;
25
-
26
- var $ pathToJava = "/opt/blackdown-jdk-1.4.2.02/bin/java " ;
27
-
28
- var $ pathToDitaa = "/var/www/sst.intern.editable/dokuwiki/htdocs/ditaa.jar " ;
29
-
30
- var $ tempdir = "/tmp " ;
31
-
32
-
33
17
/**
34
18
* What about paragraphs?
35
19
*/
@@ -66,6 +50,8 @@ function connectTo($mode) {
66
50
* Handle the match
67
51
*/
68
52
function handle ($ match , $ state , $ pos , &$ handler ) {
53
+ $ info = $ this ->getInfo ();
54
+
69
55
// prepare default data
70
56
$ return = array (
71
57
'data ' => '' ,
@@ -77,6 +63,7 @@ function handle($match, $state, $pos, &$handler) {
77
63
'shadow ' => true ,
78
64
'scale ' => 1 ,
79
65
'align ' => '' ,
66
+ 'version ' => $ info ['date ' ], //forece rebuild of images on update
80
67
);
81
68
82
69
@@ -91,7 +78,7 @@ function handle($match, $state, $pos, &$handler) {
91
78
$ return ['width ' ] = $ match [1 ];
92
79
$ return ['height ' ] = $ match [2 ];
93
80
}
94
- if (preg_match ('/\b(\d+)X\b/ ' ,$ conf ,$ match )) $ return ['scale ' ] = $ match [1 ];
81
+ if (preg_match ('/\b(\d+(\.\d+)? )X\b/ ' ,$ conf ,$ match )) $ return ['scale ' ] = $ match [1 ];
95
82
if (preg_match ('/\bwidth=([0-9]+)\b/i ' , $ conf ,$ match )) $ return ['width ' ] = $ match [1 ];
96
83
if (preg_match ('/\bheight=([0-9]+)\b/i ' , $ conf ,$ match )) $ return ['height ' ] = $ match [1 ];
97
84
// match boolean toggles
@@ -112,160 +99,54 @@ function handle($match, $state, $pos, &$handler) {
112
99
function render ($ format , &$ R , $ data ) {
113
100
if ($ format != 'xhtml ' ) return ;
114
101
115
- // prepare data for ditaa.org
116
- $ pass = array (
117
- 'grid ' => $ data ['data ' ],
118
- 'scale ' => $ data ['scale ' ]
119
- );
120
- if (!$ data ['antialias ' ]) $ pass ['A ' ] = 'on ' ;
121
- if (!$ data ['shadow ' ]) $ pass ['S ' ] = 'on ' ;
122
- if ($ data ['round ' ]) $ pass ['r ' ] = 'on ' ;
123
- if (!$ data ['edgesep ' ]) $ pass ['E ' ] = 'on ' ;
124
- $ pass ['timeout ' ] = 25 ;
125
-
126
- $ img = 'http://ditaa.org/ditaa/render? ' .buildURLparams ($ pass ,'& ' );
127
- $ img = ml ($ img ,array ('w ' =>$ data ['width ' ],'h ' =>$ data ['height ' ]));
128
-
129
- $ R ->doc .= '<img src=" ' .$ img .'" alt="x"> ' ;
130
-
131
- }
132
-
133
- /**
134
- * Store values for later ditaa-rendering
135
- *
136
- * @param object $renderer The dokuwiki-renderer
137
- * @param string $name The name for the ditaa-object
138
- * @param width $width The width for the ditaa-object
139
- * @param height $height The height for the ditaa-object
140
- * @return bool All parameters are set
141
- */
142
-
143
- function _ditaa_begin (&$ renderer , $ name , $ width , $ height )
144
- {
145
- // Check, if name is given
146
-
147
- $ name = trim (strtolower ($ name ));
148
-
149
- if ($ name == '' ) {
150
-
151
- $ renderer ->doc .= '---NO NAME FOR FLOWCHART GIVEN--- ' ;
152
- return true ;
153
-
154
- }
155
-
156
- $ width = trim ($ width );
157
- $ height = trim ($ height );
158
-
159
- if (($ width != '' ) && (settype ($ width , 'int ' ))) {
160
-
161
- $ this ->ditaa_width = $ width ;
162
-
102
+ if ($ this ->getConf ('java ' )){
103
+ // run ditaa on our own server
104
+ $ img = DOKU_BASE .'lib/plugins/ditaa/ditaa.php? ' .buildURLparams ($ data ,'& ' );
105
+ }else {
106
+ // use ditaa.org for rendering
107
+ $ pass = array (
108
+ 'grid ' => $ data ['data ' ],
109
+ 'scale ' => $ data ['scale ' ]
110
+ );
111
+ if (!$ data ['antialias ' ]) $ pass ['A ' ] = 'on ' ;
112
+ if (!$ data ['shadow ' ]) $ pass ['S ' ] = 'on ' ;
113
+ if ($ data ['round ' ]) $ pass ['r ' ] = 'on ' ;
114
+ if (!$ data ['edgesep ' ]) $ pass ['E ' ] = 'on ' ;
115
+ $ pass ['timeout ' ] = 25 ;
116
+
117
+ $ img = 'http://ditaa.org/ditaa/render? ' .buildURLparams ($ pass ,'& ' );
118
+ $ img = ml ($ img ,array ('w ' =>$ data ['width ' ],'h ' =>$ data ['height ' ]));
163
119
}
164
120
165
- if (($ height != '' ) && (settype ($ height , 'int ' ))) {
166
-
167
- $ this ->ditaa_height = $ height ;
168
-
169
- }
170
-
171
- $ this ->ditaa_name = $ name ;
172
-
173
- $ this ->ditaa_data = '' ;
174
-
175
- return true ;
176
-
121
+ $ R ->doc .= '<img src=" ' .$ img .'" alt="x"> ' ;
177
122
}
178
123
179
- /**
180
- * Expand the data for the ditaa-object
181
- *
182
- * @param string $data The data for the ditaa-object
183
- * @return bool If everything was right
184
- */
185
-
186
- function _ditaa_data ($ data )
187
- {
188
-
189
- $ this ->ditaa_data .= $ data ;
190
-
191
- return true ;
192
-
193
- }
194
124
195
125
/**
196
- * Render the ditaa-object
197
- *
198
- * @param object $renderer The dokuwiki-Renderer
199
- * @return bool If everything was right
126
+ * Run the ditaa Java program
200
127
*/
201
-
202
- function _ditaa_end (&$ renderer )
203
- {
204
- global $ conf , $ INFO ;
205
-
206
- // Write a text file for ditaa
207
-
208
- $ tempfile = tempnam ($ this ->tempdir , 'ditaa_ ' );
209
-
210
- $ file = fopen ($ tempfile .'.txt ' , 'w ' );
211
- fwrite ($ file , $ this ->ditaa_data );
212
- fclose ($ file );
213
-
214
- $ md5 = md5_file ($ tempfile .'.txt ' );
215
-
216
- $ mediadir = $ conf ["mediadir " ]."/ " .str_replace (": " , "/ " ,$ INFO ['namespace ' ] );
217
-
218
- if (!is_dir ($ mediadir )) {
219
- umask (002 );
220
- mkdir ($ mediadir ,0777 );
221
- }
222
-
223
- $ imagefile = $ mediadir .'/ditaa_ ' .$ this ->ditaa_name .'_ ' .$ md5 .'.png ' ;
224
-
225
- if ( !file_exists ($ imagefile )) {
226
-
227
- $ cmd = $ this ->pathToJava ." -Djava.awt.headless=true -jar " .$ this ->pathToDitaa ." " .$ tempfile .".txt " .$ tempfile .".png " ;
228
-
229
- exec ($ cmd , $ output , $ error );
230
-
231
- if ($ error != 0 ) {
232
- $ renderer ->doc .= '---ERROR CONVERTING DIAGRAM--- ' ;
233
- return false ;
234
- }
235
-
236
- if (file_exists ($ imagefile )) {
237
- unlink ($ imagefile );
238
- }
239
-
240
- if ( !copy ($ tempfile .'.png ' , $ imagefile ) ) {
241
- return false ;
242
- }
243
-
244
- // Remove input file
245
- unlink ($ tempfile .'.png ' );
246
- unlink ($ tempfile );
247
- }
248
-
249
- unlink ($ tempfile .'.txt ' );
250
-
251
- // Output Img-Tag
252
-
253
- $ width = NULL ;
254
-
255
- if ($ this ->ditaa_width != -1 ) {
256
- $ width = $ this ->ditaa_width ;
257
- }
258
-
259
- $ height = NULL ;
260
-
261
- if ($ this ->ditaa_height != -1 ) {
262
- $ height = $ this ->ditaa_height ;
263
- }
264
-
265
- $ renderer ->doc .= $ renderer ->internalmedia ($ INFO ['namespace ' ].':ditaa_ ' .$ this ->ditaa_name .'_ ' .$ md5 .'.png ' , $ this ->ditaa_name , NULL , $ width , $ height , false );
266
-
128
+ function _run ($ data ,$ cache ) {
129
+ global $ conf ;
130
+
131
+ $ temp = tempnam ($ conf ['tmpdir ' ],'ditaa_ ' );
132
+ io_saveFile ($ temp ,$ data ['data ' ]);
133
+
134
+ $ cmd = $ this ->getConf ('java ' );
135
+ $ cmd .= ' -Djava.awt.headless=true -jar ' ;
136
+ $ cmd .= ' ' .escapeshellarg (dirname (__FILE__ ).'/ditaa/ditaa0_9.jar ' ); //ditaa jar
137
+ $ cmd .= ' ' .escapeshellarg ($ temp ); //input
138
+ $ cmd .= ' ' .escapeshellarg ($ cache ); //output
139
+ $ cmd .= ' -s ' .escapeshellarg ($ data ['scale ' ]);
140
+ if (!$ data ['antialias ' ]) $ cmd .= ' -A ' ;
141
+ if (!$ data ['shadow ' ]) $ cmd .= ' -S ' ;
142
+ if ($ data ['round ' ]) $ cmd .= ' -r ' ;
143
+ if (!$ data ['edgesep ' ]) $ cmd .= ' -E ' ;
144
+
145
+ exec ($ cmd , $ output , $ error );
146
+ @unlink ($ temp );
147
+
148
+ if ($ error != 0 ) return false ;
267
149
return true ;
268
-
269
150
}
270
151
271
152
}
0 commit comments