Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support of Ditaa diagrams #6

Open
ebrahim opened this issue May 16, 2011 · 4 comments
Open

Support of Ditaa diagrams #6

ebrahim opened this issue May 16, 2011 · 4 comments

Comments

@ebrahim
Copy link

ebrahim commented May 16, 2011

An external filter for Ditaa would be nice.

@jperville
Copy link

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
Copy link
Author

ebrahim commented Jun 7, 2011

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
Copy link

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
Copy link
Author

ebrahim commented Jun 7, 2011

Oops! You're right.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants