summaryrefslogtreecommitdiff
path: root/html.rb
blob: 171d487ed3e2c1a594ca31cee68e9d433cebdfbb (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
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
#!/usr/bin/env ruby

MEDIA_DIR = '/srv/media'
MEDIA_URL = 'https://media.pdp8.info'
WWW_DIR = '/srv/www/pdp8-test'
SNIPPETS = File.join(File.dirname(__FILE__), 'html')

def nav(cat)
  html = '<nav>'
  html += "<a id='logo' href='/about.html'><img src='/pdp8.png' alt='pdp8'></a>"
  %w[music pictures videos climbing code contact].each do |c|
    cl = c == cat ? 'item current' : 'item'
    html += "&nbsp;<a class='#{cl}' href='/#{c}.html'>#{c}</a>"
  end
  html += "&nbsp;<a class='item' href='/social/create.html'>social</a>"
  html += "&nbsp;<a class='item' href='/rss.xml'>rss</a>"
  html += "&nbsp;<a id='menu' href='#' onclick='show_vertical_menu()'>&equiv;</a>"
  html += '</nav>'
  html
end

def file_html(basename)
  path = File.join(SNIPPETS, basename + '.html')
  html = File.read(File.join(SNIPPETS, 'head.html'))
  html += nav basename
  html += "<div class='post'>"
  html += File.read(path)
  html += '</div>'
  html += File.read(File.join(SNIPPETS, 'tail.html'))
  print_html basename, html
end

def print_html(basename, html)
  out = File.join(WWW_DIR, basename + '.html')
  puts out
  File.open(out, 'w+') { |f| f.puts html }
  puts `tidy -iqm -w 0 #{out} 2>&1`
end

def music_html
  music = Dir[File.join(MEDIA_DIR, 'music', '*')].sort.reverse
  html = File.read(File.join(SNIPPETS, 'head.html'))
  html += nav 'music'
  html += '<div class="post"><a href="https://faircamp.webr.ing/prev/pdp8.info/music.html">← prev</a> |
    <a href="https://faircamp.webr.ing/">faircamp webring</a> |
    <a href="https://faircamp.webr.ing/rand">random</a> |
    <a href="https://faircamp.webr.ing/next/pdp8.info/music.html">next →</a></div>'
  music.each do |dir|
    next if dir.match 'alfadeo'

    date = File.basename(dir).split('_')[0]
    html += "<div class='post' id='#{date}'>"
    title = File.basename(dir).split('_')[1..-1].join(' ')
    html += "<h1>#{title}</h1>"
    html += File.read(File.join(dir, 'README')).chomp.gsub("\n\n", '<p>').gsub("\n", '<br>') # + '<p>'
    cover = File.join(MEDIA_URL, dir.sub(MEDIA_DIR, ''), 'cover.webp')
    alt = File.read(File.join(dir, 'cover.txt')).chomp
    w, h = `identify -format "%w %h" #{cover}`.chomp.split(' ')
    html += "<img class='cover' loading='lazy' width='#{w}' height='#{h}' src='#{cover}' alt='#{alt}'>"
    html += '<table>'
    copyrights_file = File.join(dir, 'copyrights')
    copyrights = File.readlines(copyrights_file).collect { |l| l.chomp } if File.exist? copyrights_file
    Dir[File.join(dir, '*mp3')].each_with_index do |mp3, _i|
      mp3 = File.join(MEDIA_URL, mp3.sub(MEDIA_DIR, ''))
      name = File.basename(mp3, '.mp3')[3..-1].gsub('_', ' ')
      # name += " &copy;#{copyrights[i]}" if copyrights
      html += '<tr>'
      html += "<td>#{name}</td>"
      html += "<td>
        <audio preload='none' controls>
          <source src='#{mp3}' type='audio/mpeg'>
          <a href=#{mp3}>#{mp3}</a>
        </audio>
        </td>"
      html += '</tr>'
    end
    html += '</table>'
    ia = "https://archive.org/details/pdp8_#{title.gsub(' ', '_')}"
    html += "<p>Internet Archive: <a href='#{ia}'>#{ia}</a>"
    bc = if File.exist?(File.join(dir, 'bandcamp'))
           File.read(File.join(dir, 'bandcamp')).chomp
         else
           "https://pdp8.bandcamp.com/album/#{title.gsub(' ', '-')}"
         end
    html += "<p>Bandcamp: <a href='#{bc}'>#{bc}</a>"
    html += '</div>'
  end
  html += '<p>&copy; pdp8 <a rel="license" href="http://creativecommons.org/licenses/by/4.0/">Creative Commons Attribution 4.0 International License</a>'
  html += File.read(File.join(SNIPPETS, 'tail.html'))
  print_html 'music', html
end

def picture_html
  html = File.read(File.join(SNIPPETS, 'head.html'))
  html += nav 'pictures'
  html += '<div class="gallery">'
  Dir[File.join(MEDIA_DIR, 'pictures', '*.jpeg')].sort.reverse.each do |img|
    thumb = File.join(MEDIA_URL, 'pictures', 'thumb', File.basename(img).sub(/jpeg$/, 'webp'))
    html += "<a href='#{File.join(MEDIA_URL, 'pictures',
                                  File.basename(img))}'><img class='thumb' loading='lazy' width='150' height='100' src='#{thumb}'></a>"
  end
  html += '</div>'
  html += '<p>&copy; pdp8 <a rel="license" href="http://creativecommons.org/licenses/by/4.0/">Creative Commons Attribution 4.0 International License</a>'
  html += File.read(File.join(SNIPPETS, 'tail.html'))
  print_html 'pictures', html
