compass-svg-sprite 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/lib/compass-svg-sprite.rb +6 -0
- data/lib/compass-svg-sprite/core.rb +147 -0
- data/lib/compass-svg-sprite/helpers.rb +33 -0
- data/lib/compass-svg-sprite/sass_extensions.rb +71 -0
- metadata +48 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: 9a5b49f1d4f1da0528223cbeb3a48d14570f30fa
|
|
4
|
+
data.tar.gz: 58565af53cd47546167559b54450df4f25673443
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: fec9d2f9b8eaff002975b44843c2cd5efd203490465da14a984e62c8aba9d2e143b28458213c9b4d7677d195251f4fb92e05c683708cb7c237f84e8a24926e86
|
|
7
|
+
data.tar.gz: 0a280531eb20db12775a4241817324dfd32b606162ca4a36998bc8d78246be099dc7af69d6abd0ec7d9169f6297ffa6e6cadde0ada7504e5d09d815b2e7d9c19
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
require 'compass'
|
|
2
|
+
extension_path = File.expand_path(File.join(File.dirname(__FILE__), ".."))
|
|
3
|
+
stylesheets_dir = File.join(extension_path, 'stylesheets')
|
|
4
|
+
Compass::Frameworks.register('compass-svg-sprite', :path => extension_path, :stylesheets_directory => stylesheets_dir)
|
|
5
|
+
|
|
6
|
+
require 'compass-svg-sprite/sass_extensions'
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
require 'compass-svg-sprite/helpers'
|
|
2
|
+
|
|
3
|
+
class SvgSprite
|
|
4
|
+
|
|
5
|
+
def initialize(config)
|
|
6
|
+
@@map = Hash.new
|
|
7
|
+
@@config = Hash.new
|
|
8
|
+
@@svgs = Hash.new
|
|
9
|
+
@@width = 0
|
|
10
|
+
@@height = 0
|
|
11
|
+
|
|
12
|
+
init_config(config)
|
|
13
|
+
init_sprite
|
|
14
|
+
write_sprite
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def init_config(config)
|
|
18
|
+
@@config = map_to_hash(config)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def init_sprite
|
|
22
|
+
svg_dir = File.join(Compass.configuration.images_path, @@config['icons_dir'], "*.svg")
|
|
23
|
+
files = Dir.glob(svg_dir)
|
|
24
|
+
files.each do |file|
|
|
25
|
+
process_icon(file)
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def process_icon(file)
|
|
30
|
+
fileName = File.basename(file, ".svg")
|
|
31
|
+
svg = optimize_svg(get_contents(file))
|
|
32
|
+
width = get_svg_size(svg, 'width')
|
|
33
|
+
height = get_svg_size(svg, 'height')
|
|
34
|
+
svg = change_icon_position(svg, width, height, @@width, @@config['padding'], fileName)
|
|
35
|
+
@@svgs[fileName] = Hash.new
|
|
36
|
+
@@map[fileName] = {
|
|
37
|
+
'width' => width,
|
|
38
|
+
'height' => height,
|
|
39
|
+
'colors' => get_icon_color_variants(fileName, svg, width, height)
|
|
40
|
+
}
|
|
41
|
+
@@height = height + @@config['padding'] > @@height ? height + @@config['padding'] : @@height
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def optimize_svg(svg)
|
|
45
|
+
svg = svg.gsub(/.*?<svg(.*?)>(.*?)<\/svg>.*?/mi, "<svg\\1>\\2</svg>")
|
|
46
|
+
svg = svg.gsub(/(\sxmlns=".*?")/mi, "")
|
|
47
|
+
svg = svg.gsub(/(\sxmlns:xlink=".*?")/mi, "")
|
|
48
|
+
svg = svg.gsub(/(\sversion=".*?")/mi, "")
|
|
49
|
+
svg = svg.gsub(/\n|\r|\t/mi, "")
|
|
50
|
+
svg = svg.gsub(/\s\s+/mi, " ")
|
|
51
|
+
return svg
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def change_icon_position(svg, width, height, x, y, id)
|
|
55
|
+
svg = svg.gsub(/(<svg.*?)(\swidth=".*?")(.*?>)/im, '\1\3')
|
|
56
|
+
svg = svg.gsub(/(<svg.*?)(\sheight=".*?")(.*?>)/im, '\1\3')
|
|
57
|
+
svg = svg.gsub(/(<svg.*?)(\sviewbox=".*?")(.*?>)/im, '\1\3')
|
|
58
|
+
svg = svg.gsub(/(<svg.*?)(\sid=".*?")(.*?>)/im, '\1\3')
|
|
59
|
+
svg = svg.gsub(/(<svg.*?)(\sx=".*?")(.*?>)/im, '\1\3')
|
|
60
|
+
svg = svg.gsub(/(<svg.*?)(\sy=".*?")(.*?>)/im, '\1\3')
|
|
61
|
+
svg = svg.gsub(/(<svg.*?)(\senable-background=".*?")(.*?>)/im, '\1\3')
|
|
62
|
+
svg = svg.gsub(/^(<svg)/mi, "<svg width=\"#{width}\" height=\"#{height}\" viewBox=\"0 0 #{width} #{height}\" enable-background=\"new 0 0 #{width} #{height}\" id=\"#{id}\" x=\"#{x}\" y=\"#{y}\"")
|
|
63
|
+
return svg
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def get_svg_size(svg, kind)
|
|
67
|
+
size = svg.scan(/<svg.*? #{kind}="(.*?)".*?>/mi)
|
|
68
|
+
if size[0].kind_of?(Array)
|
|
69
|
+
size = size[0][0].to_f
|
|
70
|
+
else
|
|
71
|
+
viewBoxSizes = svg.scan(/<svg.*? viewbox="(.*?)"/mi)[0][0].split(" ")
|
|
72
|
+
if kind === 'width'
|
|
73
|
+
size = viewBoxSizes[2].to_f - viewBoxSizes[0].to_f
|
|
74
|
+
else
|
|
75
|
+
size = viewBoxSizes[3].to_f - viewBoxSizes[1].to_f
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
return size.round
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def get_icon_color_variants(fileName, svg, width, height)
|
|
82
|
+
hash = Hash.new
|
|
83
|
+
@@width += @@config['padding']
|
|
84
|
+
hash['default'] = { 'x' => @@width.to_s, 'y' => @@config['padding'].to_s}
|
|
85
|
+
@@svgs[fileName]['default'] = change_icon_position(svg, width, height, @@width, @@config['padding'], fileName)
|
|
86
|
+
@@width += width + @@config['padding']
|
|
87
|
+
if @@config['icon_variants'].kind_of?(Hash) && @@config['icon_variants'][fileName].kind_of?(Hash)
|
|
88
|
+
@@config['icon_variants'][fileName].each do |name, value|
|
|
89
|
+
@@width += @@config['padding']
|
|
90
|
+
hash[name] = { 'x' => @@width.to_s, 'y' => @@config['padding'].to_s}
|
|
91
|
+
svg = change_icon_color(svg, value)
|
|
92
|
+
@@svgs[fileName][name] = change_icon_position(svg, width, height, @@width, @@config['padding'], fileName + '-' + name)
|
|
93
|
+
@@width += width + @@config['padding']
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
return hash
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
def change_icon_color(svg, color)
|
|
100
|
+
case @@config['color_replace_mode']
|
|
101
|
+
when 'stroke'
|
|
102
|
+
svg = svg.gsub(/(\sstroke=".*?")/im, " stroke=\"#{color}\"")
|
|
103
|
+
when 'fill'
|
|
104
|
+
svg = svg.gsub(/(\sfill=".*?")/im, " fill=\"#{color}\"")
|
|
105
|
+
when 'both'
|
|
106
|
+
svg = svg.gsub(/(\sstroke=".*?")/im, " stroke=\"#{color}\"")
|
|
107
|
+
svg = svg.gsub(/(\sfill=".*?")/im, " fill=\"#{color}\"")
|
|
108
|
+
end
|
|
109
|
+
return svg
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
def get_contents(real_path)
|
|
113
|
+
if File.readable?(real_path)
|
|
114
|
+
File.open(real_path, "rb") {|io| io.read}
|
|
115
|
+
else
|
|
116
|
+
raise Compass::Error, "File not found or cannot be read: #{real_path}"
|
|
117
|
+
end
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
def write_sprite
|
|
121
|
+
File.open(File.join(Compass.configuration.images_path, "sprite.svg"), "w") do |file|
|
|
122
|
+
output = "<svg xmlns=\"http://www.w3.org/2000/svg\""
|
|
123
|
+
output += " xmlns:xlink=\"http://www.w3.org/1999/xlink\""
|
|
124
|
+
output += " width=\"#{@@width}\" height=\"#{@@height}\" viewBox=\"0 0 #{@@width} #{@@height}\">\n"
|
|
125
|
+
@@svgs.each do |icon, value|
|
|
126
|
+
value.each do |color, svg|
|
|
127
|
+
output += "\t" + svg + "\n"
|
|
128
|
+
end
|
|
129
|
+
end
|
|
130
|
+
output += "</svg>"
|
|
131
|
+
file.write(output)
|
|
132
|
+
end
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
def self.get_map
|
|
136
|
+
return hash_to_map(@@map)
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
def self.get_width
|
|
140
|
+
return @@width
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
def self.get_height
|
|
144
|
+
return @@height
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
end
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
def map_to_hash(map)
|
|
2
|
+
hash = Hash.new
|
|
3
|
+
map.value.each do |key, value|
|
|
4
|
+
case value.class.name
|
|
5
|
+
when 'Sass::Script::Value::Map'
|
|
6
|
+
hash[key.value] = map_to_hash(value)
|
|
7
|
+
when 'Sass::Script::Value::Color'
|
|
8
|
+
hash[key.value] = value.to_s
|
|
9
|
+
else
|
|
10
|
+
hash[key.value] = value.value
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
return hash
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def value_to_sass(value)
|
|
17
|
+
case value.class.name
|
|
18
|
+
when 'String'
|
|
19
|
+
sass_value = Sass::Script::Value::String.new(value)
|
|
20
|
+
when 'Fixnum'
|
|
21
|
+
sass_value = Sass::Script::Value::Number.new(value)
|
|
22
|
+
when 'Float'
|
|
23
|
+
sass_value = Sass::Script::Value::Number.new(value)
|
|
24
|
+
when 'Hash'
|
|
25
|
+
sass_value = hash_to_map(value)
|
|
26
|
+
end
|
|
27
|
+
return sass_value
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def hash_to_map(hash)
|
|
31
|
+
new_hash = Hash[hash.map{|key, value| [value_to_sass(key), value_to_sass(value)]}]
|
|
32
|
+
return Sass::Script::Value::Map.new(new_hash)
|
|
33
|
+
end
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
require 'compass-svg-sprite/core'
|
|
2
|
+
require 'compass-svg-sprite/helpers'
|
|
3
|
+
|
|
4
|
+
module Sass::Script::Functions
|
|
5
|
+
|
|
6
|
+
def svg_sprite_map(config)
|
|
7
|
+
SvgSprite.new(config)
|
|
8
|
+
SvgSprite.get_map
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def svg_icon_position_x(map, icon_name, color)
|
|
12
|
+
icon = get_icon_map(map, icon_name, color)
|
|
13
|
+
return map_get(icon, value_to_sass('x'))
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def svg_icon_position_y(map, icon_name, color)
|
|
17
|
+
icon = get_icon_map(map, icon_name, color)
|
|
18
|
+
return map_get(icon, value_to_sass('y'))
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def svg_icon_map(map, icon_name, color, size)
|
|
22
|
+
icon = map_get(map, icon_name)
|
|
23
|
+
icon_width = map_get(icon, value_to_sass('width')).value.to_f
|
|
24
|
+
icon_height = map_get(icon, value_to_sass('height')).value.to_f
|
|
25
|
+
x = svg_icon_position_x(map, icon_name, color).value.to_f
|
|
26
|
+
y = svg_icon_position_y(map, icon_name, color).value.to_f
|
|
27
|
+
bg_w = svg_sprite_width / icon_width * 100
|
|
28
|
+
bg_h = svg_sprite_height / icon_height * 100
|
|
29
|
+
new_x = x / (svg_sprite_width - icon_width) * 100
|
|
30
|
+
new_y = y / (svg_sprite_height - icon_height) * 100
|
|
31
|
+
if size.value
|
|
32
|
+
k = icon_height / size.value
|
|
33
|
+
new_width = icon_width / k
|
|
34
|
+
new_height = icon_height / k
|
|
35
|
+
icon_map = value_to_sass({
|
|
36
|
+
'width' => new_width.to_f.round,
|
|
37
|
+
'height' => new_height.to_f.round,
|
|
38
|
+
'background-size' => "#{bg_w}% #{bg_h}%",
|
|
39
|
+
'background-position' => "#{new_x}% #{new_y}%"
|
|
40
|
+
})
|
|
41
|
+
else
|
|
42
|
+
icon_map = value_to_sass({
|
|
43
|
+
'width' => icon_width,
|
|
44
|
+
'height' => icon_height,
|
|
45
|
+
'background-size' => "#{bg_w}% #{bg_h}%",
|
|
46
|
+
'background-position' => "#{new_x}% #{new_y}%"
|
|
47
|
+
})
|
|
48
|
+
end
|
|
49
|
+
return icon_map
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
private
|
|
53
|
+
def get_icon_map(map, icon_name, color)
|
|
54
|
+
icon_data = map_get(map, icon_name)
|
|
55
|
+
icon_variants = map_get(icon_data, value_to_sass('colors'))
|
|
56
|
+
if !map_has_key(icon_variants, color).value
|
|
57
|
+
return map_get(icon_variants, value_to_sass('default'))
|
|
58
|
+
else
|
|
59
|
+
return map_get(icon_variants, color)
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def svg_sprite_width
|
|
64
|
+
return SvgSprite.get_width
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def svg_sprite_height
|
|
68
|
+
return SvgSprite.get_height
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: compass-svg-sprite
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Mike Zhuk
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2015-03-17 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description: Compass plugin to make svg sprite. This plugin concats all icon from
|
|
14
|
+
folder into one sprite and provide sass function to work with it.
|
|
15
|
+
email: zhukmichael@gmail.com
|
|
16
|
+
executables: []
|
|
17
|
+
extensions: []
|
|
18
|
+
extra_rdoc_files: []
|
|
19
|
+
files:
|
|
20
|
+
- lib/compass-svg-sprite.rb
|
|
21
|
+
- lib/compass-svg-sprite/core.rb
|
|
22
|
+
- lib/compass-svg-sprite/helpers.rb
|
|
23
|
+
- lib/compass-svg-sprite/sass_extensions.rb
|
|
24
|
+
homepage:
|
|
25
|
+
licenses:
|
|
26
|
+
- MIT
|
|
27
|
+
metadata: {}
|
|
28
|
+
post_install_message:
|
|
29
|
+
rdoc_options: []
|
|
30
|
+
require_paths:
|
|
31
|
+
- lib
|
|
32
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
33
|
+
requirements:
|
|
34
|
+
- - ">="
|
|
35
|
+
- !ruby/object:Gem::Version
|
|
36
|
+
version: '0'
|
|
37
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
38
|
+
requirements:
|
|
39
|
+
- - ">="
|
|
40
|
+
- !ruby/object:Gem::Version
|
|
41
|
+
version: '0'
|
|
42
|
+
requirements: []
|
|
43
|
+
rubyforge_project:
|
|
44
|
+
rubygems_version: 2.4.5
|
|
45
|
+
signing_key:
|
|
46
|
+
specification_version: 4
|
|
47
|
+
summary: Compass plugin for vector sprites.
|
|
48
|
+
test_files: []
|