require 'json' require 'active_support/all' require 'fileutils' require 'shellwords' require 'digest/md5' class ::Middleman::Sitemap::Fonts4Web < ::Middleman::Sitemap::Resource def initialize store, path, source source_dir = Pathname.new( store.app.config[:source]).expand_path STDERR.printf "fonts4web %p\n", path: path, source: source, source_dir: source_dir super store, path, nil @source = source end def binary?() true end def render *_ convert( Pathname.new( @source)).read end def convert src STDERR.puts "convert #{src}" # files: f = src.basename md5sum = Digest::MD5.hexdigest src.read # files in cache: symlink = @cachepath/"#{f.basename f.extname}-#{md5sum}#{f.extname}" cache_dest = @cachepath/"#{symlink.basename f.extname}.woff" relsrc = src.expand_path.relative_path_from @cachepath.expand_path dest = dest ? Pathname.new( dest) : cache_dest.expand_path @cachepath.mkpath identical = true # cache_dest is older than source unless cache_dest.exist? identical = false # prepare cache-files: symlink.unlink if begin symlink.lstat; rescue Errno::ENOENT; false; end symlink.make_symlink relsrc # convert: status = spawn @converter, symlink.basename, chdir: symlink.dirname raise "Convertion #{src} failed!" unless 0 == status.exitstatus raise "Convertion #{src} failed!" unless cache_dest.exist? end #FileUtils.copy cache_dest, dest, verbose: false #builder.trigger identical ? :identical : :created, "#{dest}" dest end end class ::Middleman::Extensions::Fonts4Web < ::Middleman::Extension def initialize app, **options_hash, &block super @converter = 'sfnt2woff-zopfli' @cachepath = Pathname.new '.fonts_cache' end def included() true end alias registered included class <