Class: MiddlemanSimpleThumbnailer::Extension

Inherits:
Middleman::Extension
  • Object
show all
Defined in:
lib/middleman-simple-thumbnailer/extension.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, options_hash = {}, &block) ⇒ Extension

Returns a new instance of Extension.



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/middleman-simple-thumbnailer/extension.rb', line 23

def initialize(app, options_hash={}, &block)
    super
    @images_store = MiddlemanSimpleThumbnailer::ImageStore.new
    @resize_specs = app.data[options.specs_data] || []
    if @resize_specs.length == 0
      @resize_specs = []
    end
    if !%w(yaml yml json).include?(options.specs_data_default_format)
      raise "value assigned to option specs_data_default_format is not correct. should be one of json, yaml, or yml"
    end
end

Instance Method Details

#after_build(builder) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/middleman-simple-thumbnailer/extension.rb', line 76

def after_build(builder)
  resize_specs_modified = false
  @images_store.each do |img_path, resize_to|
    if options.use_specs && options.update_specs && !check_image_in_specs(img_path, resize_to)
      builder.thor.say_status :warning, "#{img_path}:#{resize_to} not in resize spec", :yellow
      @resize_specs.push(Middleman::Util::EnhancedHash.new({'path': img_path, 'resize_to': resize_to}))
      resize_specs_modified = true
    end
    img = MiddlemanSimpleThumbnailer::Image.new(img_path, resize_to, builder.app, options)
    if !File.exists?(img.resized_img_abs_path)
      builder.thor.say_status :create, "#{img.resized_img_abs_path}"
      img.save!
    end
  end
  @images_store.delete
  if options.update_specs && resize_specs_modified
    builder.thor.say_status :warning, "Resize specs modified, rewrite", :yellow
    write_specs_file
  end
end

#after_configurationObject



35
36
37
38
39
# File 'lib/middleman-simple-thumbnailer/extension.rb', line 35

def after_configuration
  if app.development? && options.use_cache_dev
    app.use MiddlemanSimpleThumbnailer::Rack, options, app, options
  end
end

#manipulate_resource_list(resources) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/middleman-simple-thumbnailer/extension.rb', line 54

def manipulate_resource_list(resources)
  return resources unless options.use_specs
  resources + @resize_specs.reduce([]) do |res, resize_spec|
    Dir.chdir(File.absolute_path(File.join(app.root, app.config[:source], app.config[:images_dir]))) do
      Dir.glob(resize_spec.path) do |image_file|
        store_resized_image(image_file, resize_spec.resize_to)
        img = MiddlemanSimpleThumbnailer::Image.new(image_file, resize_spec.resize_to, app, options).tap do |i|
          i.prepare_thumbnail
        end
        resized_image_path = File.join(app.config[:images_dir],img.resized_img_path)
        new_resource = MiddlemanSimpleThumbnailer::Resource.new(
          app.sitemap,
          resized_image_path,
          img.cached_resized_img_abs_path,
        )
        res.push(new_resource)
      end
    end
    res
  end
end

#store_resized_image(img_path, resize_to) ⇒ Object



41
42
43
# File 'lib/middleman-simple-thumbnailer/extension.rb', line 41

def store_resized_image(img_path, resize_to)
  @images_store.store(img_path, resize_to)
end