Class: AsciidoctorExtensions::KrokiClient

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

Overview

Kroki client

Constant Summary collapse

SUPPORTED_HTTP_METHODS =
%w[get post adaptive].freeze
MIME_TYPES =

Maps a diagram output format to its expected MIME type, mirroring the JavaScript/Node.js extension's kroki-client.js. Used to detect a Kroki server response that doesn't match the requested format (e.g. a plain-text syntax error returned for what should be an SVG).

{
  'svg' => 'image/svg+xml',
  'png' => 'image/png',
  'jpg' => 'image/jpeg',
  'jpeg' => 'image/jpeg',
  'pdf' => 'application/pdf',
  'txt' => 'text/plain',
  'atxt' => 'text/plain',
  'utxt' => 'text/plain',
  'base64' => 'text/plain'
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts, logger = ::Asciidoctor::LoggerManager.logger) ⇒ KrokiClient

Returns a new instance of KrokiClient.



547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
# File 'lib/asciidoctor/extensions/asciidoctor_kroki/extension.rb', line 547

def initialize(opts, logger = ::Asciidoctor::LoggerManager.logger)
  @server_url = opts[:server_url]
  @max_uri_length = opts.fetch(:max_uri_length, 4000)
  @http_client = opts[:http_client]
  @source_location = opts[:source_location]
  @logger = logger
  method = opts.fetch(:http_method, 'adaptive').downcase
  if SUPPORTED_HTTP_METHODS.include?(method)
    @method = method
  else
    logger.warn message_with_context "Invalid value '#{method}' for kroki-http-method attribute. The value must be either: " \
                                     "'get', 'post' or 'adaptive'. Proceeding using: 'adaptive'.",
                                     source_location: @source_location
    @method = 'adaptive'
  end
end

Instance Attribute Details

#max_uri_lengthObject (readonly)

Returns the value of attribute max_uri_length.



528
529
530
# File 'lib/asciidoctor/extensions/asciidoctor_kroki/extension.rb', line 528

def max_uri_length
  @max_uri_length
end

#methodObject (readonly)

Returns the value of attribute method.



528
529
530
# File 'lib/asciidoctor/extensions/asciidoctor_kroki/extension.rb', line 528

def method
  @method
end

#server_urlObject (readonly)

Returns the value of attribute server_url.



528
529
530
# File 'lib/asciidoctor/extensions/asciidoctor_kroki/extension.rb', line 528

def server_url
  @server_url
end

Instance Method Details

#get_image(kroki_diagram, encoding) ⇒ Object



568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
# File 'lib/asciidoctor/extensions/asciidoctor_kroki/extension.rb', line 568

def get_image(kroki_diagram, encoding)
  type = kroki_diagram.type
  format = kroki_diagram.format
  text = kroki_diagram.text
  opts = kroki_diagram.opts
  expected_content_type = MIME_TYPES[format]
  if @method == 'adaptive' || @method == 'get'
    uri = kroki_diagram.get_diagram_uri(server_url)
    if uri.length > @max_uri_length
      # The request URI is longer than the max URI length.
      if @method == 'get'
        # The server may reject the request with a 414 (URI Too Long).
        @logger.warn message_with_context "The diagram URI length (#{uri.length}) exceeds kroki-max-uri-length (#{@max_uri_length}). " \
                                          'The server may reject the request with a 414 (URI Too Long). Consider using the ' \
                                          "'kroki-http-method' attribute set to 'adaptive' or 'post'.",
                                          source_location: @source_location
        @http_client.get(uri, opts, encoding, expected_content_type)
      else
        @http_client.post("#{@server_url}/#{type}/#{format}", text, opts, encoding, expected_content_type)
      end
    else
      @http_client.get(uri, opts, encoding, expected_content_type)
    end
  else
    @http_client.post("#{@server_url}/#{type}/#{format}", text, opts, encoding, expected_content_type)
  end
end

#text_content(kroki_diagram) ⇒ Object



564
565
566
# File 'lib/asciidoctor/extensions/asciidoctor_kroki/extension.rb', line 564

def text_content(kroki_diagram)
  get_image(kroki_diagram, 'utf-8')
end