class Asciidoctor::AbstractNode
def generate_data_uri(target_image, asset_dir_key = nil)
the image is located (default: nil)
asset_dir_key - The String attribute key used to lookup the directory where
target_image - A String path to the target image
Base64. Finally, a data URI is built which can be used in an image tag.
to ancestor paths in the filesystem. The image data is then read and converted to
is set to at least SafeMode::SAFE (a condition which is true by default) to prevent access
First, and foremost, the target image path is cleaned if the document safe mode level
Public: Generate a data URI that can be used to embed an image in the output document
def generate_data_uri(target_image, asset_dir_key = nil) Helpers.require_library 'base64' ext = File.extname(target_image)[1..-1] mimetype = 'image/' + ext mimetype = "#{mimetype}+xml" if ext == 'svg' if asset_dir_key #asset_dir_path = normalize_system_path(@document.attr(asset_dir_key), nil, nil, :target_name => asset_dir_key) #image_path = normalize_system_path(target_image, asset_dir_path, nil, :target_name => 'image') image_path = normalize_system_path(target_image, @document.attr(asset_dir_key), nil, :target_name => 'image') else image_path = normalize_system_path(target_image) end if !File.readable? image_path puts "asciidoctor: WARNING: image to embed not found or not readable: #{image_path}" return "data:#{mimetype}:base64," #return 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==' end bindata = nil if IO.respond_to? :binread bindata = IO.binread(image_path) else bindata = File.open(image_path, 'rb') {|file| file.read } end "data:#{mimetype};base64,#{Base64.encode64(bindata).delete("\n")}" end