-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRakefile
69 lines (53 loc) · 2.38 KB
/
Rakefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# frozen_string_literal: true
require 'nokogiri'
require 'yaml'
require 'zip'
namespace :build do
task :svg do
Dir.glob('svg/symbols/*.svg').each do |symbol_path|
puts "Converting #{symbol_path}..."
create_svg('svg/backgrounds/white.svg', '#000000', 'white', symbol_path)
create_svg('svg/backgrounds/black.svg', '#ffffff', 'black', symbol_path)
create_svg('svg/backgrounds/transparent.svg', 'param(fill)', 'transparent', symbol_path)
end
symbol_names = Dir.glob('svg/symbols/*.svg').sort.map do |svg_file|
svg_file.split('/').last.split('.').first
end
output = {}
output['symbols'] = symbol_names
File.open('data/symbols.yml', 'w') { |f| f.write output.to_yaml }
FileUtils.mkdir_p 'source/downloads'
%w[black white transparent].each { |c| zip_symbols(c) }
end
def zip_symbols(color)
Zip.continue_on_exists_proc = true
zip_file = "source/downloads/national-park-symbols-#{color}.zip"
puts "Creating #{zip_file}..."
Zip::File.open(zip_file, Zip::File::CREATE) do |zipfile|
Dir.glob("source/images/svg/#{color}/*.svg").each do |symbol_path|
puts symbol_path
zipfile.add(symbol_path.split('/').last, symbol_path)
end
end
puts "#{zip_file} created."
end
def create_svg(background_path, fill_color, prefix, symbol_path)
FileUtils.mkdir_p "source/images/svg/#{prefix}"
symbol_name = symbol_path.split('/').last
background = Nokogiri::XML.parse(File.read(background_path))
symbol = Nokogiri::XML.parse(File.read(symbol_path.to_s))
symbol_contents = symbol.css('/svg').children
translate_group = Nokogiri::XML::Node.new('g', background)
translate_group.set_attribute('transform', 'translate(3 3)')
translate_group.add_child(symbol_contents)
translate_group.css('path').each { |g| g.set_attribute('fill', fill_color) }
translate_group.css('polygon').each { |g| g.set_attribute('fill', fill_color) }
translate_group.css('rect').each { |g| g.set_attribute('fill', fill_color) }
translate_group.css('circle').each { |g| g.set_attribute('fill', fill_color) }
translate_group.css('ellipse').each { |g| g.set_attribute('fill', fill_color) }
background.css('/svg/rect').after(translate_group)
output_path = "source/images/svg/#{prefix}/#{symbol_name}"
puts "Writing #{output_path}..."
File.write(output_path, background.to_xml)
end
end