Class: AsciidoctorExtensions::KrokiBlockMacroProcessor
- Inherits:
-
Asciidoctor::Extensions::BlockMacroProcessor
- Object
- Asciidoctor::Extensions::BlockMacroProcessor
- AsciidoctorExtensions::KrokiBlockMacroProcessor
- 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
-
#initialize(name = nil, config = {}) ⇒ KrokiBlockMacroProcessor
constructor
A new instance of KrokiBlockMacroProcessor.
-
#process(parent, target, attrs) ⇒ Asciidoctor::AbstractBlock
Processes the diagram block or block macro by converting it into an image or literal block.
Constructor Details
#initialize(name = nil, config = {}) ⇒ KrokiBlockMacroProcessor
Returns a new instance of KrokiBlockMacroProcessor.
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
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 "#{diagram_type} block macro not found: #{target}.", source_location: parent.document.reader.cursor_at_mark return create_block(parent, 'paragraph', (diagram_type, target), {}) end begin diagram_text = read(path) rescue => e # rubocop:disable Style/RescueStandardError logger.error "Failed to read #{diagram_type} file: #{path}. #{e}.", source_location: parent.document.reader.cursor_at_mark return create_block(parent, 'paragraph', (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 "Skipping #{diagram_type} block: #{e.}", source_location: parent.document.reader.cursor_at_mark attrs['role'] = role ? "#{role} kroki-error" : 'kroki-error' create_block(parent, 'paragraph', "#{e.} - #{diagram_type}::#{target}[]", attrs) end end |