Module: Asciidoctor::Diagram::DiagramProcessor

Includes:
Logging
Included in:
DiagramBlockMacroProcessor, DiagramBlockProcessor, DiagramInlineMacroProcessor
Defined in:
lib/asciidoctor-diagram/diagram_processor.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.

Defined Under Namespace

Modules: ClassMethods

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
    },
    :pdf => {
        :encoding => Encoding::ASCII_8BIT,
        :decoder => PDF
    }
}
TEXT_FORMATS =
[:txt, :atxt, :utxt]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(host_class) ⇒ Object



29
30
31
32
# File 'lib/asciidoctor-diagram/diagram_processor.rb', line 29

def self.included(host_class)
  host_class.use_dsl
  host_class.extend(ClassMethods)
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



63
64
65
66
67
68
69
70
71
72
73
74
75
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/asciidoctor-diagram/diagram_processor.rb', line 63

def process(parent, reader_or_target, attributes)
  location = parent.document.reader.cursor_at_mark

  normalised_attributes = attributes.inject({}) { |h, (k, v)| h[normalise_attribute_name(k)] = v; h }
  pos_attr_index = config.fetch(:positional_attrs, []).length + 1
  until attributes[pos_attr_index].nil?
    normalised_attributes[attributes[pos_attr_index]] = 'true'
    pos_attr_index = pos_attr_index + 1
  end

  converter = config[:converter].new

  supported_formats = supported_formats(converter)

  source = create_source(parent, reader_or_target, normalised_attributes)
  # memorize current code here for error message to avoid calling wrapped source's code method later
  code = source.code

  begin
    source = converter.wrap_source(source)

    format = source.attributes.delete('format') || source.global_attr('format', supported_formats[0])
    format = format.to_sym if format.respond_to?(:to_sym)

    raise "Format undefined" unless format

    raise "#{self.class.name} does not support output format #{format}" unless supported_formats.include?(format)


    title = source.attributes.delete 'title'
    caption = source.attributes.delete 'caption'

    case format
    when *TEXT_FORMATS
      block = create_literal_block(parent, source, format, converter)
    else
      block = create_image_block(parent, source, format, converter)
    end

    block.title = title
    block.assign_caption(caption, 'figure')
    block
  rescue => e
    case source.global_attr('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

      logger.error message_with_context warn_msg, source_location: location

      text << "\n"
      text << code
      Asciidoctor::Block.new parent, :listing, :source => text, :attributes => attributes
    end

  end
end