Class: Jekyll::GalleryGenerator

Inherits:
Generator
  • Object
show all
Defined in:
lib/jekyll-photo-gallery.rb

Instance Method Summary collapse

Instance Method Details

#generate(site) ⇒ Object



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/jekyll-photo-gallery.rb', line 183

def generate(site)
  # Get configuration option.
  config = site.config["photo_gallery"] || {}
  path = config["path"] || "photos"
  original_dir = Dir.getwd
  # Change into the photo gallery directory.
  Dir.chdir(site.source)
  begin
    puts "Generating galleries ..."
    gallery = GalleryPage.new(site, site.source, path, path)
    site.pages << gallery
    iterate(site, path, gallery)
  rescue Exception => e
    puts "Error generating galleries: #{e}"
    puts e.backtrace
  end
  # Change back.
  Dir.chdir(original_dir)
end

#is_empty(path) ⇒ Object



154
155
156
# File 'lib/jekyll-photo-gallery.rb', line 154

def is_empty(path)
  Dir["#{path}/*"].empty?
end

#iterate(site, basepath, parent) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/jekyll-photo-gallery.rb', line 158

def iterate(site, basepath, parent)
  galleries = []
  Dir.foreach(basepath) do |item|
    # Skip . and ..
    next if item == '.' or item == '..'
    # Complete path
    path = File.join(basepath, item)
    # Skip if not a directory
    next if !File.directory?(path)
    # Skip if directory is empty
    next if is_empty(path)
    # Create gallery
    puts "Generating gallery \"#{path}\" ..."
    gallery = GalleryPage.new(site, site.source, path, item, parent)
    site.pages << gallery
    galleries << gallery
    # Descend into the directory
    iterate(site, path, gallery)
  end
  if !parent.nil?
    parent.data["children"] = galleries
  end
  galleries
end