xref: /plugin/ditaa/syntax.php (revision 05cbae881d27bfa0a9b0e51091292504e3b0e34e)
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
87*05cbae88SAndreas Gohr        $input = join("\n",$lines);
88*05cbae88SAndreas Gohr        $return['md5'] = md5($input); // we only pass a hash around
89*05cbae88SAndreas Gohr
90*05cbae88SAndreas Gohr        // store input for later use
91*05cbae88SAndreas Gohr        io_saveFile($this->_cachename($return,'txt'),$input);
9272a2bf1dSAndreas Gohr
9372a2bf1dSAndreas Gohr        return $return;
94a34ed36bSDennis Ploeger    }
95a34ed36bSDennis Ploeger
96a34ed36bSDennis Ploeger    /**
97a34ed36bSDennis Ploeger     * Create output
98a34ed36bSDennis Ploeger     */
9972a2bf1dSAndreas Gohr    function render($format, &$R, $data) {
10072a2bf1dSAndreas Gohr        if($format != 'xhtml') return;
1012a956e66SAndreas Gohr        $img = DOKU_BASE.'lib/plugins/ditaa/ditaa.php?'.buildURLparams($data,'&');
102a34ed36bSDennis Ploeger
103b4142778SAndreas Gohr        $R->doc .= '<img src="'.$img.'" class="media'.$data['align'].'" alt=""';
104b4142778SAndreas Gohr        if($data['width'])  $R->doc .= ' width="'.$data['width'].'"';
105b4142778SAndreas Gohr        if($data['height']) $R->doc .= ' height="'.$data['height'].'"';
106b4142778SAndreas Gohr        if($data['align'] == 'right') $ret .= ' align="right"';
107b4142778SAndreas Gohr        if($data['align'] == 'left')  $ret .= ' align="left"';
108b4142778SAndreas Gohr        $R->doc .= '/>';
109a34ed36bSDennis Ploeger    }
110a34ed36bSDennis Ploeger
111*05cbae88SAndreas Gohr    /**
112*05cbae88SAndreas Gohr     * Cache file is based on data
113*05cbae88SAndreas Gohr     */
114*05cbae88SAndreas Gohr    function _cachename($data,$ext){
115*05cbae88SAndreas Gohr        unset($data['width']);
116*05cbae88SAndreas Gohr        unset($data['height']);
117*05cbae88SAndreas Gohr        unset($data['align']);
118*05cbae88SAndreas Gohr        return getcachename(join('x',array_values($data)),'.ditaa.'.$ext);
119*05cbae88SAndreas Gohr    }
120*05cbae88SAndreas Gohr
121*05cbae88SAndreas Gohr    /**
122*05cbae88SAndreas Gohr     * Render the output remotely at ditaa.org
123*05cbae88SAndreas Gohr     */
124*05cbae88SAndreas Gohr    function _remote($data,$in,$out){
125*05cbae88SAndreas Gohr        $http = new DokuHTTPClient();
126*05cbae88SAndreas Gohr        $http->timeout=30;
127*05cbae88SAndreas Gohr
128*05cbae88SAndreas Gohr        $pass = array();
129*05cbae88SAndreas Gohr        $pass['scale']   = $data['scale'];
130*05cbae88SAndreas Gohr        $pass['timeout'] = 25;
131*05cbae88SAndreas Gohr        $pass['grid']    = io_readFile($in);
132*05cbae88SAndreas Gohr        if(!$data['antialias']) $pass['A'] = 'on';
133*05cbae88SAndreas Gohr        if(!$data['shadow'])    $pass['S'] = 'on';
134*05cbae88SAndreas Gohr        if($data['round'])      $pass['r'] = 'on';
135*05cbae88SAndreas Gohr        if(!$data['edgesep'])   $pass['E'] = 'on';
136*05cbae88SAndreas Gohr
137*05cbae88SAndreas Gohr        $img = $http->post('http://ditaa.org/ditaa/render',$pass);
138*05cbae88SAndreas Gohr        if(!$img) return false;
139*05cbae88SAndreas Gohr
140*05cbae88SAndreas Gohr        io_saveFile($out,$img);
141*05cbae88SAndreas Gohr        return true;
142*05cbae88SAndreas Gohr    }
143*05cbae88SAndreas Gohr
1442a956e66SAndreas Gohr
145a34ed36bSDennis Ploeger    /**
1462a956e66SAndreas Gohr     * Run the ditaa Java program
147a34ed36bSDennis Ploeger     */
148*05cbae88SAndreas Gohr    function _run($data,$in,$out) {
1492a956e66SAndreas Gohr        global $conf;
150a34ed36bSDennis Ploeger
151*05cbae88SAndreas Gohr        if(!file_exists($in)){
152*05cbae88SAndreas Gohr            if($conf['debug']){
153*05cbae88SAndreas Gohr                dbglog($in,'no such ditaa input file');
154*05cbae88SAndreas Gohr            }
155*05cbae88SAndreas Gohr            return false;
156*05cbae88SAndreas Gohr        }
157a34ed36bSDennis Ploeger
1582a956e66SAndreas Gohr        $cmd  = $this->getConf('java');
15936721e14SAndreas Gohr        $cmd .= ' -Djava.awt.headless=true -Dfile.encoding=UTF-8 -jar';
1602a956e66SAndreas Gohr        $cmd .= ' '.escapeshellarg(dirname(__FILE__).'/ditaa/ditaa0_9.jar'); //ditaa jar
16136721e14SAndreas Gohr        $cmd .= ' --encoding UTF-8';
162*05cbae88SAndreas Gohr        $cmd .= ' '.escapeshellarg($in); //input
163*05cbae88SAndreas Gohr        $cmd .= ' '.escapeshellarg($out); //output
1642a956e66SAndreas Gohr        $cmd .= ' -s '.escapeshellarg($data['scale']);
1652a956e66SAndreas Gohr        if(!$data['antialias']) $cmd .= ' -A';
1662a956e66SAndreas Gohr        if(!$data['shadow'])    $cmd .= ' -S';
1672a956e66SAndreas Gohr        if($data['round'])      $cmd .= ' -r';
1682a956e66SAndreas Gohr        if(!$data['edgesep'])   $cmd .= ' -E';
169a34ed36bSDennis Ploeger
170a34ed36bSDennis Ploeger        exec($cmd, $output, $error);
171a34ed36bSDennis Ploeger
1723c74ed32SAndreas Gohr        if ($error != 0){
1733c74ed32SAndreas Gohr            if($conf['debug']){
1743c74ed32SAndreas Gohr                dbglog(join("\n",$output),'ditaa command failed: '.$cmd);
1753c74ed32SAndreas Gohr            }
1763c74ed32SAndreas Gohr            return false;
1773c74ed32SAndreas Gohr        }
178*05cbae88SAndreas Gohr
179a34ed36bSDennis Ploeger        return true;
180a34ed36bSDennis Ploeger    }
181a34ed36bSDennis Ploeger
182a34ed36bSDennis Ploeger}
183a34ed36bSDennis Ploeger
184