Another refactor to try and avoid `include`

This commit is contained in:
Joseph Wynn 2016-09-27 19:59:24 +01:00
parent a1a183c3d4
commit 354f693afc
12 changed files with 65 additions and 72 deletions

View File

@ -10,7 +10,7 @@ require 'jekyll/responsive_image/utils'
require 'jekyll/responsive_image/render_cache'
require 'jekyll/responsive_image/image_processor'
require 'jekyll/responsive_image/resize_handler'
require 'jekyll/responsive_image/common'
require 'jekyll/responsive_image/renderer'
require 'jekyll/responsive_image/tag'
require 'jekyll/responsive_image/block'
require 'jekyll/responsive_image/extra_image_generator'

View File

@ -1,20 +1,11 @@
module Jekyll
class ResponsiveImage
module ResponsiveImage
class Block < Liquid::Block
include Jekyll::ResponsiveImage::Common
include Jekyll::ResponsiveImage::Utils
def render(context)
attributes = YAML.load(super)
render_responsive_image(context, attributes)
Renderer.new(context.registers[:site], attributes).render_responsive_image
end
end
end

View File

@ -1,46 +0,0 @@
module Jekyll
class ResponsiveImage
module Common
include Jekyll::ResponsiveImage::Utils
def make_config(site)
ResponsiveImage.defaults.dup
.merge(site.config['responsive_image'])
.merge(:site_source => site.source, :site_dest => site.dest)
end
def keep_resized_image!(site, image)
keep_dir = File.dirname(image['path'])
site.config['keep_files'] << keep_dir unless site.config['keep_files'].include?(keep_dir)
end
def render_responsive_image(context, attributes)
cache_key = attributes.to_s
result = attributes['cache'] ? RenderCache.get(cache_key) : nil
if result.nil?
site = context.registers[:site]
config = make_config(site)
absolute_image_path = site.in_source_dir(attributes['path'].to_s)
image = ImageProcessor.process(absolute_image_path, attributes['path'], config)
attributes['original'] = image[:original]
attributes['resized'] = image[:resized]
attributes['resized'].each { |resized| keep_resized_image!(site, resized) }
image_template = site.in_source_dir(attributes['template'] || config['template'])
partial = File.read(image_template)
template = Liquid::Template.parse(partial)
result = template.render!(attributes.merge(site.site_payload))
RenderCache.set(cache_key, result)
end
result
end
end
end
end

View File

@ -1,5 +1,5 @@
module Jekyll
class ResponsiveImage
module ResponsiveImage
@defaults = {
'default_quality' => 85,
'base_path' => 'assets',

View File

@ -1,10 +1,11 @@
module Jekyll
class ResponsiveImage
module ResponsiveImage
class ExtraImageGenerator < Jekyll::Generator
include Jekyll::ResponsiveImage::Common
include Jekyll::ResponsiveImage::Utils
def generate(site)
config = make_config(site)
renderer = Renderer.new(site, {})
config = renderer.make_config
config['extra_images'].each do |pathspec|
Dir.glob(site.in_source_dir(pathspec)) do |image_path|

View File

@ -1,5 +1,5 @@
module Jekyll
class ResponsiveImage
module ResponsiveImage
class ImageProcessor
include ResponsiveImage::Utils

View File

@ -1,5 +1,5 @@
module Jekyll
class ResponsiveImage
module ResponsiveImage
class RenderCache
attr_accessor :store

View File

@ -0,0 +1,44 @@
module Jekyll
module ResponsiveImage
class Renderer
include Jekyll::ResponsiveImage::Utils
def initialize(site, attributes)
@site = site
@attributes = attributes
end
def make_config
ResponsiveImage.defaults.dup
.merge(@site.config['responsive_image'])
.merge(:site_source => @site.source, :site_dest => @site.dest)
end
def render_responsive_image
cache_key = @attributes.to_s
result = @attributes['cache'] ? RenderCache.get(cache_key) : nil
if result.nil?
config = make_config
absolute_image_path = @site.in_source_dir(@attributes['path'].to_s)
image = ImageProcessor.process(absolute_image_path, @attributes['path'], config)
@attributes['original'] = image[:original]
@attributes['resized'] = image[:resized]
@attributes['resized'].each { |resized| keep_resized_image!(@site, resized) }
image_template = @site.in_source_dir(@attributes['template'] || config['template'])
partial = File.read(image_template)
template = Liquid::Template.parse(partial)
result = template.render!(@attributes.merge(@site.site_payload))
RenderCache.set(cache_key, result)
end
result
end
end
end
end

View File

@ -1,5 +1,5 @@
module Jekyll
class ResponsiveImage
module ResponsiveImage
class ResizeHandler
include ResponsiveImage::Utils

View File

@ -1,8 +1,6 @@
module Jekyll
class ResponsiveImage
module ResponsiveImage
class Tag < Liquid::Tag
include Jekyll::ResponsiveImage::Common
def initialize(tag_name, markup, tokens)
super
@ -15,7 +13,7 @@ module Jekyll
end
def render(context)
render_responsive_image(context, @attributes)
Renderer.new(context.registers[:site], @attributes).render_responsive_image
end
end
end

View File

@ -1,8 +1,13 @@
require 'pathname'
module Jekyll
class ResponsiveImage
module ResponsiveImage
module Utils
def keep_resized_image!(site, image)
keep_dir = File.dirname(image['path'])
site.config['keep_files'] << keep_dir unless site.config['keep_files'].include?(keep_dir)
end
def symbolize_keys(hash)
result = {}
hash.each_key do |key|

View File

@ -1,5 +1,5 @@
module Jekyll
class ResponsiveImage
module ResponsiveImage
VERSION = '1.0.0.pre3'.freeze
end
end