lib/esvg/symbol.rb in esvg-4.2.0 vs lib/esvg/symbol.rb in esvg-4.2.1

- old
+ new

@@ -20,29 +20,47 @@ if @mtime != time @mtime = time @content = pre_optimize File.read(@path) @size = dimensions @optimized = nil + @optimized_at = nil end + + # Ensure that cache optimization matches current optimization settings + # If config has changed name, reset optimized build (name gets baked in) + if @svgo_optimized != svgo? || name != file_name + @optimized = nil + @optimized_at = nil + end + @group = dir_key @name = file_name @id = file_id file_key self end + def width + @size[:width] + end + + def height + @size[:height] + end + def data { path: @path, id: @id, name: @name, group: @group, - last_modified: @mtime, + mtime: @mtime, size: @size, content: @content, optimized: @optimized, - optimized_at: @optimized_at + optimized_at: @optimized_at, + svgo_optimized: svgo? && @svgo_optimized } end def attr { id: @id, 'data-name': @name }.merge @size @@ -56,11 +74,12 @@ use_attr = { class: [@config[:class], @config[:prefix]+"-"+@name, options[:class]].compact.join(' '), viewBox: @size[:viewBox], style: options[:style], - fill: options[:fill] + fill: options[:fill], + role: 'img' } # If user doesn't pass a size or set scale: true if !(options[:width] || options[:height] || options[:scale]) @@ -77,10 +96,12 @@ %Q{<svg #{attributes(use_attr)}>#{use_tag}#{title(options)}#{desc(options)}#{options[:content]||''}</svg>} end def use_tag(options={}) options["xlink:href"] = "##{@id}" + options[:width] ||= width + options[:height] ||= height %Q{<use #{attributes(options)}/>} end def title(options) if options[:title] @@ -96,20 +117,27 @@ else '' end end + def svgo? + @config[:optimize] && !!Esvg.node_module('svgo') + end + def optimize # Only optimize again if the file has changed - return @optimized if @optimized_at && @optimized_at > @mtime + return @optimized if @optimized && @optimized_at > @mtime @optimized = @content sub_def_ids - if @config[:optimize] && Esvg.node_module('svgo') + if svgo? response = Open3.capture3(%Q{#{Esvg.node_module('svgo')} --disable=removeUselessDefs -s '#{@optimized}' -o -}) - @optimized = response[0] if response[2].success? + if !response[0].empty? && response[2].success? + @optimized = response[0] + @svgo_optimized = true + end end post_optimize @optimized_at = Time.now.to_i @@ -119,11 +147,11 @@ private def load_data if @config[:cache] @config.delete(:cache).each do |name, value| - set_instance name.to_s, value + instance_variable_set("@#{name}", value) end end end def last_modified @@ -212,11 +240,11 @@ def set_attributes attr.keys.each do |key| @optimized.sub!(/ #{key}=".+?"/,'') end - @optimized.sub!(/<svg/, "<symbol #{attributes(attr)}") + @optimized.sub(/<svg/, "<symbol #{attributes(attr)}") end # Scans <def> blocks for IDs # If urls(#id) are used, ensure these IDs are unique to this file # Only replace IDs if urls exist to avoid replacing defs @@ -230,18 +258,18 @@ # ids be sure to update both # if @optimized.match(/url\(##{id}\)/) new_id = "def-#{@id}-#{index}" - @optimized.gsub! /id="#{id}"/, %Q{class="#{new_id}"} - @optimized.gsub! /url\(##{id}\)/, "url(##{new_id})" + @optimized = @optimized.gsub(/id="#{id}"/, %Q{class="#{new_id}"}) + .gsub(/url\(##{id}\)/, "url(##{new_id})") # Otherwise just leave the IDs of the # defs and change them to classes to # avoid SVGO ID mangling # else - @optimized.gsub! /id="#{id}"/, %Q{class="#{id}"} + @optimized = @optimized.gsub /id="#{id}"/, %Q{class="#{id}"} end end end end end