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