Skip to content
Browse files

streamlined code with ditaa plugin

ODT should work with remote rendered images now
  • Loading branch information...
1 parent eb42ed8 commit fb9a529df399e2fa9f312b28a234fffb8da4cfc5 @splitbrain committed Nov 15, 2010
Showing with 78 additions and 61 deletions.
  1. +11 −14 img.php
  2. +1 −1 plugin.info.txt
  3. +66 −46 syntax.php
View
25 img.php
@@ -1,29 +1,18 @@
<?php
/**
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
- * @author Andreas Gohr <gohr@cosmocode.de>
+ * @author Andreas Gohr <andi@splitbrain.org>
*/
if(!defined('DOKU_INC')) define('DOKU_INC',dirname(__FILE__).'/../../../');
define('NOSESSION',true);
require_once(DOKU_INC.'inc/init.php');
-require_once(DOKU_INC.'inc/pageutils.php');
-require_once(DOKU_INC.'inc/httputils.php');
-require_once(DOKU_INC.'inc/io.php');
-// let the plugin do the work
+// let the syntax plugin do the work
$data = $_REQUEST;
$plugin = plugin_load('syntax','graphviz');
$cache = $plugin->_imgfile($data);
-
-
-// something went wrong, we're missing the file
-if(!file_exists($cache)){
- header("HTTP/1.0 404 Not Found");
- header('Content-Type: image/png');
- echo io_readFile('broken.png',false);
- exit;
-}
+if(!$cache) _fail();
header('Content-Type: image/png;');
header('Expires: '.gmdate("D, d M Y H:i:s", time()+max($conf['cachetime'], 3600)).' GMT');
@@ -32,4 +21,12 @@
http_conditionalRequest($time);
echo io_readFile($cache,false);
+
+function _fail(){
+ header("HTTP/1.0 404 Not Found");
+ header('Content-Type: image/png');
+ echo io_readFile('broken.png',false);
+ exit;
+}
+
//Setup VIM: ex: et ts=4 enc=utf-8 :
View
2 plugin.info.txt
@@ -1,7 +1,7 @@
base graphviz
author Andreas Gohr
email andi@splitbrain.org
-date 2010-08-21
+date 2010-11-15
name Graphviz Plugin
desc Create graph images from Graphviz dot language
url http://www.dokuwiki.org/plugin:graphviz
View
112 syntax.php
@@ -7,8 +7,8 @@
* @author Andreas Gohr <andi@splitbrain.org>
*/
+
if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
-require_once(DOKU_INC.'inc/init.php');
if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
require_once(DOKU_PLUGIN.'syntax.php');
@@ -32,7 +32,7 @@ function getType(){
* Where to sort in?
*/
function getSort(){
- return 100;
+ return 200;
}
/**
@@ -50,7 +50,6 @@ function handle($match, $state, $pos, &$handler) {
// prepare default data
$return = array(
- 'data' => '',
'width' => 0,
'height' => 0,
'layout' => 'dot',
@@ -75,19 +74,32 @@ function handle($match, $state, $pos, &$handler) {
if(preg_match('/\bwidth=([0-9]+)\b/i', $conf,$match)) $return['width'] = $match[1];
if(preg_match('/\bheight=([0-9]+)\b/i', $conf,$match)) $return['height'] = $match[1];
- $return['data'] = join("\n",$lines);
+
+ $input = join("\n",$lines);
+ $return['md5'] = md5($input); // we only pass a hash around
+
+ // store input for later use
+ io_saveFile($this->_cachename($return,'txt'),$input);
return $return;
}
/**
+ * Cache file is based on parameters that influence the result image
+ */
+ function _cachename($data,$ext){
+ unset($data['width']);
+ unset($data['height']);
+ unset($data['align']);
+ return getcachename(join('x',array_values($data)),'.graphviz.'.$ext);
+ }
+
+ /**
* Create output
- *
- * @todo latex support?
*/
function render($format, &$R, $data) {
if($format == 'xhtml'){
- $img = $this->_imgurl($data);
+ $img = DOKU_BASE.'lib/plugins/graphviz/img.php?'.buildURLparams($data);
$R->doc .= '<img src="'.$img.'" class="media'.$data['align'].'" alt=""';
if($data['width']) $R->doc .= ' width="'.$data['width'].'"';
if($data['height']) $R->doc .= ' height="'.$data['height'].'"';
@@ -104,70 +116,78 @@ function render($format, &$R, $data) {
}
/**
- * Build the image URL using either our own generator or
- * the Google Chart API
+ * Return path to the rendered image on our local system
*/
- function _imgurl($data){
- if($this->getConf('path')){
- // run graphviz on our own server
- $img = DOKU_BASE.'lib/plugins/graphviz/img.php?'.buildURLparams($data,'&');
- }else{
- // go through google
- $pass = array(
- 'cht' => 'gv:'.$data['layout'],
- 'chl' => $data['data'],
- );
- if($data['width'] && $data['height']){
- $pass['chs'] = $data['width'].'x'.$data['height'];
+ function _imgfile($data){
+ $cache = $this->_cachename($data,'png');
+
+ // create the file if needed
+ if(!file_exists($cache)){
+ $in = $this->_cachename($data,'txt');
+ if($this->getConf('path')){
+ $ok = $this->_run($data,$in,$cache);
+ }else{
+ $ok = $this->_remote($data,$in,$cache);
}
+ if(!$ok) return false;
+ clearstatcache();
+ }
- $img = 'http://chart.apis.google.com/chart?'.buildURLparams($pass,'&');
- $img = ml($img,array('w'=>$data['width'],'h'=>$data['height']));
+ // resized version
+ if($data['width']){
+ $cache = media_resize_image($cache,'png',$data['width'],$data['height']);
}
- return $img;
+
+ // something went wrong, we're missing the file
+ if(!file_exists($cache)) return false;
+
+ return $cache;
}
/**
- * Return path to created graphviz graph (local only)
+ * Render the output remotely at google
*/
- function _imgfile($data){
- $w = (int) $data['width'];
- $h = (int) $data['height'];
- unset($data['width']);
- unset($data['height']);
- unset($data['align']);
+ function _remote($data,$in,$out){
+ if(!file_exists($in)){
+ if($conf['debug']){
+ dbglog($in,'no such graphviz input file');
+ }
+ return false;
+ }
- $cache = getcachename(join('x',array_values($data)),'graphviz.png');
+ $http = new DokuHTTPClient();
+ $http->timeout=30;
- // create the file if needed
- if(!file_exists($cache)){
- $this->_run($data,$cache);
- clearstatcache();
- }
+ $pass = array();
+ $pass['cht'] = 'gv:'.$data['layout'];
+ $pass['chl'] = io_readFile($in);
- // resized version
- if($w) $cache = media_resize_image($cache,'png',$w,$h);
+ $img = $http->post('http://chart.apis.google.com/chart',$pass,'&');
+ if(!$img) return false;
- return $cache;
+ return io_saveFile($out,$img);
}
/**
* Run the graphviz program
*/
- function _run($data,$cache) {
+ function _run($data,$in,$out) {
global $conf;
- $temp = tempnam($conf['tmpdir'],'graphviz_');
- io_saveFile($temp,$data['data']);
+ if(!file_exists($in)){
+ if($conf['debug']){
+ dbglog($in,'no such graphviz input file');
+ }
+ return false;
+ }
$cmd = $this->getConf('path');
$cmd .= ' -Tpng';
$cmd .= ' -K'.$data['layout'];
- $cmd .= ' -o'.escapeshellarg($cache); //output
- $cmd .= ' '.escapeshellarg($temp); //input
+ $cmd .= ' -o'.escapeshellarg($out); //output
+ $cmd .= ' '.escapeshellarg($in); //input
exec($cmd, $output, $error);
- @unlink($temp);
if ($error != 0){
if($conf['debug']){

0 comments on commit fb9a529

Please sign in to comment.
Something went wrong with that request. Please try again.