class Asciidoctor::Diagram::FileSource

A diagram source that retrieves the code for a diagram from an external source file.

Constants

BOM_BYTES_UTF_16BE
BOM_BYTES_UTF_16LE
BOM_BYTES_UTF_8

Raw binary strings for UTF-* Byte Order Marks

Public Class Methods

new(block_processor, parent_block, file_name, attributes) click to toggle source
Calls superclass method Asciidoctor::Diagram::BasicSource::new
# File lib/asciidoctor-diagram/diagram_source.rb, line 280
def initialize(block_processor, parent_block, file_name, attributes)
  super(block_processor, parent_block, attributes)
  @file_name = File.expand_path(file_name)
end

Public Instance Methods

base_dir() click to toggle source
# File lib/asciidoctor-diagram/diagram_source.rb, line 285
def base_dir
  if @file_name
    File.dirname(@file_name)
  else
    super
  end
end
image_name() click to toggle source
# File lib/asciidoctor-diagram/diagram_source.rb, line 293
def image_name
  if @attributes['target']
    super
  elsif @file_name
    File.basename(@file_name, File.extname(@file_name))
  else
    checksum
  end
end
load_code() click to toggle source
# File lib/asciidoctor-diagram/diagram_source.rb, line 307
def load_code
  if @file_name
    lines = prepare_source_array(File.read(@file_name, :mode => 'rb'))
    @parent_block.apply_subs(lines, resolve_diagram_subs).join("\n")
  else
    ''
  end
end
should_process?(image_file, image_metadata) click to toggle source
# File lib/asciidoctor-diagram/diagram_source.rb, line 303
def should_process?(image_file, image_metadata)
  (@file_name && File.mtime(@file_name) > File.mtime(image_file)) || super
end

Private Instance Methods

prepare_source_array(data) click to toggle source

Prepare the source data for parsing.

Encodes the data to UTF-8 and removes any trailing whitespace from every line.

data - the source data to prepare

returns a String Array of prepared lines

# File lib/asciidoctor-diagram/diagram_source.rb, line 331
def prepare_source_array data
  return [] if data.empty?

  if data.start_with?(BOM_BYTES_UTF_16LE)
    utf8_data = data.byteslice(2, data.bytesize).encode(::Encoding::UTF_8, ::Encoding::UTF_16LE)
  elsif data.start_with?(BOM_BYTES_UTF_16BE)
    utf8_data = data.byteslice(2, data.bytesize).encode(::Encoding::UTF_8, ::Encoding::UTF_16BE)
  elsif data.start_with?(BOM_BYTES_UTF_8)
    utf8_data = data.byteslice(3, data.bytesize).force_encoding(::Encoding::UTF_8)
  else
    utf8_data = data.force_encoding(::Encoding::UTF_8)
  end

  utf8_data.lines.map {|line| line.rstrip}
end