Module: Ditaa

Defined in:
lib/ditaarb.rb,
lib/ditaarb/version.rb

Constant Summary collapse

VERSION =
"0.1.1"

Class Method Summary collapse

Class Method Details

.render(ascii_art, options = {}) ⇒ String

Returns processed image.

Parameters:

  • ascii_art (String)

    original ascii diagram to render as image.

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :antialiasing (Boolean) — default: true

    Turns anti-aliasing on/off.

  • :debug (Boolean) — default: false

    Renders the debug grid over the resulting image.

  • :separation (Boolean) — default: true

    Prevents the separation of common edges of shapes.

  • :rounded_corneres (Boolean) — default: false

    Causes all corners to be rendered as round.

  • :scale (Numeric) — default: 1.0

    A natural number that determines the size of the rendered image. The units are fractions of the default size (2.5 renders 1.5 times bigger than the default).

  • :shadows (Boolean) — default: true

    Turns shadows on/off.

  • :tabs (Boolean) — default: false

    Tabs are normally interpreted as 8 spaces but it is possible to change that using this option. It is not advisable to use tabs in your diagrams.

Returns:

  • (String)

    processed image



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/ditaarb.rb', line 20

def self.render(ascii_art, options = {})
  ditaa_jar  = File.expand_path(File.dirname(__FILE__) + '/../vendor/ditaa0_9.jar')
  ditaa_jar << ' -A' if options[:antialiasing]
  ditaa_jar << ' -d' if options[:debug]
  ditaa_jar << ' -E' if options[:separation] == false
  ditaa_jar << ' -r' if options[:rounded_corneres]
  ditaa_jar << " -s #{options[:scale]}" if options[:scale]
  ditaa_jar << ' -S' if options[:shadows] == false
  ditaa_jar << ' -t' if options[:tabs]

  input_file = Tempfile.new('ditaa.input')
  input_file.write(ascii_art)
  input_file.flush

  output_file = Tempfile.new('ditaa.output')

  pid = Process.spawn("java -jar #{ditaa_jar} -v #{input_file.path} #{output_file.path}", [:err, :out] => '/dev/null')
  Process.wait(pid)
  File.read(output_file.path)
ensure
  input_file.close!
  output_file.close!
end