fa_rails 0.1.1 → 0.1.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/Gemfile.lock +1 -1
- data/fa_rails.gemspec +1 -1
- data/lib/fa.rb +94 -97
- data/lib/fa/icon.rb +16 -13
- data/lib/fa/layer.rb +16 -13
- data/lib/fa/span.rb +22 -14
- data/spec/helpers/fa_spec.rb +12 -10
- 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: 0fd53c5e9810de9d7364425d8928e032741a5df3
|
4
|
+
data.tar.gz: adb052186b5ab4bedd77e27f43cc5e6d973a8915
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e11ee40165dd680bc382c66cc2e9d2e62ba81c8e622a336b1a016c4f956712ed873791f6f1e190212058c9c5068ae7fc01bae148a856659ff3e2c375c47f0649
|
7
|
+
data.tar.gz: 02531dc100ff594a87528bf015dccee154d53bf5ba56b0418d2219b8bb6ab487eaa126331082b39a5bf892a189be741f5743f9da17047740934642c154d1cf58
|
data/Gemfile.lock
CHANGED
data/fa_rails.gemspec
CHANGED
data/lib/fa.rb
CHANGED
@@ -1,130 +1,127 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
# FontAwesome 5 (Pro) Helper
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
4
|
+
module FA
|
5
|
+
class Base
|
6
|
+
require 'fa/icon'
|
7
|
+
require 'fa/span'
|
8
|
+
require 'fa/layer'
|
9
|
+
|
10
|
+
def raw
|
11
|
+
#
|
12
|
+
end
|
12
13
|
|
13
|
-
|
14
|
-
|
15
|
-
|
14
|
+
def safe
|
15
|
+
safe_output(raw)
|
16
|
+
end
|
16
17
|
|
17
|
-
|
18
|
+
private
|
18
19
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
20
|
+
def safe_output(output)
|
21
|
+
# html_safe: No user content
|
22
|
+
output.respond_to?(:html_safe) ? output.html_safe : output
|
23
|
+
end
|
23
24
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
25
|
+
def parse_all(icons)
|
26
|
+
icons.map do |icon|
|
27
|
+
name = icon[:name]
|
28
|
+
options = icon[:options] || {}
|
28
29
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
30
|
+
if %i[counter text].include?(name)
|
31
|
+
parse_span(name, icon[:text], options)
|
32
|
+
else
|
33
|
+
parse_icon(name, options)
|
34
|
+
end
|
33
35
|
end
|
34
36
|
end
|
35
|
-
end
|
36
37
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
38
|
+
def parse_icon(name, options = {})
|
39
|
+
options = fa_options(options)
|
40
|
+
parse_options(options)
|
41
|
+
title = options[:title]
|
41
42
|
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
43
|
+
@classes << "fa-#{name}"
|
44
|
+
@classes << "fa-#{size_x(options[:size])}" if !!options[:size]
|
45
|
+
css = @classes.flatten.join(' ')
|
46
|
+
transforms = @transforms.join(' ')
|
46
47
|
|
47
|
-
|
48
|
-
|
48
|
+
"<i class='#{css}' data-fa-transform='#{transforms}' title='#{title}'></i>"
|
49
|
+
end
|
49
50
|
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
51
|
+
def parse_span(type, text, options = {})
|
52
|
+
options.delete(:style)
|
53
|
+
options = fa_options(options)
|
54
|
+
parse_options(options)
|
55
|
+
pos = options.delete(:position)
|
56
|
+
position = long_position(pos) if !!pos
|
56
57
|
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
58
|
+
@classes << "fa-layers-#{type}"
|
59
|
+
@classes << (!!position ? "fa-layers-#{position}" : '')
|
60
|
+
css = @classes.flatten.reject { |c| c.to_s.match?(/^fa.$/) }.join(' ')
|
61
|
+
transforms = @transforms.join(' ')
|
61
62
|
|
62
|
-
|
63
|
-
|
63
|
+
"<span class='#{css}' data-fa-transform='#{transforms}'>#{text}</span>"
|
64
|
+
end
|
64
65
|
|
65
|
-
|
66
|
-
|
66
|
+
def fa_options(options)
|
67
|
+
default = { style: :solid, css: '', fa: '' }
|
67
68
|
|
68
|
-
|
69
|
-
|
69
|
+
default.merge(options.to_h)
|
70
|
+
end
|
70
71
|
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
72
|
+
def combine_grows(i, grow)
|
73
|
+
if i.key?(:options) && i[:options].key?(:grow)
|
74
|
+
i[:options][:grow] + grow
|
75
|
+
else
|
76
|
+
grow
|
77
|
+
end
|
76
78
|
end
|
77
|
-
end
|
78
79
|
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
80
|
+
def combine_options(i, combined_grow)
|
81
|
+
if i.key?(:options)
|
82
|
+
i[:options].merge(grow: combined_grow)
|
83
|
+
else
|
84
|
+
{ grow: combined_grow }
|
85
|
+
end
|
84
86
|
end
|
85
|
-
end
|
86
87
|
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
88
|
+
def size_x(size)
|
89
|
+
return '' unless !!size || size == 1
|
90
|
+
"#{size}x"
|
91
|
+
end
|
91
92
|
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
93
|
+
def long_position(position)
|
94
|
+
return 'top-right' if position == :tr
|
95
|
+
return 'top-left' if position == :tl
|
96
|
+
return 'bottom-right' if position == :br
|
97
|
+
return 'bottom-left' if position == :bl
|
98
|
+
end
|
98
99
|
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
100
|
+
def parse_options(options)
|
101
|
+
parse_classes(options)
|
102
|
+
parse_transforms(options)
|
103
|
+
end
|
103
104
|
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
105
|
+
def parse_classes(options)
|
106
|
+
@classes = []
|
107
|
+
@classes << parse_style(options[:style])
|
108
|
+
@classes << options[:fa].to_s.split(' ').map { |c| "fa-#{c}" }
|
109
|
+
@classes << options[:css].to_s.split(' ')
|
110
|
+
end
|
110
111
|
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
112
|
+
def parse_transforms(options)
|
113
|
+
@transforms = []
|
114
|
+
%i[grow shrink rotate up down left right].each do |transform|
|
115
|
+
if !!options[transform]
|
116
|
+
@transforms << "#{transform}-#{options[transform]}"
|
117
|
+
end
|
116
118
|
end
|
117
119
|
end
|
118
|
-
end
|
119
120
|
|
120
|
-
|
121
|
-
|
121
|
+
def parse_style(style)
|
122
|
+
return 'fas' unless %i[solid regular light brands].include?(style)
|
122
123
|
|
123
|
-
|
124
|
-
|
125
|
-
regular: 'r',
|
126
|
-
light: 'l',
|
127
|
-
brands: 'b'
|
128
|
-
}[style]
|
124
|
+
'fa' + { solid: 's', regular: 'r', light: 'l', brands: 'b' }[style]
|
125
|
+
end
|
129
126
|
end
|
130
127
|
end
|
data/lib/fa/icon.rb
CHANGED
@@ -1,19 +1,22 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
@
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
3
|
+
module FA
|
4
|
+
# FontAwesome 5 (Pro) Helper for generating icons
|
5
|
+
class Icon < FA::Base
|
6
|
+
def initialize(fa, options = {})
|
7
|
+
@options = options
|
8
|
+
if fa.is_a?(Hash)
|
9
|
+
@name = fa[:name]
|
10
|
+
@options = fa[:options]
|
11
|
+
elsif fa.is_a?(String) || fa.is_a?(Symbol)
|
12
|
+
@name = fa.to_s
|
13
|
+
else
|
14
|
+
raise ArgumentError, 'Unexpected argument type.'
|
15
|
+
end
|
13
16
|
end
|
14
|
-
end
|
15
17
|
|
16
|
-
|
17
|
-
|
18
|
+
def raw
|
19
|
+
parse_icon(@name, @options)
|
20
|
+
end
|
18
21
|
end
|
19
22
|
end
|
data/lib/fa/layer.rb
CHANGED
@@ -1,20 +1,23 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
3
|
+
module FA
|
4
|
+
# FontAwesome 5 (Pro) Helper for generating layered icons and spans
|
5
|
+
class Layer < FA::Base
|
6
|
+
def initialize(icons = {}, title: nil, grow: 0, css: '')
|
7
|
+
# Add icons to the stack bottom to top
|
8
|
+
#
|
9
|
+
# Note: scaling counters does not work well with :grow, so should use the
|
10
|
+
# older "fa-3x" syntax in :css instead.
|
11
|
+
span_top = "<span class='icon fa-layers fa-fw #{css}' title='#{title}'>"
|
12
|
+
span_bottom = '</span>'
|
11
13
|
|
12
|
-
|
14
|
+
icons.each { |i| i[:options] = combine_options(i, combine_grows(i, grow)) }
|
13
15
|
|
14
|
-
|
15
|
-
|
16
|
+
@output = span_top + parse_all(icons).join + span_bottom
|
17
|
+
end
|
16
18
|
|
17
|
-
|
18
|
-
|
19
|
+
def raw
|
20
|
+
@output
|
21
|
+
end
|
19
22
|
end
|
20
23
|
end
|
data/lib/fa/span.rb
CHANGED
@@ -1,20 +1,28 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
3
|
+
module FA
|
4
|
+
# FontAwesome 5 (Pro) Helper for generating spans (counters and text)
|
5
|
+
class Span < FA::Base
|
6
|
+
def initialize(fa, text = '', options = {})
|
7
|
+
if fa.is_a?(Hash)
|
8
|
+
set_options(fa[:type].to_sym, fa[:text], fa[:options])
|
9
|
+
elsif fa.is_a?(String) || fa.is_a?(Symbol)
|
10
|
+
set_options(fa.to_s, text, options)
|
11
|
+
else
|
12
|
+
raise ArgumentError, 'Unexpected argument type.'
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def raw
|
17
|
+
parse_span(@type, @text, @options)
|
14
18
|
end
|
15
|
-
end
|
16
19
|
|
17
|
-
|
18
|
-
|
20
|
+
private
|
21
|
+
|
22
|
+
def set_options(type, text, options)
|
23
|
+
@type = type
|
24
|
+
@text = text
|
25
|
+
@options = options
|
26
|
+
end
|
19
27
|
end
|
20
28
|
end
|
data/spec/helpers/fa_spec.rb
CHANGED
@@ -1,34 +1,36 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require 'spec_helper'
|
4
|
+
|
3
5
|
RSpec.describe FA do
|
4
6
|
describe 'icon' do
|
5
7
|
it 'should generate the correct icon from a string or symbol name' do
|
6
|
-
expect(FA::Icon.new('help')).to eql(
|
8
|
+
expect(FA::Icon.new('help').safe).to eql(
|
7
9
|
"<i class='fas fa-help' data-fa-transform='' title=''></i>"
|
8
10
|
)
|
9
11
|
|
10
|
-
expect(FA::Icon.new(:help)).to eql(
|
12
|
+
expect(FA::Icon.new(:help).safe).to eql(
|
11
13
|
"<i class='fas fa-help' data-fa-transform='' title=''></i>"
|
12
14
|
)
|
13
15
|
end
|
14
16
|
|
15
17
|
it 'should generate the correct icon from a configuration hash' do
|
16
18
|
fa = { name: 'help', options: { style: :light, size: 2 } }
|
17
|
-
expect(FA::Icon.new(fa)).to eql(
|
19
|
+
expect(FA::Icon.new(fa).safe).to eql(
|
18
20
|
"<i class='fal fa-help fa-2x' data-fa-transform='' title=''></i>"
|
19
21
|
)
|
20
22
|
end
|
21
23
|
|
22
24
|
it 'should raise ArgumentError for other input types' do
|
23
25
|
[nil, [], 0].each do |fa|
|
24
|
-
expect { FA::Icon.new(fa) }.to raise_error(
|
26
|
+
expect { FA::Icon.new(fa).safe }.to raise_error(
|
25
27
|
ArgumentError, 'Unexpected argument type.'
|
26
28
|
)
|
27
29
|
end
|
28
30
|
end
|
29
31
|
|
30
32
|
it 'should generate the correct brand icon' do
|
31
|
-
expect(FA::Icon.new(:github, style: :brands)).to eql(
|
33
|
+
expect(FA::Icon.new(:github, style: :brands).safe).to eql(
|
32
34
|
"<i class='fab fa-github' data-fa-transform='' title=''></i>"
|
33
35
|
)
|
34
36
|
end
|
@@ -42,7 +44,7 @@ RSpec.describe FA do
|
|
42
44
|
{ name: 'exclamation', options: { style: :regular } }
|
43
45
|
]
|
44
46
|
|
45
|
-
expect(FA::Layer.new(icons, grow: 2)).to eql(
|
47
|
+
expect(FA::Layer.new(icons, grow: 2).safe).to eql(
|
46
48
|
"<span class='icon fa-layers fa-fw ' title=''>" \
|
47
49
|
"<i class='fas fa-square' data-fa-transform='grow-2' title=''></i>" \
|
48
50
|
"<i class='fas fa-circle' data-fa-transform='grow-3' title=''></i>" \
|
@@ -57,7 +59,7 @@ RSpec.describe FA do
|
|
57
59
|
{ name: :counter, text: 17, options: { position: :tl } }
|
58
60
|
]
|
59
61
|
|
60
|
-
expect(FA::Layer.new(icons)).to eql(
|
62
|
+
expect(FA::Layer.new(icons).safe).to eql(
|
61
63
|
"<span class='icon fa-layers fa-fw ' title=''>" \
|
62
64
|
"<i class='fas fa-square' data-fa-transform='grow-0' title=''></i>" \
|
63
65
|
"<span class='fa-layers-counter fa-layers-top-left' " \
|
@@ -68,14 +70,14 @@ RSpec.describe FA do
|
|
68
70
|
|
69
71
|
describe 'span' do
|
70
72
|
it 'should generate the correct span from a string or symbol type' do
|
71
|
-
expect(FA::Span.new(:text, 'Hello')).to eql(
|
73
|
+
expect(FA::Span.new(:text, 'Hello').safe).to eql(
|
72
74
|
"<span class='fa-layers-text ' data-fa-transform=''>Hello</span>"
|
73
75
|
)
|
74
76
|
end
|
75
77
|
|
76
78
|
it 'should generate the correct span from a configuration hash' do
|
77
79
|
span = { type: :text, text: 'World', options: { position: :bl } }
|
78
|
-
expect(FA::Span.new(span)).to eql(
|
80
|
+
expect(FA::Span.new(span).safe).to eql(
|
79
81
|
"<span class='fa-layers-text fa-layers-bottom-left' " \
|
80
82
|
"data-fa-transform=''>World</span>"
|
81
83
|
)
|
@@ -83,7 +85,7 @@ RSpec.describe FA do
|
|
83
85
|
|
84
86
|
it 'should raise ArgumentError for other input types' do
|
85
87
|
[nil, [], 0].each do |fa|
|
86
|
-
expect { FA::Span.new(fa) }.to raise_error(
|
88
|
+
expect { FA::Span.new(fa).safe }.to raise_error(
|
87
89
|
ArgumentError, 'Unexpected argument type.'
|
88
90
|
)
|
89
91
|
end
|