Module: Asciidoctor::Diagram::Extensions::DiagramProcessor

Included in:
DiagramBlockMacroProcessor, DiagramBlockProcessor
Defined in:
lib/asciidoctor-diagram/extensions.rb

Overview

Mixin that provides the basic machinery for image generation. When this module is included it will include the FormatRegistry into the singleton class of the target class.

Constant Summary collapse

IMAGE_PARAMS =
{
    :svg => {
        :encoding => Encoding::UTF_8,
        :decoder => SVG
    },
    :png => {
        :encoding => Encoding::ASCII_8BIT,
        :decoder => PNG
    }
}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(mod) ⇒ Object



73
74
75
76
77
# File 'lib/asciidoctor-diagram/extensions.rb', line 73

def self.included(mod)
  class << mod
    include FormatRegistry
  end
end

Instance Method Details

#process(parent, reader_or_target, attributes) ⇒ 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

  • reader_or_target (Asciidoctor::Reader, String)

    a reader that provides the contents of a block or the target value of a block macro

  • attributes (Hash)

    the attributes of the block or block macro

Returns:

  • (Asciidoctor::AbstractBlock)

    a new block that replaces the original block or block macro



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/asciidoctor-diagram/extensions.rb', line 86

def process(parent, reader_or_target, attributes)
  source = create_source(parent, reader_or_target, attributes.dup)

  format = source.attributes.delete('format') || self.class.default_format
  format = format.to_sym if format.respond_to?(:to_sym)

  raise "Format undefined" unless format

  generator_info = self.class.formats[format]

  raise "#{self.class.name} does not support output format #{format}" unless generator_info

  begin
    case generator_info[:type]
      when :literal
        create_literal_block(parent, source, generator_info)
      else
        create_image_block(parent, source, format, generator_info)
    end
  rescue => e
    text = "Failed to generate image: #{e.message}"
    warn %(asciidoctor-diagram: ERROR: #{text})
    text << "\n"
    text << source.code
    Asciidoctor::Block.new parent, :listing, :source => text, :attributes => attributes
  end
end