Another refactor to try and avoid `include`
This commit is contained in:
parent
06d34e09d2
commit
b3d81de179
|
@ -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'
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
|
@ -1,5 +1,5 @@
|
|||
module Jekyll
|
||||
class ResponsiveImage
|
||||
module ResponsiveImage
|
||||
@defaults = {
|
||||
'default_quality' => 85,
|
||||
'base_path' => 'assets',
|
||||
|
|
|
@ -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|
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
module Jekyll
|
||||
class ResponsiveImage
|
||||
module ResponsiveImage
|
||||
class ImageProcessor
|
||||
include ResponsiveImage::Utils
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
module Jekyll
|
||||
class ResponsiveImage
|
||||
module ResponsiveImage
|
||||
class RenderCache
|
||||
attr_accessor :store
|
||||
|
||||
|
|
|
@ -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
|
|
@ -1,5 +1,5 @@
|
|||
module Jekyll
|
||||
class ResponsiveImage
|
||||
module ResponsiveImage
|
||||
class ResizeHandler
|
||||
include ResponsiveImage::Utils
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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|
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
module Jekyll
|
||||
class ResponsiveImage
|
||||
module ResponsiveImage
|
||||
VERSION = '1.0.0.pre3'.freeze
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue