forked from mattvh/JekyllGalleryTag
-
Notifications
You must be signed in to change notification settings - Fork 2
/
galleries.rb
executable file
·193 lines (131 loc) · 5.11 KB
/
galleries.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
114
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
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
# Jekyll GalleryTag
#
# Automatically creates thumbnails for a directory of images.
# Adds a "gallery" Liquid tag
#
# Author: Matt Harzewski
# Copyright: Copyright 2013 Matt Harzewski
# License: GPLv2 or later
# Version: 1.1.0
require "RMagick"
module Jekyll
class GalleryTag < Liquid::Block
def initialize(tag_name, markup, tokens)
super
@gallery_name = markup
@gallery_name.strip!
end
def render(context)
@config = context.registers[:site].config['gallerytag']
columns = (@config['columns'] != nil) ? @config['columns'] : 4
width = (@config['thumb_width'] != nil) ? @config['thumb_width'] : 150
height = (@config['thumb_height'] != nil) ? @config['thumb_height'] : 150
custom_attribute_name = (@config['custom_attribute_name'] != nil) ? @config['custom_attribute_name'] : 'rel'
images = gallery_images
images_html = ""
images.each_with_index do |image, key|
images_html << "<dl class=\"gallery-item\">\n"
images_html << "<dt class=\"gallery-icon\">\n"
images_html << "<a class=\"gallery-link\" href=\"#{image['url']}\" title=\"#{image['caption']}\" #{custom_attribute_name}=\"#{@gallery_name}\">"
images_html << "<img src=\"#{image['thumbnail']}\" class=\"thumbnail\" width=\"#{height}\" height=\"#{width}\" />\n"
images_html << "</a>\n"
images_html << "</dt>\n"
images_html << "<dd class=\"gallery-caption\">#{image['caption']}</dd>"
images_html << "</dl>\n\n"
images_html << "<br style=\"clear: both;\">" if (key + 1) % columns == 0
end
images_html << "<br style=\"clear: both;\">" if images.count % columns != 0
gallery_html = "<div class=\"gallery\">\n\n#{images_html}\n\n</div>\n"
return gallery_html
end
def gallery_images
input_data = block_contents
source_dir = @config['source_dir'] != nil ? @config['source_dir'].sub(/^\//, '') : (@config['url'] != nil ? @config['url'].sub(/^\//, '') : "images/thumbs");
gallery_data = []
input_data.each do |item|
hsh = {
"url" => "/#{source_dir}/#{item[0]}",
"thumbnail" => GalleryThumbnail.new(item[0], @config), #this should be url to a generated thumbnail, eventually
"caption" => item[1]
}
gallery_data.push(hsh)
end
return gallery_data
end
def block_contents
text = @nodelist[0]
lines = text.split(/\n/).map {|x| x.strip }.reject {|x| x.empty? }
lines = lines.map { |line|
line.split(/\s*::\s*/).map(&:strip)
}
return lines
end
end
class GalleryThumbnail
def initialize(image_filename, config)
@img_filename = image_filename
@config = config
end
def to_s
get_url
end
def get_url
filename = File.path(@img_filename).sub(File.extname(@img_filename), "-thumb#{File.extname(@img_filename)}")
directory = @config['destination_dir'] != nil ? @config['destination_dir'].sub(/^\//, '') : (@config['url'] != nil ? @config['url'].sub(/^\//, '') : "images/thumbs")
"/#{directory}/#{filename}"
end
end
# This part is copied from https://github.com/kinnetica/jekyll-plugins
# Without it, generation does fail. --dmytro
# Recover from strange exception when starting server without --auto
class GalleryFile < StaticFile
def write(dest)
begin
super(dest)
rescue
end
true
end
end
class ThumbGenerator < Generator
def generate(site)
@config = site.config['gallerytag']
@gallery_dir = File.expand_path(@config['source_dir'] != nil ? @config['source_dir'] : (@config['dir'] != nil ? @config['dir'].sub(/^\//, '') : "images/gallery"))
@gallery_dest = @config['destination_dir'] != nil ? @config['destination_dir'] : (@config['url'] != nil ? @config['url'].sub(/^\//, '') : "images/thumbs")
@gallery_full_dest = File.expand_path(File.join(site.source, @gallery_dest))
thumbify(files_to_resize(site))
end
def files_to_resize(site)
to_resize = []
Dir.glob(File.join(@gallery_dir, "**", "*.{png,jpg,jpeg,gif}")).each do |file|
if !File.basename(file).include? "-thumb"
# generate thumbnails in same folder as original files
file_directory = File.dirname(file).sub(@gallery_dir, '');
name = File.join(file_directory, File.basename(file).sub(File.extname(file), "-thumb#{File.extname(file)}"))
thumbname = File.join(@gallery_full_dest, name)
# Keep the thumb files from being cleaned by Jekyll
site.static_files << Jekyll::GalleryFile.new(site, site.source, @gallery_dest + "/" + file_directory, File.basename(name))
if !File.exists?(thumbname)
to_resize.push({ "file" => file, "thumbname" => thumbname })
end
end
end
return to_resize
end
def thumbify(items)
if items.count > 0
items.each do |item|
img = Magick::Image.read(item['file']).first
thumb = img.resize_to_fill!(@config['thumb_width'], @config['thumb_height'])
# create directory for thumbnail if it not exists
if !Dir.exists?(File.dirname(item['thumbname']))
FileUtils.mkdir_p File.dirname(item['thumbname'])
end
thumb.write(item['thumbname'])
thumb.destroy!
end
end
end
end
end
Liquid::Template.register_tag('gallery', Jekyll::GalleryTag)