Class: ImageGallery::GalleryGenerator
- Inherits:
-
Jekyll::Generator
- Object
- Jekyll::Generator
- ImageGallery::GalleryGenerator
- Defined in:
- lib/gallery/gallery.rb
Defined Under Namespace
Classes: GalleryImageModel, GalleryModel
Constant Summary collapse
- IGNORED_FILES =
['.ds_store', 'thumbs.db'].freeze
Instance Method Summary collapse
- #construct_gallery(gallery_path_abs, gallery_images) ⇒ Object
- #copy_images(site, gallery, gallery_path_rel, gallery_images_and_times) ⇒ Object
- #exif_cache ⇒ Object
- #generate(site) ⇒ Object
- #load_metadata_from_dir(path) ⇒ Object
- #transform_metadata(raw_data) ⇒ Object
Instance Method Details
#construct_gallery(gallery_path_abs, gallery_images) ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/gallery/gallery.rb', line 115 def construct_gallery(gallery_path_abs, gallery_images) gallery = {} _ancestors, gallery_id = Pathname.new(gallery_path_abs).split gallery_id = gallery_id.to_s gallery_regex = /^(\d{4})[-_]?(\d{2})[-_]?(\d{2})[-_]?(.*)$/ gallery_dir_match = gallery_id.match(gallery_regex) if gallery_dir_match gallery['id'] = gallery_dir_match[4] gallery['name'] = gallery_dir_match[4].split(/[_-]+/).map(&:capitalize).join(' ') gallery['datetime'] = DateTime.new(gallery_dir_match[1].to_i, gallery_dir_match[2].to_i, gallery_dir_match[3].to_i) else ImageGallery._log(:debug, "Gallery name #{gallery_id} doesn't match the expected regex #{gallery_regex}") gallery['id'] = gallery_id gallery['name'] = gallery_id.split(/[_-]+/).map(&:capitalize).join(' ') end earliest_image = gallery_images.reject { |gi| gi[:creation_datetime].nil? }.min { |gi| gi[:creation_datetime] } gallery['datetime'] = earliest_image[:creation_datetime] if earliest_image = (gallery_path_abs) gallery = gallery.merge(()) if if gallery['datetime'].nil? raise "Gallery at #{gallery_path_abs} has no datetime specified - it needs to be in EXIF data, the gallery prefix or the _metadata.json file" end GalleryModel.new(*gallery.transform_keys(&:to_sym).values_at(*GalleryModel.members)) end |
#copy_images(site, gallery, gallery_path_rel, gallery_images_and_times) ⇒ Object
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/gallery/gallery.rb', line 176 def copy_images(site, gallery, gallery_path_rel, gallery_images_and_times) image_directory = File.join(*ImageGallery.gallery_path_array(site, gallery.datetime.year, gallery.datetime.month)) gallery_images_and_times.map do |img_and_time| src_image_file_path = img_and_time[:path] src_image_file_name = File.basename(src_image_file_path) sha = Digest::SHA256.hexdigest(src_image_file_path) image_file_name = "#{sha}#{File.extname(src_image_file_path)}" thumb_file_name = "#{sha}_t#{File.extname(src_image_file_path)}" config = ImageGallery._config_with_defaults(site) image_file = CompressedStaticGalleryFile.new(site, site.source, gallery_path_rel, src_image_file_name, image_directory, image_file_name) do |i| if config['image_size'] i.resize("#{config['image_size']['x']}x#{config['image_size']['y']}>") ImageGallery._log(:info, "Processing image #{src_image_file_path} to extents #{config['image_size']}") else ImageGallery._log(:info, "Processing image #{src_image_file_path}") end end thumb_file = CompressedStaticGalleryFile.new(site, site.source, gallery_path_rel, src_image_file_name, image_directory, thumb_file_name) do |i| i.gravity('Center') i.resize("#{config['thumbnail_size']['x']}x#{config['thumbnail_size']['y']}^") i.extent("#{config['thumbnail_size']['x']}x#{config['thumbnail_size']['y']}") ImageGallery._log(:info, "Processing image #{src_image_file_path} to #{config['thumbnail_size']}") end site.static_files.push(image_file).push(thumb_file) GalleryImageModel.new( File.join('/', image_file.relative_destination), File.join('/', thumb_file.relative_destination), img_and_time[:creation_datetime], src_image_file_path ) end end |
#exif_cache ⇒ Object
214 215 216 |
# File 'lib/gallery/gallery.rb', line 214 def exif_cache @@exif_cache ||= Jekyll::Cache.new('jekyll-image-gallery::exif') end |
#generate(site) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/gallery/gallery.rb', line 34 def generate(site) galleries_dir = File.join(site.source, '_galleries') unless File.directory?(galleries_dir) ImageGallery._log(:warn, "No directory found at #{galleries_dir}, skipping gallery generation") return end all_galleries = Dir.chdir(galleries_dir) do Dir.glob('**/*/') end galleries = [] all_galleries.each do |gallery_path| gallery_path_abs = File.join(site.source, '_galleries', gallery_path) source_images = Dir.children(gallery_path_abs) .select { |file_name| File.file?(File.join(gallery_path_abs, file_name)) } .reject { |file_name| File.basename(file_name, '.*') == '_metadata' } .reject { |file_name| IGNORED_FILES.include?(file_name.downcase) } next if source_images.empty? gallery_path_rel = File.join('_galleries', gallery_path) gallery_images_and_times = source_images.map do |src_image_file_name| src_image_file_path = File.join(gallery_path_rel, src_image_file_name) # TODO: this assumes the image at a path doesn't change exif = exif_cache.getset(src_image_file_path) do ImageGallery._log_once(:info, 'Loading EXIF data. This may take some time on the first run') MiniMagick::Image.open(src_image_file_path).exif end image_time = DateTime.strptime(exif['DateTimeOriginal'], '%Y:%m:%d %H:%M:%S') if exif['DateTimeOriginal'] { path: src_image_file_path, creation_datetime: image_time, } end gallery = construct_gallery(gallery_path_abs, gallery_images_and_times) gallery.images = copy_images(site, gallery, gallery_path_rel, gallery_images_and_times).sort_by do |gi| if gi.creation_datetime.nil? 1.0 / 0.0 else gi.creation_datetime end end.reverse gallery.highlight_image = gallery.images.detect do |gi| basename = File.basename(gi.original_path, '.*') basename.start_with?('hl_') || basename.end_with?('_hl') end || gallery.images[0] galleries << gallery site.pages << GalleryPage.new(site, gallery) end galleries = galleries.sort_by(&:datetime) galleries.reverse! galleries.group_by { |gallery| gallery.datetime.year }.each do |year, galleries| site.pages << GalleryIndexPage.new(site, year, galleries) end site.data['galleries'] = galleries gallery_years = galleries.group_by { |gallery| gallery.datetime.year }.keys.sort site.data['gallery_years'] = gallery_years if ImageGallery._config_with_defaults(site)['generate_root_index'] last_gallery_year = gallery_years.max site.pages << GalleryIndexPage.new( site, last_gallery_year, galleries.select { |g| g.datetime.year == last_gallery_year }, override_dir: File.join(*ImageGallery.gallery_path_array(site)) ) end end |
#load_metadata_from_dir(path) ⇒ Object
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/gallery/gallery.rb', line 146 def (path) ext_to_parsers = [ ['toml', ->(f) { TOML.load_file(f) }], ['json', ->(f) { JSON.parse(File.new(f)) }], ] path_to_parsers = ext_to_parsers.map do |(ext, fn)| full_path = File.join(path, "_metadata.#{ext}") [full_path, fn] end first_existing_file = path_to_parsers.find do |(full_path, _fn)| File.file?(full_path) end if first_existing_file full_path, fn = first_existing_file fn.call(full_path) end end |
#transform_metadata(raw_data) ⇒ Object
166 167 168 169 170 171 172 173 174 |
# File 'lib/gallery/gallery.rb', line 166 def (raw_data) raw_data.map do |k, v| if k == 'datetime' [k, DateTime.parse(v)] else [k, v] end end.to_h end |