linkificator 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/lib/linkificator.rb +2 -2
- data/lib/linkificator/conditions_storage.rb +12 -10
- data/lib/linkificator/conditions_storage/condition.rb +29 -13
- data/lib/linkificator/version.rb +1 -1
- data/spec/conditions_storage/condition_spec.rb +24 -0
- data/spec/conditions_storage_spec.rb +1 -1
- 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: 8d701023fd8a8105a73009361fcbd4bb3c37c913
|
4
|
+
data.tar.gz: 883dcb257b7e2d4a6817eb6de9589b552bd833d9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0138f1086f91eeef3c8b22cb056e268f5f9da9edd2a05a2574d84cc95ceed3665be7befdfd6579ecbe2d67d4b1ae51dd79b48eb6a600abbb2e9302f678020518
|
7
|
+
data.tar.gz: bf36ebce95a639dede5edab96a1110416cc79b94e82fa247a417396ca32931185ccf4f9f8009c6448a67675354a35500aeeda564b61f98d511b48fb52ac6487e
|
data/README.md
CHANGED
@@ -95,7 +95,7 @@ seo_link_to('text', 'http://other.com') # => <a href="http://other.com">text</
|
|
95
95
|
|
96
96
|
## Contributing
|
97
97
|
|
98
|
-
1. Fork it
|
98
|
+
1. Fork it
|
99
99
|
2. Create your feature branch (`git checkout -b my-new-feature`)
|
100
100
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
101
101
|
4. Push to the branch (`git push origin my-new-feature`)
|
data/lib/linkificator.rb
CHANGED
@@ -2,10 +2,11 @@ require 'base64'
|
|
2
2
|
|
3
3
|
class Linkificator::ConditionsStorage
|
4
4
|
|
5
|
-
attr_reader :conditions
|
5
|
+
attr_reader :conditions, :options
|
6
6
|
|
7
|
-
def initialize(conditions)
|
8
|
-
@conditions = conditions.map { |hash| Condition.new(hash) }
|
7
|
+
def initialize(conditions, options={})
|
8
|
+
@conditions = conditions.map { |hash| Condition.new(hash.merge(options)) }
|
9
|
+
@options = options
|
9
10
|
end
|
10
11
|
|
11
12
|
def condition_for(options={})
|
@@ -14,17 +15,18 @@ class Linkificator::ConditionsStorage
|
|
14
15
|
end
|
15
16
|
end
|
16
17
|
|
17
|
-
def link_to(
|
18
|
-
context = extract_context(
|
19
|
-
condition = condition_for(context) || Condition.new
|
20
|
-
condition.link_to(
|
18
|
+
def link_to(name = nil, options = nil, html_options = nil, &block)
|
19
|
+
context = extract_context(name, options, html_options, &block)
|
20
|
+
condition = condition_for(context) || Condition.new(self.options)
|
21
|
+
condition.link_to(name, options, html_options, &block)
|
21
22
|
end
|
22
23
|
|
23
24
|
private
|
24
25
|
|
25
|
-
def extract_context(
|
26
|
+
def extract_context(name = nil, options = nil, html_options = nil, &block)
|
27
|
+
html_options, options = options, name if block_given?
|
26
28
|
context = {}
|
27
|
-
context[:target] =
|
28
|
-
context.merge!((
|
29
|
+
context[:target] = options if options.is_a?(String)
|
30
|
+
context.merge!((html_options || {}).delete(:context) || {})
|
29
31
|
end
|
30
32
|
end
|
@@ -1,8 +1,8 @@
|
|
1
1
|
class Linkificator::ConditionsStorage::Condition
|
2
|
-
attr_reader :
|
2
|
+
attr_reader :options
|
3
3
|
|
4
|
-
def initialize(
|
5
|
-
@
|
4
|
+
def initialize(options={})
|
5
|
+
@options = options
|
6
6
|
end
|
7
7
|
|
8
8
|
def match?(options)
|
@@ -14,21 +14,27 @@ class Linkificator::ConditionsStorage::Condition
|
|
14
14
|
].all?
|
15
15
|
end
|
16
16
|
|
17
|
-
def link_to(
|
18
|
-
|
17
|
+
def link_to(name = nil, options = nil, html_options = nil, &block)
|
18
|
+
html_options, options, name = options, name, block if block_given?
|
19
|
+
options ||= {}
|
19
20
|
|
20
|
-
view_context = view_context((
|
21
|
+
view_context = view_context((html_options || {}).delete(:view_context))
|
21
22
|
|
22
23
|
if processing[:data_href]
|
23
|
-
url
|
24
|
-
|
24
|
+
url = view_context.url_for(*options)
|
25
|
+
html_options = (html_options || {}).merge('href' => '#', 'data-href' => Base64.strict_encode64(url))
|
25
26
|
end
|
26
27
|
|
27
28
|
if processing[:rel_nofollow]
|
28
|
-
|
29
|
+
html_options = (html_options || {}).merge(rel: 'nofollow')
|
29
30
|
end
|
30
31
|
|
31
|
-
|
32
|
+
|
33
|
+
link = if block_given?
|
34
|
+
call_link_method(view_context, options, html_options, &block)
|
35
|
+
else
|
36
|
+
call_link_method(view_context, name, options, html_options)
|
37
|
+
end
|
32
38
|
|
33
39
|
if processing[:wrap_noindex]
|
34
40
|
view_context.content_tag(:noindex) { link }
|
@@ -45,7 +51,7 @@ class Linkificator::ConditionsStorage::Condition
|
|
45
51
|
include ActionView::Context
|
46
52
|
end
|
47
53
|
|
48
|
-
def view_context(view_context)
|
54
|
+
def view_context(view_context=nil)
|
49
55
|
view_context || (@view_context ||= ViewContext.new)
|
50
56
|
end
|
51
57
|
|
@@ -63,10 +69,20 @@ class Linkificator::ConditionsStorage::Condition
|
|
63
69
|
end
|
64
70
|
|
65
71
|
def context
|
66
|
-
@context ||=
|
72
|
+
@context ||= options[:context] || {}
|
67
73
|
end
|
68
74
|
|
69
75
|
def processing
|
70
|
-
@processing ||=
|
76
|
+
@processing ||= options[:processing] || {}
|
77
|
+
end
|
78
|
+
|
79
|
+
def call_link_method(view_context, *params, &block)
|
80
|
+
@link_method = if options[:link_via] && view_context(view_context).respond_to?(options[:link_via])
|
81
|
+
options[:link_via]
|
82
|
+
else
|
83
|
+
:link_to
|
84
|
+
end
|
85
|
+
|
86
|
+
view_context(view_context).send(*([@link_method] + params), &block)
|
71
87
|
end
|
72
88
|
end
|
data/lib/linkificator/version.rb
CHANGED
@@ -129,5 +129,29 @@ describe Linkificator::ConditionsStorage::Condition do
|
|
129
129
|
end
|
130
130
|
end
|
131
131
|
|
132
|
+
context 'with block' do
|
133
|
+
let(:condition) {
|
134
|
+
Linkificator::ConditionsStorage::Condition.new(
|
135
|
+
processing: {rel_nofollow: true}
|
136
|
+
)
|
137
|
+
}
|
138
|
+
it do
|
139
|
+
expect(condition.link_to('http://example.com'){ 'text' }).to eq('<a href="http://example.com" rel="nofollow">text</a>')
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
context 'use custom link_to methods' do
|
144
|
+
let(:condition) {
|
145
|
+
Linkificator::ConditionsStorage::Condition.new(
|
146
|
+
processing: {rel_nofollow: true},
|
147
|
+
link_via: :custom_link_to
|
148
|
+
)
|
149
|
+
}
|
150
|
+
it do
|
151
|
+
expect_any_instance_of(Linkificator::ConditionsStorage::Condition::ViewContext).to receive(:custom_link_to)
|
152
|
+
expect(condition.link_to('text', 'http://example.com')).to eq(nil)
|
153
|
+
end
|
154
|
+
end
|
155
|
+
|
132
156
|
end
|
133
157
|
end
|
@@ -19,7 +19,7 @@ describe Linkificator::ConditionsStorage do
|
|
19
19
|
|
20
20
|
context '#link_to' do
|
21
21
|
it 'dont match anything' do
|
22
|
-
expect(storage.link_to('text', 'http://example.com/c')).to
|
22
|
+
expect(storage.link_to('text', 'http://example.com/c')).to eq('<a href="http://example.com/c">text</a>')
|
23
23
|
end
|
24
24
|
it 'match by target' do
|
25
25
|
expect(storage.link_to('text', 'http://example.com/a/b')).to eq('<noindex><a href="http://example.com/a/b">text</a></noindex>')
|