| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- 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)
- @source_dir = Fotos::SourceDirectory.new(options[:source])
- @dest_dir = Fotos::SourceDirectory.new(options[:dest])
- @generate_thumbnails = options[:thumbnails]
- end
- attr_reader :source_dir, :dest_dir, :generate_thumbnails
- def call
- html_output = []
- html_output << render_template('header')
- html_output << render_period(@source_dir.dates)
- @source_dir.files_by_date.each do |date, assets|
- html_output << "<h2 id=\"#{date}\">#{date} <small><a href=\"#toc\">top</a></small></h2>"
- assets.reject(&:is_thumbnail?).each do |asset|
- ensure_thumbnail(asset)
- if File.exist?(asset.thumbnail_path(@dest_dir.path))
- html_output << "<a href=\"#{asset.path}\"><img src=\"#{asset.thumbnail_path}\" /></a>"
- else
- html_output << "<a href=\"#{asset.dirname}/#{asset.file_name}\"><img src=\"#{asset.path}\" /></a>"
- end
- end
- end
- html_output << render_template('footer')
- html_output.join("\n")
- end
- private
- def ensure_thumbnail(asset)
- return if File.exist?(asset.thumbnail_path(@dest_dir.path))
- if @generate_thumbnails
- begin
- Fotos::Thumbnail.new(asset).create(@dest_dir.path)
- rescue StandardError => e
- warn "Failed to create thumbnail for #{asset.file_name}: #{e.message}"
- end
- end
- end
- def render_period(dates)
- output = []
- dates.group_by(&:year).each do |year, y_dates|
- output << "<h2>#{year}</h2>"
- y_dates.group_by(&:month).each do |month, m_dates|
- month_name = MONTHS[month - 1]
- output << render_template('month', month: month_name, m_dates: m_dates)
- end
- end
- output.join("\n")
- end
- def render_template(name, context = {})
- path = Pathname.new(__FILE__).join('..', '..', '..', 'templates', "_#{name}.html.erb")
- template = ERB.new(File.read(path))
- template.result_with_hash(context)
- end
- end
- end
|