zfben_extend 0.0.1
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 +4 -0
- data/.travis.yml +3 -0
- data/Gemfile +4 -0
- data/README.md +15 -0
- data/Rakefile +8 -0
- data/lib/zfben_extend/string.rb +58 -0
- data/lib/zfben_extend.rb +4 -0
- data/test/zfben_extend_test.rb +30 -0
- data/zfben_extend.gemspec +17 -0
- metadata +54 -0
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
# zfben_extend
|
2
|
+
|
3
|
+
A tool to extend ruby string.
|
4
|
+
|
5
|
+
## Install
|
6
|
+
|
7
|
+
gem 'zfben_extend'
|
8
|
+
|
9
|
+
## Use
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
ZfbenExtend::String.to_html('text http://link #tag mail@mail.com text') #=> 'text <a href="http://link">http://link</a> <a href="/tags/tag">#tag</a> <a href="mailto:mail@mail.com">mail@mail.com</a> text'
|
13
|
+
```
|
14
|
+
|
15
|
+
See more at https://github.com/benz303/zfben_extend/blob/master/test/zfben_extend_test.rb
|
data/Rakefile
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
class ZfbenExtend::String
|
2
|
+
def self.to_html text, options={}
|
3
|
+
options = {
|
4
|
+
link: true,
|
5
|
+
link_regexp: /[a-zA-Z]{3,}:\/\/\S+/,
|
6
|
+
tag: true,
|
7
|
+
tag_regexp: /#[a-zA-Z0-9]+/,
|
8
|
+
tag_url: '/tags/',
|
9
|
+
mail: true,
|
10
|
+
mail_regexp: /[a-zA-Z0-9.-]+@[a-zA-Z0-9]+\.[a-zA-Z0-9.]+/,
|
11
|
+
user: true,
|
12
|
+
user_regexp: /@[a-zA-Z0-9]+/,
|
13
|
+
user_url: '/users/'
|
14
|
+
}.merge(options)
|
15
|
+
|
16
|
+
data = {}
|
17
|
+
|
18
|
+
if options[:link]
|
19
|
+
data[:link] = []
|
20
|
+
text = text.gsub(options[:link_regexp]){ |link|
|
21
|
+
data[:link].push('<a href="' + link + '">' + link + '</a>')
|
22
|
+
'ZFBENLINK' + (data[:link].length - 1).to_s + 'ZFBENLINK'
|
23
|
+
}
|
24
|
+
end
|
25
|
+
|
26
|
+
if options[:tag]
|
27
|
+
data[:tag] = []
|
28
|
+
text = text.gsub(options[:tag_regexp]){ |tag|
|
29
|
+
data[:tag].push('<a href="' + options[:tag_url] + tag.gsub('#', '') + '">' + tag + '</a>')
|
30
|
+
'ZFBENTAG' + (data[:tag].length - 1).to_s + 'ZFBENTAG'
|
31
|
+
}
|
32
|
+
end
|
33
|
+
|
34
|
+
if options[:mail]
|
35
|
+
data[:mail] = []
|
36
|
+
text = text.gsub(options[:mail_regexp]){ |mail|
|
37
|
+
data[:mail].push('<a href="mailto:' + mail + '">' + mail + '</a>')
|
38
|
+
'ZFBENMAIL' + (data[:mail].length - 1).to_s + 'ZFBENMAIL'
|
39
|
+
}
|
40
|
+
end
|
41
|
+
|
42
|
+
if options[:user]
|
43
|
+
data[:user] = []
|
44
|
+
text = text.gsub(options[:user_regexp]){ |user|
|
45
|
+
data[:user].push('<a href="' + options[:user_url] + user.gsub('@', '') + '">' + user + '</a>')
|
46
|
+
'ZFBENUSER' + (data[:user].length - 1).to_s + 'ZFBENUSER'
|
47
|
+
}
|
48
|
+
end
|
49
|
+
|
50
|
+
[:link, :tag, :mail, :user].each do |name|
|
51
|
+
if options[name]
|
52
|
+
text = text.gsub(Regexp.new('ZFBEN' + name.to_s.upcase + '[0-9]+ZFBEN' + name.to_s.upcase)){ |t| data[name][t.match(/[0-9]+/)[0].to_i] }
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
text
|
57
|
+
end
|
58
|
+
end
|
data/lib/zfben_extend.rb
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
class TestZfbenExtend < Test::Unit::TestCase
|
2
|
+
def test_link
|
3
|
+
assert_equal ZfbenExtend::String.to_html('http://link', link: false), 'http://link'
|
4
|
+
assert_equal ZfbenExtend::String.to_html('http://link'), '<a href="http://link">http://link</a>'
|
5
|
+
assert_equal ZfbenExtend::String.to_html('text http://link text'), 'text <a href="http://link">http://link</a> text'
|
6
|
+
assert_equal ZfbenExtend::String.to_html('text http://link ftp://link/folder'), 'text <a href="http://link">http://link</a> <a href="ftp://link/folder">ftp://link/folder</a>'
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_tag
|
10
|
+
assert_equal ZfbenExtend::String.to_html('#a', tag: false), '#a'
|
11
|
+
assert_equal ZfbenExtend::String.to_html('#a'), '<a href="/tags/a">#a</a>'
|
12
|
+
assert_equal ZfbenExtend::String.to_html('text #a #b'), 'text <a href="/tags/a">#a</a> <a href="/tags/b">#b</a>'
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_mail
|
16
|
+
assert_equal ZfbenExtend::String.to_html('abc@abc.com', mail: false, user: false), 'abc@abc.com'
|
17
|
+
assert_equal ZfbenExtend::String.to_html('abc@abc.com'), '<a href="mailto:abc@abc.com">abc@abc.com</a>'
|
18
|
+
assert_equal ZfbenExtend::String.to_html('text abc@abc.com zhu.feng@zfben.com'), 'text <a href="mailto:abc@abc.com">abc@abc.com</a> <a href="mailto:zhu.feng@zfben.com">zhu.feng@zfben.com</a>'
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_user
|
22
|
+
assert_equal ZfbenExtend::String.to_html('@user', user: false), '@user'
|
23
|
+
assert_equal ZfbenExtend::String.to_html('@user'), '<a href="/users/user">@user</a>'
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_all
|
27
|
+
assert_equal ZfbenExtend::String.to_html('text http://link #tag mail@mail.com text', link: false, tag: false, mail: false, user: false), 'text http://link #tag mail@mail.com text'
|
28
|
+
assert_equal ZfbenExtend::String.to_html('text http://link #tag mail@mail.com text'), 'text <a href="http://link">http://link</a> <a href="/tags/tag">#tag</a> <a href="mailto:mail@mail.com">mail@mail.com</a> text'
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path('../lib', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.name = 'zfben_extend'
|
6
|
+
s.version = '0.0.1'
|
7
|
+
s.authors = ['Ben']
|
8
|
+
s.email = ['ben@zfben.com']
|
9
|
+
s.homepage = 'https://github.com/benz303/zfben_extend'
|
10
|
+
s.summary = %q{}
|
11
|
+
s.description = %q{}
|
12
|
+
|
13
|
+
s.files = `git ls-files`.split("\n")
|
14
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
15
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
16
|
+
s.require_paths = ['lib']
|
17
|
+
end
|
metadata
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: zfben_extend
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Ben
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-12-23 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: ''
|
15
|
+
email:
|
16
|
+
- ben@zfben.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- .gitignore
|
22
|
+
- .travis.yml
|
23
|
+
- Gemfile
|
24
|
+
- README.md
|
25
|
+
- Rakefile
|
26
|
+
- lib/zfben_extend.rb
|
27
|
+
- lib/zfben_extend/string.rb
|
28
|
+
- test/zfben_extend_test.rb
|
29
|
+
- zfben_extend.gemspec
|
30
|
+
homepage: https://github.com/benz303/zfben_extend
|
31
|
+
licenses: []
|
32
|
+
post_install_message:
|
33
|
+
rdoc_options: []
|
34
|
+
require_paths:
|
35
|
+
- lib
|
36
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
37
|
+
none: false
|
38
|
+
requirements:
|
39
|
+
- - ! '>='
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
43
|
+
none: false
|
44
|
+
requirements:
|
45
|
+
- - ! '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
requirements: []
|
49
|
+
rubyforge_project:
|
50
|
+
rubygems_version: 1.8.10
|
51
|
+
signing_key:
|
52
|
+
specification_version: 3
|
53
|
+
summary: ''
|
54
|
+
test_files: []
|