Last active
December 26, 2015 09:58
-
-
Save aegypius/7132917 to your computer and use it in GitHub Desktop.
FontAwesome Iconset Generator for Zooper Widget Pro
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "open-uri" | |
require "erb" | |
task :default => ['fontawesome:build'] | |
namespace :fontawesome do | |
version = 'v4.0.0' | |
task :build => ['fontawesome:generate_config', 'fontawesome:download_ttf'] do | |
%x{zip FontAwesome-#{version}.zip config.xml FontAwesome-#{version}.ttf && rm config.xml FontAwesome-#{version}.ttf} | |
end | |
task :generate_config do | |
# Parse font mapping from github | |
modes = {} | |
conditions = {} | |
pattern = /^@(?<id>fa-var-(?<label>[^:]+)):\s+"\\(?<hexcode>[[:xdigit:]]{4})";/ | |
open("https://raw.github.com/FortAwesome/Font-Awesome/#{version}/less/variables.less").read.each_line do |line| | |
if pattern =~ line | |
m = pattern.match(line) | |
id = m['id'].gsub('-var-', '-').gsub('-', '_').upcase | |
label = m['label'] | |
code = '&#x' + m["hexcode"].upcase + ';' | |
if /-(right|left|up|down)$/ =~ label | |
directional = /-(right|left|up|down)$/.match(label)[1] | |
label.gsub!(/-(right|left|up|down)$/, '') | |
end | |
if /-(square|circle)/ =~ label | |
shape = /-(square|circle)/.match(label)[1] | |
label.gsub!(/-(square|circle)/, '') | |
end | |
if /-(o-|o$)/ =~label | |
outlined = 'Outlined' | |
label.gsub!(/-(o-|o$)/, '') | |
end | |
label = [label, directional, shape, outlined].join(' ').gsub('-', ' ').split.map(&:capitalize).join(' ') | |
modes[label] = id | |
conditions[code] = "#MODE#=#{id}" | |
end | |
end | |
modes = modes.sort_by { |value, key| key } | |
conditions = conditions.sort_by { |value, key| key } | |
class IconSetConfig | |
include ERB::Util | |
attr_accessor :modes, :conditions, :version | |
def initialize(modes, conditions, version) | |
@modes = modes | |
@conditions = conditions | |
@version = version | |
@template = <<-ERB | |
<?xml version="1.0" encoding="utf-8" ?> | |
<config> | |
<title>FontAwesome IconSet <%= version %></title> | |
<description>FontAwesome'ness for Zooper</description> | |
<font>FontAwesome-<%= version %>.ttf</font> | |
<demo>    </demo> | |
<default></default> | |
<% for @label, @value in @modes %> | |
<mode value="<%= @value %>"><%= @label %></mode><% end %> | |
<% for @image, @test in @conditions %> | |
<condition><test><%= @test %></test><image><%= @image %></image></condition><% end %> | |
</config> | |
ERB | |
end | |
def render() | |
ERB.new(@template).result(binding) | |
end | |
def save(file) | |
File.open(file, 'w+') do |f| | |
f.write(render) | |
end | |
end | |
end | |
config = IconSetConfig.new(modes, conditions, version) | |
config.save('config.xml') | |
end | |
task :download_ttf do | |
File.open("FontAwesome-#{version}.ttf", "wb") do |file| | |
file.write open("https://github.com/FortAwesome/Font-Awesome/raw/#{version}/fonts/fontawesome-webfont.ttf").read | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment