DokuWiki

It's better when it's simple

User Tools

Site Tools


plugin:format

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
plugin:format [2008-03-09 14:58] garlicbreadplugin:format [2023-12-21 19:04] (current) – new download url Aleksandr
Line 1: Line 1:
 +====== Format Plugin ======
  
 +---- plugin ----
 +description: Sends text to external programs and embeds the resulting media in a wiki page (can replace the basic functionality of the graphviz and gnuplot plugins, for example)
 +author     : Jason Grout
 +email      : jason-doku@creativetrax.com
 +type       : syntax
 +lastupdate : 2007-02-22
 +compatible : 2006-11-06
 +depends    : 
 +conflicts 
 +similar    : graphviz, gnuplot, latex
 +tags       : media, embed, program
 +
 +downloadurl: https://trello.com/1/cards/5b06532f1c1e9f8328a46f14/attachments/5b065620db48ea763a40a986/download/format-2007-02-22.zip
 +bugtracker : 
 +sourcerepo : 
 +donationurl: 
 +
 +screenshot_img: 
 +----
 +
 +===== Description =====
 +
 +This plugin allows you to send text to an external program and display the resulting media (image, pdf, text, etc.) in the current wiki page.
 +
 +==== DISCLAIMER ====
 +
 +This plugin can be **very** dangerous, as it allows anyone who can edit a page on your wiki to potentially execute arbitrary code on your web server.  Use the plugin at your own risk.  You may only use the plugin with the understanding that I will not be liable for anything that you do with it.
 +
 +Personally, I am not using the gnuplot configuration noted below, since gnuplot gives the wiki user the ability to execute arbitrary code on the server.  There is apparently no easy way around this.  I am only using "safe" versions of other programs that disallow any file access and any system access to the user.
 +
 +The plugin takes precautions in that it does not put any user input on the command line.  Instead, all user input is loaded in a file, wrapped in the 'pre' and 'post' text specified in the configuration file.  The resulting file is then made available through the ''@INPUTFILE@'' variable for the command line.
 +
 +==== Syntax ====
 +
 +After setting up the external programs in the ''conf/default.php'' file, you can call an external program like:
 +
 +<code>
 +<format program-name>
 +text to feed into the program
 +</format>
 +</code>
 +
 +==== Configuration settings ====
 +
 +All of the interesting setup happens in the ''conf/default.php'' file. For each program to be called, add a block to the ''conf/default.php file'' that is similar to:
 +
 +<code php>
 +$conf['<program-name>']=array('name' => "<user name>",
 +                    '<mode>' => array('ext' => "<extension>",
 +                                     'pre' => "<pre>",
 +                                     'post' => "<post>",
 +                                     'command' => "<command line>" ));
 +
 +</code>
 +
 +The various fields are:
 +
 +^ %%<program-name>%% | The name of the program, as it will be used in the ''%%<format program-name>%%'' syntax |
 +^ %%<user name>%% | The user-friendly name of the program |
 +^ %%<mode>%% | The mode these particular commands apply to. You can run different programs based on the rendering pass of the page.  For example, you can return some text in the 'metadata' mode, but render an image in XHTML mode, and render a PDFimage in 'latex' mode. |
 +^ %%<extension>%% | The extension of the file that will be generated. This will also determine the mime type of the generated media |
 +^ %%<pre>%% | Text to go before the text the user wrote in the wiki page. You can use this to set parameters in the program or otherwise set up the environment of the program. |
 +^ %%<post>%% | Text to go after the text the user wrote in the wiki page. You can use this to clean up variables, generate pictures, etc. |
 +^ %%<command line>%% | The command to run via the PHP system call. There are two variables that can be substituted in the command line. ''@INPUTFILE@'' refers to the file containing, in order, the ''%%<pre>%%'' text, the text the user wrote in the wiki, and the ''%%<post>%%'' text. ''@MEDIAFILE@'' contains the path and name of the file you should put your result into.  There's actually another variable substituted (''@OUTFILE@''), but that's experimental only.  |
 +
 +Here is an example ''conf/default.php'' file:
 +
 +<code php>
 +<?php 
 +$conf['tmpdir']="/tmp";
 +
 +$conf['dpic']=array('name' => "PIC diagram",
 +   'xhtml' => array('ext' => 'png',
 +                    'pre' => ".PS\n",
 +                    'post' => "\n.PE\n",
 +                    'command' => '/usr/local/bin/dpic-safe -r @INFILE@ | /usr/local/bin/convert -density 200 - png:- > @MEDIAFILE@'));
 +
 +foreach (array('dot','neato','twopi','circo','fdp') as $key => $prog) {
 +  $conf[$prog]=array('name' => "Graph",
 +     'xhtml' => array('ext' => 'png',
 +                      'pre' => "",
 +                      'post' => "",
 +                      'command' => 'SERVER_NAME=xxx GV_FILE_PATH="" /usr/local/bin/'.$prog.' -Tpng -o @MEDIAFILE@ @INFILE@'));
 +}
 +
 +$conf['gnuplot']=array('name' => 'Plot',
 +   'xhtml' => array('ext' => 'png',
 +                    'pre' => "set terminal PNG transparent nocrop enhanced size 420,320\nset output '@MEDIAFILE@'",
 +// if you are using a version of gnuplot supporting the OLD PNG library
 +// this line has to be
 +//                    'pre' => "set terminal PNG transparent picsize 420 320\nset output '@MEDIAFILE@'",
 +                    'post' => "\nexit\n",
 +                    'command' => 'cat @INFILE@ | /usr/local/bin/gnuplot'));
 +
 +$conf['graph']=array('name' => 'GLE Graph',
 +   'xhtml' => array('ext' => 'png',
 +                    'pre' => "",
 +                    'post' => "",
 +                    'command' => '/usr/local/bin/gle -safemode -r 200 -d png -output @MEDIAFILE@ @INFILE@ '));
 +</code>
 +
 +(the SERVERNAME and GV_FILE_PATH variables are set to help with security---see [[http://www.graphviz.org/doc/info/command.html]] )
 +
 +With this setup (assuming that you have all of the right programs in place), you could use gnuplot in a wiki page like this:
 +
 +<code>
 +<format gnuplot>
 +plot [-20:20] sin(x)/x
 +</format>
 +</code>
 +and the block of code is replaced by the graph generated by gnuplot.
 +
 +You could also use the GNU PIC implementation [[http://www.ece.uwaterloo.ca/~aplevich/dpic/|dpic]] to generate diagrams, like this:
 +
 +<code>
 +<format dpic>
 +box "start"
 +arrow 2 "process" above
 +circle "end"
 +</format>
 +</code>
 +and the resulting diagram would show up on the page (see the Demo below for this example).
 +
 +===== File storage =====
 +
 +The generated media files are stored in the %%data/media/format/<program-name>/%% folder (the %%:format:<program-name>:%% media namespace).  Permissions are not checked on this directory.  If someone wants to make a patch to check permissions, feel free to send it to me.  I'm thinking it would be nice to check permissions on the %%:format:<program-name>:%% namespace so that administrators can easily disable an external program for certain users/groups.
 +
 +===== Thanks =====
 +
 +This plugin is a modification and generalization of the existing graphviz and gnuplot plugins.  Special thanks goes to the authors of those plugins.
 +
 +===== Demo =====
 +
 +I've put a short page up demonstrating this plugin at [[http://math.byu.edu/~grout/software/dokuwiki/format-plugin]]. 
 +
 +I've put an other short page up demonstrating this plugin generating sequence diagrams. Look at [[http://astade.homeip.net/wiki/doku.php?id=howto:trace2uml_in_dokuwiki|my Wiki page]].
 +
 +This demonstrates how to use it with Trace2UML [[http://trace2uml.tigris.org]]
 +
 +<code php>
 +'command' => 'xvfb-run /usr/bin/Trace2UML -i @INFILE@ -o @MEDIAFILE@'))
 +</code>
 +
 +
 +===== Discussion =====
 +
 +Some things had to be changed
 +
 +On my machine, compiling dpic in safe mode and renaming it to dpic-safe had the consequence that it would not
 +produce output when called by PHP. Therefore, the command had to be
 +
 +<code php>
 +                    'command' => '/usr/local/bin/dpic -r @INFILE@ | /usr/local/bin/convert -density 200 - png:- > @MEDIAFILE@'));
 +</code>
 +instead of
 +<code php>
 +                    'command' => '/usr/local/bin/dpic-safe -r @INFILE@ | /usr/local/bin/convert -density 200 - png:- > @MEDIAFILE@'));
 +</code>
 +
 +dpic still is compiled for safe mode, so security is not broken.
 +
 +gnuplot has a problem also, because how gnuplot handles PNG output depends on the libraries available at compile time.
 +
 +It the command
 +<code php>
 +                    'pre' => "set terminal PNG transparent nocrop enhanced size 420,320\nset output '@MEDIAFILE@'",
 +</code>
 +does not work, changing the prologue for the older version of PNG like this
 +<code php>
 +                    'pre' => "set terminal PNG transparent picsize 420 320\nset output '@MEDIAFILE@'",
 +</code>
 +can help.
 +Finally, for all the graphviz programs to work, the command had to be changed to
 +
 +<code php>
 +                      'command' => 'SERVER_NAME=xxx GV_FILE_PATH="" /usr/local/bin/'.$prog.' -Tpng -o @MEDIAFILE@ @INFILE@'));
 +</code>
 +from
 +<code php>
 +                      'command' => 'SERVER_NAME=xxx GV_FILE_PATH="" /usr/local/bin/'.$prog.' -Tpng -o @MEDIAFILE@'));
 +</code>
 +
 +\\
 +I've recently created a patch against the 2007-02-22 version\\
 +[[http://homepage.ntlworld.com/garlicbread/images/web/projects/dokuwiki_web/format.diff|patch]]\\
 +this puts the output image files into %%data/media/auto/<plugin name>/<program-name>/<Namespace>/<Pagename>/%%\\
 +(I've added "auto" to the path to try and keep any files auto-generated from other plugins all in one location)\\
 +also on the first invocation of header() for a given page, it will remove any existing files within this directory\\ 
 +(just the directory/namespace of the page being loaded) as a sort of cleanup of old files\\
 +probably not the best way to do things, but it does work, and avoids lots of old image files lying around when making small changes to the diagrams\\
 +
 +Also I've put up an example of altering the formatting of the diagram for gnuplot / graphviz (for example with dark themes), from within the default.php\\
 +[[http://homepage.ntlworld.com/garlicbread/pages/web/projects/dokuwiki_web/plugin_setup.html|example page]]\\
 + --- //[[garlicbready@googlemail.com|Richard Westwell]] 2008-03-09 14:48//
 +
 +\\
 +For Windows (at least) installations, I found some changes were necessary; in particular, the @MEDIAFILE@ and @INFILE@ items in the default.php had to be quoted. (Otherwise, the c:\Program Files\... path would cause problems, among others.)\\
 +\\
 +Here's my working mscgen section from default.php.  (mscgen is c:\usr\bin\mscgen.exe)\\
 +<code>
 +  /*  mscgen setup */
 +  foreach (array('mscgen') as $key => $prog) {
 +    $conf[$prog]=array('name' => "MSCGen",
 +     'xhtml' => array('ext' => 'png',
 +           'pre' => "msc{",
 +           'post' => "}",
 +           'command' => '/usr/bin/'.$prog.' -Tpng -o "@MEDIAFILE@" "@INFILE@"'));
 +  }
 +</code>
 + --- // bchartrand, 2010-02-25//
 +\\
 +I have just completed a comment for the [[plugin:chain]] plugin that will apply wiki formatting to syntax text. Since these two plugins are very similar, it should work here too. If you've been interested in bringing in external wiki-formatted text, check it out.\\
 +- Jared Meeker (jaredm@tni.com)  25Aug2010

Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Share Alike 4.0 International
CC Attribution-Share Alike 4.0 International Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki