Class: Komic::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/komic/generator/generator.rb

Instance Method Summary collapse

Instance Method Details

#create_fake_image(filename, size) ⇒ Object



103
104
105
106
107
108
109
110
111
# File 'lib/komic/generator/generator.rb', line 103

def create_fake_image(filename, size)
  size = Utils.parse_size(size)
  file = Tempfile.new([filename, '.svg'])
  image_width = size[:width]
  image_height = size[:height]
  file.write render_fake_svg({ width: image_width, height: image_height })
  file.close
  return { src: file.path, width: image_width, height: image_height }
end

#create_package(data, options) ⇒ Object



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
# File 'lib/komic/generator/generator.rb', line 120

def create_package(data, options)
  root_dir = File.join(Dir.pwd, options[:name])
  image_dir = File.join(root_dir, 'images')

  [root_dir, image_dir].each { |path| FileUtils.mkdir_p path }

  files = data[:images]

  files.map.with_index do |image, index|
    image_manager = MiniMagick::Image.open(image[:src])

    image_path = File.join(image_dir, [index, image_manager.type.downcase].join('.'))
    FileUtils.mv image[:src], image_path
    image[:src] = image_path
    image
  end

  thumbnails_builder = ThumbnailsBuilder.new(files)
  thumbnail_path = File.join(image_dir, './thumbnail.svg')
  File.open(thumbnail_path, 'w') do |file|
    file.write thumbnails_builder.to_build
  end

  files.map do |image, index|
    image[:src] = Utils.get_relative_path(image[:src], root_dir)
    if options[:'remote-url']
      image[:src] = "https://placeimg.com/#{image[:width]}/#{image[:height]}/any"
    end
    image
  end

  meta = {
    description: 'TEST',
    name: 'TEST',
    author: { name: 'TEST' },
    thumbnails: {
      height: 200,
      path: Utils.get_relative_path(thumbnail_path, root_dir),
    },
  }

  unless data[:meta].nil?
    meta = Utils.deep_merge_hashes(meta, data[:meta])
  end

  content_builder = ContentBuilder.new(meta, files)
  File.open(File.join(root_dir, './content.json'), 'w') do |file|
    file.write content_builder.to_build
  end

end

#generate_mocks(options) ⇒ Object



113
114
115
116
117
118
# File 'lib/komic/generator/generator.rb', line 113

def generate_mocks(options)
  images = Array.new(options[:'page-number'])
    .map.with_index do |value, index|
      create_fake_image index.to_s, options[:size]
    end
end

#render_fake_svg(props) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/komic/generator/generator.rb', line 86

def render_fake_svg(props)
  width, height = props.values

  return %(
    <svg width="#{ width }" height="#{ height }"
      xmlns="http://www.w3.org/2000/svg"
      xmlns:xlink="http://www.w3.org/1999/xlink">
      <rect width="100%" height="100%" fill="gray"></rect>
      <text x="#{ width / 2 }" y="50%" dy=".3em" text-anchor="middle"
        fill="white" font-size="30"
        font-family="Comic Sans, Comic Sans MS, cursive">
        #{ width } x #{ height }
      </text>
    </svg>
  )
end