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



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

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



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
171
172
173
174
175
# File 'lib/komic/generator/generator.rb', line 122

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|
    manager = MiniMagick::Image.open(image[:src])

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

#create_website(data, options) ⇒ Object



177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/komic/generator/generator.rb', line 177

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
end

#generate_mocks(options) ⇒ Object



115
116
117
118
119
120
# File 'lib/komic/generator/generator.rb', line 115

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



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

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