This commit is contained in:
Denis Knauf 2019-01-12 23:05:24 +01:00
commit 4664814b80
13 changed files with 450 additions and 0 deletions

3
.gitignore vendored Normal file
View file

@ -0,0 +1,3 @@
*.sw[opn]
*.gem
/pkg

8
Gemfile Normal file
View file

@ -0,0 +1,8 @@
source 'https://rubygems.org'
group :development do
gem "rake", '~> 12', require: false
gem "rdoc", '~> 4'
gem "bundler", '~> 1'
gem "jeweler", '~> 2'
end

62
Gemfile.lock Normal file
View file

@ -0,0 +1,62 @@
GEM
remote: https://rubygems.org/
specs:
addressable (2.4.0)
builder (3.2.3)
descendants_tracker (0.0.4)
thread_safe (~> 0.3, >= 0.3.1)
faraday (0.9.2)
multipart-post (>= 1.2, < 3)
git (1.5.0)
github_api (0.16.0)
addressable (~> 2.4.0)
descendants_tracker (~> 0.0.4)
faraday (~> 0.8, < 0.10)
hashie (>= 3.4)
mime-types (>= 1.16, < 3.0)
oauth2 (~> 1.0)
hashie (3.6.0)
highline (2.0.0)
jeweler (2.3.9)
builder
bundler
git (>= 1.2.5)
github_api (~> 0.16.0)
highline (>= 1.6.15)
nokogiri (>= 1.5.10)
psych
rake
rdoc
semver2
jwt (2.1.0)
mime-types (2.99.3)
mini_portile2 (2.4.0)
multi_json (1.13.1)
multi_xml (0.6.0)
multipart-post (2.0.0)
nokogiri (1.10.0)
mini_portile2 (~> 2.4.0)
oauth2 (1.4.1)
faraday (>= 0.8, < 0.16.0)
jwt (>= 1.0, < 3.0)
multi_json (~> 1.3)
multi_xml (~> 0.5)
rack (>= 1.2, < 3)
psych (3.1.0)
rack (2.0.6)
rake (12.3.2)
rdoc (4.3.0)
semver2 (3.4.2)
thread_safe (0.3.6)
PLATFORMS
ruby
DEPENDENCIES
bundler (~> 1)
jeweler (~> 2)
rake (~> 12)
rdoc (~> 4)
BUNDLED WITH
1.17.3

20
LICENSE.txt Normal file
View file

@ -0,0 +1,20 @@
Copyright (c) 2014 Denis Knauf
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

0
README.md Normal file
View file

50
Rakefile Normal file
View file

