ndl / wiki_external_filter

Redmine plugin which allows defining macros that process macro argument using external filter program and render its result in wiki.
http://www.ndl.kiev.ua/content/redmine-wiki-external-filter-plugin
Other
36 stars 35 forks source link

Support of Ditaa diagrams #6

Open ebrahim opened 13 years ago

ebrahim commented 13 years ago

An external filter for Ditaa would be nice.

jperville commented 13 years ago

I integrated ditaa with this plugin quite easily. The main difficulty was to convince ditaa to work in "pipe" mode instead of creating files on the filesystem. To achieve that I wrote the following python wrapper:

#!/usr/bin/python

"Wraps the ditaa diagram rendering tool to work in piped mode." 

from tempfile import mkdtemp
from shutil import rmtree
from os.path import join
from os import system
from sys import stdin, exit

if __name__ == "__main__":
    tmpdir, ret = mkdtemp(prefix='ditaa'), 1
    try:
        infile, outfile = join(tmpdir, "in.ditaa"), join(tmpdir, "out.png")
        f = open(infile, "wb")
        f.write(stdin.read())
        f.close()
        ret = system("/usr/bin/ditaa %s %s 1>&2" % (infile, outfile))
        if ret == 0:
            print open(outfile, "rb").read()
    finally:
        rmtree(tmpdir)
    exit(ret)

Save the above program as /usr/local/bin/ditaa-pipe and make it executable. Now edit the global config/wiki_external_filter.yml file to declare a ditaa macro in your environment.

  ditaa:
    description: "Constructs diagram image from its textual description in ditaa language, see http://ditaa.sourceforge.net/"
    template: image
    outputs:
      - command: "/usr/local/bin/ditaa-pipe"
        content_type: "image/png"

Restart redmine and enjoy!

To test, simply invoke the "ditaa" macro with the ditaa source as macro argument, like this:

{{ditaa(
/----\ /----\
|c33F| |cC02|
|    | |    |
\----/ \----/

/----\ /----\
|c1FF| |c1AB|
|    | |    |
\----/ \----/
)}}

Feel free to reuse my source (or better: integrate it into the official plugin).

ebrahim commented 13 years ago

Good job. Just a tiny bug: ditaa-pipe may fail if it is run twice simultaneously, because of similar output file name of out.png shared between two Ditaa processes. Here is my own ditaa-pipe shell script:

#!/bin/bash

base=`mktemp`
cat >"$base.ditaa"
ditaa "$base.ditaa" "$base.png" >/dev/null
cat "$base.png"
rm -f "$base.ditaa" "$base.png"
jperville commented 13 years ago

Hello ebrahim and thank you for your contribution. I used "mkdtemp" in my script to be sure that my work files are safe ; even if the basenames are fixed all files are created in a protected directory. Happy diagramming with ditaa in redmine!

ebrahim commented 13 years ago

Oops! You're right.