Wiki source for GraphvizFormatter


Show raw source

===== GraphvizFormatter : Adds support of graphviz inline syntax =====

**Author : ** [[http://cendres.net | Tam Kien Duong]] - nicky@deveha.com - [[http://sans-cloison.net/DevGraphvizExtension | development's page]]

[[http://www.graphviz.org/ | Graphviz]] is a very nice tool to produce and generate graphs. It's maye be usefull to let user create directly graph to illustrate the content (or import figures from documents). This extension aims to provide a support of graphviz graph language description.


==== Requirements ====

- Functional installation of [[http://www.graphviz.org/ | Graphviz]]

==== Installation ====

- Copy the ##graphviz.php## file (code below) in the ##formatters## directory

If you're using URL Rewriting, don't forget to add a rule in your ##.htaccess## file.

%%
RewriteCond %{REQUEST_FILENAME} !uploads/.*$
%%

==== How to use it ====

~##""%%(graphviz)<br/>digraph G {Hello->World}<br/>%%""##

==== To Do ====

- Improve the code. It needs serious refactoring (object modelisation, etc)
- Cache generate images. Right now, the images are generate constantly what can causes some troubles.
- Delete unpublished graphs
- ACL Support (waiting the next Wikka version)

==== Live Test ====

Some live tests are available on the [[http://sans-cloison.net/DevGraphvizExtension | development's page]].

==== The Code ====

%%(php;1;graphviz.php)
<?php
#
# WikkaWiki Formatter to display graphviz-graphs
#
# $Id: $
#
# Copyright (C) 2007, Tam Kien Duong http://cendres.net
#
# This file is Free Software.
#
# It is licensed under the following three licenses as alternatives:
# 1. GNU Lesser General Public License (LGPL) V2.1 or any newer version
# 2. GNU General Public License (GPL) V2 or any newer version
# 3. Apache License, V2.0 or any newer version
#
# You may not use this file except in compliance with at least one of
# the above three licenses.
#
# This file is based on Sebastian Dietzold Graphviz extension for WackoWiki
# https://wiki.imise.uni-leipzig.de/Projects/WackoTuning?v=xii#h702-8

# if set to "" we test is the file in PATH
$GraphVizSettings['bindir'] = "";
$GraphVizSettings['filedir'] = $this->config["upload_path"];

# check, if there should be another tool than dot used
if (isset($options['neato'])) $bin = "neato";
elseif (isset($options['circo'])) $bin = "circo";
elseif (isset($options['twopi'])) $bin = "twopi";
elseif (isset($options['circo'])) $bin = "circo";
elseif (isset($options['fdp'])) $bin = "fdp";
else $bin = "dot";
$GraphVizSettings['bin'] = $GraphVizSettings['bindir'] . $bin;

# write graphviz code to temporary text-file
$tmpname = tempnam ($GraphVizSettings['filedir'], "temp");
$tmpfile = fopen($tmpname, "w");
fwrite($tmpfile, $text);
fclose($tmpfile);

### here starts the big code block ###

## step 1: convert the source-code to generated source code
$cmd = $GraphVizSettings['bin']." -Tdot -o $tmpname.dot $tmpname 2>&1";
$cmdOut = `{$cmd}`;
if ($cmdOut != "")
{
echo "<i>$cmdOut</i><br/>";
unlink($tmpname);
}
else
{
# extract the name of the graph (second string in first line)
$gname = file ("$tmpname.dot"); $gname = split(" ", $gname[0]); $gname = $gname[1];

# the basename is generated in the same way like the upload-handler do this
$fname = $GraphVizSettings['filedir'] .'/'.$this->page['tag'].'/'.$gname;

if(!file_exists($GraphVizSettings['filedir'] .'/'.$this->page['tag'])) mkdir($GraphVizSettings['filedir'] .'/'.$this->page['tag']);

## step 2: prepare and run the image command and put the image in the upload dir
$imagecmd = $GraphVizSettings['bin']." -Tpng -o $fname.png $tmpname.dot";
$cmdOut = `{$imagecmd}`;

$imgurl = $this->config["base_url"].$GraphVizSettings['filedir'] .'/'.$this->page['tag'].'/'.$gname.".png";
$imagesize = getimagesize ($fname.".png");

## step 3: prepare and run the map command
$mapcmd = $GraphVizSettings['bin']." -Tcmap $tmpname.dot";
$map = `{$mapcmd}`;
if ($map == "")
{
# output without no image map
echo "<img border=\"0\" src=\"$imgurl\">\n";
}
else
{
# output with image map
echo "<map name=\"$gname\">$map</map>";
echo "<img border=\"0\" usemap=\"#$gname\" src=\"$imgurl\">\n";
}

# clear the directory and vars
unset($cmdOut); # maybe this can cause trouble if un-unsetted
unlink($tmpname);
unlink($tmpname.".dot");
}
?>
%%

----
CategoryDevelopmentFormatters
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki