wiki_external_filter icon indicating copy to clipboard operation
wiki_external_filter copied to clipboard

Support of Ditaa diagrams

Open ebrahim opened this issue 13 years ago • 4 comments

An external filter for Ditaa would be nice.

ebrahim avatar May 16 '11 04:05 ebrahim

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).

jperville avatar Jun 07 '11 10:06 jperville

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"

ebrahim avatar Jun 07 '11 11:06 ebrahim

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!

jperville avatar Jun 07 '11 12:06 jperville

Oops! You're right.

ebrahim avatar Jun 07 '11 12:06 ebrahim