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



108
109
110
111
112
113
114
115
116
# File 'lib/komic/generator/generator.rb', line 108

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



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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/komic/generator/generator.rb', line 125

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 }

  images = data[:images]

  images.map!.with_index do |image, index|
    # dirty but work
    if image[:src].instance_of? Tempfile
      will_be_open = image[:src].path
    else
      will_be_open = image[:src]
    end

    manager = MiniMagick::Image.open(will_be_open)

    image_type = manager.type.downcase
    image_path = File.join(image_dir,
      [index, manager.type.downcase].join('.'))

    manager.quality(60)
    manager.strip() unless manager.type.downcase == 'svg'
    manager.write image_path
    image[:src] = image_path

    manager.format 'webp'
    webp_path = File.join(image_dir,
      [[index, 'webp'].join('-'), manager.type.downcase].join('.'))
    manager.write webp_path

    src = {}
    src[:default] = image_type
    src[image_type] = Utils.get_relative_path(image_path, root_dir)
    src[:webp] = Utils.get_relative_path(webp_path, root_dir)

    web = Utils.deep_merge_hashes(image, { src: src })
    Utils.deep_merge_hashes(image, { web: web })
  end

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

  images.map do |image|
    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, images)
  File.open(File.join(root_dir, './content.json'), 'w') do |file|
    file.write content_builder.to_build(options)
  end
end

#create_website(data, options) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/komic/generator/generator.rb', line 200

def create_website(data, options)
  root_dir = File.join(Dir.pwd, options[:name])
  create_package(data, options)
  dist_project = "komic-web-dist"
  dist_branch = "master"
  uri = "https://github.com/komic-awesome/#{dist_project}/archive/#{dist_branch}.zip"
  source = open(uri)
  ::Zip::File.open(source.path) do |zip_file|
    zip_file.each do |entry|
      entry.extract(File.join(root_dir, File.basename(entry.name))) \
        if File.fnmatch("#{dist_project}-#{dist_branch}/?*", entry.name)
    end
  end
  Dir.glob("#{root_dir}/**/*.js") do |path|
    uglified = Uglifier.compile(File.read(path))
    File.open(path, 'w') do |file|
      file.write uglified
    end
  end
end

#generate_mocks(options) ⇒ Object



118
119
120
121
122
123
# File 'lib/komic/generator/generator.rb', line 118

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



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/komic/generator/generator.rb', line 91

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