svg_icon 0.2.0 → 0.3.0

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.
@@ -2,7 +2,7 @@
2
2
 
3
3
  module SvgIcon
4
4
  class Configuration
5
- DEFAULT_ICON = "bi"
5
+ DEFAULT_ICON = "lucide"
6
6
 
7
7
  attr_accessor :icon
8
8
  attr_accessor :default_class
@@ -23,5 +23,6 @@ module SvgIcon
23
23
 
24
24
  def self.configure
25
25
  yield configuration
26
+ SvgIcon.clear_cache!
26
27
  end
27
28
  end
@@ -1,31 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "cgi"
4
+
1
5
  module SvgIcon
2
6
  module Helper
3
7
  def svg_icon(name, options = {})
4
8
  options = options.dup
5
-
6
9
  icons = SvgIcon.icons_json
10
+
7
11
  height = icons["height"] || 24
8
12
  width = icons["width"] || 24
9
13
 
10
- options[:viewBox] = "0 0 #{width} #{height}"
11
- options[:version] = "1.1"
14
+ options[:viewBox] ||= "0 0 #{width} #{height}"
12
15
 
13
16
  default_class = SvgIcon.configuration.default_class
14
-
15
17
  options[:class] = "#{default_class} #{options[:class]}".strip if default_class.present?
16
18
 
17
- begin
18
- path = icons["icons"][name]["body"]
19
- "<svg xmlns='http://www.w3.org/2000/svg' #{icon_html_attributes(options)}>#{path}</svg>".html_safe
20
- rescue
21
- "<svg xmlns='http://www.w3.org/2000/svg' #{icon_html_attributes(options)}><!-- SVG icon file not found: '#{name}' --></svg>".html_safe
19
+ body = icons.dig("icons", name.to_s, "body")
20
+
21
+ content = if body
22
+ body
23
+ else
24
+ "<!-- SVG icon file not found: '#{safe_comment_text(name.to_s)}' -->"
22
25
  end
26
+
27
+ "<svg xmlns='http://www.w3.org/2000/svg' #{icon_html_attributes(options)}>#{content}</svg>".html_safe
23
28
  end
24
29
 
30
+ private
31
+
25
32
  def icon_html_attributes(options)
