Class: Asciidoctor::Diagram::SyntraxConverter

Inherits:
Object
  • Object
show all
Includes:
CliGenerator, DiagramConverter
Defined in:
lib/asciidoctor-diagram/syntrax/converter.rb

Constant Summary collapse

CLASSPATH_ENV =
Java.environment_variable('DIAGRAM_JSYNTRAX_CLASSPATH')
CLI_HOME_ENV =
Java.environment_variable('DIAGRAM_JSYNTRAX_HOME')
JSYNTRAX_JARS =
if CLASSPATH_ENV
  CLASSPATH_ENV.split(File::PATH_SEPARATOR)
elsif CLI_HOME_ENV
  lib_dir = File.expand_path('lib', CLI_HOME_ENV)
  Dir.children(lib_dir).select { |c| c.end_with? '.jar' }.map { |c| File.expand_path(c, lib_dir) }
else
  nil
end

Instance Method Summary collapse

Methods included from CliGenerator

#generate_file, #generate_file_stdout, #generate_stdin, #generate_stdin_file, #generate_stdin_stdout

Methods included from DiagramConverter

#wrap_source

Instance Method Details

#collect_options(source) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/asciidoctor-diagram/syntrax/converter.rb', line 32

def collect_options(source)
  {
      :heading => source.attr('heading'),
      :scale => source.attr('scale'),
      :transparent => source.attr('transparent'),
      :style => source.attr('style-file') || source.attr("#{source.diagram_type}-style", nil, true)
  }
end

#convert(source, format, options) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/asciidoctor-diagram/syntrax/converter.rb', line 41

def convert(source, format, options)
  shared_args = []
  title = options[:heading]
  if title
    shared_args << '--title' << title
  end

  scale = options[:scale]
  if scale
    shared_args << '--scale' << scale
  end

  transparent = options[:transparent]
  if transparent == 'true'
    shared_args << '--transparent'
  end
  style = options[:style]
  if style
    shared_args << '--style' << style
  end

  if JSYNTRAX_JARS
    Java.load

    options_string = shared_args.join(' ')

    case format
    when :png
      mime_type = 'image/png'
    when :svg
      mime_type = 'image/svg+xml'
    else
      raise "Unsupported format: #{format}"
    end

    headers = {
      'Accept' => mime_type,
      'X-Options' => options_string
    }

    response = Java.send_request(
      :url => '/syntrax',
      :body => source.to_s,
      :headers => headers
    )

    unless response[:code] == 200
      raise Java.create_error("JSyntrax code generation failed", response)
    end

    response[:body]
  else
    generate_file(source.find_command('syntrax'), 'spec', format.to_s, source.to_s) do |tool_path, input_path, output_path|
      {
        :args => ([tool_path, '-i', Platform.native_path(input_path), '-o', Platform.native_path(output_path)] + shared_args),
        :chdir => source.base_dir
      }
    end
  end

end

#native_scaling?Boolean

Returns:

  • (Boolean)


103
104
105
# File 'lib/asciidoctor-diagram/syntrax/converter.rb', line 103

def native_scaling?
  true
end

#supported_formatsObject



28
29
30
# File 'lib/asciidoctor-diagram/syntrax/converter.rb', line 28

def supported_formats
  [:png, :svg]
end