zfben_extend 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -2,3 +2,5 @@ source "http://rubygems.org"
2
2
 
3
3
  # Specify your gem's dependencies in zfben_extend.gemspec
4
4
  gemspec
5
+
6
+ gem 'rake'
data/README.md CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  A tool to extend ruby string.
4
4
 
5
+ [![Build Status](https://secure.travis-ci.org/benz303/zfben_extend.png)](http://travis-ci.org/benz303/zfben_extend)
6
+
5
7
  ## Install
6
8
 
7
9
  gem 'zfben_extend'
@@ -9,7 +11,12 @@ A tool to extend ruby string.
9
11
  ## Use
10
12
 
11
13
  ```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'
14
+ ZfbenExtend::String.to_html('text http://link #tag mail@mail.com @user text', tag_url: '/tagurl/', user_url: '/userurl/')
15
+
16
+ # => 'text <a href="http://link">http://link</a> <a href="/tagurl/tag">#tag</a> <a href="mailto:mail@mail.com">mail@mail.com</a> <a href="/userurl/user">@user</a> text'
17
+
18
+ ZfbenExtend::String.to_cnbr('a.b', chars: /\./, br: '<hr />')
19
+ # => 'a.<hr />b'
13
20
  ```
14
21
 
15
- See more at https://github.com/benz303/zfben_extend/blob/master/test/zfben_extend_test.rb
22
+ See more at https://github.com/benz303/zfben_extend/blob/master/test/string_test.rb
data/Rakefile CHANGED
@@ -6,3 +6,5 @@ task :test do
6
6
  require 'test/unit'
7
7
  Dir['test/*_test.rb'].each{ |f| require File.realpath(f) }
8
8
  end
9
+
10
+ task :default => :test
@@ -1,4 +1,5 @@
1
- class ZfbenExtend::String
1
+ # encoding: UTF-8
2
+ class ZfbenExtend::String < String
2
3
  def self.to_html text, options={}
3
4
  options = {
4
5
  link: true,
@@ -53,6 +54,23 @@ class ZfbenExtend::String
53
54
  end
54
55
  end
55
56
 
56
- text
57
+ self.new text
58
+ end
59
+
60
+ def to_html options = {}
61
+ self.class.to_html self, options
62
+ end
63
+
64
+ def self.to_cnbr text, options = {}
65
+ options = {
66
+ chars: /(。|!|?)/,
67
+ br: '<br />'
68
+ }.merge(options)
69
+
70
+ self.new text.gsub(options[:chars]){ |char| char << options[:br] }
71
+ end
72
+
73
+ def to_cnbr options = {}
74
+ self.class.to_cnbr self, options
57
75
  end
58
76
  end
@@ -0,0 +1,82 @@
1
+ # encoding: UTF-8
2
+ class TestStringBase < Test::Unit::TestCase
3
+ def test_class
4
+ assert_equal ZfbenExtend::String.new.class.to_s, 'ZfbenExtend::String'
5
+ assert_equal ZfbenExtend::String.to_html('').class.to_s, 'ZfbenExtend::String'
6
+ assert_equal ZfbenExtend::String.to_cnbr('').class.to_s, 'ZfbenExtend::String'
7
+ assert_equal ZfbenExtend::String.new.to_html.class.to_s, 'ZfbenExtend::String'
8
+ assert_equal ZfbenExtend::String.new.to_cnbr.class.to_s, 'ZfbenExtend::String'
9
+
10
+ assert_equal ZfbenExtend::String.new.to_s.class.to_s, 'String'
11
+ end
12
+ end
13
+
14
+ class TestToHtml < Test::Unit::TestCase
15
+ def test_link
16
+ assert_equal ZfbenExtend::String.to_html('http://link', link: false), 'http://link'
17
+ assert_equal ZfbenExtend::String.to_html('http://link'), '<a href="http://link">http://link</a>'
18
+ assert_equal ZfbenExtend::String.to_html('text http://link text'), 'text <a href="http://link">http://link</a> text'
19
+ 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>'
20
+
21
+ assert_equal ZfbenExtend::String.new('http://link').to_html(link: false), 'http://link'
22
+ assert_equal ZfbenExtend::String.new('http://link').to_html, '<a href="http://link">http://link</a>'
23
+ assert_equal ZfbenExtend::String.new('text http://link text').to_html, 'text <a href="http://link">http://link</a> text'
24
+ assert_equal ZfbenExtend::String.new('text http://link ftp://link/folder').to_html, 'text <a href="http://link">http://link</a> <a href="ftp://link/folder">ftp://link/folder</a>'
25
+ end
26
+
27
+ def test_tag
28
+ assert_equal ZfbenExtend::String.to_html('#a', tag: false), '#a'
29
+ assert_equal ZfbenExtend::String.to_html('#a'), '<a href="/tags/a">#a</a>'
30
+ assert_equal ZfbenExtend::String.to_html('text #a #b'), 'text <a href="/tags/a">#a</a> <a href="/tags/b">#b</a>'
31
+
32
+ assert_equal ZfbenExtend::String.new('#a').to_html(tag: false), '#a'
33
+ assert_equal ZfbenExtend::String.new('#a').to_html, '<a href="/tags/a">#a</a>'
34
+ assert_equal ZfbenExtend::String.new('text #a #b').to_html, 'text <a href="/tags/a">#a</a> <a href="/tags/b">#b</a>'
35
+ end
36
+
37
+ def test_mail
38
+ assert_equal ZfbenExtend::String.to_html('abc@abc.com', mail: false, user: false), 'abc@abc.com'
39
+ assert_equal ZfbenExtend::String.to_html('abc@abc.com'), '<a href="mailto:abc@abc.com">abc@abc.com</a>'
40
+ 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>'
41
+
42
+ assert_equal ZfbenExtend::String.new('abc@abc.com').to_html(mail: false, user: false), 'abc@abc.com'
43
+ assert_equal ZfbenExtend::String.new('abc@abc.com').to_html, '<a href="mailto:abc@abc.com">abc@abc.com</a>'
44
+ assert_equal ZfbenExtend::String.new('text abc@abc.com zhu.feng@zfben.com').to_html, 'text <a href="mailto:abc@abc.com">abc@abc.com</a> <a href="mailto:zhu.feng@zfben.com">zhu.feng@zfben.com</a>'
45
+ end
46
+
47
+ def test_user
48
+ assert_equal ZfbenExtend::String.to_html('@user', user: false), '@user'
49
+ assert_equal ZfbenExtend::String.to_html('@user'), '<a href="/users/user">@user</a>'
50
+
51
+ assert_equal ZfbenExtend::String.new('@user').to_html(user: false), '@user'
52
+ assert_equal ZfbenExtend::String.new('@user').to_html, '<a href="/users/user">@user</a>'
53
+ end
54
+
55
+ def test_all
56
+ assert_equal ZfbenExtend::String.to_html('text http://link #tag mail@mail.com @user text', link: false, tag: false, mail: false, user: false), 'text http://link #tag mail@mail.com @user text'
57
+ assert_equal ZfbenExtend::String.to_html('text http://link #tag mail@mail.com @user text', tag_url: '/tagurl/', user_url: '/userurl/'), 'text <a href="http://link">http://link</a> <a href="/tagurl/tag">#tag</a> <a href="mailto:mail@mail.com">mail@mail.com</a> <a href="/userurl/user">@user</a> text'
58
+
59
+ assert_equal ZfbenExtend::String.new('text http://link #tag mail@mail.com @user text').to_html(link: false, tag: false, mail: false, user: false), 'text http://link #tag mail@mail.com @user text'
60
+ assert_equal ZfbenExtend::String.new('text http://link #tag mail@mail.com @user text').to_html(tag_url: '/tagurl/', user_url: '/userurl/'), 'text <a href="http://link">http://link</a> <a href="/tagurl/tag">#tag</a> <a href="mailto:mail@mail.com">mail@mail.com</a> <a href="/userurl/user">@user</a> text'
61
+ end
62
+ end
63
+
64
+ class TestToCnBr < Test::Unit::TestCase
65
+ def test_base
66
+ assert_equal ZfbenExtend::String.to_cnbr('a。b'), 'a。<br />b'
67
+ assert_equal ZfbenExtend::String.to_cnbr('a。b!c?'), 'a。<br />b!<br />c?<br />'
68
+
69
+ assert_equal ZfbenExtend::String.new('a。b').to_cnbr, 'a。<br />b'
70
+ assert_equal ZfbenExtend::String.new('a。b!c?').to_cnbr, 'a。<br />b!<br />c?<br />'
71
+ end
72
+
73
+ def test_options
74
+ assert_equal ZfbenExtend::String.to_cnbr('a.b', chars: /\./), 'a.<br />b'
75
+ assert_equal ZfbenExtend::String.to_cnbr('a。b', br: '<hr />'), 'a。<hr />b'
76
+ assert_equal ZfbenExtend::String.to_cnbr('a.b', chars: /\./, br: '<hr />'), 'a.<hr />b'
77
+
78
+ assert_equal ZfbenExtend::String.new('a.b').to_cnbr(chars: /\./), 'a.<br />b'
79
+ assert_equal ZfbenExtend::String.new('a。b').to_cnbr(br: '<hr />'), 'a。<hr />b'
80
+ assert_equal ZfbenExtend::String.new('a.b').to_cnbr(chars: /\./, br: '<hr />'), 'a.<hr />b'
81
+ end
82
+ end
data/zfben_extend.gemspec CHANGED
@@ -3,7 +3,7 @@ $:.push File.expand_path('../lib', __FILE__)
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = 'zfben_extend'
6
- s.version = '0.0.1'
6
+ s.version = '0.0.2'
7
7
  s.authors = ['Ben']
8
8
  s.email = ['ben@zfben.com']
9
9
  s.homepage = 'https://github.com/benz303/zfben_extend'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zfben_extend
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-12-23 00:00:00.000000000 Z
12
+ date: 2012-01-27 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: ''
15
15
  email:
@@ -25,7 +25,7 @@ files:
25
25
  - Rakefile
26
26
  - lib/zfben_extend.rb
27
27
  - lib/zfben_extend/string.rb
28
- - test/zfben_extend_test.rb
28
+ - test/string_test.rb
29
29
  - zfben_extend.gemspec
30
30
  homepage: https://github.com/benz303/zfben_extend
31
31
  licenses: []
@@ -1,30 +0,0 @@
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