1a34ed36bSDennis Ploeger<?php 2a34ed36bSDennis Ploeger/** 3a34ed36bSDennis Ploeger * Ditaa-Plugin: Converts Ascii-Flowcharts into a png-File 4a34ed36bSDennis Ploeger * 5a34ed36bSDennis Ploeger * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 6a34ed36bSDennis Ploeger * @author Dennis Ploeger <develop [at] dieploegers [dot] de> 7a34ed36bSDennis Ploeger * @author Christoph Mertins <c [dot] mertins [at] gmail [dot] com> 8d402f512SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 9a34ed36bSDennis Ploeger */ 10a34ed36bSDennis Ploeger 11a34ed36bSDennis Ploegerif(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/'); 12a34ed36bSDennis Ploegerif(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/'); 13a34ed36bSDennis Ploegerrequire_once(DOKU_PLUGIN.'syntax.php'); 14a34ed36bSDennis Ploeger 15a34ed36bSDennis Ploegerclass syntax_plugin_ditaa extends DokuWiki_Syntax_Plugin { 16a34ed36bSDennis Ploeger 17a34ed36bSDennis Ploeger /** 18a34ed36bSDennis Ploeger * What about paragraphs? 19a34ed36bSDennis Ploeger */ 20a34ed36bSDennis Ploeger function getPType(){ 21a34ed36bSDennis Ploeger return 'normal'; 22a34ed36bSDennis Ploeger } 23a34ed36bSDennis Ploeger 24a34ed36bSDennis Ploeger /** 25a34ed36bSDennis Ploeger * What kind of syntax are we? 26a34ed36bSDennis Ploeger */ 27a34ed36bSDennis Ploeger function getType(){ 28a34ed36bSDennis Ploeger return 'substition'; 29a34ed36bSDennis Ploeger } 30a34ed36bSDennis Ploeger 31a34ed36bSDennis Ploeger /** 32a34ed36bSDennis Ploeger * Where to sort in? 33a34ed36bSDennis Ploeger */ 34a34ed36bSDennis Ploeger function getSort(){ 35a34ed36bSDennis Ploeger return 200; 36a34ed36bSDennis Ploeger } 37a34ed36bSDennis Ploeger 38a34ed36bSDennis Ploeger /** 3972a2bf1dSAndreas Gohr * Connect pattern to lexer 40a34ed36bSDennis Ploeger */ 41a34ed36bSDennis Ploeger 42a34ed36bSDennis Ploeger function connectTo($mode) { 4372a2bf1dSAndreas Gohr $this->Lexer->addSpecialPattern('<ditaa.*?>\n.*?\n</ditaa>',$mode,'plugin_ditaa'); 44a34ed36bSDennis Ploeger } 45a34ed36bSDennis Ploeger 46a34ed36bSDennis Ploeger /** 47a34ed36bSDennis Ploeger * Handle the match 48a34ed36bSDennis Ploeger */ 4972a2bf1dSAndreas Gohr function handle($match, $state, $pos, &$handler) { 502a956e66SAndreas Gohr $info = $this->getInfo(); 512a956e66SAndreas Gohr 5272a2bf1dSAndreas Gohr // prepare default data 5372a2bf1dSAndreas Gohr $return = array( 5472a2bf1dSAndreas Gohr 'width' => 0, 5572a2bf1dSAndreas Gohr 'height' => 0, 5672a2bf1dSAndreas Gohr 'antialias' => true, 5772a2bf1dSAndreas Gohr 'edgesep' => true, 5872a2bf1dSAndreas Gohr 'round' => false, 5972a2bf1dSAndreas Gohr 'shadow' => true, 6072a2bf1dSAndreas Gohr 'scale' => 1, 6172a2bf1dSAndreas Gohr 'align' => '', 622a956e66SAndreas Gohr 'version' => $info['date'], //forece rebuild of images on update 6372a2bf1dSAndreas Gohr ); 64a34ed36bSDennis Ploeger 65a34ed36bSDennis Ploeger 6672a2bf1dSAndreas Gohr // prepare input 6772a2bf1dSAndreas Gohr $lines = explode("\n",$match); 6872a2bf1dSAndreas Gohr $conf = array_shift($lines); 6972a2bf1dSAndreas Gohr array_pop($lines); 7072a2bf1dSAndreas Gohr 7172a2bf1dSAndreas Gohr // match config options 7272a2bf1dSAndreas Gohr if(preg_match('/\b(left|center|right)\b/i',$conf,$match)) $return['align'] = $match[1]; 7372a2bf1dSAndreas Gohr if(preg_match('/\b(\d+)x(\d+)\b/',$conf,$match)){ 7472a2bf1dSAndreas Gohr $return['width'] = $match[1]; 7572a2bf1dSAndreas Gohr $return['height'] = $match[2]; 76a34ed36bSDennis Ploeger } 772a956e66SAndreas Gohr if(preg_match('/\b(\d+(\.\d+)?)X\b/',$conf,$match)) $return['scale'] = $match[1]; 7872a2bf1dSAndreas Gohr if(preg_match('/\bwidth=([0-9]+)\b/i', $conf,$match)) $return['width'] = $match[1]; 7972a2bf1dSAndreas Gohr if(preg_match('/\bheight=([0-9]+)\b/i', $conf,$match)) $return['height'] = $match[1]; 8072a2bf1dSAndreas Gohr // match boolean toggles 8172a2bf1dSAndreas Gohr if(preg_match_all('/\b(no)?(antialias|edgesep|round|shadow)\b/i',$conf,$matches,PREG_SET_ORDER)){ 8272a2bf1dSAndreas Gohr foreach($matches as $match){ 8372a2bf1dSAndreas Gohr $return[$match[2]] = ! $match[1]; 8472a2bf1dSAndreas Gohr } 8572a2bf1dSAndreas Gohr } 8672a2bf1dSAndreas Gohr 8705cbae88SAndreas Gohr $input = join("\n",$lines); 8805cbae88SAndreas Gohr $return['md5'] = md5($input); // we only pass a hash around 8905cbae88SAndreas Gohr 9005cbae88SAndreas Gohr // store input for later use 9105cbae88SAndreas Gohr io_saveFile($this->_cachename($return,'txt'),$input); 9272a2bf1dSAndreas Gohr 9372a2bf1dSAndreas Gohr return $return; 94a34ed36bSDennis Ploeger } 95a34ed36bSDennis Ploeger 96a34ed36bSDennis Ploeger /** 9795615d6cSAndreas Gohr * Cache file is based on parameters that influence the result image 9805cbae88SAndreas Gohr */ 9905cbae88SAndreas Gohr function _cachename($data,$ext){ 10005cbae88SAndreas Gohr unset($data['width']); 10105cbae88SAndreas Gohr unset($data['height']); 10205cbae88SAndreas Gohr unset($data['align']); 10305cbae88SAndreas Gohr return getcachename(join('x',array_values($data)),'.ditaa.'.$ext); 10405cbae88SAndreas Gohr } 10505cbae88SAndreas Gohr 10605cbae88SAndreas Gohr /** 10795615d6cSAndreas Gohr * Create output 10895615d6cSAndreas Gohr */ 10995615d6cSAndreas Gohr function render($format, &$R, $data) { 11095615d6cSAndreas Gohr if($format == 'xhtml'){ 11195615d6cSAndreas Gohr $img = DOKU_BASE.'lib/plugins/ditaa/img.php?'.buildURLparams($data); 11295615d6cSAndreas Gohr $R->doc .= '<img src="'.$img.'" class="media'.$data['align'].'" alt=""'; 11395615d6cSAndreas Gohr if($data['width']) $R->doc .= ' width="'.$data['width'].'"'; 11495615d6cSAndreas Gohr if($data['height']) $R->doc .= ' height="'.$data['height'].'"'; 115*c77baa73SWilli Schönborn if($data['align'] == 'right') $R->doc .= ' align="right"'; 116*c77baa73SWilli Schönborn if($data['align'] == 'left') $R->doc .= ' align="left"'; 11795615d6cSAndreas Gohr $R->doc .= '/>'; 11895615d6cSAndreas Gohr return true; 11995615d6cSAndreas Gohr }elseif($format == 'odt'){ 12095615d6cSAndreas Gohr $src = $this->_imgfile($data); 12195615d6cSAndreas Gohr $R->_odtAddImage($src,$data['width'],$data['height'],$data['align']); 12295615d6cSAndreas Gohr return true; 12395615d6cSAndreas Gohr } 12495615d6cSAndreas Gohr return false; 12595615d6cSAndreas Gohr } 12695615d6cSAndreas Gohr 12795615d6cSAndreas Gohr 12895615d6cSAndreas Gohr /** 12995615d6cSAndreas Gohr * Return path to the rendered image on our local system 13095615d6cSAndreas Gohr */ 13195615d6cSAndreas Gohr function _imgfile($data){ 13295615d6cSAndreas Gohr $cache = $this->_cachename($data,'png'); 13395615d6cSAndreas Gohr 13495615d6cSAndreas Gohr // create the file if needed 13595615d6cSAndreas Gohr if(!file_exists($cache)){ 13695615d6cSAndreas Gohr $in = $this->_cachename($data,'txt'); 13795615d6cSAndreas Gohr if($this->getConf('java')){ 13895615d6cSAndreas Gohr $ok = $this->_run($data,$in,$cache); 13995615d6cSAndreas Gohr }else{ 14095615d6cSAndreas Gohr $ok = $this->_remote($data,$in,$cache); 14195615d6cSAndreas Gohr } 14295615d6cSAndreas Gohr if(!$ok) return false; 14395615d6cSAndreas Gohr clearstatcache(); 14495615d6cSAndreas Gohr } 14595615d6cSAndreas Gohr 14695615d6cSAndreas Gohr // resized version 14795615d6cSAndreas Gohr if($data['width']){ 14895615d6cSAndreas Gohr $cache = media_resize_image($cache,'png',$data['width'],$data['height']); 14995615d6cSAndreas Gohr } 15095615d6cSAndreas Gohr 15195615d6cSAndreas Gohr // something went wrong, we're missing the file 15295615d6cSAndreas Gohr if(!file_exists($cache)) return false; 15395615d6cSAndreas Gohr 15495615d6cSAndreas Gohr return $cache; 15595615d6cSAndreas Gohr } 15695615d6cSAndreas Gohr 15795615d6cSAndreas Gohr /** 15805cbae88SAndreas Gohr * Render the output remotely at ditaa.org 15905cbae88SAndreas Gohr */ 16005cbae88SAndreas Gohr function _remote($data,$in,$out){ 16195615d6cSAndreas Gohr if(!file_exists($in)){ 16295615d6cSAndreas Gohr if($conf['debug']){ 16395615d6cSAndreas Gohr dbglog($in,'no such ditaa input file'); 16495615d6cSAndreas Gohr } 16595615d6cSAndreas Gohr return false; 16695615d6cSAndreas Gohr } 16795615d6cSAndreas Gohr 16805cbae88SAndreas Gohr $http = new DokuHTTPClient(); 16905cbae88SAndreas Gohr $http->timeout=30; 17005cbae88SAndreas Gohr 17105cbae88SAndreas Gohr $pass = array(); 17205cbae88SAndreas Gohr $pass['scale'] = $data['scale']; 17305cbae88SAndreas Gohr $pass['timeout'] = 25; 17405cbae88SAndreas Gohr $pass['grid'] = io_readFile($in); 17505cbae88SAndreas Gohr if(!$data['antialias']) $pass['A'] = 'on'; 17605cbae88SAndreas Gohr if(!$data['shadow']) $pass['S'] = 'on'; 17705cbae88SAndreas Gohr if($data['round']) $pass['r'] = 'on'; 17805cbae88SAndreas Gohr if(!$data['edgesep']) $pass['E'] = 'on'; 17905cbae88SAndreas Gohr 18005cbae88SAndreas Gohr $img = $http->post('http://ditaa.org/ditaa/render',$pass); 18105cbae88SAndreas Gohr if(!$img) return false; 18205cbae88SAndreas Gohr 18395615d6cSAndreas Gohr return io_saveFile($out,$img); 18405cbae88SAndreas Gohr } 18505cbae88SAndreas Gohr 186a34ed36bSDennis Ploeger /** 1872a956e66SAndreas Gohr * Run the ditaa Java program 188a34ed36bSDennis Ploeger */ 18905cbae88SAndreas Gohr function _run($data,$in,$out) { 1902a956e66SAndreas Gohr global $conf; 191a34ed36bSDennis Ploeger 19205cbae88SAndreas Gohr if(!file_exists($in)){ 19305cbae88SAndreas Gohr if($conf['debug']){ 19405cbae88SAndreas Gohr dbglog($in,'no such ditaa input file'); 19505cbae88SAndreas Gohr } 19605cbae88SAndreas Gohr return false; 19705cbae88SAndreas Gohr } 198a34ed36bSDennis Ploeger 1992a956e66SAndreas Gohr $cmd = $this->getConf('java'); 20036721e14SAndreas Gohr $cmd .= ' -Djava.awt.headless=true -Dfile.encoding=UTF-8 -jar'; 2012a956e66SAndreas Gohr $cmd .= ' '.escapeshellarg(dirname(__FILE__).'/ditaa/ditaa0_9.jar'); //ditaa jar 20236721e14SAndreas Gohr $cmd .= ' --encoding UTF-8'; 20305cbae88SAndreas Gohr $cmd .= ' '.escapeshellarg($in); //input 20405cbae88SAndreas Gohr $cmd .= ' '.escapeshellarg($out); //output 2052a956e66SAndreas Gohr $cmd .= ' -s '.escapeshellarg($data['scale']); 2062a956e66SAndreas Gohr if(!$data['antialias']) $cmd .= ' -A'; 2072a956e66SAndreas Gohr if(!$data['shadow']) $cmd .= ' -S'; 2082a956e66SAndreas Gohr if($data['round']) $cmd .= ' -r'; 2092a956e66SAndreas Gohr if(!$data['edgesep']) $cmd .= ' -E'; 210a34ed36bSDennis Ploeger 211a34ed36bSDennis Ploeger exec($cmd, $output, $error); 212a34ed36bSDennis Ploeger 2133c74ed32SAndreas Gohr if ($error != 0){ 2143c74ed32SAndreas Gohr if($conf['debug']){ 2153c74ed32SAndreas Gohr dbglog(join("\n",$output),'ditaa command failed: '.$cmd); 2163c74ed32SAndreas Gohr } 2173c74ed32SAndreas Gohr return false; 2183c74ed32SAndreas Gohr } 21905cbae88SAndreas Gohr 220a34ed36bSDennis Ploeger return true; 221a34ed36bSDennis Ploeger } 222a34ed36bSDennis Ploeger 223a34ed36bSDennis Ploeger} 224a34ed36bSDennis Ploeger 225