Class: Wraith::GalleryGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/wraith/gallery.rb

Defined Under Namespace

Classes: ErbBinding

Constant Summary collapse

MATCH_FILENAME =
/(\S+)_(\S+)\.\S+/
TEMPLATE_LOCATION =
File.expand_path('gallery_template/gallery_template.erb', File.dirname(__FILE__))
TEMPLATE_BY_DOMAIN_LOCATION =
File.expand_path('gallery_template/gallery_template.erb', File.dirname(__FILE__))
BOOTSTRAP_LOCATION =
File.expand_path('gallery_template/bootstrap.min.css', File.dirname(__FILE__))

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ GalleryGenerator

Returns a new instance of GalleryGenerator.



15
16
17
18
19
# File 'lib/wraith/gallery.rb', line 15

def initialize(config)
  @wraith = Wraith::Wraith.new(config)
  @location = wraith.directory
  @folder_manager = Wraith::FolderManager.new(config)
end

Instance Attribute Details

#wraithObject (readonly)

Returns the value of attribute wraith.



7
8
9
# File 'lib/wraith/gallery.rb', line 7

def wraith
  @wraith
end

Instance Method Details



95
96
97
98
99
100
# File 'lib/wraith/gallery.rb', line 95

def generate_gallery(withPath = '')
  dest = "#{@location}/gallery.html"
  directories = parse_directories(@location)
  generate_html(@location, directories, TEMPLATE_BY_DOMAIN_LOCATION, dest, withPath)
  FileUtils.cp(BOOTSTRAP_LOCATION, "#{@location}/bootstrap.min.css")
end

#generate_html(location, directories, template, destination, path) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/wraith/gallery.rb', line 82

def generate_html(location, directories, template, destination, path)
  template = File.read(template)
  locals = {
    location: location,
    directories: directories,
    path: path
  }
  html = ERB.new(template).result(ErbBinding.new(locals).get_binding)
  File.open(destination, 'w') do |outf|
    outf.write(html)
  end
end

#match(categories, dirname) ⇒ Object



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
# File 'lib/wraith/gallery.rb', line 37

def match(categories, dirname)
  categories.each do |category|
    @dirs[category] = {}
    Dir.foreach("#{dirname}/#{category}") do |filename|
      match = MATCH_FILENAME.match(filename)
      unless match.nil?
        size = match[1].to_i
        group = match[2]
        filepath = category + '/' + filename
        thumbnail = "thumbnails/#{category}/#{filename}"

        if @dirs[category][size].nil?
          @dirs[category][size] = { variants: [] }
        end
        size_dict = @dirs[category][size]

        case group
        when 'diff'
          size_dict[:diff] = {
            filename: filepath, thumb: thumbnail
          }
        when 'data'
          size_dict[:data] = File.read("#{dirname}/#{filepath}").to_f
        else
          size_dict[:variants] << {
            name: group,
            filename: filepath,
            thumb: thumbnail
          }

        end
        size_dict[:variants].sort! { |a, b| a[:name] <=> b[:name] }
      end
    end
  end
  @folder_manager.tidy_shots_folder(@dirs)
  if %w(diffs_only diffs_first).include?(wraith.mode)
    @sorted = @dirs.sort_by { |_category, sizes| -1 * sizes.max_by { |_size, dict| dict[:data] }[1][:data] }
  else
    @sorted = @dirs.sort_by { |category, _sizes| category }
  end
  # The sort has made this into an enumerable, convert it back to a Hash
  Hash[@sorted]
end

#parse_directories(dirname) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/wraith/gallery.rb', line 21

def parse_directories(dirname)
  @dirs = {}
  categories = Dir.foreach(dirname).select do |category|
    if ['.', '..', 'thumbnails'].include? category
      # Ignore special dirs
      false
    elsif File.directory? "#{dirname}/#{category}" then
      # Ignore stray files
      true
    else
      false
    end
  end
  match(categories, dirname)
end