summaryrefslogtreecommitdiff
path: root/build.rb
blob: a617fc0df69444751615a409059fc1486ceefa9c (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

require 'yaml'
require 'optparse'
require 'digest'
require 'mini_magick'
require 'fileutils'
require 'erb'

def copyimage(path, file, target)
  hash = Digest::MD5.file(file).hexdigest
  hash = Digest::MD5.hexdigest(hash.to_s + target)
  filename = "#{hash}.jpg"
  dest = File.join(path, filename)

  if File.exist?(dest)
    puts "Skipping"
    return filename
  end

  puts "Writing #{dest}"
  # Resize image and write it to destination
  image = MiniMagick::Image.open(file)
  image.resize(target)
  image.write(dest)

  return filename
end

def forImgs(imgs, &block)
  if imgs.respond_to?("each")
    imgs.each &block
  else
    block.call(imgs)
  end
end

def placeImage(file, src, href = nil)
  if href
    file.puts "<a href=\"#{href}\">"
  end
  file.puts "<img src=\"#{src}\">"
  if href
    file.puts "</a>"
  end
end

options = {}
OptionParser.new do |opts|
  opts.banner = "Usage: build.rb dest"

  opts.on("-d", "--dest PATH", "PATH to put build files") do |path|
    options[:path] = path
  end
  opts.on("-s", "--size SIZE", "Size to resize images to") do |size|
    options[:size] = size
  end
  opts.on("-g", "--commit HASH", "Commit to display on website") do |hash|
    options[:commit] = hash
  end
  opts.on("-c", "--clean", "Cleanup unused images") do |v|
    options[:clean] = true
  end
  opts.on("-b", "--linkbase URL", "Add links to images, with URL as base") do |url|
    options[:base] = url
  end
end.parse!

options[:path] = "build" unless options[:path]
options[:size] = "1920x1080" unless options[:size]
options[:clean] = false unless options[:clean]
puts options

# Create build dir
FileUtils.mkdir_p(options[:path])

defs = YAML.load(File.read("imginfo.yml"))
files = {}

puts defs

defs["groups"].each do |value|

  # For each image inside group
  forImgs(value["imgs"]) do |img|
    puts "Converting file #{img}"
    dest = copyimage(options[:path], img, options[:size])
    files[img] = dest
  end

end

puts files

template = File.read("index.tmpl")
template = template.split("%BODY%")

File.open(File.join(options[:path], "index.html"), "w") do |file|
  # Print preample
  file.write(template[0])

  defs["groups"].each do |value|
    file.puts "<div class=\"group\">"
    if value[:title]
      file.puts "<h2>#{value[:title]}</h2>"
    end
    forImgs(value["imgs"]) do |img|
      href = options[:base]
      # If href is given add a filename at the end
      if href
        href = File.join(href, img)
      end

      placeImage(file, files[img], href)
    end

    if value["what"]
      file.puts "<p>#{value["what"]}"
    end
    file.puts "</div>"
  end

  if options[:commit]
    file.puts "<p>Build from commit #{options[:commit]}</p>"
  end

  # Print postampleee?
  file.write(template[1])
end

# Cleanup old files
if options[:clean]
  Dir.glob(File.join(options[:path], "*.jpg")) do |file|
    hash = File.basename(file)
    if !files.values.include? hash 
      puts "Cleaning #{hash}"
      File.delete(file)
    end
  end
end