class Middleman::AutomaticClowncar::Extension

Constants

SVG_TEMPLATE

Public Class Methods

new(app, options_hash={}, &block) click to toggle source
Calls superclass method
# File lib/middleman-automatic-clowncar/extension.rb, line 23
def initialize(app, options_hash={}, &block)
  super
  options_hash[:filetypes] ||= [:jpg, :jpeg, :png]
  Extension.options_hash = options_hash
  # Clowncar bits
  require 'uri'
  require 'pathname'
  @ready = false

  #Thumbnailer
  app.after_configuration do

    #stash the source images dir in options for the Rack middleware
    Extension.options_hash[:source_dir] = app.source_dir

    sizes = Extension.options_hash[:sizes]
    namespace = Extension.options_hash[:namespace_directory].join(',')

    dir = Pathname.new(app.source_dir)
    glob = "#{dir}/{#{namespace}}/*.{#{Extension.options_hash[:filetypes].join(',')}}"
    files = Dir[glob]
  end
end

Public Instance Methods

after_configuration() click to toggle source
# File lib/middleman-automatic-clowncar/extension.rb, line 51
def after_configuration
  @ready = true
end
automatic_clowncar_tag(name, options={}) click to toggle source
# File lib/middleman-automatic-clowncar/extension.rb, line 194
def automatic_clowncar_tag(name, options={})
  internal = ""

  if options[:fallback]
    fallback = File.basename thumbnail_url(name,:small)
    fallback_path = extensions[:automatic_clowncar].get_image_path(name, fallback, true, false)
    internal = %{<!--[if lte IE 8]><img src="#{fallback_path}"><![endif]-->}
  end

  width, height = extensions[:automatic_clowncar].get_physical_image_size(name)
  object_style = ""
  if options.has_key?(:prevent_upscaling) && options[:prevent_upscaling]
    #if options.has_key?(:include_original) && options[:include_original]
    #else
    #  width = extensions[:automatic_clowncar].options.sizes.map{|k,v| v }.sort.last
    #end

    object_style = "max-width:#{width}px;"
  end

  if options.has_key?(:inline) && (options[:inline] === false)
    url = asset_path(:images, "#{name}.svg")
    %Q{<object type="image/svg+xml" style="#{object_style}" data-aspect-ratio="#{width.to_f/height.to_f}" data="#{url}">#{internal}</object>}
  else
    data = extensions[:automatic_clowncar].generate_svg(name, true, options)
    %Q{<object type="image/svg+xml" style="#{object_style}" data-aspect-ratio="#{width.to_f/height.to_f}" data="data:image/svg+xml,#{::URI.escape(data)}">#{internal}</object>}
  end
end
generate_clowncar(name, options={}) click to toggle source
# File lib/middleman-automatic-clowncar/extension.rb, line 188
def generate_clowncar(name, options={})
  Extension.svg_files_to_generate << [name, options]
end
generate_media_queries(name, sizes, is_relative, fallback_host) click to toggle source
# File lib/middleman-automatic-clowncar/extension.rb, line 98
def generate_media_queries(name, sizes, is_relative, fallback_host)
  output = []

  if sizes.keys.length === 1
    return "svg{background-image:url(#{get_image_path(name, sizes[sizes.keys.first], is_relative, fallback_host)});}"
  end

  previous_key = nil
  sizes.keys.sort.each_with_index do |key, i|
    line = ["@media screen and "]

    if i == 0
      line << "(max-width:#{key}px)"
    elsif i == (sizes.keys.length - 1)
      line << "(min-width:#{previous_key+1}px)"
    else
      line << "(min-width:#{previous_key+1}px) and (max-width:#{key}px)"
    end

    line << "{svg{background-image:url(#{get_image_path(name, sizes[key], is_relative, fallback_host)});}}"

    output << line.join("")
    previous_key = key
  end

  output.join("")
end
generate_svg(name, is_relative, options) click to toggle source
# File lib/middleman-automatic-clowncar/extension.rb, line 160
def generate_svg(name, is_relative, options)
  #puts "name for generate_svg = #{name}"
  #puts "options for generate_svg = #{options}"
  sizes, width, height = get_image_sizes(name, options)

  fallback_host = false
  if is_relative
    test_path = app.asset_path(:images, "#{name}.svg")
    if is_relative_url?(test_path)
      if options.has_key?(:host)
        fallback_host = options[:host]
      elsif app.config[:asset_host]
        fallback_host = app.config[:asset_host]
      else
        warn "WARNING: Inline clowncar images require absolute paths. Please set a :host value"
      end
    end
  end

  media_queries = generate_media_queries(name, sizes, is_relative, fallback_host)

  xml = SVG_TEMPLATE.dup
  xml.sub!("::media_queries::", media_queries)
  xml.sub!("::width::", width.to_s)
  xml.sub!("::height::", height.to_s)
  xml
end
get_image_path(name, path, is_relative, fallback_host) click to toggle source
# File lib/middleman-automatic-clowncar/extension.rb, line 66
def get_image_path(name, path, is_relative, fallback_host)
  #puts "@@@@@@@ calling get_image_path for name:#{name} path:#{path}, is_relative:#{is_relative}, fallback_host:#{fallback_host}"
  begin
    uri = URI(path)
  rescue URI::InvalidURIError
    # Nothing we can do with it, it's not really a URI
    return path
  end

  if uri.host
    path
  else

    svg_path = File.join(File.dirname(name),File.basename(name,".*"), path)

    if is_relative
      url = app.asset_path(:images, svg_path)
      images_dir = app.config[:images_dir] || 'images'
      url = url.sub("/#{images_dir}/",'/')
      if fallback_host && is_relative_url?(url)
        File.join(fallback_host, url)
      else
        url
      end
    else
      svg_path
    end
  end
end
get_image_sizes(name, options) click to toggle source
# File lib/middleman-automatic-clowncar/extension.rb, line 131
def get_image_sizes(name, options)
  #puts "getting images sizes for #{name}"

  main_abs_path = File.join(app.source_dir,name)

  extname = File.extname(name)
  basename = File.basename(name, ".*")

  return {} unless File.exist? main_abs_path

  width, height = ::FastImage.size(main_abs_path, :raise_on_failure => true)


  sizes = {}
  Extension.options_hash[:sizes].each_pair do |sname,swidth|
    next if swidth > width
    sizes[swidth] = "#{basename}-#{sname}#{extname}"
  end

  if options[:include_original] || Extension.options_hash[:include_originals]
    sizes[width] = "../#{basename}#{extname}"
  end

  #puts "-"*30
  #puts [sizes, width, height]
  [sizes, width, height]
end
get_physical_image_size(name) click to toggle source
# File lib/middleman-automatic-clowncar/extension.rb, line 126
def get_physical_image_size(name)
  main_abs_path = File.join(app.source_dir,name)
  FastImage.size(main_abs_path, :raise_on_failure => true)
end
is_relative_url?(path) click to toggle source
# File lib/middleman-automatic-clowncar/extension.rb, line 55
def is_relative_url?(path)
  begin
    uri = URI(path)
  rescue URI::InvalidURIError
    # Nothing we can do with it, it's not really a URI
    return false
  end

  !uri.host
end
manipulate_resource_list(resources) click to toggle source
# File lib/middleman-automatic-clowncar/extension.rb, line 47
def manipulate_resource_list(resources)
  SitemapExtension.new(self).manipulate_resource_list(resources)
end
thumbnail_specs(image, name) click to toggle source
# File lib/middleman-automatic-clowncar/extension.rb, line 223
def thumbnail_specs(image, name)
  sizes = Extension.options_hash[:sizes]
  ThumbnailGenerator.specs(image, sizes, app.source_dir)
end
thumbnail_url(image, name, options = {}) click to toggle source
# File lib/middleman-automatic-clowncar/extension.rb, line 228
def thumbnail_url(image, name, options = {})
  include_images_dir = options.delete :include_images_dir

  url = thumbnail_specs(image, name)[name][:name]
  url = File.join(url) if include_images_dir

  url
end