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
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 base64.
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) if (ext = Helpers.extname target_image, nil) mimetype = ext == '.svg' ? 'image/svg+xml' : %(image/#{ext.slice 1, ext.length}) else mimetype = 'application/octet-stream' end if asset_dir_key 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 # NOTE pack 'm0' is equivalent to Base64.strict_encode64 %(data:#{mimetype};base64,#{[(::File.binread image_path)].pack 'm0'}) else logger.warn %(image to embed not found or not readable: #{image_path}) %(data:#{mimetype};base64,) # uncomment to return 1 pixel white dot instead #'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==' end end