Created
May 22, 2014 15:11
-
-
Save AndrewBelt/9a51cc1427209d889fe9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Static HTML gallery generator in Ruby | |
# v0.1 | |
# Andrew Belt | |
# Public Domain | |
require 'haml' | |
require 'RMagick' | |
class Page | |
INDEX_TEMPLATE = <<EOS | |
!!! | |
%html | |
%head | |
:css | |
%body | |
.folders | |
%ul | |
- @folders.each do |folder| | |
%li< | |
%a{href: folder}= folder | |
.images | |
- @images.each do |image| | |
%a{href: image} | |
%img{src: 'thumb_' + image} | |
EOS | |
INDEX_ENGINE = Haml::Engine.new(INDEX_TEMPLATE) | |
def initialize(path) | |
@path = path | |
end | |
def generate | |
# Filter list of folders and images | |
entries = Dir.entries(@path) | |
@folders = entries.select {|f| | |
File.directory? File.join(@path, f) | |
}.sort | |
@images = entries.select {|f| | |
File.file? File.join(@path, f) and f.match(/^(?!thumb_).+\.(jpe?g|gif|png)$/i) | |
}.sort | |
# Generate thumbnails | |
@images.each do |f| | |
img_path = File.join(@path, f) | |
puts "Generating thumbnail for #{img_path}..." | |
img = Magick::Image::read(img_path).first | |
thumb = img.resize_to_fill(100, 100) | |
thumb.write(File.join(@path, 'thumb_' + f)) | |
end | |
# Render index.html | |
index_path = File.join(@path, 'index.html') | |
puts "Rendering #{index_path}..." | |
html = INDEX_ENGINE.render(self) | |
File.write(File.join(@path, 'index.html'), html) | |
# Recurse into the folders | |
@folders.each do |folder| | |
next if folder.start_with? '.' | |
Page.new(File.join(@path, folder)).generate | |
end | |
end | |
end | |
Page.new('.').generate |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment