Skip to content

Instantly share code, notes, and snippets.

@madrobby
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save madrobby/8722dda972d2daecee59 to your computer and use it in GitHub Desktop.
Save madrobby/8722dda972d2daecee59 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'json'
FILENAME = "../app/helpers/icon_helper.rb"
JAVASCRIPT_HELPER = "../public/js/icons/icons.js"
puts "Generating helper #{FILENAME}"
File.open(FILENAME, 'w') do |helper|
helper.write "module IconHelper\n\n"
Dir.glob(File.join("..","public","images","icons","*.svg")).each do |icon|
svg = File.read(icon).delete("\n").delete("\r").delete("\t")
name = icon.split('/').last.tr('-','_').gsub(/\.svg$/,'')
puts "#{name}: #{svg.size} bytes"
helper.write " def icon_#{name}\n"
helper.write " '#{svg}'\n"
helper.write " end\n\n"
end
helper.write "end"
end
puts "Generating JavaScript helper"
File.open(JAVASCRIPT_HELPER, 'w') do |helper|
Dir.glob(File.join("..","public","images","icons","*.svg")).each do |icon|
svg = File.read(icon).delete("\n").delete("\r").delete("\t")
name = icon.split('/').last.tr('-','_').gsub(/\.svg$/,'')
# we only need a few specific icons in JavaScript, so skip those we don't want
next unless name =~ /^cursor/ || name =~ /^arrow_right/
puts "(JavaScript) #{name}: #{svg.size} bytes"
helper.write "window.__icon_#{name} = #{svg.to_json};\n"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment