summaryrefslogtreecommitdiff
path: root/pictures.rb
blob: 71b06d438598de0b184e39ef6f3a386fc2eab585 (plain)
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
#!/usr/bin/env ruby
require_relative 'lib'
require 'fileutils'

PICTURE_PATH = '/srv/media/pictures'
WEBP_PATH = File.join PICTURE_PATH, 'webp'
TXT_PATH = File.join PICTURE_PATH, 'txt'

ALBUM_PATH = File.join PICTURE_PATH, 'albums'

PICTURE_URL = File.join MEDIA_URL, 'pictures'
WEBP_URL = File.join PICTURE_URL, 'webp'
THUMB_URL = File.join PICTURE_URL, 'thumb'
JPEG_URL = File.join PICTURE_URL, 'jpeg'

def meta(img, album)
  basename = File.basename(img, File.extname(img))
  path = File.join(WEBP_PATH, basename + '.webp')
  {
    basename: basename,
    webp: File.join(WEBP_URL, basename + '.webp'),
    txt: File.join(TXT_PATH, basename + '.txt'),
    src: File.join(JPEG_URL, basename + File.extname(img)),
    thumb: File.join(THUMB_URL, basename + '.webp'),
    href: File.join('/pictures', File.basename(album),
                    File.basename(img, File.extname(img)) + '.html')
  }
end

albums = Dir[File.join ALBUM_PATH, '*'].sort_by { |a| File.mtime a }.reverse

albums.each do |album|
  www_dir = File.join('/srv/www/pdp8-test/pictures', File.basename(album))
  FileUtils.mkdir_p www_dir
  `cd /srv/www/pdp8-test/pictures; git add #{File.basename(album)}`
  images = File.readlines(album, chomp: true)
  n = images.size
  images.each_with_index do |img, i|
    before = meta(images[(i - 1) % n], album)[:href]
    after = meta(images[(i + 1) % n], album)[:href]
    meta = meta img, album
    html = File.read(File.join(SNIPPETS, 'head.html'))
    html += nav 'pictures'
    html += "<div class='slide'>"
    html += '<img '
    html += "alt='#{File.read(meta[:txt])}' " if File.exist?(meta[:txt])
    html += "src='#{meta[:webp]}'>"
    html += "<div class='slidenav'>"
    html += "<a href='#{before}' class='navarrow'>←</a>"
    html += "<a href='/pictures.html##{File.basename(album)}'>back</a>"
    html += "<a href='#{after}' class='navarrow'>→</a>"
    html += '</div>'
    html += '</div>'
    html += File.read(File.join(SNIPPETS, 'tail.html'))
    html_file = File.join(www_dir, File.basename(img, File.extname(img)) + '.html')
    File.open(html_file, 'w+') { |f| f.puts html }
    puts `tidy -iqm -w 0 #{html_file} 2>&1`
  end
end

html = File.read(File.join(SNIPPETS, 'head.html'))
html += nav 'pictures'
albums.each do |album|
  www_dir = File.join('/srv/www/pdp8-test/pictures', File.basename(album))
  FileUtils.mkdir_p www_dir
  images = File.readlines(album, chomp: true)
  n = images.size
  html += "<section id='#{File.basename album}'>"
  html += "<h1><a href='#{meta(images.first,
                               album)[:href]}'>#{File.basename(album).gsub('_', ' ')}</a></h1>\n"
  html += "<div class='gallery'>"
  images.each_with_index do |img, _i|
    meta = meta img, album
    html += "<a href='#{meta[:href]}'><img class='thumb' loading='lazy' width='150' height='100' "
    html += "alt='#{File.read(meta[:txt])}' " if File.exist?(meta[:txt])
    html += "src='#{meta[:thumb]}'></a>"
  end
  html += '</div>'
  html += '</section>'
end
html += File.read(File.join(SNIPPETS, 'tail.html'))
print_html 'pictures', html