class AsciidoctorExtensions::KrokiBlockMacroProcessor

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

Attributes

logger[R]

Public Class Methods

new(name = nil, config = {}) click to toggle source

@param name [String] name of the block macro (optional) @param config [Hash] a config hash (optional)

- :logger a logger used to log warning and errors (optional)
Calls superclass method
# File lib/asciidoctor/extensions/asciidoctor_kroki/extension.rb, line 49
def initialize(name = nil, config = {})
  @logger = (config || {}).delete(:logger) { ::Asciidoctor::LoggerManager.logger }
  super(name, config)
end

Public Instance Methods

process(parent, target, attrs) click to toggle source
# File lib/asciidoctor/extensions/asciidoctor_kroki/extension.rb, line 54
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 "#{diagram_type} block macro not found: #{target}."
    create_block(parent, 'paragraph', unresolved_block_macro_message(diagram_type, target), {})
  end

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

Protected Instance Methods

read(target) click to toggle source
# File lib/asciidoctor/extensions/asciidoctor_kroki/extension.rb, line 89
def read(target)
  if target.start_with?('http://') || target.start_with?('https://')
    require 'open-uri'
    URI.open(target, &:read)
  else
    File.open(target, &:read)
  end
end
read_allowed?(_target) click to toggle source
# File lib/asciidoctor/extensions/asciidoctor_kroki/extension.rb, line 85
def read_allowed?(_target)
  true
end
resolve_target_path(target) click to toggle source
# File lib/asciidoctor/extensions/asciidoctor_kroki/extension.rb, line 81
def resolve_target_path(target)
  target
end
unresolved_block_macro_message(name, target) click to toggle source
# File lib/asciidoctor/extensions/asciidoctor_kroki/extension.rb, line 98
def unresolved_block_macro_message(name, target)
  "Unresolved block macro - #{name}::#{target}[]"
end