-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfoxcat-generator
executable file
·90 lines (69 loc) · 2.14 KB
/
foxcat-generator
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env ruby
require "fileutils"
require "json"
require "nokogiri"
require "optparse"
Options = {
input: "#{__dir__}/foxcats.json",
svg: "#{__dir__}/svg",
png: "#{__dir__}/png",
avatars: true,
thumbnails: true
}
OptionParser.new do |opts|
opts.banner = "Usage: foxcat-generator [options]"
opts.on("-s", "--svg SVG", "SVG output folder") do |v|
Options[:svg] = v
end
opts.on("-p", "--png PNG", "PNG output folder") do |v|
Options[:png] = v
end
opts.on("-a", "--[no-]avatars", "Generate PNG avatars") do |v|
Options[:avatars] = v
end
opts.on("-t", "--[no-]thumbnails", "Generate PNG thumbnails") do |v|
Options[:thumbnails] = v
end
end.parse!
FileUtils.mkdir_p(Options[:svg])
FileUtils.mkdir_p(Options[:png] + "/128")
FileUtils.mkdir_p(Options[:png] + "/1024")
Document = <<-END
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg
xmlns="http://www.w3.org/2000/svg"
xmlns:svg="http://www.w3.org/2000/svg"
width="128px"
height="128px"
viewBox="0 0 750 750"
style="shape-rendering: geometricPrecision">
</svg>
END
Stylesheet = <<-END
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="UTF-8" indent="yes" />
<xsl:strip-space elements="*" />
<xsl:template match="/">
<xsl:copy-of select="." />
</xsl:template>
</xsl:stylesheet>
END
XSLT = Nokogiri::XSLT.parse(Stylesheet)
ARGV.each do |filename|
dirname = File.dirname(filename)
json = JSON.parse(File.read(filename))
name = json["name"]
layers = json.fetch("layers")
svg = Nokogiri::XML.parse(Document)
g = svg.create_element("g", fill: "none")
svg.root.add_child(svg.create_element("title", name)) if name
svg.root.add_child(g)
layers.each do |layer|
g.add_child(Nokogiri::XML.fragment(File.read("#{dirname}/#{layer}")))
end
svg_path = "#{Options[:svg]}/#{name}.svg"
File.write(svg_path, XSLT.transform(svg).to_s)
system("svgexport #{svg_path} #{Options[:png]}/128/#{name}.png") if Options[:thumbnails]
system("svgexport #{svg_path} #{Options[:png]}/1024/#{name}.png 1024:1024") if Options[:avatars]
end