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
Byte arrays 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 243 def initialize(block_processor, parent_block, file_name, attributes) super(block_processor, parent_block, attributes) @file_name = file_name end
Public Instance Methods
base_dir()
click to toggle source
Calls superclass method
Asciidoctor::Diagram::DiagramSource#base_dir
# File lib/asciidoctor-diagram/diagram_source.rb, line 248 def base_dir if @file_name File.dirname(@file_name) else super end end
code()
click to toggle source
# File lib/asciidoctor-diagram/diagram_source.rb, line 270 def code @code ||= read_code end
image_name()
click to toggle source
Calls superclass method
Asciidoctor::Diagram::BasicSource#image_name
# File lib/asciidoctor-diagram/diagram_source.rb, line 256 def image_name if @attributes['target'] super elsif @file_name File.basename(@file_name, File.extname(@file_name)) else checksum end end
read_code()
click to toggle source
# File lib/asciidoctor-diagram/diagram_source.rb, line 274 def read_code if @file_name lines = File.readlines(@file_name) lines = prepare_source_array(lines) @parent_block.apply_subs(lines, resolve_diagram_subs).join("\n") else '' end end
should_process?(image_file, image_metadata)
click to toggle source
Calls superclass method
Asciidoctor::Diagram::BasicSource#should_process?
# File lib/asciidoctor-diagram/diagram_source.rb, line 266 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 Array for parsing.
Encodes the data to UTF-8, if necessary, and removes any trailing whitespace from every line.
If a BOM is found at the beginning of the data, a best attempt is made to encode it to UTF-8 from the specified source encoding.
data - the source data Array to prepare (no nil entries allowed)
returns a String Array of prepared lines
# File lib/asciidoctor-diagram/diagram_source.rb, line 302 def prepare_source_array data return [] if data.empty? if (leading_2_bytes = (leading_bytes = (first = data[0]).unpack 'C3').slice 0, 2) == BOM_BYTES_UTF_16LE data[0] = first.byteslice 2, first.bytesize # NOTE you can't split a UTF-16LE string using .lines when encoding is UTF-8; doing so will cause this line to fail return data.map {|line| (line.encode ::Encoding::UTF_8, ::Encoding::UTF_16LE).rstrip} elsif leading_2_bytes == BOM_BYTES_UTF_16BE data[0] = first.byteslice 2, first.bytesize return data.map {|line| (line.encode ::Encoding::UTF_8, ::Encoding::UTF_16BE).rstrip} elsif leading_bytes == BOM_BYTES_UTF_8 data[0] = first.byteslice 3, first.bytesize end if first.encoding == ::Encoding::UTF_8 data.map {|line| line.rstrip} else data.map {|line| (line.encode ::Encoding::UTF_8).rstrip} end end