Skip to content

Instantly share code, notes, and snippets.

@iosdevzone
Created June 7, 2013 03:08
Show Gist options
  • Save iosdevzone/5726815 to your computer and use it in GitHub Desktop.
Save iosdevzone/5726815 to your computer and use it in GitHub Desktop.
A ruby script to produce an Objective C literal from the information in FontAwesome's icons.yml file.
#
# Ruby Script to convert FontAwesome
# glyph information into Objective-C literals
# (Ends up being a big array of NSDictionary objects)
# Output is to stdout. Assumes icons.yml is in working directory.
# Copyright (c) 2013 iOSDeveloperZone.com
# MIT license -- See: http://opensource.org/licenses/MIT
require 'yaml'
#
# Box a string
# s -- a string
# return -- a boxed string as a string (e.g. hello -> @"hello")
def str(s)
"@\"#{s}\""
end
#
# Box a hex constant
# x -- a hex value
# return -- a boxed hex number as a string (e.g. f045 -> @0xf045)
#
def hex(x)
"@0x" + x
end
#
# Convert a Ruby array to an Objective-C literal array
# a -- the array
# l -- a lambda to box the array elements the lambda is passed the array element
# and should return the boxed element as a string
# s -- a separator (for small arrays this is "," for large ",\n")
# return -- a string containing the entire boxed array
#
def arr(a,l,s)
t = []
a.each { |s| t.push(l.call(s)) }
return "@[ " + t.join(s) + " ] "
end
#
# Convert a Ruby hash to an Objective-C literal dictionary
# d -- the hash
# return -- a string containing the entire literal
#
def dict(d)
ls = lambda { |s| "@\"#{s}\"" }
t = []
d.each { |k,v|
s = str(k) + ":"
case k
when "unicode" then s += hex(v)
when "categories","aliases" then s += arr(v,ls,", ")
else
s += str(v)
end
t.push(s)
}
return "@{ " + t.join(",") + "}"
end
#
icons = YAML.load_file('icons.yml')
ld = lambda { |d| dict(d) }
puts arr(icons["icons"], ld, ",\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment