Skip to content

Instantly share code, notes, and snippets.

@zetter
Created December 9, 2013 15:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zetter/7874395 to your computer and use it in GitHub Desktop.
Save zetter/7874395 to your computer and use it in GitHub Desktop.
Parse the icon file from icomoon and print out a ruby hash of character names to characters.
# Usage:
# run the file with the dev.svg from icomoon (not the plain svg)
# ruby parse_icons_from_icomoon.rb /path/to/iconfont.dev.svg
require 'nokogiri'
file = ARGV[0]
svg = File.open(file)
document = Nokogiri::XML(svg)
glyphs = document.css('glyph')
name_and_chars = glyphs.map do |glyph|
name = glyph['data-tags'].to_s.downcase.gsub(/[- \_]/, '_').gsub(/[^a-z0-9\_]/, '')
char = glyph['unicode'].to_s
[name, char]
end
name_and_chars = name_and_chars.reject {|name, char| name == '' || char == ''}
name_and_chars = name_and_chars.sort
string = name_and_chars.map{|name, char|
" #{name}: '#{char}'"
}.join(",\n")
puts "{\n#{string}\n}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment