Class: AsciidoctorExtensions::KrokiDiagram

Inherits:
Object
  • Object
show all
Defined in:
lib/asciidoctor/extensions/asciidoctor_kroki/extension.rb

Overview

Kroki diagram

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, format, text, target = nil, opts = {}) ⇒ KrokiDiagram

Returns a new instance of KrokiDiagram.



283
284
285
286
287
288
289
# File 'lib/asciidoctor/extensions/asciidoctor_kroki/extension.rb', line 283

def initialize(type, format, text, target = nil, opts = {})
  @text = text
  @type = type
  @format = format
  @target = target
  @opts = opts
end

Instance Attribute Details

#formatObject (readonly)

Returns the value of attribute format.



281
282
283
# File 'lib/asciidoctor/extensions/asciidoctor_kroki/extension.rb', line 281

def format
  @format
end

#optsObject (readonly)

Returns the value of attribute opts.



281
282
283
# File 'lib/asciidoctor/extensions/asciidoctor_kroki/extension.rb', line 281

def opts
  @opts
end

#targetObject (readonly)

Returns the value of attribute target.



281
282
283
# File 'lib/asciidoctor/extensions/asciidoctor_kroki/extension.rb', line 281

def target
  @target
end

#textObject (readonly)

Returns the value of attribute text.



281
282
283
# File 'lib/asciidoctor/extensions/asciidoctor_kroki/extension.rb', line 281

def text
  @text
end

#typeObject (readonly)

Returns the value of attribute type.



281
282
283
# File 'lib/asciidoctor/extensions/asciidoctor_kroki/extension.rb', line 281

def type
  @type
end

Instance Method Details

#encodeObject



296
297
298
# File 'lib/asciidoctor/extensions/asciidoctor_kroki/extension.rb', line 296

def encode
  ([Zlib::Deflate.deflate(@text, 9)].pack 'm0').tr '+/', '-_'
end

#get_diagram_uri(server_url) ⇒ Object



291
292
293
294
# File 'lib/asciidoctor/extensions/asciidoctor_kroki/extension.rb', line 291

def get_diagram_uri(server_url)
  query_params = opts.map { |k, v| "#{k}=#{_url_encode(v.to_s)}" }.join('&') unless opts.empty?
  _join_uri_segments(server_url, @type, @format, encode) + (query_params ? "?#{query_params}" : '')
end

#save(output_dir_path, kroki_client) ⇒ Object



300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
# File 'lib/asciidoctor/extensions/asciidoctor_kroki/extension.rb', line 300

def save(output_dir_path, kroki_client)
  diagram_url = get_diagram_uri(kroki_client.server_url)
  diagram_name = "#{@target || 'diag'}-#{Digest::SHA256.hexdigest diagram_url}.#{@format}"
  file_path = File.join(output_dir_path, diagram_name)
  encoding = case @format
             when 'txt', 'atxt', 'utxt', 'svg'
               'utf8'
             else
               'binary'
             end
  # file is either (already) on the file system or we should read it from Kroki
  unless File.exist?(file_path)
    contents = kroki_client.get_image(self, encoding)
    FileUtils.mkdir_p(output_dir_path)
    File.write(file_path, contents, mode: 'wb')
  end

  diagram_name
end