smartlinks 0.1.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.
- data/.gitignore +23 -0
- data/.rspec +1 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +24 -0
- data/README.md +46 -0
- data/Rakefile +4 -0
- data/lib/smartlinks/base.rb +11 -0
- data/lib/smartlinks/linkificator.rb +67 -0
- data/lib/smartlinks/version.rb +3 -0
- data/lib/smartlinks.rb +5 -0
- data/smartlinks.gemspec +22 -0
- data/spec/lib/smartlinks/smartlinks_spec.rb +70 -0
- data/spec/spec_helper.rb +6 -0
- metadata +79 -0
data/.gitignore
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
coverage
|
6
|
+
InstalledFiles
|
7
|
+
lib/bundler/man
|
8
|
+
pkg
|
9
|
+
rdoc
|
10
|
+
spec/reports
|
11
|
+
test/tmp
|
12
|
+
test/version_tmp
|
13
|
+
tmp
|
14
|
+
|
15
|
+
# YARD artifacts
|
16
|
+
.yardoc
|
17
|
+
_yardoc
|
18
|
+
doc/
|
19
|
+
|
20
|
+
# VIM tmp files
|
21
|
+
*.swp
|
22
|
+
*.swo
|
23
|
+
~*
|
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color --tty
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
smartlinks (0.0.1)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
diff-lcs (1.1.3)
|
10
|
+
rspec (2.12.0)
|
11
|
+
rspec-core (~> 2.12.0)
|
12
|
+
rspec-expectations (~> 2.12.0)
|
13
|
+
rspec-mocks (~> 2.12.0)
|
14
|
+
rspec-core (2.12.2)
|
15
|
+
rspec-expectations (2.12.1)
|
16
|
+
diff-lcs (~> 1.1.3)
|
17
|
+
rspec-mocks (2.12.2)
|
18
|
+
|
19
|
+
PLATFORMS
|
20
|
+
ruby
|
21
|
+
|
22
|
+
DEPENDENCIES
|
23
|
+
rspec
|
24
|
+
smartlinks!
|
data/README.md
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
# SmartLinks
|
2
|
+
|
3
|
+
Gem for linkifying text
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Just as usual. If you're using bundler - just this to your Gemfile:
|
8
|
+
|
9
|
+
gem 'smartlinks'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
bundle install
|
14
|
+
|
15
|
+
Or install it yourself:
|
16
|
+
|
17
|
+
gem install smartlinks
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
This gem is very simple. You have only one method, that you need. For example:
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
text = "@sqrel and @troyanryller just made a cool gem!"
|
25
|
+
|
26
|
+
Smartlinks.linkify text
|
27
|
+
```
|
28
|
+
|
29
|
+
That's it! Now you have linkified text:
|
30
|
+
|
31
|
+
<a href="https://twitter.com/sqrel">@sqrel</a> and <a href="https://twitter.com/troyanryller">@troyanryller</a> just made a cool gem!
|
32
|
+
|
33
|
+
By default, gem interprets @user and #hash as twitter user and hashtag, but you can explicitly define domain to user and hashtag:
|
34
|
+
|
35
|
+
Smartlinks.linkify text, account: "http://example.com/%s", hashtag: "http://example.com/search/%s"
|
36
|
+
|
37
|
+
`%s` will be replaced to username or hash
|
38
|
+
|
39
|
+
Also you can define additional links params, for example:
|
40
|
+
|
41
|
+
Smartlinks.linkify text, params: { class: "some-link-class", rel: "nofollow" }
|
42
|
+
|
43
|
+
## Credits
|
44
|
+
|
45
|
+
* Vasilij Melnychuk ([@SqREL](https://twitter.com/sqrel))
|
46
|
+
* Igor Petruh ([@Troyanryller](https://twitter.com/troyanryller))
|
data/Rakefile
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
module Smartlinks
|
2
|
+
class Linkificator
|
3
|
+
|
4
|
+
attr_reader :text
|
5
|
+
|
6
|
+
# ---------- Regex patterns ----------
|
7
|
+
PAT_LINK = %r{
|
8
|
+
(?: ((?:ed2k|ftp|http|https|irc|news|gopher|nntp|telnet|webcal|xmpp|callto|feed|svn|urn|aim|rsync|tag|ssh|sftp|rtsp|afs):)// | www\. )
|
9
|
+
[^\s<]+
|
10
|
+
}x
|
11
|
+
PAT_EMAIL = %r{
|
12
|
+
[a-z0-9!#$\%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$\%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?
|
13
|
+
}x
|
14
|
+
PAT_ACCOUNT = %r{(^|\s)@(\w+)}
|
15
|
+
PAT_HASH = %r{#(\w+)}
|
16
|
+
# ------------------------------------
|
17
|
+
|
18
|
+
def initialize(text, attrs={})
|
19
|
+
@text = text
|
20
|
+
@attrs = attrs
|
21
|
+
end
|
22
|
+
|
23
|
+
def email
|
24
|
+
@text.gsub! PAT_EMAIL do |email|
|
25
|
+
"<a href=\"mailto:#{email}\">#{email}</a>"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def link
|
30
|
+
@text.gsub! PAT_LINK do |url|
|
31
|
+
if @attrs[:params]
|
32
|
+
params = @attrs[:params].map{|k, v| " #{k}=\"#{v}\""}.join''
|
33
|
+
"<a href=\"#{url}\"#{params}>#{url}</a>"
|
34
|
+
else
|
35
|
+
"<a href=\"#{url}\">#{url}</a>"
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def account
|
41
|
+
@text.gsub!(PAT_ACCOUNT) do
|
42
|
+
space, account = $~[1], $~[2]
|
43
|
+
|
44
|
+
if @attrs[:account]
|
45
|
+
url = @attrs[:account].sub "%s", account
|
46
|
+
"#{space}<a href=\"#{url}\">@#{account}</a>"
|
47
|
+
else
|
48
|
+
"#{space}<a href=\"https://twitter.com/#{account}\">@#{account}</a>"
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def hashtag
|
54
|
+
@text.gsub!(PAT_HASH) do
|
55
|
+
hashtag = $~[1]
|
56
|
+
|
57
|
+
if @attrs[:hashtag]
|
58
|
+
url = @attrs[:hashtag].sub "%s", hashtag
|
59
|
+
"<a href=\"#{url}\">##{hashtag}</a>"
|
60
|
+
else
|
61
|
+
"<a href=\"https://twitter.com/search?q=%23#{hashtag}&src=hash\">##{hashtag}</a>"
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
67
|
+
end
|
data/lib/smartlinks.rb
ADDED
data/smartlinks.gemspec
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'smartlinks/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "smartlinks"
|
8
|
+
gem.version = Smartlinks::VERSION
|
9
|
+
gem.authors = ["Vasilij Melnychuck", "Igor Petruh"]
|
10
|
+
gem.email = ["isqrel@gmail.com", "pro100igor@gmail.com"]
|
11
|
+
gem.description = %q{ Gem for linkfying your text }
|
12
|
+
gem.summary = %q{ Smartlinks converts your palin-text urls, twitter-like accounts and hashtag links to html a tags }
|
13
|
+
gem.homepage = "https://github.com/SqREL/smartlinks"
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
17
|
+
gem.require_paths = ["lib"]
|
18
|
+
|
19
|
+
['rspec'].each do |dep|
|
20
|
+
gem.add_development_dependency(dep)
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Smartlinks do
|
4
|
+
|
5
|
+
it "modifies http url" do
|
6
|
+
text, result = data "http"
|
7
|
+
Smartlinks.linkify(text).should eql(result)
|
8
|
+
end
|
9
|
+
it "modifies https url" do
|
10
|
+
text, result = data "https"
|
11
|
+
Smartlinks.linkify(text).should eql(result)
|
12
|
+
end
|
13
|
+
it "modifies ftp url" do
|
14
|
+
text, result = data "ftp"
|
15
|
+
Smartlinks.linkify(text).should eql(result)
|
16
|
+
end
|
17
|
+
it "modifies http url" do
|
18
|
+
text, result = data "http"
|
19
|
+
Smartlinks.linkify(text).should eql(result)
|
20
|
+
end
|
21
|
+
|
22
|
+
it "do not modifyes if allready link" do
|
23
|
+
text = "Allready <a href=\"http://google.com\">linkifyed</a> url"
|
24
|
+
Smartlinks.linkify(text).should eql(text)
|
25
|
+
end
|
26
|
+
|
27
|
+
it "modifyes plaintext mail account to link" do
|
28
|
+
text = "Reply to admin@test.com and check user@gmail.com account"
|
29
|
+
result = "Reply to <a href=\"mailto:admin@test.com\">admin@test.com</a> and check <a href=\"mailto:user@gmail.com\">user@gmail.com</a> account"
|
30
|
+
Smartlinks.linkify(text).should eql(result)
|
31
|
+
end
|
32
|
+
|
33
|
+
it "creates twitter links" do
|
34
|
+
text = "@sqrel, check out @dht twitter"
|
35
|
+
result = "<a href=\"https://twitter.com/sqrel\">@sqrel</a>, check out <a href=\"https://twitter.com/dht\">@dht</a> twitter"
|
36
|
+
Smartlinks.linkify(text).should eql(result)
|
37
|
+
end
|
38
|
+
|
39
|
+
it "creates twitter-like links with custom domain" do
|
40
|
+
text = "@sqrel, check out @dht account"
|
41
|
+
result = "<a href=\"http://test.com/sqrel\">@sqrel</a>, check out <a href=\"http://test.com/dht\">@dht</a> account"
|
42
|
+
Smartlinks.linkify(text, account: "http://test.com/%s").should eql(result)
|
43
|
+
end
|
44
|
+
|
45
|
+
it "creates twitter hash links" do
|
46
|
+
text = "#dude, check out #rails framework"
|
47
|
+
result = "<a href=\"https://twitter.com/search?q=%23dude&src=hash\">#dude</a>, check out <a href=\"https://twitter.com/search?q=%23rails&src=hash\">#rails</a> framework"
|
48
|
+
Smartlinks.linkify(text).should eql(result)
|
49
|
+
end
|
50
|
+
|
51
|
+
it "creates twitter-like links with custom domain" do
|
52
|
+
text = "#dude, check out #rails framework"
|
53
|
+
result = "<a href=\"http://example.com/dude?src=hash\">#dude</a>, check out <a href=\"http://example.com/rails?src=hash\">#rails</a> framework"
|
54
|
+
Smartlinks.linkify(text, hashtag: "http://example.com/%s?src=hash").should eql(result)
|
55
|
+
end
|
56
|
+
|
57
|
+
it "converts url to link with some link params" do
|
58
|
+
text = "http://example.com/3 Word http://lorem.com/id/1 seperator http://test.com/foo/bar Foo"
|
59
|
+
result = "<a href=\"http://example.com/3\" class=\"main\" rel=\"nofollow\">http://example.com/3</a> Word <a href=\"http://lorem.com/id/1\" class=\"main\" rel=\"nofollow\">http://lorem.com/id/1</a> seperator <a href=\"http://test.com/foo/bar\" class=\"main\" rel=\"nofollow\">http://test.com/foo/bar</a> Foo"
|
60
|
+
Smartlinks.linkify(text, params: { class: "main", rel: "nofollow" }).should eql(result)
|
61
|
+
end
|
62
|
+
|
63
|
+
private
|
64
|
+
def data(protocol)
|
65
|
+
text = "#{protocol}://example.com/3 Word #{protocol}://lorem.com/id/1 seperator #{protocol}://test.com/foo/bar Foo"
|
66
|
+
result = "<a href=\"#{protocol}://example.com/3\">#{protocol}://example.com/3</a> Word <a href=\"#{protocol}://lorem.com/id/1\">#{protocol}://lorem.com/id/1</a> seperator <a href=\"#{protocol}://test.com/foo/bar\">#{protocol}://test.com/foo/bar</a> Foo"
|
67
|
+
[text, result]
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: smartlinks
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.1.0
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Vasilij Melnychuck
|
9
|
+
- Igor Petruh
|
10
|
+
autorequire:
|
11
|
+
bindir: bin
|
12
|
+
cert_chain: []
|
13
|
+
date: 2013-02-16 00:00:00.000000000 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
prerelease: false
|
17
|
+
type: :development
|
18
|
+
name: rspec
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
none: false
|
21
|
+
requirements:
|
22
|
+
- - ! '>='
|
23
|
+
- !ruby/object:Gem::Version
|
24
|
+
version: '0'
|
25
|
+
requirement: !ruby/object:Gem::Requirement
|
26
|
+
none: false
|
27
|
+
requirements:
|
28
|
+
- - ! '>='
|
29
|
+
- !ruby/object:Gem::Version
|
30
|
+
version: '0'
|
31
|
+
description: ! ' Gem for linkfying your text '
|
32
|
+
email:
|
33
|
+
- isqrel@gmail.com
|
34
|
+
- pro100igor@gmail.com
|
35
|
+
executables: []
|
36
|
+
extensions: []
|
37
|
+
extra_rdoc_files: []
|
38
|
+
files:
|
39
|
+
- .gitignore
|
40
|
+
- .rspec
|
41
|
+
- Gemfile
|
42
|
+
- Gemfile.lock
|
43
|
+
- README.md
|
44
|
+
- Rakefile
|
45
|
+
- lib/smartlinks.rb
|
46
|
+
- lib/smartlinks/base.rb
|
47
|
+
- lib/smartlinks/linkificator.rb
|
48
|
+
- lib/smartlinks/version.rb
|
49
|
+
- smartlinks.gemspec
|
50
|
+
- spec/lib/smartlinks/smartlinks_spec.rb
|
51
|
+
- spec/spec_helper.rb
|
52
|
+
homepage: https://github.com/SqREL/smartlinks
|
53
|
+
licenses: []
|
54
|
+
post_install_message:
|
55
|
+
rdoc_options: []
|
56
|
+
require_paths:
|
57
|
+
- lib
|
58
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
59
|
+
none: false
|
60
|
+
requirements:
|
61
|
+
- - ! '>='
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: '0'
|
64
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
requirements: []
|
71
|
+
rubyforge_project:
|
72
|
+
rubygems_version: 1.8.25
|
73
|
+
signing_key:
|
74
|
+
specification_version: 3
|
75
|
+
summary: Smartlinks converts your palin-text urls, twitter-like accounts and hashtag
|
76
|
+
links to html a tags
|
77
|
+
test_files:
|
78
|
+
- spec/lib/smartlinks/smartlinks_spec.rb
|
79
|
+
- spec/spec_helper.rb
|