jekyll-picture-tag-ng 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 3a3bd675a4d3cc241525f392330a1c83bb216ea3a0b493af4c6c962e64443719
4
+ data.tar.gz: b97828fb4b03cffe94b235a6ee09212b2d9f12c0c8bbd24ecbc1772809edebf6
5
+ SHA512:
6
+ metadata.gz: b3aafd32911452696625b1f5cff39b86bfe707e3caa7d41b94a2172834c0d229884d5517b44bb7c7da13985e8e2a6164727fb3680f03100ba024bb3317df6be2
7
+ data.tar.gz: 8720af12c5eaa95d89edd1ce3300611c8d1037b3b9ff2cadccd8d9548b9190127b8541c36a3675a98b61fbda0eae27b1dc5668c85985053563646f82f2f8960b
data/.rubocop.yml ADDED
@@ -0,0 +1,25 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.6
3
+
4
+ Style/StringLiterals:
5
+ Enabled: true
6
+ EnforcedStyle: double_quotes
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ Enabled: true
10
+ EnforcedStyle: double_quotes
11
+
12
+ Layout/LineLength:
13
+ Max: 120
14
+
15
+ Metrics/MethodLength:
16
+ Enabled: false
17
+
18
+ Metrics/AbcSize:
19
+ Enabled: false
20
+
21
+ Naming/MethodParameterName:
22
+ Enabled: false
23
+
24
+ Naming/FileName:
25
+ Enabled: false
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in jekyll-picture_tag.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rubocop", "~> 1.21"
11
+
12
+ gem "jekyll", "~> 4.3"
13
+
14
+ gem "kramdown", "~> 2.4"
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 pcouy
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.
data/README.md ADDED
@@ -0,0 +1,73 @@
1
+ # Jekyll Picture Tag NG
2
+
3
+ This plugin will automatically generate variants of your pictures on build, and change the Kramdown rendering to use the variants with HTML picture tags when including pictures from markdown.
4
+
5
+ ## Installation
6
+
7
+ Install the gem and add to the application's Gemfile by executing:
8
+
9
+ $ bundle add jekyll-picture-tag-ng
10
+
11
+ If bundler is not being used to manage dependencies, install the gem by executing:
12
+
13
+ $ gem install jekyll-picture-tag-ng
14
+
15
+ Additionally, you will need [ImageMagick](https://imagemagick.org/) installed on the system you're using to build your website in order to generate the picture variations. On Debian/Ubuntu systems, you can execute:
16
+
17
+ $ sudo apt-get install imagemagick
18
+
19
+ After installing, update your `_config.yml` to include the plugin :
20
+
21
+ ```
22
+ plugins: [other-plugins, jekyll-picture-tag-ng]
23
+ ```
24
+
25
+ ### Using with GitHub Pages
26
+
27
+ If you're using GitHub Pages to deploy your site, you'll need to use a custom action to be able to use this plugin and install ImageMagick. You can create such GitHub action by browsing to `https://github.com/{YOUR/REPO}/new/main?filename=.github%2Fworkflows%2Fjekyll.yml&workflow_template=pages%2Fjekyll`. You will need to add the following lines as a step for the build job of your GitHub action (before the `jekyll build` command) :
28
+
29
+ ```
30
+ - name: Install imagemagick
31
+ run: sudo apt-get update && sudo apt-get install imagemagick
32
+ ```
33
+
34
+ After adding the custom action to your repository, you'll need to update the repository settings on GitHub : go to the "Pages" section and change the "Source" setting from "Deploy from a branch" to "GitHub Actions".
35
+
36
+ ## Usage
37
+
38
+ By installing the plugin and activating it, `jekyll build` and `jekyll serve` commands will perform an additional step to generate several versions of your `jpeg` and `webp` files : for each of these files in the source directory, and each version defined, a file will be output in the `img/{version}` directory of the rendered website.
39
+
40
+ When using the default markdown syntax for including pictures (`![Alt text](PICTURE_URL)`) with the Kramdown renderer, `<picture>` tags with appropriate `<source>` children tags will be output.
41
+
42
+ When working locally, it is recommended to use the `--incremental` option which will prevent Jekyll from re-generating all the picture versions (can take quite some time) every time you save a file. However, there is a catch : when using `--incremental`, if you edit your configuration file to change the output formats and build again, your pages will not be updated with the appropriate `<source>` tags for the new versions.
43
+
44
+ ### Configuration
45
+
46
+ Configuration is done in the `_config.yml` of your website, under the `picture_tag_ng` variable :
47
+
48
+ ```yaml
49
+ picture_tag_ng:
50
+ background_color: FFFFFF
51
+ picture_versions:
52
+ m: 700
53
+ s: 400
54
+ ```
55
+
56
+ The example above is equivalent to the defaults.
57
+
58
+ - `background_color` is the color used to replace transparency when converting from `webp` to `jpeg`
59
+ - `picture_versions` maps version names to target widths in pixels. The default configuration above produces output files 700px wide in `img/m/` and 400px wide in `img/s/`.
60
+
61
+ ## Development
62
+
63
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
64
+
65
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
66
+
67
+ ## Contributing
68
+
69
+ Bug reports and pull requests are welcome on GitHub at https://github.com/pcouy/jekyll-picture-tag-ng.
70
+
71
+ ## License
72
+
73
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rubocop/rake_task"
5
+
6
+ RuboCop::RakeTask.new
7
+
8
+ task default: :rubocop
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Jekyll
4
+ module PictureTag
5
+ VERSION = "0.1.0"
6
+ end
7
+ end
@@ -0,0 +1,168 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "jekyll"
4
+ require "kramdown"
5
+ require_relative "jekyll-picture-tag-ng/version"
6
+
7
+ module Jekyll
8
+ # Override default static file to make some instance variables readable
9
+ class StaticFile
10
+ attr_reader :site, :dest, :dir, :name
11
+ end
12
+
13
+ module PictureTag
14
+ CONFIG = {
15
+ "picture_versions" => {
16
+ "s" => "400",
17
+ "m" => "700"
18
+ },
19
+ "background_color" => "FFFFFF"
20
+ }.freeze
21
+
22
+ class Error < StandardError; end
23
+
24
+ # Class that holds generated variants of pictures
25
+ class OutImageFile < StaticFile
26
+ def initialize(site, orig_static_file, version, pictype)
27
+ super(site, site.source, orig_static_file.dir, orig_static_file.name)
28
+ @version = version
29
+ @picture_dim = picture_versions[@version]
30
+ @pictype = pictype
31
+ @collection = nil
32
+ end
33
+
34
+ def config
35
+ @config ||= CONFIG.merge(@site.config["picture_tag_ng"] || {})
36
+ end
37
+
38
+ def picture_versions
39
+ config["picture_versions"]
40
+ end
41
+
42
+ def picture?
43
+ extname =~ /(\.jpg|\.jpeg|\.webp)$/i
44
+ end
45
+
46
+ def destination(dest)
47
+ output_dir = File.join("img", @version, @dir)
48
+ output_basename = @site.in_dest_dir(@site.dest, File.join(output_dir, "#{basename}.#{@pictype}"))
49
+ FileUtils.mkdir_p(File.dirname(output_dir))
50
+ @destination ||= {}
51
+ @destination[dest] ||= output_basename
52
+ end
53
+
54
+ def write(*args)
55
+ Jekyll.logger.debug "write : #{args} Modified : #{modified?}"
56
+ super(*args)
57
+ end
58
+
59
+ def popen_args(dest_path)
60
+ args = ["convert", @path, "-resize", "#{@picture_dim}x>"]
61
+ if @pictype == "jpg"
62
+ args.concat ["-background", "##{@config["background_color"]}",
63
+ "-flatten", "-alpha", "off"]
64
+ end
65
+ args.push dest_path
66
+ end
67
+
68
+ def copy_file(dest_path)
69
+ Jekyll.logger.debug "copy_file : #{path} -> #{dest_path}"
70
+ p = IO.popen(popen_args(dest_path))
71
+ p.close
72
+ File.utime(self.class.mtimes[path], self.class.mtimes[path], dest_path)
73
+ end
74
+ end
75
+
76
+ # Will generate the picture variants
77
+ class PicsGenerator < Generator
78
+ safe true
79
+ priority :lowest
80
+
81
+ def generate(site)
82
+ @config ||= CONFIG.merge(site.config["picture_tag_ng"] || {})
83
+ @picture_versions = @config["picture_versions"]
84
+ new_statics = []
85
+ site.static_files.filter { |f| f.extname =~ /(\.jpg|\.jpeg|\.webp)$/i }.each do |f|
86
+ @config["picture_versions"].each do |v, _s|
87
+ img_f = OutImageFile.new(site, f, v, "jpg")
88
+ new_statics << img_f
89
+ img_f = OutImageFile.new(site, f, v, "webp")
90
+ new_statics << img_f
91
+ end
92
+ end
93
+
94
+ new_statics.each { |f| site.static_files << f }
95
+ end
96
+ end
97
+ end
98
+ end
99
+
100
+ Jekyll::Hooks.register :site, :after_init do |site|
101
+ Kramdown::Converter::JEKYLL_SITE = site
102
+ end
103
+
104
+ module Kramdown
105
+ module Parser
106
+ # Override Kramdown parser
107
+ class Kramdown
108
+ def add_link(el, href, title, alt_text = nil, ial = nil)
109
+ el.options[:ial] = ial
110
+ update_attr_with_ial(el.attr, ial) if ial
111
+ if el.type == :a
112
+ el.attr["href"] = href
113
+ else
114
+ el.attr["src"] = href
115
+ el.attr["alt"] = alt_text
116
+ el.attr["loading"] = el.attr["loading"] || "lazy"
117
+ el.children.clear
118
+ end
119
+ el.attr["title"] = title if title
120
+ @tree.children << el
121
+ end
122
+
123
+ require "kramdown/parser/kramdown"
124
+ end
125
+ end
126
+
127
+ module Converter
128
+ # Override Kramdown HTML converter
129
+ class Html
130
+ def site_config
131
+ @site_config ||= Jekyll::PictureTag::CONFIG.merge(JEKYLL_SITE.config["picture_tag_ng"] || {})
132
+ end
133
+
134
+ def picture_versions
135
+ site_config["picture_versions"]
136
+ end
137
+
138
+ def convert_img(el, _indent)
139
+ require "cgi"
140
+ res = "<picture>"
141
+ new_src = el.attr["src"]
142
+ if File.extname(el.attr["src"]) =~ /(\.jpg|\.jpeg|\.webp)$/i
143
+ picture_versions.each_with_index do |(version, geometry), index|
144
+ src_base = File.join(
145
+ "/img",
146
+ version,
147
+ File.dirname(el.attr["src"]).split("/").map do |x|
148
+ x.gsub(" ", "%20")
149
+ end.join("/"),
150
+ File.basename(el.attr["src"], File.extname(el.attr["src"])).gsub(" ", "%20")
151
+ )
152
+ if index == picture_versions.size - 1
153
+ media = ""
154
+ new_src = "#{src_base}.jpg"
155
+ else
156
+ media = "media=\"(max-width: #{geometry}px)\""
157
+ end
158
+ res += "<source #{media} srcset=\"#{src_base}.webp\" type=\"image/webp\">"
159
+ res += "<source #{media} srcset=\"#{src_base}.jpg\" type=\"image/jpeg\">"
160
+ end
161
+ end
162
+ el.attr["src"] = new_src
163
+ res += "<img#{html_attributes(el.attr)}>"
164
+ res += "</picture>"
165
+ end
166
+ end
167
+ end
168
+ end
@@ -0,0 +1,6 @@
1
+ module Jekyll
2
+ module PictureTag
3
+ VERSION: String
4
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
5
+ end
6
+ end
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jekyll-picture-tag-ng
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - pcouy
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-04-10 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: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: kramdown
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '13.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '13.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubocop
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.21'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.21'
69
+ description: |-
70
+ This plugin will auto-generate alternative versions of your jpg and webp files. For each specified
71
+ version, a jpeg and a webp version will be generated. When including images in markdown documents, the HTML `picture`
72
+ tag will be used to display the appropriate version using media queries.
73
+ email:
74
+ - contact@pierre-couy.dev
75
+ executables: []
76
+ extensions: []
77
+ extra_rdoc_files: []
78
+ files:
79
+ - ".rubocop.yml"
80
+ - Gemfile
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - lib/jekyll-picture-tag-ng.rb
85
+ - lib/jekyll-picture-tag-ng/version.rb
86
+ - sig/jekyll/picture_tag.rbs
87
+ homepage: https://github.com/pcouy/jekyll-picture-tag-ng
88
+ licenses:
89
+ - MIT
90
+ metadata:
91
+ allowed_push_host: https://rubygems.org
92
+ homepage_uri: https://github.com/pcouy/jekyll-picture-tag-ng
93
+ source_code_uri: https://github.com/pcouy/jekyll-picture-tag-ng
94
+ changelog_uri: https://github.com/pcouy/jekyll-picture-tag-ng/main/CHANGELOG.md
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: 2.6.0
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubygems_version: 3.3.25
111
+ signing_key:
112
+ specification_version: 4
113
+ summary: Replace the default Kramdown rendering of pictures to use auto-generated
114
+ alternatives
115
+ test_files: []