Class: Middleman::SVGFallback

Inherits:
Extension
  • Object
show all
Defined in:
lib/middleman/svg_fallback.rb

Instance Method Summary collapse

Instance Method Details

#after_build(builder) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/middleman/svg_fallback.rb', line 8

def after_build(builder)
  dir = File.join(app.config[:build_dir], app.config[:images_dir])

  run_args = { capture: true, verbose:  app.config.cli_options['verbose'] }

  Dir[File.join(dir, '**', '*.svg*')].each do |file|
    # make sure we have both an svg and an svgz version
    unprefixed = file.sub(/\.svgz?$/, '')
    basename = File.expand_path(unprefixed)

    unless File.exists? "#{basename}.svg"
      builder.thor.run("gunzip --to-stdout #{basename}.svgz > #{basename}.svg", run_args)
      builder.thor.say_status :svg, "#{unprefixed}.svg"
    end

    unless File.exists? "#{basename}.svgz"
      builder.thor.run("gzip --to-stdout #{basename}.svg > #{basename}.svgz", run_args)
      builder.thor.say_status :svgz, "#{unprefixed}.svgz"
    end

    # generate fallbacks
    builder.thor.run("#{options.inkscape_bin} --export-filename=#{basename}.png #{options.inkscape_options} #{basename}.svg", run_args)
    builder.thor.say_status :svg_fallback, "#{unprefixed}.png"

    builder.thor.run("#{options.convert_bin} #{options.convert_options} #{basename}.png #{basename}.jpg", run_args)
    builder.thor.say_status :svg_fallback, "#{unprefixed}.jpg"
  end
end