Skip to content

Commit

Permalink
use the term "format" instead of "type" to refer to image format
Browse files Browse the repository at this point in the history
  • Loading branch information
mojavelinux committed Sep 6, 2016
1 parent 3951f4d commit 354b14e
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 23 deletions.
14 changes: 7 additions & 7 deletions lib/asciidoctor-pdf/asciidoctor_ext/image.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
module Asciidoctor
module Image
class << self
def image_type path
(::File.extname path)[1..-1].downcase
def format path, node = nil
(node && (node.attr 'format', nil, false)) || (::File.extname path).downcase[1..-1]
end
end

def image_type
(attr 'format', nil, false) || ::File.extname(inline? ? target : (attr 'target'))[1..-1].downcase
def format
(attr 'format', nil, false) || ::File.extname(inline? ? target : (attr 'target')).downcase[1..-1]
end

def target_with_image_type
image_path = inline? ? (target) : (attr 'target')
[image_path, (attr 'format', nil, false) || (::File.extname image_path)[1..-1].downcase]
def target_and_format
image_path = inline? ? target : (attr 'target')
[image_path, (attr 'format', nil, false) || (::File.extname image_path).downcase[1..-1]]
end
end
end
26 changes: 13 additions & 13 deletions lib/asciidoctor-pdf/converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -791,20 +791,20 @@ def convert_content_for_list_item node, opts = {}
def convert_image node
node.extend ::Asciidoctor::Image unless ::Asciidoctor::Image === node
valid_image = true
target, image_type = node.target_with_image_type
target, image_format = node.target_and_format

if image_type == 'gif'
if image_format == 'gif'
valid_image = false
warn %(asciidoctor: WARNING: GIF image format not supported. Please convert #{target} to PNG.)
end

unless (image_path = resolve_image_path node, target, image_type) && (::File.readable? image_path)
unless (image_path = resolve_image_path node, target, image_format) && (::File.readable? image_path)
valid_image = false
warn %(asciidoctor: WARNING: image to embed not found or not readable: #{image_path || target})
end

# NOTE import_page automatically advances to next page afterwards
return import_page image_path if image_type == 'pdf'
return import_page image_path if image_format == 'pdf'

# QUESTION if we advance to new page, shouldn't dest point there too?
add_dest_for_block node if node.id
Expand Down Expand Up @@ -835,7 +835,7 @@ def convert_image node
end

span_page_width_if width_relative_to_page do
case image_type
case image_format
when 'svg'
begin
svg_data = ::IO.read image_path
Expand Down Expand Up @@ -1504,19 +1504,19 @@ def convert_inline_image node
end
else
node.extend ::Asciidoctor::Image unless ::Asciidoctor::Image === node
target, image_type = node.target_with_image_type
target, image_format = node.target_and_format
valid = true
if image_type == 'gif'
if image_format == 'gif'
warn %(asciidoctor: WARNING: GIF image format not supported. Please convert #{target} to PNG.) unless scratch?
valid = false
end
unless (image_path = resolve_image_path node, target, image_type) && (::File.readable? image_path)
unless (image_path = resolve_image_path node, target, image_format) && (::File.readable? image_path)
warn %(asciidoctor: WARNING: image to embed not found or not readable: #{image_path || target}) unless scratch?
valid = false
end
if valid
width_attr = (node.attr? 'width', nil, false) ? %( width="#{node.attr 'width'}") : nil
img = %(<img src="#{image_path}" type="#{image_type}" alt="#{node.attr 'alt'}"#{width_attr} tmp="#{TemporaryPath === image_path}">)
img = %(<img src="#{image_path}" format="#{image_format}" alt="#{node.attr 'alt'}"#{width_attr} tmp="#{TemporaryPath === image_path}">)
end
end
img ||= %([#{node.attr 'alt'}])
Expand Down Expand Up @@ -2514,10 +2514,10 @@ def resolve_imagesdir doc
# is not set, or the URI cannot be read, this method returns a nil value.
#
# When a temporary file is used, the TemporaryPath type is mixed into the path string.
def resolve_image_path node, image_path = nil, image_type = nil
def resolve_image_path node, image_path = nil, image_format = nil
imagesdir = resolve_imagesdir(doc = node.document)
image_path ||= node.attr 'target'
image_type ||= ((node.attr 'format', nil, false) || (::Asciidoctor::Image.image_type image_path))
image_format ||= ::Asciidoctor::Image.format image_path, (::Asciidoctor::Image === node ? node : nil)
# handle case when image is a URI
if (node.is_uri? image_path) || (imagesdir && (node.is_uri? imagesdir) &&
(image_path = (node.normalize_web_path image_path, imagesdir, false)))
Expand All @@ -2532,8 +2532,8 @@ def resolve_image_path node, image_path = nil, image_type = nil
else
::OpenURI
end
tmp_image = ::Tempfile.new ['image-', %(.#{image_type || 'jpg'})]
tmp_image.binmode if (binary = image_type != 'svg')
tmp_image = ::Tempfile.new ['image-', image_format && %(.#{image_format})]
tmp_image.binmode if (binary = image_format != 'svg')
begin
open(image_path, (binary ? 'rb' : 'r')) {|fd| tmp_image.write(fd.read) }
tmp_image_path = tmp_image.path
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def arrange_images fragments
end

# TODO make helper method to calculate width and height of image
if fragment[:image_type] == 'svg'
if fragment[:image_format] == 'svg'
svg_obj = ::Prawn::Svg::Interface.new (::IO.read image_path), doc, at: doc.bounds.top_left, width: image_w, fallback_font_name: doc.default_svg_font
if image_w
fragment[:image_width] = svg_obj.document.sizing.output_width
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def render_behind fragment
fragment.top - ((fragment.height - data[:image_height]) / 2.0)
end
image_left = fragment.left + ((fragment.width - data[:image_width]) / 2.0)
case data[:image_type]
case data[:image_format]
when 'svg'
# prawn-svg messes with the cursor; use float as a workaround
pdf.float do
Expand Down
2 changes: 1 addition & 1 deletion lib/asciidoctor-pdf/formatted_text/transform.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def apply(parsed)
attributes = node[:attributes]
fragment = {
image_path: attributes[:src],
image_type: attributes[:type],
image_format: attributes[:format],
image_tmp: (attributes[:tmp] == 'true'),
text: attributes[:alt],
callback: InlineImageRenderer
Expand Down

0 comments on commit 354b14e

Please sign in to comment.