Class: Asciidoctor::Diagram::PlantUmlConverter

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

Direct Known Subclasses

SaltConverter, UmlConverter

Constant Summary collapse

CLASSPATH_ENV =
Java.environment_variable('DIAGRAM_PLANTUML_CLASSPATH')
LIB_DIR =
File.expand_path('../..', File.dirname(__FILE__))
PLANTUML_JARS =
if CLASSPATH_ENV
  CLASSPATH_ENV.split(File::PATH_SEPARATOR)
else
  begin
    require 'asciidoctor-diagram/plantuml/classpath'
    ::Asciidoctor::Diagram::PlantUmlClasspath::JAR_FILES
  rescue LoadError
    nil
  end
end

Class Method Summary collapse

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

#native_scaling?

Class Method Details

.find_plantuml_native(source) ⇒ Object



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

def self.find_plantuml_native(source)
  source.find_command(
    'plantuml-full',
    :raise_on_error => false,
    :attrs => ['plantuml-native'],
    :alt_cmds => ['plantuml-headless']
  )
end

Instance Method Details

#add_common_args(args, source) ⇒ Object



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/asciidoctor-diagram/plantuml/converter.rb', line 195

def add_common_args(args, source)
  base_dir = File.expand_path(source.base_dir)
  args << '-filedir'
  args << base_dir

  config_file = source.attr('plantumlconfig', nil, true) || source.attr('config')
  if config_file
    args << '-config'
    args << File.expand_path(config_file, base_dir)
  end

  include_dir = source.attr('includedir')
  if include_dir
    args << "-Dplantuml.include.path=#{Platform.native_path(File.expand_path(include_dir, base_dir))}"
  end
end

#add_common_headers(headers, source) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/asciidoctor-diagram/plantuml/converter.rb', line 82

def add_common_headers(headers, source)
  base_dir = source.base_dir

  config_file = source.attr('plantumlconfig', nil, true) || source.attr('config')
  if config_file
    headers['X-PlantUML-Config'] = File.expand_path(config_file, base_dir)
  end

  headers['X-PlantUML-Basedir'] = Platform.native_path(File.expand_path(base_dir))

  include_dir = source.attr('includedir')
  if include_dir
    headers['X-PlantUML-IncludeDir'] = Platform.native_path(File.expand_path(include_dir, base_dir))
  end
end

#add_size_limit_header(headers, limit) ⇒ Object



102
103
104
# File 'lib/asciidoctor-diagram/plantuml/converter.rb', line 102

def add_size_limit_header(headers, limit)
  headers['X-PlantUML-SizeLimit'] = limit if limit
end

#add_theme_arg(args, theme) ⇒ Object



189
190
191
192
193
# File 'lib/asciidoctor-diagram/plantuml/converter.rb', line 189

def add_theme_arg(args, theme)
  if theme
    args << '-theme' << theme
  end
end

#add_theme_header(headers, theme) ⇒ Object



98
99
100
# File 'lib/asciidoctor-diagram/plantuml/converter.rb', line 98

def add_theme_header(headers, theme)
  headers['X-PlantUML-Theme'] = theme if theme
end

#collect_options(source) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/asciidoctor-diagram/plantuml/converter.rb', line 50

def collect_options(source)
  options = {
      :size_limit => source.attr('size-limit', '4096'),
  }

  options[:smetana] = true if source.opt('smetana')

  theme = source.attr('theme', nil)
  options[:theme] = theme if theme

  options[:debug] = true if source.opt('debug')

  options
end

#convert(source, format, options) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/asciidoctor-diagram/plantuml/converter.rb', line 69

def convert(source, format, options)
  plantuml_native = PlantUmlConverter.find_plantuml_native(source)
  if plantuml_native
    convert_native(plantuml_native, source, format, options)
  elsif PLANTUML_JARS
    convert_http(source, format, options)
  else
    raise "Could not load PlantUML. Either require 'asciidoctor-diagram-plantuml' " \
            "or specify the location of the PlantUML JAR(s) using the 'DIAGRAM_PLANTUML_CLASSPATH' environment variable. " \
            "Alternatively a PlantUML binary can be provided (plantuml-native in $PATH)."
  end
