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
end

Instance Method Details

#process(parent, target, attrs) ⇒ Asciidoctor::AbstractBlock

Processes the diagram block or block macro by converting it into an image or literal block.

Parameters:

  • parent (Asciidoctor::AbstractBlock)

    the parent asciidoc block of the block or block macro being processed

  • target (String)

    the target value of a block macro

  • attrs (Hash)

    the attributes of the block or block macro

Returns:

  • (Asciidoctor::AbstractBlock)

    a new block that replaces the original block or block macro



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/asciidoctor/extensions/asciidoctor_kroki/extension.rb', line 62

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(parent, 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