jekyll-lazy-load-image 0.1.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 7712fe91dd0b7ef1babc45a91c5a893a23efa66e00826edc877ecaafc5af6ec7
4
+ data.tar.gz: e5a6a60c6f15a6ec9004e58a0279a1d4b9ed79724cfa3fb86eddc03701d604dd
5
+ SHA512:
6
+ metadata.gz: f3ad1ef29a57b6106dbc75c2c8561140f9c4f52a75e62f739188052e680c692bf38e47d777ea7448795e8d5d2b36f7d83483c2e1591a15993b1145b2c36c72b3
7
+ data.tar.gz: f13a15133e199775a19213471dbfce7270bc57807c127200b42941ed26d2c3ce0b3c75d7b050c713a0afa7b0594f574b38bc9d3946e99d44a8265df21e658542
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ Gemfile.lock
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,71 @@
1
+ ---
2
+ AllCops:
3
+ TargetRubyVersion: 2.4
4
+ Exclude:
5
+ - bin/**/*
6
+ - exe/**/*
7
+ - benchmark/**/*
8
+ - script/**/*
9
+ - vendor/**/*
10
+ - tmp/**/*
11
+
12
+ Layout/SpaceAroundOperators:
13
+ Enabled: false
14
+
15
+ Lint/AssignmentInCondition:
16
+ Enabled: false
17
+
18
+ Metrics/AbcSize:
19
+ Max: 24
20
+
21
+ Metrics/BlockLength:
22
+ Max: 150
23
+
24
+ Metrics/ClassLength:
25
+ Enabled: false
26
+
27
+ Metrics/CyclomaticComplexity:
28
+ Enabled: false
29
+
30
+ Metrics/LineLength:
31
+ Max: 150
32
+ IgnoredPatterns:
33
+ - '\A\s*#'
34
+
35
+ Metrics/MethodLength:
36
+ Enabled: false
37
+
38
+ Metrics/PerceivedComplexity:
39
+ Enabled: false
40
+
41
+ Style/BlockDelimiters:
42
+ Exclude:
43
+ - spec/**/*
44
+
45
+ Style/Documentation:
46
+ Enabled: false
47
+
48
+ Style/DoubleNegation:
49
+ Enabled: false
50
+
51
+ Style/PercentLiteralDelimiters:
52
+ PreferredDelimiters:
53
+ "%q": "{}"
54
+ "%Q": "{}"
55
+ "%r": "!!"
56
+ "%s": "()"
57
+ "%w": "()"
58
+ "%W": "()"
59
+ "%x": "()"
60
+
61
+ Style/StringLiterals:
62
+ EnforcedStyle: double_quotes
63
+
64
+ Style/StringLiteralsInInterpolation:
65
+ EnforcedStyle: double_quotes
66
+
67
+ Style/TrailingCommaInArrayLiteral:
68
+ EnforcedStyleForMultiline: consistent_comma
69
+
70
+ Style/TrailingCommaInHashLiteral:
71
+ EnforcedStyleForMultiline: consistent_comma
@@ -0,0 +1,15 @@
1
+ ---
2
+ language: ruby
3
+ sudo: false
4
+ cache: bundler
5
+ before_install:
6
+ - gem update bundler
7
+ script: "bundle exec rake spec"
8
+ rvm:
9
+ - 2.4.0
10
+ - 2.4.1
11
+ - 2.4.2
12
+ - 2.4.3
13
+ - 2.4.4
14
+ - 2.5.0
15
+ - 2.5.1
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gemspec
6
+
7
+ gem "rake", "~> 12.0"
8
+ gem "rspec", "~> 3.7.0"
9
+ gem "rubocop", "~> 0.58"
10
+
11
+ group :test do
12
+ gem "pry-byebug"
13
+ end
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Tadayuki Onishi
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,73 @@
1
+ # Jekyll LazyLoadImage [![Build Status](https://travis-ci.org/kenchan0130/jekyll-lazy-load-image.svg?branch=master)](https://travis-ci.org/kenchan0130/jekyll-lazy-load-image)
2
+
3
+ _Edit img tag optimized lazy load images for your Jekyll site_
4
+
5
+ ## Install
6
+
7
+ Add `gem 'jekyll-lazy-load-image'` to your site's `Gemfile` and run bundle.
8
+
9
+ ## Usage
10
+
11
+ ### `_config.yml`
12
+
13
+ Add the following to your site's `_config.yml`:
14
+
15
+ ```yaml
16
+ lazy_load_image:
17
+ src_attr_name: data-src # [required] You need to specify the attributes to be saved for lazy loading
18
+ preload_image: /path/to/image # [optional] you can add a data uri or loading image as fallback src
19
+ class_attr_values: # [optional] if you want to add custom class value, please add values
20
+ - lazyload
21
+ ignore_selectors: # [optional] if you want to ignore applying lazy load image, please add selector (css or xpath)
22
+ - ".ignore-lazy-image-load"
23
+ - "/*[@class='ignore-lazy-image-load']"
24
+ additional_attrs: # [optional] if you want to add attributes, please add key value
25
+ "data-size": auto
26
+ ```
27
+
28
+ ### `_plugins`
29
+
30
+ Add the following to your site's `_plugins/lazy-load-image.rb`:
31
+
32
+ ```ruby
33
+ Jekyll::LazyLoadImage.execute
34
+ ```
35
+
36
+ #### Change container
37
+
38
+ If you want to change applying container, please add the following:
39
+
40
+ ```ruby
41
+ Jekyll::LazyLoadImage.configure do |config|
42
+ config.owners = %i[posts documents]
43
+ end
44
+
45
+ Jekyll::LazyLoadImage.execute
46
+ ```
47
+
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
+ ## Development
60
+
61
+ - Use `bin/setup` to setup your local development environment.
62
+ - Use `bin/console` to load a local Pry console with the Gem.
63
+
64
+ ## Testing
65
+
66
+ - `bundle exec rake spec`
67
+
68
+ ## Contributing
69
+
70
+ 1. Fork the project
71
+ 2. Create a descriptively named feature branch
72
+ 3. Add your feature
73
+ 4. Submit a pull request
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "jekyll-lazy-load-image"
5
+
6
+ require "pry"
7
+ Pry.start
8
+
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path("lib", __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require "jekyll-lazy-load-image/version"
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = "jekyll-lazy-load-image"
9
+ spec.version = Jekyll::LazyLoadImage::VERSION
10
+ spec.authors = ["Tadayuki Onishi"]
11
+ spec.email = ["tt.tanishi100@gmail.com"]
12
+ spec.summary = "Edit img tag optimized lazy load images for your Jekyll site"
13
+ spec.homepage = "https://github.com/kenchan0130/jekyll-lazy-load-image"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
17
+ `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r!^(test|spec|features)/!)
19
+ end
20
+ end
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.required_ruby_version = ">= 2.4.0"
24
+
25
+ spec.add_dependency "jekyll", "~> 3.8"
26
+ spec.add_dependency "nokogiri", "~> 1.8"
27
+
28
+ spec.add_development_dependency "bundler", "~> 1.16"
29
+ end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "jekyll-lazy-load-image/version"
4
+ require "jekyll/lazy_load_image"
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Jekyll
4
+ module LazyLoadImage
5
+ VERSION = "0.1.0"
6
+ end
7
+ end
@@ -0,0 +1,45 @@
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
@@ -0,0 +1,22 @@
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
@@ -0,0 +1,37 @@
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
@@ -0,0 +1,46 @@
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
@@ -0,0 +1,72 @@
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
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jekyll-lazy-load-image
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Tadayuki Onishi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-07-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: jekyll
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.8'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.8'
27
+ - !ruby/object:Gem::Dependency
28
+ name: nokogiri
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.8'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.8'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.16'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.16'
55
+ description:
56
+ email:
57
+ - tt.tanishi100@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".rubocop.yml"
65
+ - ".travis.yml"
66
+ - Gemfile
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - bin/console
71
+ - bin/setup
72
+ - jekyll-lazy-load-image.gemspec
73
+ - lib/jekyll-lazy-load-image.rb
74
+ - 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
+ homepage: https://github.com/kenchan0130/jekyll-lazy-load-image
81
+ licenses:
82
+ - MIT
83
+ metadata: {}
84
+ post_install_message:
85
+ rdoc_options: []
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: 2.4.0
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ requirements: []
99
+ rubyforge_project:
100
+ rubygems_version: 2.7.7
101
+ signing_key:
102
+ specification_version: 4
103
+ summary: Edit img tag optimized lazy load images for your Jekyll site
104
+ test_files: []