Class: AsciidoctorExtensions::KrokiBlockMacroProcessor

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

Overview

A block macro extension that converts a diagram into an image.

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of KrokiBlockMacroProcessor.

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)



51
52
53
54
# File 'lib/asciidoctor/extensions/asciidoctor_kroki/extension.rb', line 51

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

Instance Method Details

#process(parent, target, attrs) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/asciidoctor/extensions/asciidoctor_kroki/extension.rb', line 56

def process(parent, target, attrs)
  diagram_type = @name
  target = parent.apply_subs(target, [:attributes])

  unless read_allowed?(target)
    link = create_inline(parent, :anchor, target, type: :link, target: target)
    return create_block(parent, :paragraph, link.convert, {}, content_model: :raw)
  end

  unless (path = resolve_target_path(target))
    logger.error message_with_context "#{diagram_type} block macro not found: #{target}.", source_location: parent.document.reader.cursor_at_mark
    return create_block(parent, 'paragraph', unresolved_block_macro_message(diagram_type, target), {})
  end

  begin
    diagram_text = read(path)
  rescue => e # rubocop:disable Style/RescueStandardError
    logger.error message_with_context "Failed to read #{diagram_type} file: #{path}. #{e}.", source_location: parent.document.reader.cursor_at_mark
    return create_block(parent, 'paragraph', unresolved_block_macro_message(diagram_type, path), {})
  end
  KrokiProcessor.process(self, parent, attrs, diagram_type, diagram_text, @logger)
end