@ -0,0 +1,50 @@
# encoding: utf-8
require 'rubygems'
require 'bundler'
begin
Bundler.setup(:default, :development)
rescue Bundler::BundlerError => e
$stderr.puts e.message
$stderr.puts "Run `bundle install` to install missing gems"
exit e.status_code
end
require 'jeweler'
Jeweler::Tasks.new do |gem|
gem.name = "middleman-fonts"
gem.homepage = "http://github.com/DenisKnauf/middleman-fonts"
gem.license = "MIT"
gem.summary = %Q{Fonts formats convertions}
gem.description = %{Generates WOFFs from TTFs and fonts.css}
gem.email = %q{#{"de"}nis}+?@+'den'+%Q{kn}+"#{?.}at"
gem.authors = ["Denis Knauf"]
gem.files = %w[README.md VERSION lib/**/*.rb test/**/*.rb]
gem.require_paths = %w[lib]
end
Jeweler::RubygemsDotOrgTasks.new
require 'rake/testtask'
Rake::TestTask.new(:test) do |test|
test.libs << 'lib' << 'test'
test.pattern = 'test/**/test_*.rb'
test.verbose = true
end
desc "Code coverage detail"
task :simplecov do
ENV['COVERAGE'] = "true"
Rake::Task['test'].execute
end
task :default => :test
require 'rdoc/task'
Rake::RDocTask.new do |rdoc|
version = File.exist?('VERSION') ? File.read('VERSION') : ""
rdoc.rdoc_dir = 'rdoc'
rdoc.title = "middleman-fonts #{version}"
rdoc.rdoc_files.include('README*')
rdoc.rdoc_files.include('lib/**/*.rb')
end

1
VERSION Normal file
View file

@ -0,0 +1 @@
0.0.1

16
lib/middleman-fonts.rb Normal file
View file

@ -0,0 +1,16 @@
require "middleman-core"
::Middleman::Extensions.register :fonts_css do
require 'middleman-fonts/fonts_css'
::Middleman::Extensions::Metadata2FontsCss
end
::Middleman::Extensions.register :fonts4web do
require 'middleman-fonts/fonts4web'
::Middleman::Extensions::Fonts4Web
end
::Middleman::Extensions.register :font_awesome do
require 'middleman-fonts/font_awesome'
::Middleman::Extensions::FontAwesome
end

View file

@ -0,0 +1,15 @@
require 'pathname'
require 'fileutils'
require 'font-awesome-sass'
class ::Middleman::Extensions::FontAwesome < ::Middleman::Extension
def manipulate_resource_list resources
path = Pathname.new FontAwesome::Sass::fonts_path
path.find do |f|
next unless f.file?
dest = Pathname.new( 'assets')/'fonts'/f.relative_path_from( path)
resources << ::Middleman::Sitemap::Resource.new( @app.sitemap, dest.to_s, f.to_s)
end
resources
end
end

View file

@ -0,0 +1,156 @@
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 <<self
def included() true end
alias registered included
end
def spawn *a, **options
a = a.map &:to_s
if options[:verbose]
pre = ''
pre = "cd #{Shellwords.escape options[:chdir]}; " if options.has_key? :chdir
STDERR.puts "$ #{pre}#{a.shelljoin}"
end
options.delete :verbose
options[:chdir] &&= options[:chdir].to_s
pid = Process.spawn *a, **options
_, status = Process.wait2 pid
status
end
def convert builder, src, dest = nil
# 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
#def manipulate_resource_list resources
# source_dir = Pathname.new @app.config[:source]
# paths = {}
# resources.each {|res| paths[res.path] = res }
# resources.each do |res|
# path = Pathname.new res.path
# if 'assets' == path.descend.first.to_s
# case path.extname
# when '.otf', '.ttf'
# new = path.dirname/"#{path.basename path.extname}.woff"
# unless paths.has_key? new
# STDERR.puts "convert #{new} <= #{path}"
# resource = ::Middleman::Sitemap::Fonts4Web.new( @app.sitemap, new.to_s, res)
# if false
# woff = convert source_dir.expand_path/path #, path.dirname/"#{path.basename path.extname}.woff"
# resources << ::Middleman::Sitemap::Fonts4Web.new( @app.sitemap, new.to_s, woff.to_s)
# end
# resources << resource
# paths[new.to_s] = resource
# end
# end
# end
# end
# resources
#end
def after_build builder
paths = ::Middleman::Util.all_files_under app.config[:build_dir]
paths.each do |path|
if 'assets' == path.relative_path_from( Pathname.new(@app.config[:build_dir])).descend.first.to_s
case path.extname
when '.otf', '.ttf'
new = path.dirname/"#{path.basename path.extname}.woff"
unless paths.include?( new)
convert builder, path, path.dirname/"#{path.basename path.extname}.woff"
end
end
end
end
end
end

View file

@ -0,0 +1,62 @@
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

View file

@ -0,0 +1 @@
require 'middleman-fonts'

56
middleman-fonts.gemspec Normal file
View file

@ -0,0 +1,56 @@
# Generated by jeweler
# DO NOT EDIT THIS FILE DIRECTLY
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
# -*- encoding: utf-8 -*-
# stub: middleman-fonts 0.0.1 ruby lib
Gem::Specification.new do |s|
s.name = "middleman-fonts".freeze
s.version = "0.0.1"
s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version=
s.require_paths = ["lib".freeze]
s.authors = ["Denis Knauf".freeze]
s.date = "2019-01-05"
s.description = "Generates WOFFs from TTFs and fonts.css".freeze
s.email = "\#{\"de\"}nis@denkn.at".freeze
s.extra_rdoc_files = [
"LICENSE.txt",
"README.md"
]
s.files = [
"README.md",
"VERSION",
"lib/middleman-fonts.rb",
"lib/middleman-fonts/font_awesome.rb",
"lib/middleman-fonts/fonts4web.rb",
"lib/middleman-fonts/fonts_css.rb",
"lib/middleman_extension.rb"
]
s.homepage = "http://github.com/DenisKnauf/middleman-fonts".freeze
s.licenses = ["MIT".freeze]
s.rubygems_version = "2.7.6".freeze
s.summary = "Fonts formats convertions".freeze
if s.respond_to? :specification_version then
s.specification_version = 4
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
s.add_development_dependency(%q<rake>.freeze, ["~> 12"])
s.add_development_dependency(%q<rdoc>.freeze, ["~> 4"])
s.add_development_dependency(%q<bundler>.freeze, ["~> 1"])
s.add_development_dependency(%q<jeweler>.freeze, ["~> 2"])
else
s.add_dependency(%q<rake>.freeze, ["~> 12"])
s.add_dependency(%q<rdoc>.freeze, ["~> 4"])
s.add_dependency(%q<bundler>.freeze, ["~> 1"])
s.add_dependency(%q<jeweler>.freeze, ["~> 2"])
end
else
s.add_dependency(%q<rake>.freeze, ["~> 12"])
s.add_dependency(%q<rdoc>.freeze, ["~> 4"])
s.add_dependency(%q<bundler>.freeze, ["~> 1"])
s.add_dependency(%q<jeweler>.freeze, ["~> 2"])
end
end