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
    },
    :gif => {
        :encoding => Encoding::ASCII_8BIT,
        :decoder => GIF
    },
    :png => {
        :encoding => Encoding::ASCII_8BIT,
        :decoder => PNG
    }
}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(mod) ⇒ Object



80
81
82
83
84
# File 'lib/asciidoctor-diagram/extensions.rb', line 80

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



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/asciidoctor-diagram/extensions.rb', line 93

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
    case parent.attr('diagram-on-error') || 'log'
      when 'abort'
        raise e
      else
        text = "Failed to generate image: #{e.message}"
        warn_msg = text.dup
        if $VERBOSE
          warn_msg << "\n" << e.backtrace.join("\n")
        end
        warn %(asciidoctor-diagram: ERROR: #{warn_msg})
        text << "\n"
        text << source.code
        Asciidoctor::Block.new parent, :listing, :source => text, :attributes => attributes
    end

  end
end