26
- attrs = ""
27
- options.each { |attr, value| attrs += "#{attr}=\"#{value}\" " }
28
- attrs.strip
33
+ options.map do |attr, value|
34
+ %(#{CGI.escapeHTML(attr.to_s)}="#{CGI.escapeHTML(value.to_s)}")
35
+ end.join(" ")
36
+ end
37
+
38
+ def safe_comment_text(value)
39
+ CGI.escapeHTML(value).gsub("-", "&#45;")
29
40
  end
30
41
  end
31
42
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SvgIcon
4
- VERSION = "0.2.0"
4
+ VERSION = "0.3.0"
5
5
  end
data/lib/svg_icon.rb CHANGED
@@ -5,50 +5,94 @@ require_relative "svg_icon/helper"
5
5
  require_relative "svg_icon/version"
6
6
 
7
7
  require "multi_json"
8
+ require "active_support/core_ext/object/blank"
9
+ require "active_support/core_ext/string/output_safety"
8
10
 
9
11
  module SvgIcon
10
12
  class Error < StandardError; end
11
13
 
12
14
  extend self
13
15
 
14
- def extra_file_data
15
- extra_data_path = SvgIcon.configuration.extra_icons_path
16
- return nil if extra_data_path.nil?
17
- @extra_file_data ||= File.read(extra_data_path)
16
+ def icons
17
+ @icons ||= {}
18
+ @icons[icon] ||= MultiJson.load(file_data)
18
19
  end
19
20
 
20
- def extra_icons
21
- return {} if extra_file_data.nil?
22
- @extra_icons ||= MultiJson.load(extra_file_data)
21
+ def icons_json
22
+ @icons_json ||= {}
23
+ @icons_json[cache_key] ||= merge_extra_icons(icons)
23
24
  end
24
25
 
25
- # def file_data
26
- # icons_data_path = SvgIcon.configuration.icons_data_path
26
+ def clear_cache!
27
+ @file_data = nil
28
+ @icons = nil
29
+ @icons_json = nil
30
+ @extra_file_data = nil
31
+ @extra_icons = nil
32
+ end
33
+
34
+ private
27
35
 
28
- # icons_path = if icons_data_path.present?
29
- # File.join(icons_data_path)
30
- # else
31
- # File.join(__dir__, "data/#{SvgIcon.configuration.icon}.json")
32
- # end
33
- # @file_data ||= File.read(icons_path)
34
- # end
36
+ def icon
37
+ configuration.icon
38
+ end
39
+
40
+ def configuration
41
+ SvgIcon.configuration
42
+ end
43
+
44
+ def data_path
45
+ File.join(__dir__, "data", "#{icon}.json")
46
+ end
35
47
 
36
48
  def file_data
37
- icons_path = File.join(__dir__, "data/#{SvgIcon.configuration.icon}.json")
38
- @file_data ||= File.read(icons_path)
49
+ @file_data ||= {}
50
+ @file_data[data_path] ||= begin
51
+ path = data_path
52
+ raise Error, "Icon data file not found: #{path}" unless File.exist?(path)
53
+
54
+ File.read(path)
55
+ end
39
56
  end
40
57
 
41
- def icons
42
- @icons ||= MultiJson.load(file_data)
58
+ def extra_icons
59
+ return {} unless configuration.extra_icons_path
60
+
61
+ @extra_icons ||= {}
62
+ @extra_icons[extra_path] ||= begin
63
+ data = MultiJson.load(extra_file_data)
64
+ raise Error, "Extra icons file must contain a JSON object: #{extra_path}" unless data.is_a?(Hash)
65
+
66
+ data
67
+ end
43
68
  end
44
69
 
45
- def icons_json
46
- @icons_json ||= if extra_icons
47
- puts "icons_json"
48
- icons["icons"] = icons["icons"].merge(extra_icons)
49
- icons
50
- else
51
- icons
70
+ def extra_path
71
+ configuration.extra_icons_path
72
+ end
73
+
74
+ def extra_file_data
75
+ @extra_file_data ||= {}
76
+ @extra_file_data[extra_path] ||= begin
77
+ path = extra_path
78
+ raise Error, "Extra icons file not found: #{path}" unless File.exist?(path)
79
+
80
+ File.read(path)
52
81
  end
53
82
  end
83
+
84
+ def merge_extra_icons(base_icons)
85
+ icons_set = base_icons["icons"]
86
+ raise Error, "Icon data must contain an 'icons' object: #{data_path}" unless icons_set.is_a?(Hash)
87
+
88
+ return base_icons if extra_icons.empty?
89
+
90
+ merged = base_icons.dup
91
+ merged["icons"] = icons_set.merge(extra_icons)
92
+ merged
93
+ end
94
+
95
+ def cache_key
96
+ "#{icon}:#{extra_path}"
97
+ end
54
98
  end
data/svg_icon.gemspec CHANGED
@@ -26,6 +26,8 @@ Gem::Specification.new do |spec|
26
26
  spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
27
27
  spec.require_paths = ["lib"]
28
28
 
29
- # Uncomment to register a new dependency of your gem
30
29
  spec.add_dependency "multi_json"
30
+ spec.add_dependency "activesupport", ">= 5.0"
31
+
32
+ spec.add_development_dependency "minitest"
31
33
  end
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: svg_icon
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - doabit
8
- autorequire:
9
8
  bindir: exe
10
9
  cert_chain: []
11
- date: 2022-03-31 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: multi_json
@@ -24,6 +23,34 @@ dependencies:
24
23
  - - ">="
25
24
  - !ruby/object:Gem::Version
26
25
  version: '0'
26
+ - !ruby/object:Gem::Dependency
27
+ name: activesupport
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '5.0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '5.0'
40
+ - !ruby/object:Gem::Dependency
41
+ name: minitest
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ type: :development
48
+ prerelease: false
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
27
54
  description: Svg icon render helper for rails.
28
55
  email:
29
56
  - doinsist@gmail.com
@@ -41,6 +68,7 @@ files:
41
68
  - lib/data/bi.json
42
69
  - lib/data/bx.json
43
70
  - lib/data/heroicons.json
71
+ - lib/data/lucide.json
44
72
  - lib/svg_icon.rb
45
73
  - lib/svg_icon/configuration.rb
46
74
  - lib/svg_icon/helper.rb
@@ -52,7 +80,6 @@ metadata:
52
80
  homepage_uri: https://github.com/ruby-gems/svg_icon
53
81
  source_code_uri: https://github.com/ruby-gems/svg_icon
54
82
  changelog_uri: https://github.com/ruby-gems/svg_icon
55
- post_install_message:
56
83
  rdoc_options: []
57
84
  require_paths:
58
85
  - lib
@@ -67,8 +94,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
67
94
  - !ruby/object:Gem::Version
68
95
  version: '0'
69
96
  requirements: []
70
- rubygems_version: 3.2.3
71
- signing_key:
97
+ rubygems_version: 3.6.9
72
98
  specification_version: 4
73
99
  summary: Svg icon render helper for rails
74
100
  test_files: []