44require 'mini_magick'
55
66module Jekyll
7+ # Jekyll Image Filters
8+ # =========================
9+ #
10+ # This module provides a set of Liquid filters and tags for creating responsive images in Jekyll.
11+ #
12+ # It allows you to easily generate multiple versions of an image in different formats and resolutions,
13+ # and includes them in your Jekyll site using a simple syntax.
14+ #
15+ # Usage
16+ # -----
17+ #
18+ # To use this module, simply add the following line to your Jekyll site's `Gemfile`:
19+ #
20+ # gem 'jekyll-transcode-image-filters'
21+ #
22+ # Then, in your Jekyll templates, you can use the following Liquid filters and tags:
23+ #
24+ # * `jpg`: Convert an image to JPEG format
25+ # * `webp`: Convert an image to WebP format
26+ # * `avif`: Convert an image to AVIF format
27+ #
28+ # See the documentation for each filter and tag for more information on usage and options.
29+ #
30+ # Contributing
31+ # ------------
32+ #
33+ # Contributions to this module are welcome! Please see the CONTRIBUTING file for more information.
34+ #
35+ # License
36+ # -------
37+ #
38+ # This module is released under the MIT License. See the LICENSE file for more information.
739 module TranscodeImageFilters
840 # Computes a sanitized name for use as a directory.
941 # @param name The (file) name to sanitize.
@@ -30,7 +62,7 @@ def _compute_cache_filename(absolute_path_source, resolution, format)
3062 end
3163
3264 # Compute all necessary paths for the plugin.
33- # @param absolute_path_site [String] As an example: "C:/jekyll/jekyll-transcode-image-filters "
65+ # @param absolute_path_site [String] As an example: "C:/jekyll/jekyll_transcode_image_filters "
3466 # @param relative_path_source [String] As an example: "/assets/image-a.png" or "/assets/style/main.css"
3567 # @param relative_cache_dir [String] Directory of the cache folder, as an example: "cache/bmp/"
3668 # @param resolution [String] As an example: 900x900
@@ -51,25 +83,30 @@ def _compute_paths(absolute_path_site, relative_path_source, relative_cache_dir,
5183 file_name_destination = _compute_cache_filename ( absolute_path_source , resolution , format )
5284
5385 relative_path_cache = File . join ( relative_cache_dir , file_name_source_sanitized )
54- absolute_path_cache = File . join ( absolute_path_site , relative_cache_dir , file_name_source_sanitized )
86+ absolute_path_cache = File . join ( absolute_path_site , relative_cache_dir ,
87+ file_name_source_sanitized )
5588 absolute_path_destination = File . join ( absolute_path_cache , file_name_destination )
56- relative_path_destination = File . join ( relative_cache_dir , file_name_source_sanitized , file_name_destination )
57-
58- [ absolute_path_source , absolute_path_destination , absolute_path_cache , file_name_destination ,
59- relative_path_destination , relative_path_cache ]
89+ relative_path_destination = File . join ( relative_cache_dir , file_name_source_sanitized ,
90+ file_name_destination )
91+
92+ [
93+ absolute_path_source , absolute_path_destination ,
94+ absolute_path_cache , file_name_destination ,
95+ relative_path_destination , relative_path_cache
96+ ]
6097 end
6198
6299 # Determine whether the file exists in the cache.
63- # @param absolute_path_source [String] As an example: "C:/jekyll/jekyll-transcode-image-filters "
100+ # @param absolute_path_source [String] As an example: "C:/jekyll"
64101 # @param absolute_path_destination [String] As an example: "/assets/image-a.png" or "/assets/image-a.bmp"
65102 # @return [Boolean] If true, the file exists in the cache.
66103 def _in_cache? ( _absolute_path_source , absolute_path_destination )
67104 File . exist? ( absolute_path_destination )
68105 end
69106
70107 # Read, process, and write the (new) image to disk.
71- # @param absolute_path_source [String] As an example: "C:/jekyll/jekyll-transcode-image-filters/ assets/image-a.png"
72- # @param absolute_path_destination [String] As an example: "C:/jekyll/jekyll-transcode-image-filters/ assets/image-a-processed.png"
108+ # @param absolute_path_source [String] As an example: "C:/jekyll/assets/image-a.png"
109+ # @param absolute_path_destination [String] As an example: "C:/jekyll/assets/image-a-processed.png"
73110 def _process_img ( absolute_path_source , absolute_path_destination , resolution , format )
74111 image = MiniMagick ::Image . open ( absolute_path_source )
75112 initial_size = image . size
@@ -86,7 +123,8 @@ def _process_img(absolute_path_source, absolute_path_destination, resolution, fo
86123
87124 image . write absolute_path_destination
88125
89- puts "Transcoded image '#{ absolute_path_source } ' to '#{ absolute_path_destination } ' (#{ initial_size / 1024 } kb -> #{ image . size / 1024 } kb)"
126+ statistics = "#{ initial_size / 1024 } kb -> #{ image . size / 1024 } kb"
127+ puts "Transcoded image '#{ absolute_path_source } ' to '#{ absolute_path_destination } ' (#{ statistics } )"
90128 end
91129
92130 # Transcodes an image to the given format and resolution. Processed files are cached to speed up builds.
@@ -98,24 +136,24 @@ def _process_img(absolute_path_source, absolute_path_destination, resolution, fo
98136 def _transcode_image ( relative_source_path , cache_dir , resolution , format )
99137 site = @context . registers [ :site ]
100138
101- absolute_path_source , absolute_path_destination , absolute_path_cache , file_name_destination , relative_path_destination , relative_path_cache = _compute_paths (
139+ absolute_path_source ,
140+ absolute_path_destination ,
141+ absolute_path_cache ,
142+ file_name_destination ,
143+ relative_path_destination ,
144+ relative_path_cache = _compute_paths (
102145 site . source , relative_source_path , cache_dir , resolution , format
103146 )
104147
105148 # Guarantee the existence of the cache directory
106149 FileUtils . mkdir_p ( absolute_path_cache )
107150
108- if _in_cache? ( absolute_path_source , absolute_path_destination )
109- # if the file is cached and valid we can just return the relative path
110- return relative_path_destination
111- else
112- # otherwise, we process the file and add it to the cache
113-
114- _process_img ( absolute_path_source , absolute_path_destination , resolution , format )
115- site . static_files << Jekyll ::StaticFile . new ( site , site . source , relative_path_cache , file_name_destination )
116- end
151+ # if the file is cached and valid we can just return the relative path
152+ return relative_path_destination if _in_cache? ( absolute_path_source , absolute_path_destination )
117153
118- relative_path_destination
154+ # otherwise, we process the file and add it to the cache
155+ _process_img ( absolute_path_source , absolute_path_destination , resolution , format )
156+ site . static_files << Jekyll ::StaticFile . new ( site , site . source , relative_path_cache , file_name_destination )
119157 end
120158
121159 # Transcodes an image to the webp format in the given resolution. Processed files are cached to speed up builds.
0 commit comments