jekyll-lazy-load-image 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7712fe91dd0b7ef1babc45a91c5a893a23efa66e00826edc877ecaafc5af6ec7
4
- data.tar.gz: e5a6a60c6f15a6ec9004e58a0279a1d4b9ed79724cfa3fb86eddc03701d604dd
3
+ metadata.gz: b3172715c60eea86ceb441ac1b14426f5fac9acd625f6312fc46b82d7da993ca
4
+ data.tar.gz: 77ae96b69eb3262d3317a16d40dad04cbf5fe98ef1e043c9a7e47bf1877e5870
5
5
  SHA512:
6
- metadata.gz: f3ad1ef29a57b6106dbc75c2c8561140f9c4f52a75e62f739188052e680c692bf38e47d777ea7448795e8d5d2b36f7d83483c2e1591a15993b1145b2c36c72b3
7
- data.tar.gz: f13a15133e199775a19213471dbfce7270bc57807c127200b42941ed26d2c3ce0b3c75d7b050c713a0afa7b0594f574b38bc9d3946e99d44a8265df21e658542
6
+ metadata.gz: 1e10e503b85382c46eceaa9b13433e08a7246a5b199f6d467c6a0ad00050bb531e802a3daddfa0cdb0f389bd358865f3d128e1d8973724a48a358ccaa67dbc8a
7
+ data.tar.gz: 5eb99b095c9db8722a8ae31f96b350f8cf91ccbb281a532cfb2599cf10ca88d8e42015bb6ad4967478e6ac5d07875bfb17dab7492910ccb29db2037df3fe7ad5
data/.travis.yml CHANGED
@@ -4,7 +4,9 @@ sudo: false
4
4
  cache: bundler
5
5
  before_install:
6
6
  - gem update bundler
7
- script: "bundle exec rake spec"
7
+ script:
8
+ - bundle exec rubocop
9
+ - bundle exec rake spec
8
10
  rvm:
9
11
  - 2.4.0
10
12
  - 2.4.1
data/README.md CHANGED
@@ -2,11 +2,16 @@
2
2
 
3
3
  _Edit img tag optimized lazy load images for your Jekyll site_
4
4
 
5
- ## Install
5
+ ## Usage
6
+ ### `Gemfile`
6
7
 
7
- Add `gem 'jekyll-lazy-load-image'` to your site's `Gemfile` and run bundle.
8
+ Add the following to your site's `Gemfile`:
8
9
 
9
- ## Usage
10
+ ```ruby
11
+ gem 'jekyll-lazy-load-image', require: 'jekyll-lazy-load-image/auto-execution'
12
+ ```
13
+
14
+ and run bundle.
10
15
 
11
16
  ### `_config.yml`
12
17
 
@@ -25,37 +30,41 @@ lazy_load_image:
25
30
  "data-size": auto
26
31
  ```
27
32
 
28
- ### `_plugins`
33
+ ### Select lazy load library
34
+
35
+ Select your favorite library and add your site. For example:
36
+ - [lazysizes](https://github.com/aFarkas/lazysizes) [Recommend]
37
+ - [Echo.js](https://github.com/toddmotto/echo)
38
+ - [TADA](https://github.com/fallroot/tada)
39
+
40
+ ## Custom
41
+ ### Customize container
42
+
43
+ You can change applying jekyll hook container.
44
+ This library is `:posts` by default.
29
45
 
30
- Add the following to your site's `_plugins/lazy-load-image.rb`:
46
+ See also: https://jekyllrb.com/docs/plugins/#hooks
47
+
48
+ #### `Gemfile`
49
+
50
+ Add the following to your site's `Gemfile`:
31
51
 
32
52
  ```ruby
33
- Jekyll::LazyLoadImage.execute
53
+ gem 'jekyll-lazy-load-image'
34
54
  ```
35
55
 
36
- #### Change container
56
+ #### `_plugins`
37
57
 
38
- If you want to change applying container, please add the following:
58
+ Add the following to your site's `_plugins`, for example create `_plugins/lazy-load-image.rb`:
39
59
 
40
60
  ```ruby
