Class: AsciidoctorExtensions::KrokiBlockProcessor

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

Overview

A block extension that converts a diagram into an image.

Instance Method Summary collapse

Constructor Details

#initialize(name = nil, config = {}) ⇒ KrokiBlockProcessor

Returns a new instance of KrokiBlockProcessor.

Parameters:

  • name (String) (defaults to: nil)

    name of the block macro (optional)

  • config (Hash) (defaults to: {})

    a config hash (optional)

    • :logger a logger used to log warning and errors (optional)


27
28
29
30
# File 'lib/asciidoctor/extensions/asciidoctor_kroki/extension.rb', line 27

def initialize(name = nil, config = {})
  @logger = (config || {}).delete(:logger) { ::Asciidoctor::LoggerManager.logger }
  super
end

Instance Method Details

#process(parent, reader, attrs) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/asciidoctor/extensions/asciidoctor_kroki/extension.rb', line 32

def process(parent, reader, attrs)
  diagram_type = @name
  role = attrs['role']
  source_location = reader.cursor
  diagram_text = reader.string
  KrokiProcessor.process(self, parent, attrs, diagram_type, diagram_text, @logger)
rescue => e # rubocop:disable Style/RescueStandardError
  # Matches the JavaScript/Node.js extension: a failure talking to the Kroki server (network
  # error, non-2xx response, unexpected content-type) shouldn't abort the whole document
  # conversion, so it's degraded to a warning and the raw diagram source is kept visible.
  logger.warn message_with_context "Skipping #{diagram_type} block: #{e.message}", source_location: source_location
  attrs['role'] = role ? "#{role} kroki-error" : 'kroki-error'
  create_block(parent, attrs['cloaked-context'], diagram_text, attrs)
end