|
|
@@ -0,0 +1,60 @@
|
|
|
+require 'date'
|
|
|
+require 'erb'
|
|
|
+
|
|
|
+module Fotos
|
|
|
+ class HtmlGenerator
|
|
|
+ MONTHS = %w(Styczeń Luty Marzec Kwiecień Maj Czerwiec Lipiec Sierpień Wrzesień Październik Listopad Grudzień)
|
|
|
+
|
|
|
+ def initialize(options)
|
|
|
+ @src_path = options[:source]
|
|
|
+ end
|
|
|
+
|
|
|
+ def call
|
|
|
+ dirs = Dir.glob("#{@src_path}/*")
|
|
|
+ dir_names = dirs.map {|d| d.split('/').last}
|
|
|
+ dates = dir_names.map do |d|
|
|
|
+ Date.strptime(d, DATE_FORMAT)
|
|
|
+ rescue Date::Error
|
|
|
+ next
|
|
|
+ end.compact!.sort.reverse
|
|
|
+ render_period(dates)
|
|
|
+
|
|
|
+ dates.each do |d|
|
|
|
+ dir_name = d.strftime(DATE_FORMAT)
|
|
|
+ puts "<h2 id=\"#{dir_name}\">#{dir_name} <small><a href=\"#toc\">top</a></small></h2>"
|
|
|
+ files = Dir.glob("#{@src_path}/#{dir_name}/*.JPEG")
|
|
|
+ files.each do |file|
|
|
|
+ basename = File.basename(file)
|
|
|
+ thumbnail_name = "TH_#{basename}"
|
|
|
+ thumbnail_path = File.join(dir_name, thumbnail_name)
|
|
|
+ file_path = File.join(dir_name, basename)
|
|
|
+ if File.exist?(thumbnail_path)
|
|
|
+ puts "<a href=\"#{dir_name}/#{basename}\"><img src=\"#{thumbnail_path}\" /></a>"
|
|
|
+ else
|
|
|
+ puts "<a href=\"#{dir_name}/#{basename}\"><img src=\"#{file_path}\" /></a>"
|
|
|
+ end
|
|
|
+ end
|
|
|
+ end
|
|
|
+
|
|
|
+ render_template('footer')
|
|
|
+ end
|
|
|
+
|
|
|
+ private
|
|
|
+ def render_period(dates)
|
|
|
+ dates.group_by(&:year).each do |year, y_dates|
|
|
|
+ puts "<h2>#{year}</h2>"
|
|
|
+ y_dates.group_by(&:month).each do |month, m_dates|
|
|
|
+ month_name = MONTHS[month - 1]
|
|
|
+ render_template('month', month: month_name, m_dates: m_dates)
|
|
|
+ end
|
|
|
+ end
|
|
|
+ end
|
|
|
+
|
|
|
+ def render_template(name, context = {})
|
|
|
+ path = Pathname.new(__FILE__).join('..', '..', '..', 'templates', "_#{name}.html.erb")
|
|
|
+ template = ERB.new(File.read(path))
|
|
|
+ puts template.result_with_hash(context)
|
|
|
+ end
|
|
|
+
|
|
|
+ end
|
|
|
+end
|