phosphor_icons 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "action_view"
4
+
5
+ module PhosphorIcons
6
+ module Helper
7
+ include ActionView::Helpers::TagHelper
8
+
9
+ mattr_accessor :phosphor_icons_helper_cache, default: {}
10
+
11
+ # rubocop:disable Style/IdenticalConditionalBranches
12
+ def phosphor_icon(symbol, options = {})
13
+ return "" if symbol.nil?
14
+
15
+ cache_key = [symbol, options]
16
+
17
+ if (tag = phosphor_icons_helper_cache[cache_key])
18
+ tag
19
+ else
20
+ icon = PhosphorIcons::Icon.new(symbol, options)
21
+
22
+ tag = content_tag(:svg, icon.path.html_safe, icon.options).freeze
23
+ phosphor_icons_helper_cache[cache_key] = tag
24
+ tag
25
+ end
26
+ end
27
+ # rubocop:enable Style/IdenticalConditionalBranches
28
+ end
29
+ end
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PhosphorIcons
4
+ class Icon
5
+ DEFAULT_STYLE = "regular"
6
+ SUPPORTED_STYLES = [DEFAULT_STYLE, "bold", "light", "duotone", "fill", "thin"]
7
+
8
+ attr_reader :symbol, :style, :options, :path
9
+
10
+ def initialize(symbol, options = {})
11
+ @symbol = symbol.to_s
12
+ @style = options[:style] || DEFAULT_STYLE
13
+ if (phosphor_icon = get_phosphor_icon(@symbol, options))
14
+ @path = phosphor_icon["path"]
15
+ @options = {
16
+ class: "phosphor-icon #{options[:class]}".strip,
17
+ viewBox: viewbox,
18
+ xmlns: "http://www.w3.org/2000/svg",
19
+ fill: "currentColor",
20
+ }
21
+ else
22
+ raise IconNotFoundError, "Couldn't find #{self}"
23
+ end
24
+ end
25
+
26
+ def to_svg
27
+ "<svg #{html_attributes}>#{path}</svg>"
28
+ end
29
+
30
+ def to_s
31
+ "Phosphor Icon for #{symbol.inspect} with style #{style.inspect}"
32
+ end
33
+
34
+ private
35
+
36
+ def html_attributes
37
+ attrs = ""
38
+ options.each { |attr, value| attrs += "#{attr}=\"#{value}\" " }
39
+ attrs.strip
40
+ end
41
+
42
+ def viewbox
43
+ "0 0 256 256"
44
+ end
45
+
46
+ def get_phosphor_icon(symbol, options = {})
47
+ if (phosphor_icon = PhosphorIcons::ICON_SYMBOLS[symbol])
48
+ style = get_style(options[:style])
49
+
50
+ {
51
+ "name" => phosphor_icon["name"],
52
+ "path" => phosphor_icon["styles"][style.to_s]["path"],
53
+ }
54
+ end
55
+ rescue
56
+ nil
57
+ end
58
+
59
+ def get_style(style)
60
+ SUPPORTED_STYLES.include?(style.to_s) ? style : DEFAULT_STYLE
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rails"
4
+
5
+ module PhosphorIcons
6
+ class Railtie < Rails::Railtie
7
+ initializer "phosphor_icons_helper.helper" do
8
+ ActionView::Base.send(:include, PhosphorIcons::Helper)
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PhosphorIcons
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "phosphor_icons/version"
4
+ require "phosphor_icons/icon"
5
+ require "phosphor_icons/helper"
6
+ require "phosphor_icons/railtie" if defined?(Rails)
7
+ require "json"
8
+
9
+ module PhosphorIcons
10
+ file_data = File.read(File.join(File.dirname(__FILE__), "./build/data.json"))
11
+ ICON_SYMBOLS = JSON.parse(file_data).freeze
12
+
13
+ class IconNotFoundError < StandardError; end
14
+ end
@@ -0,0 +1,60 @@
1
+ # frozen_string_literal: true
2
+
3
+ desc "Transform icons to JSON object"
4
+ namespace :transform do
5
+ task :icons do
6
+ require "ox"
7
+ require "json"
8
+
9
+ REGEX_FILENAME = /^(.+?)(?:-(bold|light|duotone|fill|thin))?\.svg$/
10
+ INPUT_ARG = "core/assets/**/*.svg"
11
+ OUTPUT_FILE = "lib/build/data.json"
12
+
13
+ def build_path(obj)
14
+ doc = Ox::Document.new
15
+ obj.each do |k, v|
16
+ top = Ox::Element.new(k)
17
+ v.each do |pk|
18
+ pk.each { |jk, jv| top[jk] = jv }
19
+ end
20
+ doc << top
21
+ end
22
+ Ox.dump(doc).strip
23
+ end
24
+
25
+ def recursive_merge(origin, other)
26
+ origin.merge(other) { |_k, v1, v2| v1.is_a?(Hash) && v2.is_a?(Hash) ? recursive_merge(v1, v2) : v2 }
27
+ end
28
+
29
+ icons = Dir[INPUT_ARG].map do |filepath|
30
+ filename = File.basename(filepath)
31
+ match_filename = REGEX_FILENAME.match(filename)
32
+ svg_element = Ox.load(File.read(filepath), mode: :hash)[:svg]
33
+ svg_path = svg_element[1..].map { |s| build_path(s) }.join("")
34
+
35
+ {
36
+ name: match_filename[1],
37
+ style: match_filename[2] || "regular",
38
+ path: svg_path,
39
+ }
40
+ end
41
+
42
+ icons_by_name = icons.reduce({}) do |acc, icon|
43
+ recursive_merge(acc, {
44
+ icon[:name] => {
45
+ name: icon[:name],
46
+ styles: {
47
+ icon[:style] => { path: icon[:path] },
48
+ },
49
+ },
50
+ })
51
+ end
52
+
53
+ folder_path = File.dirname(OUTPUT_FILE)
54
+ FileUtils.mkdir_p(folder_path)
55
+
56
+ File.open(OUTPUT_FILE, "w") do |f|
57
+ f.write(icons_by_name.to_json)
58
+ end
59
+ end
60
+ end
metadata ADDED
@@ -0,0 +1,95 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: phosphor_icons
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Maful Prayoga
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-06-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: ox
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.14'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.14'
27
+ - !ruby/object:Gem::Dependency
28
+ name: railties
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: actionview
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: A package that distributes Phosphor Icons in a gem
56
+ email:
57
+ - me@maful.web.id
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - LICENSE
63
+ - README.md
64
+ - lib/build/data.json
65
+ - lib/phosphor_icons.rb
66
+ - lib/phosphor_icons/helper.rb
67
+ - lib/phosphor_icons/icon.rb
68
+ - lib/phosphor_icons/railtie.rb
69
+ - lib/phosphor_icons/version.rb
70
+ - lib/tasks/transform.rake
71
+ homepage: https://github.com/maful/ruby-phosphor-icons
72
+ licenses:
73
+ - MIT
74
+ metadata:
75
+ allowed_push_host: https://rubygems.org
76
+ post_install_message:
77
+ rdoc_options: []
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: 2.7.0
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ requirements: []
91
+ rubygems_version: 3.4.10
92
+ signing_key:
93
+ specification_version: 4
94
+ summary: Phosphor Icons for Ruby and Rails
95
+ test_files: []