63 lines
1.6 KiB
Ruby
63 lines
1.6 KiB
Ruby
require 'json'
|
|
require 'active_support/all'
|
|
require 'fileutils'
|
|
|
|
class ::Middleman::Extensions::Metadata2FontsCss < ::Middleman::Extension
|
|
def initialize app, **options_hash, &block
|
|
super
|
|
@additional_extentions = %w[woff]
|
|
end
|
|
def included() true end
|
|
alias registered included
|
|
class <<self
|
|
def included() true end
|
|
alias registered included
|
|
end
|
|
|
|
def manipulate_resource_list resources
|
|
fonts_css = StringIO.new
|
|
resources.lazy.
|
|
select {|res| Pathname.new( res.path).basename.to_s == 'METADATA.json' }.
|
|
map do |res|
|
|
JSON.parse( File.read( f))['fonts'].each do |font|
|
|
font = font.symbolize_keys
|
|
font[:ttf] = "/assets/fonts/#{font[:filename]}"
|
|
font[:woff] = "/assets/fonts/#{font[:filename].gsub /\.[ot]tf\Z/, '.woff'}"
|
|
fonts_css.printf <<EOF.gsub( /^\s*(.*?)\s*$/, '\1').gsub( /\n/m, ''), font
|
|
@font-face {
|
|
font-family: "%<name>s";
|
|
font-weight: %<weight>i;
|
|
font-style: %<style>s;
|
|
src:
|
|
local("%<fullName>s"),
|
|
local("%<postScriptName>s"),
|
|
url("%<woff>s") format("woff"),
|
|
url("%<ttf>s") format("truetype");
|
|
}
|
|
EOF
|
|
end
|
|
end
|
|
|
|
resources.delete_if do |res|
|
|
path = Pathname.new res.path
|
|
if 'assets' == path.descend.first.to_s
|
|
case path.basename.to_s
|
|
when 'METADATA.pb' then true
|
|
when 'METADATA.json' then true
|
|
when 'DESCRIPTION.en_us.html' then true
|
|
when 'OFL.txt' then true
|
|
else false
|
|
end
|
|
else false
|
|
end
|
|
end
|
|
|
|
resources << ::Middleman::Sitemap::StringResource.new(
|
|
self.app.sitemap,
|
|
'assets/styles/fonts.css',
|
|
fonts_css.string)
|
|
|
|
resources
|
|
end
|
|
end
|