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)


64
65
66
67
# File 'lib/asciidoctor/extensions/asciidoctor_kroki/extension.rb', line 64

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.

rubocop:disable Metrics/AbcSize

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



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/asciidoctor/extensions/asciidoctor_kroki/extension.rb', line 76

def process(parent, target, attrs)
  diagram_type = @name
  role = attrs['role']
  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
  begin
    KrokiProcessor.process(self, parent, attrs, diagram_type, diagram_text, @logger, resource_path: path)
  rescue => e # rubocop:disable Style/RescueStandardError
    # Matches the JavaScript/Node.js extension: a failure talking to the Kroki server
    # shouldn't abort the whole document conversion, so it's degraded to a warning instead.
    logger.warn message_with_context "Skipping #{diagram_type} block: #{e.message}", source_location: parent.document.reader.cursor_at_mark
    attrs['role'] = role ? "#{role} kroki-error" : 'kroki-error'
    create_block(parent, 'paragraph', "#{e.message} - #{diagram_type}::#{target}[]", attrs)
  end
end