1<?php 2/** 3 * graphviz-Plugin: Parses graphviz-blocks 4 * 5 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 6 * @author Carl-Christian Salvesen <calle@ioslo.net> 7 * @version 0.1.20050525 8 */ 9 10if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/'); 11require_once(DOKU_INC.'inc/init.php'); 12if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/'); 13require_once(DOKU_PLUGIN.'syntax.php'); 14 15 16/** 17 * All DokuWiki plugins to extend the parser/rendering mechanism 18 * need to inherit from this class 19 */ 20class syntax_plugin_graphviz extends DokuWiki_Syntax_Plugin { 21 22 23 function getInfo(){ 24 return array( 25 'author' => 'Carl-Christian Salvesen', 26 'email' => 'calle@ioslo.net', 27 'date' => '2007-02-11', 28 'name' => 'graphviz Plugin', 29 'desc' => 'Parses graphviz-blocks', 30 'url' => 'http://wiki.ioslo.net/dokuwiki/graphviz', 31 ); 32 } 33 34 /** 35 * What kind of syntax are we? 36 */ 37 function getType(){ 38 return 'protected'; 39 } 40 41 /** 42 * Where to sort in? 43 */ 44 function getSort(){ 45 return 100; 46 } 47 48 49 /** 50 * Connect pattern to lexer 51 */ 52 function connectTo($mode) { 53 $this->Lexer->addEntryPattern('<graphviz(?=.*\x3C/graphviz\x3E)',$mode,'plugin_graphviz'); 54 } 55 56 function postConnect() { 57 $this->Lexer->addExitPattern('</graphviz>','plugin_graphviz'); 58 } 59 60 /** 61 * Handle the match 62 */ 63 64 65 function handle($match, $state, $pos) { 66 if ( $state == DOKU_LEXER_UNMATCHED ) { 67 $matches = preg_split('/>/u',$match,2); 68 $matches[0] = trim($matches[0]); 69 if ( trim($matches[0]) == '' ) { 70 $matches[0] = NULL; 71 } 72 return array($matches[1],$matches[0]); 73 } 74 return TRUE; 75 } 76 /** 77 * Create output 78 */ 79 function render($mode, &$renderer, $data) { 80 global $conf; 81 if($mode == 'xhtml' && strlen($data[0]) > 1) { 82 if ( !is_dir($conf['mediadir'] . '/graphviz') ) 83 io_mkdir_p($conf['mediadir'] . '/graphviz'); //Using dokuwiki framework 84 $hash = md5(serialize($data)); 85 $filename = $conf['mediadir'] . '/graphviz/'.$hash.'.png'; 86 $url = ml('graphviz:'.$hash.'.png'); //Using dokuwiki framework 87 88 if ( is_readable($filename) ) { 89 // cached. 90 $renderer->doc .= '<img src="'.$url.'" class="media" title="Graph" alt="Graph" />'; 91 // $renderer->doc .= $renderer->internalmedialink('graphviz:'.$hash.'.png'); 92 return true; 93 } 94 95 if (!$this->createImage($filename, $data[0], $data[1])) { 96 $renderer->doc .= '<img src="'.$url.'" class="media" title="Graph" alt="Graph" /> '; 97 // $renderer->doc .= $renderer->internalmedialink('graphviz:'.$hash.'.png'); 98 } else { 99 $renderer->doc .= '**ERROR RENDERING GRAPHVIZ**'; 100 } 101 return true; 102 } 103 elseif($mode == 'odt' && strlen($data[0])>1){ 104 list($state, $datae) = $data; 105 106 $hash = md5(serialize($data)); 107 $imfilename=$conf['mediadir'] . '/graphviz/'.$hash.'.png'; 108 109 if (is_readable($filename)){ 110 // Content 111 $renderer->p_close(); 112 $renderer->doc .= '<text:p text:style-name="Table_20_Contents">'; 113 $renderer->_odtAddImage($imfilename); 114 $renderer->doc .= '</text:p>'; 115 $renderer->p_open(); 116 } 117 elseif (!$this->createImage($imfilename, $data[0], $data[1])) { 118 // Content 119 $renderer->p_close(); 120 121 $renderer->doc .= '<text:p text:style-name="Table_20_Contents">'; 122 $renderer->_odtAddImage($imfilename); 123 $renderer->doc .= '</text:p>'; 124 $renderer->p_open(); 125 } 126 else{ 127 $renderer->doc .= "UNABLE TO ADD GRAPHVIZ GRAPH"; 128 } 129 return true; 130 } 131 elseif($mode == 'latex' && strlen($data[0]) > 1) { //Latex mode for dokuTeXit 132 global $TeXitImage_glob; 133 global $_dokutexit_conf; 134 $hash = md5(serialize($data)); 135 if (isset($_dokutexit_conf) && $_dokutexit_conf['mode'] == 'pdflatex') { 136 $filename = $conf['mediadir'] . '/graphviz/'.$hash.'.png'; 137 } else { 138 $filename = $conf['mediadir'] . '/graphviz/'.$hash.'.ps'; 139 } 140 //Saving filename for zipfile 141 $TeXitImage_glob['plugin_list'][$hash] = $filename; 142 if (is_readable($filename) ) { 143 // cached. 144 $renderer->doc .= "\\begin{figure}[h]\n"; 145 $renderer->doc .= "\t\\begin{center}\n"; 146 $renderer->doc .= "\t\t\\includegraphics{"; 147 $renderer->doc .= $filename; 148 $renderer->doc .= "}\n"; 149 $renderer->doc .= "\t\\end{center}\n"; 150 $renderer->doc .= "\\end{figure}\n"; 151 return true; 152 } 153 if (!$this->createImageLatex($filename, $data[0], $data[1])) { 154 $renderer->doc .= "\\begin{figure}[h]\n"; 155 $renderer->doc .= "\t\\begin{center}\n"; 156 $renderer->doc .= "\t\t\\includegraphics{"; 157 $renderer->doc .= $filename; 158 $renderer->doc .= "}\n"; 159 $renderer->doc .= "\t\\end{center}\n"; 160 $renderer->doc .= "\\end{figure}\n"; 161 } else { 162 $renderer->putent('**ERROR RENDERING GRAPHVIZ**'); 163 } 164 return true; 165 } 166 return false; 167 } 168 169 function createImageLatex($filename, &$data, $graphcmd='dot') { //Latex mode have better rendering with ps 170 if (isset($_dokutexit_conf) && $_dokutexit_conf['mode'] == 'pdflatex') { 171 return $this->createImage($filename, $data, $graphcmd); 172 } 173 $cmds = array('dot','neato','twopi','circo','fdp'); 174 if ( !in_array($graphcmd, $cmds) ) $graphcmd = 'dot'; 175 176 $tmpfname = tempnam("/tmp", "dokuwiki.graphviz"); 177 io_saveFile($tmpfname, $data); 178 $retval = exec('/usr/bin/'.$graphcmd.' -Gsize="5,4" -Tps ' . 179 $tmpfname.' -o '. $filename); 180 unlink($tmpfname); 181 return $retval; 182 } 183 184 function createImage($filename, &$data, $graphcmd='dot') { 185 186 $cmds = array('dot','neato','twopi','circo','fdp'); 187 if ( !in_array($graphcmd, $cmds) ) $graphcmd = 'dot'; 188 189 $tmpfname = tempnam("/tmp", "dokuwiki.graphviz"); 190 io_saveFile($tmpfname, $data); //Using dokuwiki framework 191 // file_put_contents($tmpfname, $data); 192 //$retval = exec('/usr/bin/'.$graphcmd.' -Tps '.$tmpfname.'|/usr/bin/convert ps:- png:'.$filename); 193 // Comment out the line over this and uncomment the line below to NOT use ImageMagick for antialiazing. 194 // Comment out the line over this and uncomment the line below to NOT use ImageMagick for antialiazing. 195 196 197 $retval = exec('/usr/bin/'.$graphcmd.' -Tpng -o '.$filename .' '.$tmpfname); 198 /* WINDOWS VERSION */ 199 // change $tmpfname = tempnam("C:\temp", "dokuwiki.graphviz"); 200 //change $retval = exec('C:\grapviz\bin\'.$graphcmd.' -Tpng -o '.$filename .' '.$tmpfname); 201 unlink($tmpfname); 202 return $retval; 203 } 204 205} 206 207?> 208