Class: Yuzu::Command::Generate

Inherits:
ConfiglessCommand show all
Defined in:
lib/yuzu/commands/generate.rb

Overview

Generate handles some cross-media production (e.g. pdf) and handling of the creation of files (e.g. thumbnails).

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ConfiglessCommand

#initialize, requires_config?

Constructor Details

This class inherits a constructor from Yuzu::Command::ConfiglessCommand

Class Method Details

.help(method) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/yuzu/commands/generate.rb', line 82

def self.help method
  case method
  when :thumbnails
    %Q{Generates small, medium, and large thumbnail images from a given path.
Uses the Mac 'sips' command-line tool.}

  else
    "No help available for #{method}."

  end
end

Instance Method Details

#configObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/yuzu/commands/generate.rb', line 65

def config
  if not File.exists?("yuzu.yml") and not File.exists?("config/yuzu.yml")
    # TODO Update to use Path objects.
    FileUtils.copy(
      "#{File.dirname(__FILE__)}/../templates/yuzu.yml",
      "#{Dir.pwd}/config/yuzu.yml"
    )

    $stderr.puts %Q{Copied a sample config file into the current folder. \
Update it with your remote server information.}

  else
    $stderr.puts %Q{Config file yuzu.yml already exists. Please rename or \
erase it to generate a new one.}
  end
end

#indexObject



8
9
# File 'lib/yuzu/commands/generate.rb', line 8

def index
end

#thumbnailsObject

TODO: Put this in its own class, update it, and integrate with the rest of the app.



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
# File 'lib/yuzu/commands/generate.rb', line 24

def thumbnails
  path = args.first
  web_image_types = [".png", ".jpg", ".gif"]
  thumbnail_types = @config.thumbnails.keys

  thumbnail_endings = []
  thumbnail_types.each do |type|
    web_image_types.each do |ext|
      thumbnail_endings += ["-#{type}#{ext}"]
    end
  end

  if File.directory?(path)
    # Get all the original images requested.
    all_files = Dir[File.join(path, "**/*")]

    images = all_files.select { |f|
                web_image_types.include?(File.extname(f))
              }.reject { |f|
                f[0].chr == "_" or 
                f.include?("#{File::SEPARATOR}_") or
                f.includes_one_of?(thumbnail_endings)
              }
  else
    # A single image.
    images = [path]
  end

  images.each do |image_path|
    ext = File.extname(image_path)

    # Loop through the configured thumbnail types ("small", "medium", etc.)
    thumbnail_types.each do |thumbnail_type|
      thumbnail_path = image_path.gsub(ext, "-#{thumbnail_type}#{ext}")
      thumbnail_size = @config.thumbnails[thumbnail_type]

      $stderr.puts `cp #{image_path} #{thumbnail_path}; sips --resampleWidth #{thumbnail_size} #{thumbnail_path}`
    end
  end
end