end

#convert_http(source, format, options) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/asciidoctor-diagram/plantuml/converter.rb', line 106

def convert_http(source, format, options)
  Java.load

  code = source.code

  case format
  when :png
    mime_type = 'image/png'
  when :svg
    mime_type = 'image/svg+xml'
  when :txt, :utxt
    mime_type = 'text/plain;charset=utf-8'
  when :atxt
    mime_type = 'text/plain'
  else
    raise "Unsupported format: #{format}"
  end

  headers = {
    'Accept' => mime_type
  }

  unless should_preprocess(source)
    add_common_headers(headers, source)
  end

  add_theme_header(headers, options[:theme])
  add_size_limit_header(headers, options[:size_limit])

  dot = source.find_command('dot', :alt_attrs => ['graphvizdot'], :raise_on_error => false)
  if options[:smetana] || !dot
    headers['X-Graphviz'] = 'smetana'
  else
    headers['X-Graphviz'] = ::Asciidoctor::Diagram::Platform.host_os_path(dot)
  end

  if options[:debug]
    headers['X-PlantUML-Debug'] = 'true'
  end

  response = Java.send_request(
    :url => '/plantuml',
    :body => code,
    :headers => headers
  )

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

  if response[:headers]['content-type'] =~ /multipart\/form-data;\s*boundary=(.*)/
    boundary = $1
    parts = {}

    multipart_data = StringIO.new(response[:body])
    while true
      multipart_data.readline
      marker = multipart_data.readline
      if marker.start_with? "--#{boundary}--"
        break
      elsif marker.start_with? "--#{boundary}"
        part = Java.parse_body(multipart_data)
        if part[:headers]['content-disposition'] =~ /form-data;\s*name="([^"]*)"/
          if $1 == 'image'
            parts[:result] = part[:body]
          else
            parts[:extra] ||= {}
            parts[:extra][$1] = part[:body]
          end
        else
          raise "Unexpected multipart content disposition"
        end
      else
        raise "Unexpected multipart boundary"
      end
    end

    parts
  else
    response[:body]
  end
end

#convert_native(plantuml, source, format, options) ⇒ Object



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/asciidoctor-diagram/plantuml/converter.rb', line 212

def convert_native(plantuml, source, format, options)
  code = source.code

  args = []
  env = {}

  args << case format
  when :png
    '-tpng'
  when :svg
    '-tsvg'
  when :txt, :utxt
    '-tutxt'
  when :atxt
    '-ttxt'
  else
    raise "Unsupported format: #{format}"
  end

  add_common_args(args, source)
  add_theme_arg(args, options[:theme])

  if options[:size_limit]
    env['PLANTUML_LIMIT_SIZE'] = options[:size_limit]
  end

  dot = source.find_command('dot', :alt_attrs => ['graphvizdot'], :raise_on_error => false)
  if options[:smetana] || !dot
    args << '-Playout=smetana'
  else
    args << '-graphvizdot'
    args << ::Asciidoctor::Diagram::Platform.host_os_path(dot)
  end

  generate_stdin_stdout(plantuml, code) do |tool, _|
    cmd = [tool]
    cmd << '-pipe'
    cmd << '-failfast2'
    cmd << '-stdrpt:1'
    cmd.concat args

    {
      :args => cmd,
      :env => env
    }
  end
end

#should_preprocess(source) ⇒ Object



65
66
67
# File 'lib/asciidoctor-diagram/plantuml/converter.rb', line 65

def should_preprocess(source)
  source.attr('preprocess', 'true') == 'true'
end

#supported_formatsObject



46
47
48
# File 'lib/asciidoctor-diagram/plantuml/converter.rb', line 46

def supported_formats
  [:png, :svg, :txt, :atxt, :utxt]
end

#wrap_source(source) ⇒ Object



42
43
44
# File 'lib/asciidoctor-diagram/plantuml/converter.rb', line 42

def wrap_source(source)
  PlantUMLPreprocessedSource.new(source, self)
end