linkificator 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +15 -0
- data/.rspec +1 -0
- data/Gemfile +10 -0
- data/LICENSE.txt +22 -0
- data/README.md +102 -0
- data/Rakefile +2 -0
- data/lib/linkificator.rb +11 -0
- data/lib/linkificator/conditions_storage.rb +30 -0
- data/lib/linkificator/conditions_storage/condition.rb +72 -0
- data/lib/linkificator/version.rb +3 -0
- data/linkificator.gemspec +23 -0
- data/spec/conditions_storage/condition_spec.rb +133 -0
- data/spec/conditions_storage_spec.rb +32 -0
- data/spec/spec_helper.rb +5 -0
- metadata +89 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: cd55f7b84fe5b8cc95452c4233f849cc56fb4b06
|
4
|
+
data.tar.gz: c4761b2b55ed9cc0f54a36f49a976db5f6b7e776
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 2e47037d7c5caa01dfb5bb96176e54d7fc98149435beb72055127ce4c5a20650a872665ad7d2dd77f1d6a9b7ce7487b86af4b9dff68a29486db5801de2591f73
|
7
|
+
data.tar.gz: 10db3e4381d5dddadce0387d8a4ec2c60d2d507d58945b080073e660cfdfa807531e58b4a933413350a3963063eae70ba153bef391cbf93131ae3599461a42af
|
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 dizer
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
# Linkificator
|
2
|
+
|
3
|
+
Changes link depending on context
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'linkificator'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install linkificator
|
20
|
+
|
21
|
+
## Options
|
22
|
+
|
23
|
+
Full list of options:
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
{
|
27
|
+
context: {
|
28
|
+
target: %w(http://example.com.*), # href url
|
29
|
+
target_not: %w(http://example.com/a.*),
|
30
|
+
current: %w(http://example.dev.*), # Rely on context[:current] field in you app.
|
31
|
+
# Usually current request url.
|
32
|
+
current_not: %w(http://example.dev),
|
33
|
+
},
|
34
|
+
processing: {
|
35
|
+
wrap_noindex: true # Wrap with <noindex> tag
|
36
|
+
data_href: true # Clear href and put it in base64 to data-href attribute
|
37
|
+
rel_nofollow: true # Adds rel="nofollow" attribute to link tag
|
38
|
+
}
|
39
|
+
}
|
40
|
+
```
|
41
|
+
|
42
|
+
## Example
|
43
|
+
|
44
|
+
Lets wrap links to example.com with ```<noindex>``` tag:
|
45
|
+
|
46
|
+
Add to your ```application_controller.rb``` linkificator preferences:
|
47
|
+
|
48
|
+
```ruby
|
49
|
+
class ApplicationController < ActionController::Base
|
50
|
+
|
51
|
+
helper_method :linkificator
|
52
|
+
|
53
|
+
def linkificator
|
54
|
+
require 'linkificator'
|
55
|
+
@linkificator ||= Linkificator.for_conditions([
|
56
|
+
{
|
57
|
+
context: {target: %w(http://example.com.*)},
|
58
|
+
processing: {wrap_noindex: true}
|
59
|
+
}
|
60
|
+
])
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
```
|
65
|
+
|
66
|
+
Now you can define ```seo_link_to``` helper in ```application_helper.rb```:
|
67
|
+
|
68
|
+
```ruby
|
69
|
+
module ApplicationHelper
|
70
|
+
|
71
|
+
def seo_link_to(*params, &block)
|
72
|
+
linkificator.link_to(
|
73
|
+
*params[0, 1],
|
74
|
+
(params[2] || {}).merge(
|
75
|
+
view_context: self,
|
76
|
+
context: {
|
77
|
+
current: request.original_url
|
78
|
+
}
|
79
|
+
),
|
80
|
+
&block
|
81
|
+
)
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
```
|
86
|
+
|
87
|
+
Finally use new helper ```seo_link_to``` like default ```link_to```
|
88
|
+
|
89
|
+
```ruby
|
90
|
+
seo_link_to('text', 'http://example.com') # => <noindex><a href="http://example.com">text</a></noindex>
|
91
|
+
seo_link_to('text', 'http://other.com') # => <a href="http://other.com">text</a>
|
92
|
+
|
93
|
+
```
|
94
|
+
|
95
|
+
|
96
|
+
## Contributing
|
97
|
+
|
98
|
+
1. Fork itр
|
99
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
100
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
101
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
102
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/lib/linkificator.rb
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'base64'
|
2
|
+
|
3
|
+
class Linkificator::ConditionsStorage
|
4
|
+
|
5
|
+
attr_reader :conditions
|
6
|
+
|
7
|
+
def initialize(conditions)
|
8
|
+
@conditions = conditions.map { |hash| Condition.new(hash) }
|
9
|
+
end
|
10
|
+
|
11
|
+
def condition_for(options={})
|
12
|
+
@conditions.detect do |condition|
|
13
|
+
condition if condition.match?(options)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def link_to(*params, &block)
|
18
|
+
context = extract_context(params)
|
19
|
+
condition = condition_for(context) || Condition.new
|
20
|
+
condition.link_to(*params, &block)
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def extract_context(params)
|
26
|
+
context = {}
|
27
|
+
context[:target] = params[1] if params[1].is_a?(String)
|
28
|
+
context.merge!((params[2] || {}).delete(:context) || {})
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
class Linkificator::ConditionsStorage::Condition
|
2
|
+
attr_reader :conditions
|
3
|
+
|
4
|
+
def initialize(conditions={})
|
5
|
+
@conditions = conditions
|
6
|
+
end
|
7
|
+
|
8
|
+
def match?(options)
|
9
|
+
[
|
10
|
+
context[:target] ? detect_presence(context[:target], options[:target]) : true,
|
11
|
+
context[:target_not] ? !detect_presence(context[:target_not], options[:target]) : true,
|
12
|
+
context[:current] ? detect_presence(context[:current], options[:current]) : true,
|
13
|
+
context[:current_not] ? !detect_presence(context[:current_not], options[:current]) : true,
|
14
|
+
].all?
|
15
|
+
end
|
16
|
+
|
17
|
+
def link_to(*params, &block)
|
18
|
+
updated_params = params.clone
|
19
|
+
|
20
|
+
view_context = view_context((updated_params[2] || {}).delete(:view_context))
|
21
|
+
|
22
|
+
if processing[:data_href]
|
23
|
+
url = view_context.url_for(*updated_params[1])
|
24
|
+
updated_params[2] = (updated_params[2] || {}).merge('href' => '#', 'data-href' => Base64.strict_encode64(url))
|
25
|
+
end
|
26
|
+
|
27
|
+
if processing[:rel_nofollow]
|
28
|
+
updated_params[2] = (updated_params[2] || {}).merge(rel: 'nofollow')
|
29
|
+
end
|
30
|
+
|
31
|
+
link = view_context.link_to(*updated_params, &block)
|
32
|
+
|
33
|
+
if processing[:wrap_noindex]
|
34
|
+
view_context.content_tag(:noindex) { link }
|
35
|
+
else
|
36
|
+
link
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
|
42
|
+
class ViewContext
|
43
|
+
require 'action_view'
|
44
|
+
include ActionView::Helpers
|
45
|
+
include ActionView::Context
|
46
|
+
end
|
47
|
+
|
48
|
+
def view_context(view_context)
|
49
|
+
view_context || (@view_context ||= ViewContext.new)
|
50
|
+
end
|
51
|
+
|
52
|
+
def detect_presence(pool, element)
|
53
|
+
pool.detect { |target| match_regexp(element, target) }.present?
|
54
|
+
end
|
55
|
+
|
56
|
+
def match_regexp(str, regexp_str)
|
57
|
+
regexp = wrap_regexp(regexp_str)
|
58
|
+
str.to_s.match(regexp)
|
59
|
+
end
|
60
|
+
|
61
|
+
def wrap_regexp(str)
|
62
|
+
/^#{str}$/i
|
63
|
+
end
|
64
|
+
|
65
|
+
def context
|
66
|
+
@context ||= conditions[:context] || {}
|
67
|
+
end
|
68
|
+
|
69
|
+
def processing
|
70
|
+
@processing ||= conditions[:processing] || {}
|
71
|
+
end
|
72
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'linkificator/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "linkificator"
|
8
|
+
spec.version = Linkificator::VERSION
|
9
|
+
spec.authors = ["dizer"]
|
10
|
+
spec.email = ["dizeru@gmail.com"]
|
11
|
+
spec.summary = %q{Add link params}
|
12
|
+
# spec.description = %q{TODO: Write a longer description. Optional.}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
end
|
@@ -0,0 +1,133 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Linkificator::ConditionsStorage::Condition do
|
4
|
+
context 'context section' do
|
5
|
+
context 'target is' do
|
6
|
+
let(:condition) {
|
7
|
+
Linkificator::ConditionsStorage::Condition.new(
|
8
|
+
context: {target: %w(http://example.com/a.*)}
|
9
|
+
)
|
10
|
+
}
|
11
|
+
|
12
|
+
{
|
13
|
+
'http://example.com/a/b' => true,
|
14
|
+
'http://example.com/a/' => true,
|
15
|
+
'http://example.com/a' => true,
|
16
|
+
'http://example.com/' => false
|
17
|
+
|
18
|
+
}.each do |target, expected_result|
|
19
|
+
it "match #{target} is #{expected_result}" do
|
20
|
+
expect(condition.match?(target: target)).to eq(expected_result)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
context 'target_not' do
|
26
|
+
let(:condition) {
|
27
|
+
Linkificator::ConditionsStorage::Condition.new(
|
28
|
+
context: {target_not: %w(http://example.com/a/e/.* http://example.com/a/e2), }
|
29
|
+
)
|
30
|
+
}
|
31
|
+
|
32
|
+
{
|
33
|
+
'http://example.com/a/b' => true,
|
34
|
+
'http://example.com/a/e/' => false,
|
35
|
+
'http://example.com/a/e2' => false,
|
36
|
+
'http://example.com/a/e2/c' => true
|
37
|
+
}.each do |target, expected_result|
|
38
|
+
it "match #{target} is #{expected_result}" do
|
39
|
+
expect(condition.match?(target: target)).to eq(expected_result)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
context 'current is' do
|
45
|
+
let(:condition) {
|
46
|
+
Linkificator::ConditionsStorage::Condition.new(
|
47
|
+
context: {current: %w(http://example.com/a.*)}
|
48
|
+
)
|
49
|
+
}
|
50
|
+
|
51
|
+
{
|
52
|
+
'http://example.com/a/b' => true,
|
53
|
+
'http://example.com/a/' => true,
|
54
|
+
'http://example.com/a' => true,
|
55
|
+
'http://example.com/' => false,
|
56
|
+
'http://example.com/b' => false
|
57
|
+
}.each do |target, expected_result|
|
58
|
+
it "match #{target} is #{expected_result}" do
|
59
|
+
expect(condition.match?(current: target)).to eq(expected_result)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
context 'current_not' do
|
65
|
+
let(:condition) {
|
66
|
+
Linkificator::ConditionsStorage::Condition.new(
|
67
|
+
context: {current_not: %w(http://example.com/a.*)}
|
68
|
+
)
|
69
|
+
}
|
70
|
+
|
71
|
+
{
|
72
|
+
'http://example.com/a' => false,
|
73
|
+
'http://example.com/' => true,
|
74
|
+
'http://example.com/b' => true
|
75
|
+
}.each do |target, expected_result|
|
76
|
+
it "match #{target} is #{expected_result}" do
|
77
|
+
expect(condition.match?(current: target)).to eq(expected_result)
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
context 'processing section' do
|
84
|
+
context 'wrap_noindex' do
|
85
|
+
let(:condition) {
|
86
|
+
Linkificator::ConditionsStorage::Condition.new(
|
87
|
+
processing: {wrap_noindex: true}
|
88
|
+
)
|
89
|
+
}
|
90
|
+
it do
|
91
|
+
expect(condition.link_to('text', 'http://example.com')).to eq('<noindex><a href="http://example.com">text</a></noindex>')
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
context 'data_href' do
|
96
|
+
let(:condition) {
|
97
|
+
Linkificator::ConditionsStorage::Condition.new(
|
98
|
+
processing: {data_href: true}
|
99
|
+
)
|
100
|
+
}
|
101
|
+
it do
|
102
|
+
expect(condition.link_to('text', 'http://example.com')).to eq('<a data-href="aHR0cDovL2V4YW1wbGUuY29t" href="#">text</a>')
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
context 'rel_nofollow' do
|
107
|
+
let(:condition) {
|
108
|
+
Linkificator::ConditionsStorage::Condition.new(
|
109
|
+
processing: {rel_nofollow: true}
|
110
|
+
)
|
111
|
+
}
|
112
|
+
it do
|
113
|
+
expect(condition.link_to('text', 'http://example.com')).to eq('<a href="http://example.com" rel="nofollow">text</a>')
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
context 'combined' do
|
118
|
+
let(:condition) {
|
119
|
+
Linkificator::ConditionsStorage::Condition.new(
|
120
|
+
processing: {
|
121
|
+
wrap_noindex: true,
|
122
|
+
data_href: true,
|
123
|
+
rel_nofollow: true
|
124
|
+
}
|
125
|
+
)
|
126
|
+
}
|
127
|
+
it do
|
128
|
+
expect(condition.link_to('text', 'http://example.com')).to eq('<noindex><a data-href="aHR0cDovL2V4YW1wbGUuY29t" href="#" rel="nofollow">text</a></noindex>')
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
end
|
133
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Linkificator::ConditionsStorage do
|
4
|
+
let(:storage) {
|
5
|
+
Linkificator::ConditionsStorage.new([
|
6
|
+
{
|
7
|
+
context: { target: %w(http://example.com/a/.*) },
|
8
|
+
processing: { wrap_noindex: true }
|
9
|
+
},
|
10
|
+
{
|
11
|
+
context: { current: %w(http://example.dev/a/.*) },
|
12
|
+
processing: { wrap_noindex: true }
|
13
|
+
}
|
14
|
+
])
|
15
|
+
}
|
16
|
+
it 'selects condition' do
|
17
|
+
expect(storage.condition_for(target: 'http://example.com/a/')).to be_instance_of(Linkificator::ConditionsStorage::Condition)
|
18
|
+
end
|
19
|
+
|
20
|
+
context '#link_to' do
|
21
|
+
it 'dont match anything' do
|
22
|
+
expect(storage.link_to('text', 'http://example.com/c')).to eq('<a href="http://example.com/c">text</a>')
|
23
|
+
end
|
24
|
+
it 'match by target' do
|
25
|
+
expect(storage.link_to('text', 'http://example.com/a/b')).to eq('<noindex><a href="http://example.com/a/b">text</a></noindex>')
|
26
|
+
end
|
27
|
+
it 'match by current' do
|
28
|
+
expect(storage.link_to('text', 'http://example.com/c', context: {current: 'http://example.dev/a/'}))
|
29
|
+
.to eq('<noindex><a href="http://example.com/c">text</a></noindex>')
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: linkificator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- dizer
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-12-01 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.7'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
description:
|
42
|
+
email:
|
43
|
+
- dizeru@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".gitignore"
|
49
|
+
- ".rspec"
|
50
|
+
- Gemfile
|
51
|
+
- LICENSE.txt
|
52
|
+
- README.md
|
53
|
+
- Rakefile
|
54
|
+
- lib/linkificator.rb
|
55
|
+
- lib/linkificator/conditions_storage.rb
|
56
|
+
- lib/linkificator/conditions_storage/condition.rb
|
57
|
+
- lib/linkificator/version.rb
|
58
|
+
- linkificator.gemspec
|
59
|
+
- spec/conditions_storage/condition_spec.rb
|
60
|
+
- spec/conditions_storage_spec.rb
|
61
|
+
- spec/spec_helper.rb
|
62
|
+
homepage: ''
|
63
|
+
licenses:
|
64
|
+
- MIT
|
65
|
+
metadata: {}
|
66
|
+
post_install_message:
|
67
|
+
rdoc_options: []
|
68
|
+
require_paths:
|
69
|
+
- lib
|
70
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
75
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
76
|
+
requirements:
|
77
|
+
- - ">="
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: '0'
|
80
|
+
requirements: []
|
81
|
+
rubyforge_project:
|
82
|
+
rubygems_version: 2.4.3
|
83
|
+
signing_key:
|
84
|
+
specification_version: 4
|
85
|
+
summary: Add link params
|
86
|
+
test_files:
|
87
|
+
- spec/conditions_storage/condition_spec.rb
|
88
|
+
- spec/conditions_storage_spec.rb
|
89
|
+
- spec/spec_helper.rb
|