Module: Rabbit::Parser::Ext::Image

Included in:
RD::Ext::Image
Defined in:
lib/rabbit/parser/ext/image.rb

Defined Under Namespace

Modules: Private

Constant Summary collapse

ALLOWED_IMG_URL_SCHEME =
['http', 'https', 'file', '']

Class Method Summary collapse

Class Method Details

.make_image(canvas, uri_str, prop = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/rabbit/parser/ext/image.rb', line 15

def make_image(canvas, uri_str, prop={})
  uri = URI.parse(uri_str)
  scheme = uri.scheme
  unless ALLOWED_IMG_URL_SCHEME.include?(scheme.to_s.downcase)
    return nil
  end
  begin
    Element::Image.new(Private.image_filename(canvas, uri), prop)
  rescue Error
    canvas.logger.warn($!.message)
    nil
  end
end

.make_image_from_file(canvas, source) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/rabbit/parser/ext/image.rb', line 29

def make_image_from_file(canvas, source)
  src_file = Tempfile.new("rabbit-image-source")
  src_file.open
  src_file.print(source)
  src_file.close
  image_file = prop = nil
  begin
    image_file, prop = yield(src_file.path)
  rescue ImageLoadError
    canvas.logger.warn($!.message)
  end
  return nil if image_file.nil?
  image = make_image(canvas, %Q[file://#{image_file.path}], prop)
  return nil if image.nil?
  image["_src"] = image_file # for protecting from GC
  image
end