asciinema.org/app/services/asciicast_image_generator.rb

47 lines
1.3 KiB
Ruby
Raw Normal View History

2015-03-30 10:27:59 +00:00
class AsciicastImageGenerator
2015-03-29 14:15:05 +00:00
PIXEL_DENSITY = 2
2015-03-30 10:23:58 +00:00
attr_reader :template_renderer, :rasterizer, :image_inspector
2015-03-30 10:23:58 +00:00
def initialize(template_renderer, rasterizer = Rasterizer.new, image_inspector = ImageInspector.new)
@template_renderer = template_renderer
2015-03-29 14:15:05 +00:00
@rasterizer = rasterizer
@image_inspector = image_inspector
end
2015-03-30 10:27:59 +00:00
def generate(asciicast)
Dir.mktmpdir do |dir|
asciicast_url = asciicast.data.absolute_url
2015-03-29 14:15:05 +00:00
image_path = "#{dir}/#{asciicast.image_filename}"
2017-01-11 14:25:48 +00:00
time = asciicast.snapshot_at || asciicast.duration / 2
2017-01-11 18:06:38 +00:00
theme = AsciicastDecorator.new(asciicast).theme_name
2017-01-11 18:06:38 +00:00
rasterizer.generate_image(asciicast_url, image_path, time, PIXEL_DENSITY, theme)
image_width, image_height = get_size(image_path)
2015-03-30 10:23:58 +00:00
update_asciicast(asciicast, image_path, image_width, image_height)
end
end
private
def get_size(image_path)
width, height = image_inspector.get_size(image_path)
# "display" size is 1/PIXEL_DENSITY of the actual one
[width / PIXEL_DENSITY, height / PIXEL_DENSITY]
end
2015-03-30 10:23:58 +00:00
def update_asciicast(asciicast, image_path, image_width, image_height)
File.open(image_path) do |f|
asciicast.image = f
end
2015-03-30 10:23:58 +00:00
asciicast.image_width = image_width
asciicast.image_height = image_height
2015-03-30 10:23:58 +00:00
asciicast.save!
end
end