Skip to content

Commit 2a956e6

Browse files
committed
(re)added support for locally installed ditaa
1 parent 72a2bf1 commit 2a956e6

File tree

8 files changed

+457
-163
lines changed

8 files changed

+457
-163
lines changed

conf/default.php

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
$conf['java'] = '';

conf/metadata.php

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?php
2+
3+
$meta['java'] = array('string');
4+

ditaa.php

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
/**
3+
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
4+
* @author Andreas Gohr <[email protected]>
5+
*/
6+
7+
if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../../').'/');
8+
define('NOSESSION',true);
9+
require_once(DOKU_INC.'inc/init.php');
10+
require_once(DOKU_INC.'inc/pageutils.php');
11+
require_once(DOKU_INC.'inc/io.php');
12+
13+
$data = $_REQUEST;
14+
$w = (int) $data['width'];
15+
$h = (int) $data['height'];
16+
unset($data['width']);
17+
unset($data['height']);
18+
unset($data['align']);
19+
20+
$cache = getcachename(join('x',array_values($data)),'ditaa.png');
21+
22+
// create the file if needed
23+
if(!file_exists($cache)){
24+
$plugin = plugin_load('syntax','ditaa');
25+
$plugin->_run($data,$cache);
26+
clearstatcache();
27+
}
28+
29+
// resized version
30+
if($w) $cache = media_resize_image($cache,'png',$w,$h);
31+
32+
// something went wrong, we're missing the file
33+
if(!file_exists($cache)){
34+
header("HTTP/1.0 404 Not Found");
35+
echo 'Not Found';
36+
exit;
37+
}
38+
39+
header('Content-Type: image/png;');
40+
header('Expires: '.gmdate("D, d M Y H:i:s", time()+max($conf['cachetime'], 3600)).' GMT');
41+
header('Cache-Control: public, proxy-revalidate, no-transform, max-age='.max($conf['cachetime'], 3600));
42+
header('Pragma: public');
43+
http_conditionalRequest($time);
44+
echo io_readFile($cache,false);
45+
46+
//Setup VIM: ex: et ts=4 enc=utf-8 :

ditaa/COPYING

+340
Large diffs are not rendered by default.

ditaa/HISTORY

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
Version 0.9
2+
* New shapes:
3+
* Ellipse {o}
4+
* Manual operation {mo}
5+
* Decision {c} ("Choice")
6+
* Trapezoid {tr}
7+
* Eliminated memory leak
8+
* Further parsing optimisations (pre-compiled regexes)
9+
10+
Version 0.8
11+
* Parsing optimisation (~90% faster)
12+
* Fixed line separation bugs
13+
* Fixed open shape rendering bugs (missing lines)
14+
* Generally more reliable
15+
* Tidying-up of command-line options (now using Apache commons-cli)
16+
* Streamlined release process

ditaa/ditaa0_9.jar

182 KB
Binary file not shown.

lang/en/settings.php

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?php
2+
3+
$lang['java'] = 'The path to your Java runtime interpreter (eg. <code>/usr/bin/java</code>). Leave empty to use remote rendering at ditaa.org.';
4+

syntax.php

+44-163
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<?php
2-
32
/**
43
* Ditaa-Plugin: Converts Ascii-Flowcharts into a png-File
54
*
@@ -15,21 +14,6 @@
1514

1615
class syntax_plugin_ditaa extends DokuWiki_Syntax_Plugin {
1716

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-
3317
/**
3418
* What about paragraphs?
3519
*/
@@ -66,6 +50,8 @@ function connectTo($mode) {
6650
* Handle the match
6751
*/
6852
function handle($match, $state, $pos, &$handler) {
53+
$info = $this->getInfo();
54+
6955
// prepare default data
7056
$return = array(
7157
'data' => '',
@@ -77,6 +63,7 @@ function handle($match, $state, $pos, &$handler) {
7763
'shadow' => true,
7864
'scale' => 1,
7965
'align' => '',
66+
'version' => $info['date'], //forece rebuild of images on update
8067
);
8168

8269

@@ -91,7 +78,7 @@ function handle($match, $state, $pos, &$handler) {
9178
$return['width'] = $match[1];
9279
$return['height'] = $match[2];
9380
}
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];
9582
if(preg_match('/\bwidth=([0-9]+)\b/i', $conf,$match)) $return['width'] = $match[1];
9683
if(preg_match('/\bheight=([0-9]+)\b/i', $conf,$match)) $return['height'] = $match[1];
9784
// match boolean toggles
@@ -112,160 +99,54 @@ function handle($match, $state, $pos, &$handler) {
11299
function render($format, &$R, $data) {
113100
if($format != 'xhtml') return;
114101

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']));
163119
}
164120

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">';
177122
}
178123

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-
}
194124

195125
/**
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
200127
*/
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;
267149
return true;
268-
269150
}
270151

271152
}

0 commit comments

Comments
 (0)