Browse Source

feature: HTML page generation

Lukasz Badura 5 months ago
parent
commit
31ea5c4c9e
6 changed files with 103 additions and 6 deletions
  1. 19 6
      bin/fotos
  2. 3 0
      lib/fotos.rb
  3. 60 0
      lib/fotos/html_generator.rb
  4. 3 0
      templates/_footer.html.erb
  5. 11 0
      templates/_header.html.erb
  6. 7 0
      templates/_month.html.erb

+ 19 - 6
bin/fotos

@@ -2,10 +2,23 @@
 
 
 require 'fotos'
 require 'fotos'
 require 'optparse'
 require 'optparse'
-
+COMMANDS = %w(extract build)
 parser = OptionParser.new
 parser = OptionParser.new
-parser.on('-z PATH', '--zip', 'Extract a zip file with images from PATH')
-parser.on('-d [PATH]', '--dest', 'Copy to a destinatio dir')
-options = {dest: '.'}
-parser.parse!(into: options)
-Fotos::ZipExtractor.new(options).call
+
+case ARGV[0]
+when 'extract'
+  parser.on('-z PATH', '--zip', 'Extract a zip file with images from PATH')
+  parser.on('-d [PATH]', '--dest', 'Copy to a destinatio dir')
+  options = {dest: '.'}
+  parser.parse!(into: options)
+  Fotos::ZipExtractor.new(options).call
+when 'build'
+  parser.on('-s [PATH]', '--source', 'Input directory with image directories')
+  parser.on('-d [PATH]', '--dest', 'Output directory for html and images')
+  options = {dest: '.'}
+  parser.parse!(into: options)
+  Fotos::HtmlGenerator.new(options).call
+else
+  puts "Unknown command! Available commands: #{COMMANDS.join(',')}."
+  exit(-1)
+end

+ 3 - 0
lib/fotos.rb

@@ -2,4 +2,7 @@
 
 
 require_relative "fotos/version"
 require_relative "fotos/version"
 require_relative "fotos/zip_extractor"
 require_relative "fotos/zip_extractor"
+require_relative "fotos/html_generator"
 require_relative "fotos/thumbnail"
 require_relative "fotos/thumbnail"
+
+DATE_FORMAT = "%d-%m-%Y".freeze

+ 60 - 0
lib/fotos/html_generator.rb

@@ -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

+ 3 - 0
templates/_footer.html.erb

@@ -0,0 +1,3 @@
+    <div><p>&copy; 2023-2025 <a href="https://lukasz.sh" target="_top">Łukasz Badura</a></p></div>
+    </body>
+  </html>

+ 11 - 0
templates/_header.html.erb

@@ -0,0 +1,11 @@
+<html>
+  <head>
+    <title>Krowoderska - log</title>
+    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
+  </head>
+  <script data-goatcounter="https://krowoderska.goatcounter.com/count" async src="//gc.zgo.at/count.js"></script>
+  <body>
+  <div id="toc">
+    <p>To jest fotograficzny zapis zmian w naszym mieszkaniu na Krowoderskiej</p>
+    <p>Ostatnia aktualizacja: <%= DateTime.now %></p>
+  </div>

+ 7 - 0
templates/_month.html.erb

@@ -0,0 +1,7 @@
+<div>
+  <p><%= month %>:
+    <% m_dates.each do |d| %>
+      <a href="#<%= d.strftime(DATE_FORMAT) %>"><%= d.strftime('%d.%m') %></a>
+    <% end %>
+  </p>
+</div>