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/render_cache'
|
||||||
require 'jekyll/responsive_image/image_processor'
|
require 'jekyll/responsive_image/image_processor'
|
||||||
require 'jekyll/responsive_image/resize_handler'
|
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/tag'
|
||||||
require 'jekyll/responsive_image/block'
|
require 'jekyll/responsive_image/block'
|
||||||
require 'jekyll/responsive_image/extra_image_generator'
|
require 'jekyll/responsive_image/extra_image_generator'
|
||||||
|
|
|
@ -1,20 +1,11 @@
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
module Jekyll
|
module Jekyll
|
||||||
class ResponsiveImage
|
module ResponsiveImage
|
||||||
class Block < Liquid::Block
|
class Block < Liquid::Block
|
||||||
include Jekyll::ResponsiveImage::Common
|
include Jekyll::ResponsiveImage::Utils
|
||||||
|
|
||||||
def render(context)
|
def render(context)
|
||||||
attributes = YAML.load(super)
|
attributes = YAML.load(super)
|
||||||
render_responsive_image(context, attributes)
|
Renderer.new(context.registers[:site], attributes).render_responsive_image
|
||||||
end
|
end
|
||||||
end
|
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
|
module Jekyll
|
||||||
class ResponsiveImage
|
module ResponsiveImage
|
||||||
@defaults = {
|
@defaults = {
|
||||||
'default_quality' => 85,
|
'default_quality' => 85,
|
||||||
'base_path' => 'assets',
|
'base_path' => 'assets',
|
||||||
|
|
|
@ -1,10 +1,11 @@
|
||||||
module Jekyll
|
module Jekyll
|
||||||
class ResponsiveImage
|
module ResponsiveImage
|
||||||
class ExtraImageGenerator < Jekyll::Generator
|
class ExtraImageGenerator < Jekyll::Generator
|
||||||
include Jekyll::ResponsiveImage::Common
|
include Jekyll::ResponsiveImage::Utils
|
||||||
|
|
||||||
def generate(site)
|
def generate(site)
|
||||||
config = make_config(site)
|
renderer = Renderer.new(site, {})
|
||||||
|
config = renderer.make_config
|
||||||
|
|
||||||
config['extra_images'].each do |pathspec|
|
config['extra_images'].each do |pathspec|
|
||||||
Dir.glob(site.in_source_dir(pathspec)) do |image_path|
|
Dir.glob(site.in_source_dir(pathspec)) do |image_path|
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
module Jekyll
|
module Jekyll
|
||||||
class ResponsiveImage
|
module ResponsiveImage
|
||||||
class ImageProcessor
|
class ImageProcessor
|
||||||
include ResponsiveImage::Utils
|
include ResponsiveImage::Utils
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
module Jekyll
|
module Jekyll
|
||||||
class ResponsiveImage
|
module ResponsiveImage
|
||||||
class RenderCache
|
class RenderCache
|
||||||
attr_accessor :store
|
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
|
module Jekyll
|
||||||
class ResponsiveImage
|
module ResponsiveImage
|
||||||
class ResizeHandler
|
class ResizeHandler
|
||||||
include ResponsiveImage::Utils
|
include ResponsiveImage::Utils
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,6 @@
|
||||||
module Jekyll
|
module Jekyll
|
||||||
class ResponsiveImage
|
module ResponsiveImage
|
||||||
class Tag < Liquid::Tag
|
class Tag < Liquid::Tag
|
||||||
include Jekyll::ResponsiveImage::Common
|
|
||||||
|
|
||||||
def initialize(tag_name, markup, tokens)
|
def initialize(tag_name, markup, tokens)
|
||||||
super
|
super
|
||||||
|
|
||||||
|
@ -15,7 +13,7 @@ module Jekyll
|
||||||
end
|
end
|
||||||
|
|
||||||
def render(context)
|
def render(context)
|
||||||
render_responsive_image(context, @attributes)
|
Renderer.new(context.registers[:site], @attributes).render_responsive_image
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,8 +1,13 @@
|
||||||
require 'pathname'
|
require 'pathname'
|
||||||
|
|
||||||
module Jekyll
|
module Jekyll
|
||||||
class ResponsiveImage
|
module ResponsiveImage
|
||||||
module Utils
|
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)
|
def symbolize_keys(hash)
|
||||||
result = {}
|
result = {}
|
||||||
hash.each_key do |key|
|
hash.each_key do |key|
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
module Jekyll
|
module Jekyll
|
||||||
class ResponsiveImage
|
module ResponsiveImage
|
||||||
VERSION = '1.0.0.pre3'.freeze
|
VERSION = '1.0.0.pre3'.freeze
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue