xref: /plugin/ditaa/syntax.php (revision c9ea14c6b9734e3626252228241f6a621e909591)
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>
8c5eb4ae0SGerry Weißbach * @author      Gerry Weißbach / i-net software <tools [at] inetsoftware [dot] de>
9*c9ea14c6Seinhirn * @author      Christian Marg <marg@rz.tu-clausthal.de>
10d402f512SAndreas Gohr * @author      Andreas Gohr <andi@splitbrain.org>
11a34ed36bSDennis Ploeger */
12a34ed36bSDennis Ploeger
13a34ed36bSDennis Ploegerif(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
14a34ed36bSDennis Ploegerif(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
15a34ed36bSDennis Ploegerrequire_once(DOKU_PLUGIN.'syntax.php');
16a34ed36bSDennis Ploeger
17a34ed36bSDennis Ploegerclass syntax_plugin_ditaa extends DokuWiki_Syntax_Plugin {
18a34ed36bSDennis Ploeger
19a34ed36bSDennis Ploeger    /**
20a34ed36bSDennis Ploeger     * What about paragraphs?
21a34ed36bSDennis Ploeger     */
22a34ed36bSDennis Ploeger    function getPType(){
23a34ed36bSDennis Ploeger        return 'normal';
24a34ed36bSDennis Ploeger    }
25a34ed36bSDennis Ploeger
26a34ed36bSDennis Ploeger    /**
27a34ed36bSDennis Ploeger     * What kind of syntax are we?
28a34ed36bSDennis Ploeger     */
29a34ed36bSDennis Ploeger    function getType(){
30a34ed36bSDennis Ploeger        return 'substition';
31a34ed36bSDennis Ploeger    }
32a34ed36bSDennis Ploeger
33a34ed36bSDennis Ploeger    /**
34a34ed36bSDennis Ploeger     * Where to sort in?
35a34ed36bSDennis Ploeger     */
36a34ed36bSDennis Ploeger    function getSort(){
37a34ed36bSDennis Ploeger        return 200;
38a34ed36bSDennis Ploeger    }
39a34ed36bSDennis Ploeger
40a34ed36bSDennis Ploeger    /**
4172a2bf1dSAndreas Gohr     * Connect pattern to lexer
42a34ed36bSDennis Ploeger     */
43a34ed36bSDennis Ploeger
44a34ed36bSDennis Ploeger    function connectTo($mode) {
4572a2bf1dSAndreas Gohr        $this->Lexer->addSpecialPattern('<ditaa.*?>\n.*?\n</ditaa>',$mode,'plugin_ditaa');
46a34ed36bSDennis Ploeger    }
47a34ed36bSDennis Ploeger
48a34ed36bSDennis Ploeger    /**
49a34ed36bSDennis Ploeger     * Handle the match
50a34ed36bSDennis Ploeger     */
5172a2bf1dSAndreas Gohr    function handle($match, $state, $pos, &$handler) {
522a956e66SAndreas Gohr        $info = $this->getInfo();
532a956e66SAndreas Gohr
5472a2bf1dSAndreas Gohr        // prepare default data
5572a2bf1dSAndreas Gohr        $return = array(
5672a2bf1dSAndreas Gohr                        'width'     => 0,
5772a2bf1dSAndreas Gohr                        'height'    => 0,
5872a2bf1dSAndreas Gohr                        'antialias' => true,
5972a2bf1dSAndreas Gohr                        'edgesep'   => true,
6072a2bf1dSAndreas Gohr                        'round'     => false,
6172a2bf1dSAndreas Gohr                        'shadow'    => true,
6272a2bf1dSAndreas Gohr                        'scale'     => 1,
6372a2bf1dSAndreas Gohr                        'align'     => '',
6438c92790SGerry Weißbach                        'version'   => $info['date'], //force rebuild of images on update
6572a2bf1dSAndreas Gohr                       );
66a34ed36bSDennis Ploeger
67a34ed36bSDennis Ploeger
6872a2bf1dSAndreas Gohr        // prepare input
6972a2bf1dSAndreas Gohr        $lines = explode("\n",$match);
7072a2bf1dSAndreas Gohr        $conf = array_shift($lines);
7172a2bf1dSAndreas Gohr        array_pop($lines);
7272a2bf1dSAndreas Gohr
7372a2bf1dSAndreas Gohr        // match config options
7472a2bf1dSAndreas Gohr        if(preg_match('/\b(left|center|right)\b/i',$conf,$match)) $return['align'] = $match[1];
7572a2bf1dSAndreas Gohr        if(preg_match('/\b(\d+)x(\d+)\b/',$conf,$match)){
7672a2bf1dSAndreas Gohr            $return['width']  = $match[1];
7772a2bf1dSAndreas Gohr            $return['height'] = $match[2];
78a34ed36bSDennis Ploeger        }
792a956e66SAndreas Gohr        if(preg_match('/\b(\d+(\.\d+)?)X\b/',$conf,$match)) $return['scale']  = $match[1];
8072a2bf1dSAndreas Gohr        if(preg_match('/\bwidth=([0-9]+)\b/i', $conf,$match)) $return['width'] = $match[1];
8172a2bf1dSAndreas Gohr        if(preg_match('/\bheight=([0-9]+)\b/i', $conf,$match)) $return['height'] = $match[1];
8272a2bf1dSAndreas Gohr        // match boolean toggles
8372a2bf1dSAndreas Gohr        if(preg_match_all('/\b(no)?(antialias|edgesep|round|shadow)\b/i',$conf,$matches,PREG_SET_ORDER)){
8472a2bf1dSAndreas Gohr            foreach($matches as $match){
8572a2bf1dSAndreas Gohr                $return[$match[2]] = ! $match[1];
8672a2bf1dSAndreas Gohr            }
8772a2bf1dSAndreas Gohr        }
8872a2bf1dSAndreas Gohr
8905cbae88SAndreas Gohr        $input = join("\n",$lines);
9005cbae88SAndreas Gohr        $return['md5'] = md5($input); // we only pass a hash around
9105cbae88SAndreas Gohr
9205cbae88SAndreas Gohr        // store input for later use
9305cbae88SAndreas Gohr        io_saveFile($this->_cachename($return,'txt'),$input);
9472a2bf1dSAndreas Gohr
9572a2bf1dSAndreas Gohr        return $return;
96a34ed36bSDennis Ploeger    }
97a34ed36bSDennis Ploeger
98a34ed36bSDennis Ploeger    /**
99c5eb4ae0SGerry Weißbach     * Prepares the Data that is used for the cache name
100c5eb4ae0SGerry Weißbach     * Width, height and scale are left out.
101c5eb4ae0SGerry Weißbach     * Ensures sanity.
102c5eb4ae0SGerry Weißbach     */
103c5eb4ae0SGerry Weißbach    function _prepareData($input)
104c5eb4ae0SGerry Weißbach    {
105c5eb4ae0SGerry Weißbach        $output = array();
106c5eb4ae0SGerry Weißbach        foreach( $input as $key => $value ) {
107c5eb4ae0SGerry Weißbach            switch ($key) {
108c5eb4ae0SGerry Weißbach                case 'scale':
109c5eb4ae0SGerry Weißbach                case 'antialias':
110c5eb4ae0SGerry Weißbach                case 'edgesep':
111c5eb4ae0SGerry Weißbach                case 'round':
112c5eb4ae0SGerry Weißbach                case 'shadow':
113*c9ea14c6Seinhirn                case 'md5':
114c5eb4ae0SGerry Weißbach                    $output[$key] = $value;
115c5eb4ae0SGerry Weißbach            };
116c5eb4ae0SGerry Weißbach        }
11738c92790SGerry Weißbach
11838c92790SGerry Weißbach        ksort($output);
11938c92790SGerry Weißbach        return $output;
120c5eb4ae0SGerry Weißbach    }
121c5eb4ae0SGerry Weißbach
122c5eb4ae0SGerry Weißbach    /**
12395615d6cSAndreas Gohr     * Cache file is based on parameters that influence the result image
12405cbae88SAndreas Gohr     */
12505cbae88SAndreas Gohr    function _cachename($data,$ext){
126c5eb4ae0SGerry Weißbach		$data = $this->_prepareData($data);
12705cbae88SAndreas Gohr        return getcachename(join('x',array_values($data)),'.ditaa.'.$ext);
12805cbae88SAndreas Gohr    }
12905cbae88SAndreas Gohr
13005cbae88SAndreas Gohr    /**
13195615d6cSAndreas Gohr     * Create output
13295615d6cSAndreas Gohr     */
13395615d6cSAndreas Gohr    function render($format, &$R, $data) {
13438c92790SGerry Weißbach        global $ID;
13595615d6cSAndreas Gohr        if($format == 'xhtml'){
13638c92790SGerry Weißbach
13738c92790SGerry Weißbach            // Only use the md5 key
13838c92790SGerry Weißbach            $img = ml($ID, array('ditaa' => $data['md5']));
13995615d6cSAndreas Gohr            $R->doc .= '<img src="'.$img.'" class="media'.$data['align'].'" alt=""';
14095615d6cSAndreas Gohr            if($data['width'])  $R->doc .= ' width="'.$data['width'].'"';
14195615d6cSAndreas Gohr            if($data['height']) $R->doc .= ' height="'.$data['height'].'"';
142c77baa73SWilli Schönborn            if($data['align'] == 'right') $R->doc .= ' align="right"';
143c77baa73SWilli Schönborn            if($data['align'] == 'left')  $R->doc .= ' align="left"';
14495615d6cSAndreas Gohr            $R->doc .= '/>';
14595615d6cSAndreas Gohr            return true;
14695615d6cSAndreas Gohr        }else if($format == 'odt'){
14795615d6cSAndreas Gohr            $src = $this->_imgfile($data);
14895615d6cSAndreas Gohr            $R->_odtAddImage($src,$data['width'],$data['height'],$data['align']);
14995615d6cSAndreas Gohr            return true;
15038c92790SGerry Weißbach        }else if($format == 'metadata'){
15138c92790SGerry Weißbach            // Save for later use
15238c92790SGerry Weißbach            $R->meta['ditaa'][$data['md5']] = $data;
15338c92790SGerry Weißbach            return true;
15495615d6cSAndreas Gohr        }
15595615d6cSAndreas Gohr        return false;
15695615d6cSAndreas Gohr    }
15795615d6cSAndreas Gohr
15895615d6cSAndreas Gohr
15995615d6cSAndreas Gohr    /**
16095615d6cSAndreas Gohr     * Return path to the rendered image on our local system
16195615d6cSAndreas Gohr     */
16238c92790SGerry Weißbach    function _imgfile($id, $data, $secondTry=false){
16338c92790SGerry Weißbach
16495615d6cSAndreas Gohr        $cache  = $this->_cachename($data,'png');
16595615d6cSAndreas Gohr
16695615d6cSAndreas Gohr        // create the file if needed
16795615d6cSAndreas Gohr        if(!file_exists($cache)){
16895615d6cSAndreas Gohr            $in = $this->_cachename($data,'txt');
16938c92790SGerry Weißbach            // If this is nt yet here, force geting instructions and writing the thing back.
17038c92790SGerry Weißbach            if ( $secondTry != true && !file_exists($in)) {
17138c92790SGerry Weißbach                p_get_instructions( io_readFile( wikiFN( $id) ) );
17238c92790SGerry Weißbach                return $this->_imgfile($id, $data, true);
17338c92790SGerry Weißbach            }
17438c92790SGerry Weißbach
17595615d6cSAndreas Gohr            if($this->getConf('java')){
17695615d6cSAndreas Gohr                $ok = $this->_run($data,$in,$cache);
17795615d6cSAndreas Gohr            }else{
17895615d6cSAndreas Gohr                $ok = $this->_remote($data,$in,$cache);
17995615d6cSAndreas Gohr            }
18095615d6cSAndreas Gohr            if(!$ok) return false;
18195615d6cSAndreas Gohr            clearstatcache();
18295615d6cSAndreas Gohr        }
18395615d6cSAndreas Gohr
18495615d6cSAndreas Gohr        // resized version
18595615d6cSAndreas Gohr        if($data['width']){
18695615d6cSAndreas Gohr            $cache = media_resize_image($cache,'png',$data['width'],$data['height']);
18795615d6cSAndreas Gohr        }
18895615d6cSAndreas Gohr
18995615d6cSAndreas Gohr        // something went wrong, we're missing the file
19095615d6cSAndreas Gohr        if(!file_exists($cache)) return false;
19195615d6cSAndreas Gohr
19295615d6cSAndreas Gohr        return $cache;
19395615d6cSAndreas Gohr    }
19495615d6cSAndreas Gohr
19595615d6cSAndreas Gohr    /**
19605cbae88SAndreas Gohr     * Render the output remotely at ditaa.org
19705cbae88SAndreas Gohr     */
19805cbae88SAndreas Gohr    function _remote($data,$in,$out){
19995615d6cSAndreas Gohr        if(!file_exists($in)){
20095615d6cSAndreas Gohr            if($conf['debug']){
20195615d6cSAndreas Gohr                dbglog($in,'no such ditaa input file');
20295615d6cSAndreas Gohr            }
20395615d6cSAndreas Gohr            return false;
20495615d6cSAndreas Gohr        }
20595615d6cSAndreas Gohr
20605cbae88SAndreas Gohr        $http = new DokuHTTPClient();
20705cbae88SAndreas Gohr        $http->timeout=30;
20805cbae88SAndreas Gohr
20905cbae88SAndreas Gohr        $pass = array();
21005cbae88SAndreas Gohr        $pass['scale']   = $data['scale'];
21105cbae88SAndreas Gohr        $pass['timeout'] = 25;
21205cbae88SAndreas Gohr        $pass['grid']    = io_readFile($in);
21305cbae88SAndreas Gohr        if(!$data['antialias']) $pass['A'] = 'on';
21405cbae88SAndreas Gohr        if(!$data['shadow'])    $pass['S'] = 'on';
21505cbae88SAndreas Gohr        if($data['round'])      $pass['r'] = 'on';
21605cbae88SAndreas Gohr        if(!$data['edgesep'])   $pass['E'] = 'on';
21705cbae88SAndreas Gohr
21805cbae88SAndreas Gohr        $img = $http->post('http://ditaa.org/ditaa/render',$pass);
21905cbae88SAndreas Gohr        if(!$img) return false;
22005cbae88SAndreas Gohr
22195615d6cSAndreas Gohr        return io_saveFile($out,$img);
22205cbae88SAndreas Gohr    }
22305cbae88SAndreas Gohr
224a34ed36bSDennis Ploeger    /**
2252a956e66SAndreas Gohr     * Run the ditaa Java program
226a34ed36bSDennis Ploeger     */
22705cbae88SAndreas Gohr    function _run($data,$in,$out) {
2282a956e66SAndreas Gohr        global $conf;
229a34ed36bSDennis Ploeger
23005cbae88SAndreas Gohr        if(!file_exists($in)){
23105cbae88SAndreas Gohr            if($conf['debug']){
23205cbae88SAndreas Gohr                dbglog($in,'no such ditaa input file');
23305cbae88SAndreas Gohr            }
23405cbae88SAndreas Gohr            return false;
23505cbae88SAndreas Gohr        }
236a34ed36bSDennis Ploeger
2372a956e66SAndreas Gohr        $cmd  = $this->getConf('java');
23836721e14SAndreas Gohr        $cmd .= ' -Djava.awt.headless=true -Dfile.encoding=UTF-8 -jar';
2392a956e66SAndreas Gohr        $cmd .= ' '.escapeshellarg(dirname(__FILE__).'/ditaa/ditaa0_9.jar'); //ditaa jar
24036721e14SAndreas Gohr        $cmd .= ' --encoding UTF-8';
24105cbae88SAndreas Gohr        $cmd .= ' '.escapeshellarg($in); //input
24205cbae88SAndreas Gohr        $cmd .= ' '.escapeshellarg($out); //output
2432a956e66SAndreas Gohr        $cmd .= ' -s '.escapeshellarg($data['scale']);
2442a956e66SAndreas Gohr        if(!$data['antialias']) $cmd .= ' -A';
2452a956e66SAndreas Gohr        if(!$data['shadow'])    $cmd .= ' -S';
2462a956e66SAndreas Gohr        if($data['round'])      $cmd .= ' -r';
2472a956e66SAndreas Gohr        if(!$data['edgesep'])   $cmd .= ' -E';
248a34ed36bSDennis Ploeger
249a34ed36bSDennis Ploeger        exec($cmd, $output, $error);
250a34ed36bSDennis Ploeger
2513c74ed32SAndreas Gohr        if ($error != 0){
2523c74ed32SAndreas Gohr            if($conf['debug']){
2533c74ed32SAndreas Gohr                dbglog(join("\n",$output),'ditaa command failed: '.$cmd);
2543c74ed32SAndreas Gohr            }
2553c74ed32SAndreas Gohr            return false;
2563c74ed32SAndreas Gohr        }
25705cbae88SAndreas Gohr
258a34ed36bSDennis Ploeger        return true;
259a34ed36bSDennis Ploeger    }
260a34ed36bSDennis Ploeger
261a34ed36bSDennis Ploeger}
262a34ed36bSDennis Ploeger
263