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.



361
362
363
364
365
366
367
# File 'lib/asciidoctor/extensions/asciidoctor_kroki/extension.rb', line 361

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.



359
360
361
# File 'lib/asciidoctor/extensions/asciidoctor_kroki/extension.rb', line 359

def format
  @format
end

#optsObject (readonly)

Returns the value of attribute opts.



359
360
361
# File 'lib/asciidoctor/extensions/asciidoctor_kroki/extension.rb', line 359

def opts
  @opts
end

#targetObject (readonly)

Returns the value of attribute target.



359
360
361
# File 'lib/asciidoctor/extensions/asciidoctor_kroki/extension.rb', line 359

def target
  @target
end

#textObject (readonly)

Returns the value of attribute text.



359
360
361
# File 'lib/asciidoctor/extensions/asciidoctor_kroki/extension.rb', line 359

def text
  @text
end

#typeObject (readonly)

Returns the value of attribute type.



359
360
361
# File 'lib/asciidoctor/extensions/asciidoctor_kroki/extension.rb', line 359

def type
  @type
end

Instance Method Details

#encodeObject



374
375
376
# File 'lib/asciidoctor/extensions/asciidoctor_kroki/extension.rb', line 374

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

#get_diagram_uri(server_url) ⇒ Object



369
370
371
372
# File 'lib/asciidoctor/extensions/asciidoctor_kroki/extension.rb', line 369

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



381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
# File 'lib/asciidoctor/extensions/asciidoctor_kroki/extension.rb', line 381

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