41
- Jekyll::LazyLoadImage.configure do |config|
61
+ JekyllLazyLoadImage.configure do |config|
42
62
  config.owners = %i[posts documents]
43
63
  end
44
64
 
45
- Jekyll::LazyLoadImage.execute
65
+ JekyllLazyLoadImage.execute
46
66
  ```
47
67
 
48
- Default is `:posts` only.
49
-
50
- See also: https://jekyllrb.com/docs/plugins/#hooks
51
-
52
- ### Select lazy load library
53
-
54
- Select your favorite library and add your site. For example:
55
- - [lazysizes](https://github.com/aFarkas/lazysizes) [Recommend]
56
- - [Echo.js](https://github.com/toddmotto/echo)
57
- - [TADA](https://github.com/fallroot/tada)
58
-
59
68
  ## Development
60
69
 
61
70
  - Use `bin/setup` to setup your local development environment.
@@ -6,7 +6,7 @@ require "jekyll-lazy-load-image/version"
6
6
 
7
7
  Gem::Specification.new do |spec|
8
8
  spec.name = "jekyll-lazy-load-image"
9
- spec.version = Jekyll::LazyLoadImage::VERSION
9
+ spec.version = JekyllLazyLoadImage::VERSION
10
10
  spec.authors = ["Tadayuki Onishi"]
11
11
  spec.email = ["tt.tanishi100@gmail.com"]
12
12
  spec.summary = "Edit img tag optimized lazy load images for your Jekyll site"
@@ -1,4 +1,32 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "jekyll"
4
+
3
5
  require "jekyll-lazy-load-image/version"
4
- require "jekyll/lazy_load_image"
6
+ require "jekyll-lazy-load-image/config"
7
+ require "jekyll-lazy-load-image/site-config"
8
+ require "jekyll-lazy-load-image/translator"
9
+
10
+ module JekyllLazyLoadImage
11
+ HOOK_KEY = :jekyll_lazy_load_image
12
+
13
+ class << self
14
+ def configure
15
+ yield(config)
16
+ end
17
+
18
+ def config
19
+ @config ||= Config.new
20
+ end
21
+
22
+ def execute
23
+ Jekyll::Hooks.register(config.owners, :post_render) do |post|
24
+ site_config = JekyllLazyLoadImage::SiteConfig.new(
25
+ post.site.config[JekyllLazyLoadImage::SiteConfig::CONFIG_KEY]
26
+ )
27
+ auto_lazy_load_image = JekyllLazyLoadImage::Translator.new(post.output, site_config)
28
+ post.output = auto_lazy_load_image.translate
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "jekyll-lazy-load-image"
4
+
5
+ JekyllLazyLoadImage.execute
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module JekyllLazyLoadImage
4
+ class Config
5
+ ALLOWED_JEKYLL_HOOK_CONTAINERS = %i[documents pages posts site].freeze
6
+ DEFAULT_CONTAINERS = %i[posts].freeze
7
+
8
+ def owners
9
+ @owners || DEFAULT_CONTAINERS
10
+ end
11
+
12
+ def owners=(value)
13
+ return if value.nil? || value.empty?
14
+ @owners = Array(value).map(&:to_sym).tap do |owners_prospective|
15
+ not_allowed_hooks = ALLOWED_JEKYLL_HOOK_CONTAINERS & owners_prospective
16
+ raise ArgumentError, "The owners option must be #{ALLOWED_JEKYLL_HOOK_CONTAINERS.join(" or ")}." if not_allowed_hooks.size.zero?
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ module JekyllLazyLoadImage
4
+ class SiteConfig
5
+ CONFIG_KEY = "lazy_load_image"
6
+
7
+ def initialize(lazy_load_image_config)
8
+ @lazy_load_image_config = lazy_load_image_config
9
+ end
10
+
11
+ def additional_attrs
12
+ config_key = "additional_attrs"
13
+ @additional_attrs ||= (@lazy_load_image_config&.[](config_key) || {}).tap do |attrs|
14
+ raise "#{config_key} must be associative array. But passed #{attrs || "nil"}" unless attrs.is_a?(Hash)
15
+ end
16
+ end
17
+
18
+ def class_attr_values
19
+ config_key = "class_attr_values"
20
+ @class_attr_values ||= Array(
21
+ @lazy_load_image_config&.[](config_key)
22
+ ).compact
23
+ end
24
+
25
+ def ignore_selectors
26
+ config_key = "ignore_selectors"
27
+ @ignore_selectors ||= Array(
28
+ @lazy_load_image_config&.[](config_key)
29
+ ).map(&:to_s).reject(&:empty?)
30
+ end
31
+
32
+ def preload_image
33
+ config_key = "preload_image"
34
+ @preload_image ||= @lazy_load_image_config&.[](config_key).to_s.strip
35
+ end
36
+
37
+ def src_attr_name
38
+ config_key = "src_attr_name"
39
+ @src_attr_name ||= @lazy_load_image_config&.[](config_key).to_s.strip.tap do |name|
40
+ raise "You must set #{config_key} config attribute of #{CONFIG_KEY}" if name.empty?
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,70 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "nokogiri"
4
+
5
+ module JekyllLazyLoadImage
6
+ class Translator
7
+ def initialize(document, site_config)
8
+ @document = document
9
+ @site_config = site_config
10
+ end
11
+
12
+ def translate
13
+ nokogiri_doc.xpath("//img").each do |node|
14
+ next if ignore_node?(node)
15
+ apply_lazy_image_setting(node)
16
+ inject_class_attr(node)
17
+ inject_additional_attrs(node)
18
+ end
19
+ nokogiri_doc.to_html.gsub(/[\r\n]/, "")
20
+ end
21
+
22
+ private
23
+
24
+ def ignore_node?(node)
25
+ !@site_config.ignore_selectors.empty? && node.matches?(*@site_config.ignore_selectors)
26
+ end
27
+
28
+ def apply_lazy_image_setting(node)
29
+ node_src_attr = node.attributes["src"]
30
+ return if node_src_attr.nil?
31
+
32
+ src_value = node_src_attr.value
33
+ if @site_config.preload_image.empty?
34
+ node.remove_attribute("src")
35
+ else
36
+ node.attributes["src"].value = @site_config.preload_image
37
+ end
38
+
39
+ node.set_attribute(@site_config.src_attr_name, src_value)
40
+ end
41
+
42
+ def inject_class_attr(node)
43
+ class_value = node.attributes["class"]&.value
44
+ return if class_value.nil? && @site_config.class_attr_values.empty?
45
+
46
+ node.set_attribute("class", "") if class_value.nil?
47
+ node_class_attr = node.attributes["class"]
48
+ class_array_option = [node_class_attr.value, @site_config.class_attr_values].flatten
49
+ class_array = class_array_option.reject do |class_name|
50
+ class_name.nil? || class_name.empty?
51
+ end
52
+
53
+ node_class_attr.value = normalize_class_array(class_array)
54
+ end
55
+
56
+ def inject_additional_attrs(node)
57
+ @site_config.additional_attrs.each do |key, value|
58
+ node.set_attribute(key, value)
59
+ end
60
+ end
61
+
62
+ def nokogiri_doc
63
+ @nokogiri_doc ||= Nokogiri::HTML(@document)
64
+ end
65
+
66
+ def normalize_class_array(array)
67
+ array.map(&:strip).uniq.join(" ")
68
+ end
69
+ end
70
+ end
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Jekyll
4
- module LazyLoadImage
5
- VERSION = "0.1.0"
6
- end
3
+ module JekyllLazyLoadImage
4
+ VERSION = "0.2.0"
7
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jekyll-lazy-load-image
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tadayuki Onishi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-07-28 00:00:00.000000000 Z
11
+ date: 2018-07-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jekyll
@@ -71,12 +71,11 @@ files:
71
71
  - bin/setup
72
72
  - jekyll-lazy-load-image.gemspec
73
73
  - lib/jekyll-lazy-load-image.rb
74
+ - lib/jekyll-lazy-load-image/auto-execution.rb
75
+ - lib/jekyll-lazy-load-image/config.rb
76
+ - lib/jekyll-lazy-load-image/site-config.rb
77
+ - lib/jekyll-lazy-load-image/translator.rb
74
78
  - lib/jekyll-lazy-load-image/version.rb
75
- - lib/jekyll/lazy_load_image.rb
76
- - lib/jekyll/lazy_load_image/config.rb
77
- - lib/jekyll/lazy_load_image/lazy_hooks.rb
78
- - lib/jekyll/lazy_load_image/site_config.rb
79
- - lib/jekyll/lazy_load_image/translator.rb
80
79
  homepage: https://github.com/kenchan0130/jekyll-lazy-load-image
81
80
  licenses:
82
81
  - MIT
@@ -1,45 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "jekyll"
4
- require "jekyll/lazy_load_image/config"
5
- require "jekyll/lazy_load_image/site_config"
6
- require "jekyll/lazy_load_image/lazy_hooks"
7
- require "jekyll/lazy_load_image/translator"
8
-
9
- module Jekyll
10
- module LazyLoadImage
11
- class << self
12
- def configure
13
- yield(config)
14
- end
15
-
16
- def config
17
- @config ||= Config.new
18
- end
19
-
20
- def execute
21
- Jekyll::LazyLoadImage::LazyHooks.run_load_hooks(hook_key)
22
- end
23
-
24
- def register_hook
25
- Jekyll::LazyLoadImage::LazyHooks.on_load(hook_key) do
26
- Jekyll::Hooks.register(config.owners, :post_render) do |post|
27
- site_config = Jekyll::LazyLoadImage::SiteConfig.new(
28
- post.site.config[Jekyll::LazyLoadImage::SiteConfig::CONFIG_KEY]
29
- )
30
- auto_lazy_load_image = Jekyll::LazyLoadImage::Translator.new(post.output, site_config)
31
- post.output = auto_lazy_load_image.translate
32
- end
33
- end
34
- end
35
-
36
- private
37
-
38
- def hook_key
39
- :jekyll_lazy_load_image
40
- end
41
- end
42
- end
43
- end
44
-
45
- Jekyll::LazyLoadImage.register_hook
@@ -1,22 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Jekyll
4
- module LazyLoadImage
5
- class Config
6
- ALLOWED_JEKYLL_HOOK_CONTAINERS = %i[documents pages posts site].freeze
7
- DEFAULT_CONTAINERS = %i[posts].freeze
8
-
9
- def owners
10
- @owners || DEFAULT_CONTAINERS
11
- end
12
-
13
- def owners=(value)
14
- return if value.nil? || value.empty?
15
- @owners = Array(value).map(&:to_sym).tap do |owners_prospective|
16
- not_allowed_hooks = ALLOWED_JEKYLL_HOOK_CONTAINERS & owners_prospective
17
- raise ArgumentError, "The owners option must be #{ALLOWED_JEKYLL_HOOK_CONTAINERS.join(" or ")}." if not_allowed_hooks.size.zero?
18
- end
19
- end
20
- end
21
- end
22
- end
@@ -1,37 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Jekyll
4
- module LazyLoadImage
5
- module LazyHooks
6
- @load_hooks = Hash.new { |h, k| h[k] = [] }
7
- @loaded = Hash.new { |h, k| h[k] = [] }
8
-
9
- class << self
10
- def on_load(name, options = {}, &block)
11
- @loaded[name].each do |base|
12
- execute_hook(base, options, block)
13
- end
14
-
15
- @load_hooks[name] << [block, options]
16
- end
17
-
18
- def run_load_hooks(name, base = Object)
19
- @loaded[name] << base
20
- @load_hooks[name].each do |hook, options|
21
- execute_hook(base, options, hook)
22
- end
23
- end
24
-
25
- private
26
-
27
- def execute_hook(base, options, block)
28
- if options[:yield]
29
- block.call(base)
30
- else
31
- base.instance_eval(&block)
32
- end
33
- end
34
- end
35
- end
36
- end
37
- end
@@ -1,46 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Jekyll
4
- module LazyLoadImage
5
- class SiteConfig
6
- CONFIG_KEY = "lazy_load_image"
7
-
8
- def initialize(lazy_load_image_config)
9
- @lazy_load_image_config = lazy_load_image_config
10
- end
11
-
12
- def additional_attrs
13
- config_key = "additional_attrs"
14
- @additional_attrs ||= (@lazy_load_image_config&.[](config_key) || {}).tap do |attrs|
15
- raise "#{config_key} must be associative array. But passed #{attrs || "nil"}" unless attrs.is_a?(Hash)
16
- end
17
- end
18
-
19
- def class_attr_values
20
- config_key = "class_attr_values"
21
- @class_attr_values ||= Array(
22
- @lazy_load_image_config&.[](config_key)
23
- ).compact
24
- end
25
-
26
- def ignore_selectors
27
- config_key = "ignore_selectors"
28
- @ignore_selectors ||= Array(
29
- @lazy_load_image_config&.[](config_key)
30
- ).map(&:to_s).reject(&:empty?)
31
- end
32
-
33
- def preload_image
34
- config_key = "preload_image"
35
- @preload_image ||= @lazy_load_image_config&.[](config_key).to_s.strip
36
- end
37
-
38
- def src_attr_name
39
- config_key = "src_attr_name"
40
- @src_attr_name ||= @lazy_load_image_config&.[](config_key).to_s.strip.tap do |name|
41
- raise "You must set #{config_key} config attribute of #{CONFIG_KEY}" if name.empty?
42
- end
43
- end
44
- end
45
- end
46
- end
@@ -1,72 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "nokogiri"
4
-
5
- module Jekyll
6
- module LazyLoadImage
7
- class Translator
8
- def initialize(document, site_config)
9
- @document = document
10
- @site_config = site_config
11
- end
12
-
13
- def translate
14
- nokogiri_doc.xpath("//img").each do |node|
15
- next if ignore_node?(node)
16
- apply_lazy_image_setting(node)
17
- inject_class_attr(node)
18
- inject_additional_attrs(node)
19
- end
20
- nokogiri_doc.to_html.gsub(/[\r\n]/, "")
21
- end
22
-
23
- private
24
-
25
- def ignore_node?(node)
26
- !@site_config.ignore_selectors.empty? && node.matches?(*@site_config.ignore_selectors)
27
- end
28
-
29
- def apply_lazy_image_setting(node)
30
- node_src_attr = node.attributes["src"]
31
- return if node_src_attr.nil?
32
-
33
- src_value = node_src_attr.value
34
- if @site_config.preload_image.empty?
35
- node.remove_attribute("src")
36
- else
37
- node.attributes["src"].value = @site_config.preload_image
38
- end
39
-
40
- node.set_attribute(@site_config.src_attr_name, src_value)
41
- end
42
-
43
- def inject_class_attr(node)
44
- class_value = node.attributes["class"]&.value
45
- return if class_value.nil? && @site_config.class_attr_values.empty?
46
-
47
- node.set_attribute("class", "") if class_value.nil?
48
- node_class_attr = node.attributes["class"]
49
- class_array_option = [node_class_attr.value, @site_config.class_attr_values].flatten
50
- class_array = class_array_option.reject do |class_name|
51
- class_name.nil? || class_name.empty?
52
- end
53
-
54
- node_class_attr.value = normalize_class_array(class_array)
55
- end
56
-
57
- def inject_additional_attrs(node)
58
- @site_config.additional_attrs.each do |key, value|
59
- node.set_attribute(key, value)
60
- end
61
- end
62
-
63
- def nokogiri_doc
64
- @nokogiri_doc ||= Nokogiri::HTML(@document)
65
- end
66
-
67
- def normalize_class_array(array)
68
- array.map(&:strip).uniq.join(" ")
69
- end
70
- end
71
- end
72
- end