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.



402
403
404
405
406
407
408
# File 'lib/asciidoctor/extensions/asciidoctor_kroki/extension.rb', line 402

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.



400
401
402
# File 'lib/asciidoctor/extensions/asciidoctor_kroki/extension.rb', line 400

def format
  @format
end

#optsObject (readonly)

Returns the value of attribute opts.



400
401
402
# File 'lib/asciidoctor/extensions/asciidoctor_kroki/extension.rb', line 400

def opts
  @opts
end

#targetObject (readonly)

Returns the value of attribute target.



400
401
402
# File 'lib/asciidoctor/extensions/asciidoctor_kroki/extension.rb', line 400

def target
  @target
end

#textObject (readonly)

Returns the value of attribute text.



400
401
402
# File 'lib/asciidoctor/extensions/asciidoctor_kroki/extension.rb', line 400

def text
  @text
end

#typeObject (readonly)

Returns the value of attribute type.



400
401
402
# File 'lib/asciidoctor/extensions/asciidoctor_kroki/extension.rb', line 400

def type
  @type
end

Instance Method Details

#encodeObject



415
416
417
# File 'lib/asciidoctor/extensions/asciidoctor_kroki/extension.rb', line 415

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

#get_diagram_uri(server_url) ⇒ Object



410
411
412
413
# File 'lib/asciidoctor/extensions/asciidoctor_kroki/extension.rb', line 410

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, generated_files = nil, logger = nil, cache_dir: nil, cache_mode: { enabled: false, refresh: false }) ⇒ Object

Parameters:

  • cache_dir (String, nil) (defaults to: nil)

    persistent cache directory (see KrokiCache.resolve_cache_dir); required when cache_mode

  • cache_mode (Hash) (defaults to: { enabled: false, refresh: false })

    refresh: (see KrokiCache.resolve_cache_mode); disabled by default so callers that don't pass it (e.g. specs exercising #save directly) keep the pre-cache behaviour



422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
# File 'lib/asciidoctor/extensions/asciidoctor_kroki/extension.rb', line 422

def save(output_dir_path, kroki_client, generated_files = nil, logger = nil, cache_dir: nil, cache_mode: { enabled: false, refresh: false })
  diagram_url = get_diagram_uri(kroki_client.server_url)
  # An explicit name is used verbatim so links stay stable across content changes;
  # otherwise the name is content-addressed so anonymous diagrams don't collide (see #451).
  named = @target.is_a?(::String) && !@target.empty?
  diagram_name = named ? "#{@target}.#{@format}" : "diag-#{Digest::SHA256.hexdigest diagram_url}.#{@format}"
  file_path = File.join(output_dir_path, diagram_name)
  if named
    # A stable file may exist from a previous build with stale content, so it cannot be
    # trusted by name alone: go through fetch_diagram, which re-fetches only when the
    # persistent cache doesn't already have this exact content (see #90). Warn when the
    # same name is reused for a diagram with different content.
    warn_on_name_clash(generated_files, diagram_name, diagram_url, logger)
    generated_files[diagram_name] = diagram_url if generated_files
    fetch_and_write(output_dir_path, file_path, kroki_client, cache_dir, cache_mode)
  elsif !File.exist?(file_path)
    # Content-addressed name: an existing output file necessarily has identical content.
    fetch_and_write(output_dir_path, file_path, kroki_client, cache_dir, cache_mode)
  end
  diagram_name
end

#to_data_uri(kroki_client) ⇒ Object

Fetches this diagram from Kroki and returns it as a data: URI, embedding the content directly without writing any file (mirrors the JavaScript extension's fetch.js#toDataUri).



447
448
449
450
# File 'lib/asciidoctor/extensions/asciidoctor_kroki/extension.rb', line 447

def to_data_uri(kroki_client)
  contents = kroki_client.get_image(self, image_encoding)
  "data:#{media_type};base64,#{[contents].pack 'm0'}"
end