Class: EvilIcons::Generator
- Inherits:
-
Object
- Object
- EvilIcons::Generator
- Defined in:
- lib/evil_icons/generator.rb
Instance Method Summary collapse
- #files ⇒ Object
- #icons ⇒ Object
-
#initialize(svg_path) ⇒ Generator
constructor
A new instance of Generator.
- #optimize(svg) ⇒ Object
- #read_svg(filename) ⇒ Object
- #sprite(template) ⇒ Object
- #write(sprite_path, template) ⇒ Object
Constructor Details
#initialize(svg_path) ⇒ Generator
Returns a new instance of Generator.
8 9 10 11 |
# File 'lib/evil_icons/generator.rb', line 8 def initialize(svg_path) @svg_path = svg_path @templates_dir = File.('../../templates', __FILE__) end |
Instance Method Details
#files ⇒ Object
13 14 15 16 17 |
# File 'lib/evil_icons/generator.rb', line 13 def files @_files ||= begin Dir.entries(@svg_path).select { |f| File.extname(f) == '.svg' } end end |
#icons ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/evil_icons/generator.rb', line 24 def icons files.map do |name| file = read_svg(name) optimized = SvgOptimizer.optimize(file) doc = Nokogiri::HTML::DocumentFragment.parse(optimized) svg = doc.at_css('svg') viewbox = svg['viewbox'] g = svg.search('g') container = g.empty? ? svg : g shape = container.children.map {|c| c.to_s}.join('') name = File.basename(name, '.svg') { name: name, viewbox: viewbox, shape: shape } end end |
#optimize(svg) ⇒ Object
42 43 44 |
# File 'lib/evil_icons/generator.rb', line 42 def optimize(svg) svg.gsub(/$\s+/, '') end |
#read_svg(filename) ⇒ Object
19 20 21 22 |
# File 'lib/evil_icons/generator.rb', line 19 def read_svg(filename) file = File.join(@svg_path, filename) File.read(file) end |
#sprite(template) ⇒ Object
46 47 48 49 50 |
# File 'lib/evil_icons/generator.rb', line 46 def sprite(template) view = File.read File.join(@templates_dir, "#{template}.erb") result = ERB.new(view).result(binding) template == 'icons' ? optimize(result) : result end |
#write(sprite_path, template) ⇒ Object
52 53 54 55 56 |
# File 'lib/evil_icons/generator.rb', line 52 def write(sprite_path, template) file = File.new(sprite_path, 'w') file.write sprite(template) file.close end |