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

ROOT = ARGV[0]
SNIPPETS = File.join(File.dirname(__FILE__), "snippets")

def nav cat
  html = "<nav>"
  html += "<a href='/about.html'><img src='/pdp8.png' id='logo' alt='pdp8'></a>"
  ["music", "pictures", "videos", "code", "newsletter"].each do |c|
    c == cat ? cl = 'item current' : cl = 'item'
    html += "<a class='#{cl}' href='/#{c}.html'>#{c}</a>"
  end
  html += "<a id='menu' href='#' onclick='show_vertical_menu()'>"
  html += "<div class='fa fa-bars' aria-hidden='true'></div>"
  html += "</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(ROOT, 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(ROOT, "music", "*")].sort.reverse
  music.each_with_index do |dir, i|
    html = File.read(File.join(SNIPPETS, "head.html"))
    html += nav "music"
    html += "<div class='post'>"
    title = File.basename(dir).split("_")[1..-1].join(" ")
    html += "<h1>#{title}</h1>"
    html += File.read(File.join(dir, "README")) + "<p>"
    cover = File.join("/", dir.sub(ROOT, ''), "cover.jpeg")
    html += "<a title='Download cover.jpeg' href='#{cover}' download>
        <img src='#{cover}' alt='cover'></a>
        <p>"
    Dir[File.join(dir, "*mp3")].each do |f|
      f = "/" + f.sub(ROOT, '')
      html += "<audio controls>
        <source src='#{f}' type='audio/mpeg'>
        <a href='#{f}'>#{f}</a>
        </audio>&nbsp;
        <a title='Download #{f}' href='#{f}' class='btn' download>
          #{File.basename(f, '.mp3')}</a><br>
        "
    end
    bc = "https://pdp8.bandcamp.com/#{title.gsub(' ', '-')}"
    html += "<p>Bandcamp: <a href='#{bc}'>#{bc}</a>"
    html += "</div>"
    html += "<div class='pager'>"
    j = (i - 1) % music.size
    p = File.join("/", File.basename(music[j]) + ".html")
    html += "<a class='btn-left' href='#{p}'>
      <div class='fa fa-caret-left' aria-hidden='true'></div>&nbsp;previous
      </a>"
    html += ""
    j = (i + 1) % music.size
    n = File.join("/", File.basename(music[j]) + ".html")
    html += "<a class='btn-right' href='#{n}'>
      [#{i + 1}/#{music.size}] next <div class='fa fa-caret-right' aria-hidden='true'></div>
      </a>"

    html += "</div>"
    html += File.read(File.join(SNIPPETS, "tail.html"))
    print_html File.basename(dir), html
    if i == 0
      print_html "music", html
    end
  end
end

def video_html
  videos = Dir[File.join(ROOT, "videos", "*")].sort.reverse
  videos.each_with_index do |dir, i|
    html = File.read(File.join(SNIPPETS, "head.html"))
    html += nav "videos"
    html += "<div class='post'>"
    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("/videos", File.basename(dir), title.gsub(" ", "_") + ".mp4")
    webm = File.join("/videos", File.basename(dir), title.gsub(" ", "_") + ".webm")

    html += "<video controls>
        <source src='#{mp4}' type='video/mpeg'>
        <source src='#{webm}' type='video/webm'>
        <a href='#{mp4}'>#{mp4}</a>
        </video><p>
        "
    html += lines.join("<br>")
    html += "</div>"
    html += "<div class='pager'>"
    j = (i - 1) % videos.size
    p = File.join("/", File.basename(videos[j]) + ".html")
    html += "<a class='btn-left' href='#{p}'>
      <div class='fa fa-caret-left' aria-hidden='true'></div>&nbsp;previous
      </a>"
    html += ""
    j = (i + 1) % videos.size
    n = File.join("/", File.basename(videos[j]) + ".html")
    html += "<a class='btn-right' href='#{n}'>
      [#{i + 1}/#{videos.size}] next <div class='fa fa-caret-right' aria-hidden='true'></div>
      </a>"

    html += "</div>"
    html += File.read(File.join(SNIPPETS, "tail.html"))
    print_html File.basename(dir), html
    if i == 0
      print_html "videos", html
    end
  end
end

music_html
video_html

["about", "code", "pictures", "newsletter"].each do |basename|
  file_html basename
end

["pdp8.png", "style.css", "distances.js", "images.js"].each { |f| `rsync -av "#{File.join(SNIPPETS, f)}" "#{ROOT}"` }
`rsync -av #{File.join(SNIPPETS, "Fork-Awesome-1.2.0")} "#{ROOT}"`
# rss