esvg 2.0.1 → 2.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3b06ff83f195a1168a3fe62eb1a64f661218c9ce
4
- data.tar.gz: d56ce3d9d1acfcfb9378850829bfbc57b68b5c94
3
+ metadata.gz: 9b0c206a64c04632e938319e8691c9de670e3fa6
4
+ data.tar.gz: b60c62f573cee170960725e72ca8a557bef8aa74
5
5
  SHA512:
6
- metadata.gz: 31ff50c185b91638aca651808230aa46bf7d3c77c48a2a3542d59ce82ccea9b4c802f306234cf225716752657b88a06c2e5274c6191fd095b0aaf02d90f466f5
7
- data.tar.gz: 389c9ed99e9152a78d2eaa759a7c9f17ea9ef28d4c2dc4df28f5bbb60fa0b21dba3c0f9edc9d1e734d516bc585f789461645e78d495a833094a5656a10d89905
6
+ metadata.gz: 66ad8fb1565b46cb83b73c45a7265f5a4719ff4c7db7a1cc0a9f7fab8a8cbb17c8edfe3260868c2d63c2ca541a2f29e7b1133e81d7fdab7341998f335138cab8
7
+ data.tar.gz: 0f3eb9c16d85a346d47125e828e2d015a22f715bef07de6464c308ce8a5f397815e8c067b3d0ce2cae2cf51e17feabbeb63e7304547899981186be3f249325b9
data/CHANGELOG.md CHANGED
@@ -1,5 +1,8 @@
1
1
  # Changelog
2
2
 
3
+ ### 2.0.2 (2015-07-10)
4
+ - Fix: Caching on just about everything.
5
+
3
6
  ### 2.0.1 (2015-07-10)
4
7
  - Fix: Caching icons again is fixed.
5
8
 
data/lib/esvg/helpers.rb CHANGED
@@ -4,6 +4,6 @@ module Esvg::Helpers
4
4
  end
5
5
 
6
6
  def svg_icon(name, options={})
7
- Esvg.svg_icon(name, options)
7
+ Esvg.svg_icon(name, options).html_safe
8
8
  end
9
9
  end
data/lib/esvg/railties.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  module Esvg
2
2
  class Railtie < ::Rails::Railtie
3
- initializer "my_gem.configure_view_controller" do |app|
3
+ initializer "esvg.configure_view_controller" do |app|
4
4
  ActiveSupport.on_load :action_view do
5
5
  include Esvg::Helpers
6
6
  end
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
- unless @files.empty?
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
- unless @files.empty?
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
- unless @files.empty?
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
- styles = []
93
-
94
- classes = files.keys.map{|k| ".#{icon_name(k)}"}.join(', ')
95
- preamble = %Q{#{classes} {
96
- clip: auto;
97
- font-size: #{config[:font_size]};
98
- background-size: auto 1em;
99
- background-repeat: no-repeat;
100
- background-position: center center;
101
- display: inline-block;
102
- overflow: hidden;
103
- height: 1em;
104
- width: 1em;
105
- color: transparent;
106
- vertical-align: middle;
107
- }}
108
- styles << preamble
109
-
110
- files.each do |name, contents|
111
- f = contents.gsub(/</, '%3C') # escape <
112
- .gsub(/>/, '%3E') # escape >
113
- .gsub(/#/, '%23') # escape #
114
- .gsub(/\n/,'') # remove newlines
115
- styles << ".#{icon_name(name)} { background-image: url('data:image/svg+xml;utf-8,#{f}'); }"
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
- names = Array(config[:names]) # In case a single string is passed
122
-
123
- if @files.empty?
124
- ''
125
- else
126
- files.each do |name, contents|
127
- @svgs[name] = contents.gsub(/<svg.+?>/, %Q{<symbol id="#{icon_name(name)}" #{dimensions(contents)}>}) # convert svg to symbols
128
- .gsub(/<\/svg/, '</symbol') # convert svg to symbols
129
- .gsub(/style=['"].+?['"]/, '') # remove inline styles
130
- .gsub(/\n/, '') # remove endlines
131
- .gsub(/\s{2,}/, ' ') # remove whitespace
132
- .gsub(/>\s+</, '><') # remove whitespace between tags
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
- %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>}
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
- %Q{var esvg = {
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
- name = icon_name(file)
167
- %Q{<svg class="#{config[:base_class]} #{name} #{options[:class] || ""}" #{dimensions(@files[file])}><use xlink:href="##{name}"/>#{title(options)}#{desc(options)}</svg>}.html_safe
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
@@ -1,3 +1,3 @@
1
1
  module Esvg
2
- VERSION = "2.0.1"
2
+ VERSION = "2.0.2"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: esvg
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.1
4
+ version: 2.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brandon Mathis