end

def video_html
  videos = Dir[File.join(MEDIA_DIR, 'videos', '*')].sort.reverse
  html = File.read(File.join(SNIPPETS, 'head.html'))
  html += nav 'videos'
  videos.each_with_index do |dir, _i|
    date = File.basename(dir).split('_')[0]
    html += "<div class='post' id='#{date}'>"
    title = File.basename(dir).split('_')[1..-1].join(' ')
    html += "<h1>#{title}</h1>"
    txt = File.read(File.join(dir, 'README'))
    txt = txt.gsub(/(http\S*)/, '<a href="\1">\1</a>')
    lines = txt.split("\n")
    html += lines.shift + '<p>'

    mp4 =   File.join(MEDIA_URL, 'videos', File.basename(dir), title.gsub(' ', '_') + '.mp4')
    webm = File.join(MEDIA_URL, 'videos', File.basename(dir), title.gsub(' ', '_') + '.webm')
    poster = File.join(MEDIA_URL, 'videos', File.basename(dir), 'poster.png')
    w, h = `ffprobe -v error -select_streams v:0 -show_entries stream=width,height -of csv=p=0 #{webm}`.chomp.split(',')

    html += "<video controls preload='none'  width='#{w}' height='#{h}' poster='#{poster}'>
          <source src='#{webm}' type='video/webm'>
          <source src='#{mp4}' type='video/mp4'>
          <a href=#{mp4}>#{mp4}</a>
        </video><p>
        "
    html += lines.join('<br>')
    html += '</div>'
  end
  html += '<p>&nbsp;&copy; pdp8 <a rel="license" href="http://creativecommons.org/licenses/by/4.0/">Creative Commons Attribution 4.0 International License</a>'
  html += File.read(File.join(SNIPPETS, 'tail.html'))
  print_html 'videos', html
end

def climbing_html
  html = File.read(File.join(SNIPPETS, 'head.html'))
  html += nav 'climbing'
  Dir[File.join(MEDIA_DIR, 'climbing', '*.txt')].collect do |txt|
    lines = File.read(txt).lines.collect { |l| l.chomp }
    { date: lines.shift,
      text: lines.join('<br>'),
      mp4: txt.sub(MEDIA_DIR, MEDIA_URL).sub('.txt', '.mp4'),
      webm: txt.sub(MEDIA_DIR, MEDIA_URL).sub('.txt', '.webm'),
      webp: txt.sub(MEDIA_DIR, MEDIA_URL).sub('.txt', '.webp') }
  end.sort_by { |m| m[:date] }.reverse.each do |post|
    w, h = `ffprobe -v error -select_streams v:0 -show_entries stream=width,height -of csv=p=0 #{post[:webm]}`.chomp.split(',')
    html += '<div class="post">'
    html += "<h1>#{post[:date]}</h1>"
    html += "<video controls preload='none' width='#{w}' height='#{h}' poster=#{post[:webp]}>
          <source src='#{post[:webm]}' type='video/webm'>
          <source src='#{post[:mp4]}' type='video/mp4'>
          <a href=#{post[:mp4]}>#{post[:mp4]}</a>
        </video><p>
        "
    html += post[:text]
    html += '</div>'
  end
  html += '<p>&nbsp;&copy; pdp8 <a rel="license" href="http://creativecommons.org/licenses/by/4.0/">Creative Commons Attribution 4.0 International License</a>'
  html += File.read(File.join(SNIPPETS, 'tail.html'))
  print_html 'climbing', html
end

music_html
# picture_html
video_html
climbing_html

%w[about code pictures contact].each do |basename|
  file_html basename
end

last = (Dir[File.join(MEDIA_DIR, 'music', '*')] + Dir[File.join(MEDIA_DIR, 'videos', '*')]).sort_by { |d|
  File.basename(d)
}.last.split('/')[-2] + '.html'
puts `cp "#{File.join(WWW_DIR, last)}" "#{File.join(WWW_DIR, 'index.html')}"`

[
  'pdp8.png',
  '540px-PDP-8_.jpg',
  'style.css',
  'slideshow.js',
  'robots.txt'
].each { |f| puts `rsync -av "#{File.join(SNIPPETS, f)}" "#{WWW_DIR}"` }