Class: Jekyll::GalleryPage

Inherits:
ReadYamlPage show all
Defined in:
lib/jekyll-gallery-generator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from ReadYamlPage

#read_yaml

Constructor Details

#initialize(site, base, dir, gallery_name) ⇒ GalleryPage

Returns a new instance of GalleryPage.



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/jekyll-gallery-generator.rb', line 129

def initialize(site, base, dir, gallery_name)
  @site = site
  @base = base
  @dest_dir = dir.gsub("source/", "")
  @dir = @dest_dir
  @name = "index.html"
  @images = []
  @hidden = false

  config = site.config["gallery"] || {}
  gallery_config = {}
  max_size_x = 400
  max_size_y = 400
  symlink = config["symlink"] || false
  scale_method = config["scale_method"] || "fit"
  begin
    max_size_x = config["thumbnail_size"]["x"]
  rescue Exception
  end
  begin
    max_size_y = config["thumbnail_size"]["y"]
  rescue Exception
  end
  begin
    gallery_config = config["galleries"][gallery_name] || {}
  rescue Exception
  end
  self.process(@name)
  gallery_page = File.join(base, "_layouts", "gallery_page.html")
  unless File.exists?(gallery_page)
    gallery_page = File.join(File.dirname(__FILE__), "gallery_page.html")
  end
  self.read_yaml(File.dirname(gallery_page), File.basename(gallery_page))
  self.data["gallery"] = gallery_name
  gallery_title_prefix = config["title_prefix"] || "Photos: "
  gallery_name = gallery_name.gsub(/[_-]/, " ").gsub(/\w+/) {|word| word.capitalize}
  begin
    gallery_name = gallery_config["name"] || gallery_name
  rescue Exception
  end
  self.data["name"] = gallery_name
  self.data["title"] = "#{gallery_title_prefix}#{gallery_name}"
  self.data["exif"] = {}
  begin
    @hidden = gallery_config["hidden"] || false
  rescue Exception
  end
  if @hidden
    self.data["sitemap"] = false
  end

  thumbs_dir = File.join(site.dest, @dest_dir, "thumbs")
  FileUtils.mkdir_p(thumbs_dir, :mode => 0755)
  date_times = {}
  entries = Dir.entries(dir)
  entries.each_with_index do |name, i|
    next if name.chars.first == "."
    next unless name.downcase().end_with?(*$image_extensions)
    image = GalleryImage.new(name, dir)
    @images.push(image)
    date_times[name] = image.date_time
    @site.static_files << GalleryFile.new(site, base, File.join(@dest_dir, "thumbs"), name)

    if symlink
      link_src = site.in_source_dir(image.path)
      link_dest = site.in_dest_dir(image.path)
      @site.static_files.delete_if {|sf|
        sf.relative_path == "/" + image.path
      }
      @site.static_files << GalleryFile.new(site, base, dir, name)
      if File.exists?(link_dest) or File.symlink?(link_dest)
        if not File.symlink?(link_dest)
          puts "#{link_dest} exists but is not a symlink. Deleting."
          File.delete(link_dest)
        elsif File.readlink(link_dest) != link_src
          puts "#{link_dest} points to the wrong file. Deleting."
          File.delete(link_dest)
        end
      end
      if not File.exists?(link_dest) and not File.symlink?(link_dest)
        puts "Symlinking #{link_src} -> #{link_dest}"
        File.symlink(link_src, link_dest)
      end
    end
    thumb_path = File.join(thumbs_dir, name)
    if File.file?(thumb_path) == false or File.mtime(image.path) > File.mtime(thumb_path)
      begin
        m_image = ImageList.new(image.path)
        m_image.auto_orient!
        m_image.send("resize_to_#{scale_method}!", max_size_x, max_size_y)
        puts "Writing thumbnail to #{thumb_path}"
        m_image.write(thumb_path)
      rescue Exception => e
        printf "Error generating thumbnail for #{image.path}: #{e}\r"
        puts e.backtrace
      end
      if i % 5 == 0
        GC.start
      end
    end

    printf "#{gallery_name} #{i+1}/#{entries.length} images\r"
  end
  puts ""

  begin
    sort_field = gallery_config["sort_field"] || "date_time"
    if sort_field == "date_time"
      @images.sort!
    elsif sort_field == "name"
      @images.sort! {|a,b| cmp = a.name <=> b.name}
    else
      puts "Invalid sort_field for gallery #{gallery_name}: #{sort_field}. Sorting by datetime."
      @images.sort!
    end

    if gallery_config["sort_reverse"]
      @images.reverse!
    end
  rescue Exception => e
    puts "Error sorting images in gallery #{gallery_name}: #{e}"
    puts e.backtrace
  end

  site.static_files = @site.static_files
  self.data["images"] = @images
  best_image = nil
  if @images.length > 0
    best_image = @images[0].name
  end
  best_image = gallery_config["best_image"] || best_image
  self.data["best_image"] = best_image
  if date_times.has_key?(best_image)
    gallery_date_time = date_times[best_image]
  else
    puts "#{gallery_name}: best_image #{best_image} not found!"
    gallery_date_time = 0
  end
  self.data["date_time"] = gallery_date_time

  self.data["info"] = gallery_config["info"] if gallery_config.key?("info")
end

Instance Attribute Details

#hiddenObject (readonly)

Returns the value of attribute hidden.



127
128
129
# File 'lib/jekyll-gallery-generator.rb', line 127

def hidden
  @hidden
end