esvg 2.0.1 → 2.0.2
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 +4 -4
- data/CHANGELOG.md +3 -0
- data/lib/esvg/helpers.rb +1 -1
- data/lib/esvg/railties.rb +1 -1
- data/lib/esvg/svg.rb +58 -56
- data/lib/esvg/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9b0c206a64c04632e938319e8691c9de670e3fa6
|
4
|
+
data.tar.gz: b60c62f573cee170960725e72ca8a557bef8aa74
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 66ad8fb1565b46cb83b73c45a7265f5a4719ff4c7db7a1cc0a9f7fab8a8cbb17c8edfe3260868c2d63c2ca541a2f29e7b1133e81d7fdab7341998f335138cab8
|
7
|
+
data.tar.gz: 0f3eb9c16d85a346d47125e828e2d015a22f715bef07de6464c308ce8a5f397815e8c067b3d0ce2cae2cf51e17feabbeb63e7304547899981186be3f249325b9
|
data/CHANGELOG.md
CHANGED
data/lib/esvg/helpers.rb
CHANGED
data/lib/esvg/railties.rb
CHANGED
data/lib/esvg/svg.rb
CHANGED
@@ -9,7 +9,6 @@ module Esvg
|
|
9
9
|
namespace_before: true,
|
10
10
|
font_size: '1em',
|
11
11
|
output_path: Dir.pwd,
|
12
|
-
names: [],
|
13
12
|
format: 'js'
|
14
13
|
}
|
15
14
|
|
@@ -20,9 +19,11 @@ module Esvg
|
|
20
19
|
def initialize(options={})
|
21
20
|
config(options)
|
22
21
|
read_icons
|
22
|
+
@cache = {}
|
23
23
|
end
|
24
24
|
|
25
25
|
def embed
|
26
|
+
return if @files.empty?
|
26
27
|
case config[:format]
|
27
28
|
when "html"
|
28
29
|
html
|
@@ -38,6 +39,10 @@ module Esvg
|
|
38
39
|
@mtime != mtime
|
39
40
|
end
|
40
41
|
|
42
|
+
def cache_name(input, options)
|
43
|
+
input + options.flatten.join('-')
|
44
|
+
end
|
45
|
+
|
41
46
|
def read_icons
|
42
47
|
@files = {}
|
43
48
|
@mtime = {}
|
@@ -53,6 +58,7 @@ module Esvg
|
|
53
58
|
end
|
54
59
|
|
55
60
|
def write
|
61
|
+
return if @files.empty?
|
56
62
|
case config[:format]
|
57
63
|
when "html"
|
58
64
|
write_html
|
@@ -71,79 +77,72 @@ module Esvg
|
|
71
77
|
end
|
72
78
|
|
73
79
|
def write_js
|
74
|
-
|
75
|
-
write_file config[:js_path], js
|
76
|
-
end
|
80
|
+
write_file config[:js_path], js
|
77
81
|
end
|
78
82
|
|
79
83
|
def write_css
|
80
|
-
|
81
|
-
write_file config[:css_path], css
|
82
|
-
end
|
84
|
+
write_file config[:css_path], css
|
83
85
|
end
|
84
86
|
|
85
87
|
def write_html
|
86
|
-
|
87
|
-
write_file config[:html_path], html
|
88
|
-
end
|
88
|
+
write_file config[:html_path], html
|
89
89
|
end
|
90
90
|
|
91
91
|
def css
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
92
|
+
@cache['css'] ||= begin
|
93
|
+
styles = []
|
94
|
+
|
95
|
+
classes = files.keys.map{|k| ".#{icon_name(k)}"}.join(', ')
|
96
|
+
preamble = %Q{#{classes} {
|
97
|
+
clip: auto;
|
98
|
+
font-size: #{config[:font_size]};
|
99
|
+
background-size: auto 1em;
|
100
|
+
background-repeat: no-repeat;
|
101
|
+
background-position: center center;
|
102
|
+
display: inline-block;
|
103
|
+
overflow: hidden;
|
104
|
+
height: 1em;
|
105
|
+
width: 1em;
|
106
|
+
color: transparent;
|
107
|
+
vertical-align: middle;
|
108
|
+
}}
|
109
|
+
styles << preamble
|
110
|
+
|
111
|
+
files.each do |name, contents|
|
112
|
+
f = contents.gsub(/</, '%3C') # escape <
|
113
|
+
.gsub(/>/, '%3E') # escape >
|
114
|
+
.gsub(/#/, '%23') # escape #
|
115
|
+
.gsub(/\n/,'') # remove newlines
|
116
|
+
styles << ".#{icon_name(name)} { background-image: url('data:image/svg+xml;utf-8,#{f}'); }"
|
117
|
+
end
|
118
|
+
styles.join("\n")
|
116
119
|
end
|
117
|
-
styles.join("\n")
|
118
120
|
end
|
119
121
|
|
120
122
|
def html
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
end
|
123
|
+
@cache['html'] ||= begin
|
124
|
+
if @files.empty?
|
125
|
+
''
|
126
|
+
else
|
127
|
+
files.each do |name, contents|
|
128
|
+
@svgs[name] = contents.gsub(/<svg.+?>/, %Q{<symbol id="#{icon_name(name)}" #{dimensions(contents)}>}) # convert svg to symbols
|
129
|
+
.gsub(/<\/svg/, '</symbol') # convert svg to symbols
|
130
|
+
.gsub(/style=['"].+?['"]/, '') # remove inline styles
|
131
|
+
.gsub(/\n/, '') # remove endlines
|
132
|
+
.gsub(/\s{2,}/, ' ') # remove whitespace
|
133
|
+
.gsub(/>\s+</, '><') # remove whitespace between tags
|
134
|
+
end
|
134
135
|
|
135
|
-
if names.empty?
|
136
136
|
icons = @svgs
|
137
|
-
else
|
138
|
-
icons = @svgs.select { |k,v| names.include?(k) }
|
139
|
-
end
|
140
137
|
|
141
|
-
|
138
|
+
%Q{<svg id="esvg-symbols" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" style="display:none">#{icons.values.join("\n")}</svg>}
|
139
|
+
end
|
142
140
|
end
|
143
141
|
end
|
144
142
|
|
145
143
|
def js
|
146
|
-
|
144
|
+
@cache['js'] ||= begin
|
145
|
+
%Q{var esvg = {
|
147
146
|
embed: function(){
|
148
147
|
if (!document.querySelector('#esvg-symbols')) {
|
149
148
|
document.querySelector('body').insertAdjacentHTML('afterbegin', '#{html.gsub(/\n/,'').gsub("'"){"\\'"}}')
|
@@ -160,11 +159,14 @@ document.addEventListener("page:change", function(event) { esvg.embed() })
|
|
160
159
|
// Handle standard DOM ready events
|
161
160
|
document.addEventListener("DOMContentLoaded", function(event) { esvg.embed() })
|
162
161
|
}
|
162
|
+
end
|
163
163
|
end
|
164
164
|
|
165
165
|
def svg_icon(file, options={})
|
166
|
-
|
167
|
-
|
166
|
+
@cache[cache_name(file, options)] ||= begin
|
167
|
+
name = icon_name(file)
|
168
|
+
%Q{<svg class="#{config[:base_class]} #{name} #{options[:class] || ""}" #{dimensions(@files[file])}><use xlink:href="##{name}"/>#{title(options)}#{desc(options)}</svg>}
|
169
|
+
end
|
168
170
|
end
|
169
171
|
|
170
172
|
def title(options)
|
data/lib/esvg/version.rb
CHANGED