Class: AsciidoctorExtensions::KrokiProcessor

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

Overview

Internal processor

Constant Summary collapse

TEXT_FORMATS =
%w[txt atxt utxt].freeze
BUILTIN_ATTRIBUTES =
%w[target width height format fallback link float align role caption title cloaked-context subs].freeze
PLANTUML_TYPES =
%i[plantuml c4plantuml].freeze

Class Method Summary collapse

Class Method Details

.process(processor, parent, attrs, diagram_type, diagram_text, logger, resource_path: nil) ⇒ Object

rubocop:disable Metrics/AbcSize, Metrics/PerceivedComplexity



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/asciidoctor/extensions/asciidoctor_kroki/extension.rb', line 185

def process(processor, parent, attrs, diagram_type, diagram_text, logger, resource_path: nil)
  doc = parent.document
  diagram_text = prepend_plantuml_config(diagram_text, diagram_type, doc, logger)
  diagram_text = preprocess_plantuml_includes(diagram_text, diagram_type, doc, resource_path, logger)
  # If "subs" attribute is specified, substitute accordingly.
  # Be careful not to specify "specialcharacters" or your diagram code won't be valid anymore!
  if (subs = attrs['subs'])
    diagram_text = parent.apply_subs(diagram_text, parent.resolve_subs(subs))
  end
  attrs.delete('opts')
  # Apply the option defined on the block/macro or, as a fallback, the document-wide kroki-default-options.
  if (option = get_option(attrs, doc)) && option != 'none'
    attrs["#{option}-option"] = ''
  end
  format = get_format(doc, attrs, diagram_type)
  attrs['role'] = get_role(format, attrs['role'])
  attrs['format'] = format
  opts = attrs.filter { |key, _| key.is_a?(String) && BUILTIN_ATTRIBUTES.none? { |k| key == k } && !key.end_with?('-option') }
  kroki_diagram = KrokiDiagram.new(diagram_type, format, diagram_text, attrs['target'], opts)
  kroki_client = KrokiClient.new({
                                   server_url: server_url(doc),
                                   http_method: http_method(doc),
                                   max_uri_length: max_uri_length(doc),
                                   source_location: doc.reader.cursor_at_mark,
                                   http_client: KrokiHttpClient
                                 }, logger)
  alt = get_alt(attrs)
  title = attrs.delete('title')
  caption = attrs.delete('caption')
  if TEXT_FORMATS.include?(format)
    text_content = kroki_client.text_content(kroki_diagram)
    block = processor.create_block(parent, 'literal', text_content, attrs)
  else
    attrs['alt'] = alt
    apply_image_src(attrs, create_image_src(doc, kroki_diagram, kroki_client, logger, inline: option == 'inline'))
    block = processor.create_image_block(parent, attrs)
  end
  block.title = title if title
  block.assign_caption(caption, 'figure')
  block
end