truncate_html 0.5.4 → 0.5.5
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +0 -1
- data/Gemfile.lock +16 -16
- data/lib/truncate_html/html_string.rb +12 -11
- data/lib/truncate_html/html_truncator.rb +5 -2
- data/lib/truncate_html/version.rb +1 -1
- data/spec/truncate_html/html_truncator_spec.rb +8 -3
- data/truncate_html.gemspec +1 -1
- metadata +9 -9
data/.travis.yml
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
truncate_html (0.5.
|
4
|
+
truncate_html (0.5.5)
|
5
5
|
|
6
6
|
GEM
|
7
7
|
remote: http://rubygems.org/
|
@@ -35,7 +35,7 @@ GEM
|
|
35
35
|
activesupport (3.0.3)
|
36
36
|
arel (2.0.7)
|
37
37
|
builder (2.1.2)
|
38
|
-
diff-lcs (1.1.
|
38
|
+
diff-lcs (1.1.3)
|
39
39
|
erubis (2.6.6)
|
40
40
|
abstract (>= 1.0.0)
|
41
41
|
i18n (0.5.0)
|
@@ -65,19 +65,19 @@ GEM
|
|
65
65
|
rake (>= 0.8.7)
|
66
66
|
thor (~> 0.14.4)
|
67
67
|
rake (0.8.7)
|
68
|
-
rspec (2.
|
69
|
-
rspec-core (~> 2.
|
70
|
-
rspec-expectations (~> 2.
|
71
|
-
rspec-mocks (~> 2.
|
72
|
-
rspec-core (2.
|
73
|
-
rspec-expectations (2.
|
74
|
-
diff-lcs (~> 1.1.
|
75
|
-
rspec-mocks (2.
|
76
|
-
rspec-rails (2.
|
77
|
-
actionpack (
|
78
|
-
activesupport (
|
79
|
-
railties (
|
80
|
-
rspec (~> 2.
|
68
|
+
rspec (2.9.0)
|
69
|
+
rspec-core (~> 2.9.0)
|
70
|
+
rspec-expectations (~> 2.9.0)
|
71
|
+
rspec-mocks (~> 2.9.0)
|
72
|
+
rspec-core (2.9.0)
|
73
|
+
rspec-expectations (2.9.1)
|
74
|
+
diff-lcs (~> 1.1.3)
|
75
|
+
rspec-mocks (2.9.0)
|
76
|
+
rspec-rails (2.9.0)
|
77
|
+
actionpack (>= 3.0)
|
78
|
+
activesupport (>= 3.0)
|
79
|
+
railties (>= 3.0)
|
80
|
+
rspec (~> 2.9.0)
|
81
81
|
thor (0.14.6)
|
82
82
|
treetop (1.4.9)
|
83
83
|
polyglot (>= 0.3.1)
|
@@ -88,5 +88,5 @@ PLATFORMS
|
|
88
88
|
|
89
89
|
DEPENDENCIES
|
90
90
|
rails (~> 3.0.3)
|
91
|
-
rspec-rails (~> 2.
|
91
|
+
rspec-rails (~> 2.9)
|
92
92
|
truncate_html!
|
@@ -9,23 +9,23 @@ module TruncateHtml
|
|
9
9
|
end
|
10
10
|
|
11
11
|
def html_tokens
|
12
|
-
scan(regex).map do
|
13
|
-
|
14
|
-
|
15
|
-
/\n/,''
|
16
|
-
|
17
|
-
|
18
|
-
|
12
|
+
scan(regex).map do |token|
|
13
|
+
HtmlString.new(
|
14
|
+
token.gsub(
|
15
|
+
/\n/,'' #remove newline characters
|
16
|
+
).gsub(
|
17
|
+
/\s+/, ' ' #clean out extra consecutive whitespace
|
18
|
+
)
|
19
19
|
)
|
20
|
-
end
|
20
|
+
end
|
21
21
|
end
|
22
22
|
|
23
23
|
def html_tag?
|
24
|
-
|
24
|
+
/<\/?[^>]+>/ === self
|
25
25
|
end
|
26
26
|
|
27
27
|
def open_tag?
|
28
|
-
|
28
|
+
/<(?!(?:#{UNPAIRED_TAGS.join('|')}|script|\/))[^>]+>/i === self
|
29
29
|
end
|
30
30
|
|
31
31
|
def matching_close_tag
|
@@ -34,7 +34,8 @@ module TruncateHtml
|
|
34
34
|
|
35
35
|
private
|
36
36
|
def regex
|
37
|
-
|
37
|
+
punctuation = if RUBY_VERSION < '1.9' then "\\p\\{P\\}" else "[[:punct:]]" end
|
38
|
+
/(?:<script.*>.*<\/script>)+|<\/?[^>]+>|[#{"[[:alpha:]]" if RUBY_VERSION >= '1.9'}\w\|`~!@#\$%^&*\(\)\-_\+=\[\]{}:;'",\.\/?]+|\s+|#{punctuation}/
|
38
39
|
end
|
39
40
|
|
40
41
|
end
|
@@ -12,6 +12,7 @@ module TruncateHtml
|
|
12
12
|
@chars_remaining = length - @omission.length
|
13
13
|
@open_tags, @truncated_html = [], ['']
|
14
14
|
|
15
|
+
return @omission if @chars_remaining < 0
|
15
16
|
@original_html.html_tokens.each do |token|
|
16
17
|
#if truncate_more?(token)
|
17
18
|
if @chars_remaining > 0
|
@@ -36,6 +37,9 @@ module TruncateHtml
|
|
36
37
|
end
|
37
38
|
else
|
38
39
|
@chars_remaining -= (@word_boundary ? token.length : token[0, @chars_remaining].length)
|
40
|
+
if @chars_remaining <= 0
|
41
|
+
@truncated_html[-1] = @truncated_html[-1].rstrip + @omission
|
42
|
+
end
|
39
43
|
end
|
40
44
|
end
|
41
45
|
|
@@ -43,14 +47,13 @@ module TruncateHtml
|
|
43
47
|
if token.html_tag?
|
44
48
|
@truncated_html << token
|
45
49
|
elsif @word_boundary
|
46
|
-
@truncated_html << token if @chars_remaining - token.length >= 0
|
50
|
+
@truncated_html << token if (@chars_remaining - token.length) >= 0
|
47
51
|
else
|
48
52
|
@truncated_html << token[0, @chars_remaining]
|
49
53
|
end
|
50
54
|
end
|
51
55
|
|
52
56
|
def close_open_tags
|
53
|
-
@truncated_html[-1] = @truncated_html[-1].rstrip + @omission
|
54
57
|
@open_tags.reverse_each do |open_tag|
|
55
58
|
@truncated_html << open_tag.matching_close_tag
|
56
59
|
end
|
@@ -15,7 +15,7 @@ describe TruncateHtml::HtmlTruncator do
|
|
15
15
|
|
16
16
|
it 'retains the tags within the text' do
|
17
17
|
html = 'some text <span class="caps">CAPS</span> some text'
|
18
|
-
truncate(html, :length => 25, :word_boundary => false).should == 'some text <span class="caps">CAPS</span> some te'
|
18
|
+
truncate(html, :length => 25, :word_boundary => false).should == 'some text <span class="caps">CAPS</span> some te...'
|
19
19
|
end
|
20
20
|
end
|
21
21
|
|
@@ -23,6 +23,11 @@ describe TruncateHtml::HtmlTruncator do
|
|
23
23
|
truncate('a b c', :length => 4, :omission => '...').should == 'a...'
|
24
24
|
end
|
25
25
|
|
26
|
+
it "includes omission even on the edge (issue #18)" do
|
27
|
+
opts = { :word_boundary => false, :length => 12 }
|
28
|
+
truncate('One two three', opts).should == 'One two t...'
|
29
|
+
end
|
30
|
+
|
26
31
|
it "never returns a string longer than :length" do
|
27
32
|
truncate("test this shit", :length => 10).should == 'test...'
|
28
33
|
end
|
@@ -115,8 +120,8 @@ describe TruncateHtml::HtmlTruncator do
|
|
115
120
|
“我现在使用的是中文的拼音。”<br>
|
116
121
|
测试一下具体的truncate</em>html功能。</p>"
|
117
122
|
|
118
|
-
result = truncate(html, omission
|
119
|
-
result.should
|
123
|
+
result = truncate(html, :omission => "", :length => 50)
|
124
|
+
result.should include "<p>“我现在使用的是中文的拼音。”<br>"
|
120
125
|
end
|
121
126
|
|
122
127
|
end
|
data/truncate_html.gemspec
CHANGED
@@ -16,6 +16,6 @@ Gem::Specification.new do |s|
|
|
16
16
|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
17
17
|
s.require_paths = ["lib"]
|
18
18
|
|
19
|
-
s.add_development_dependency "rspec-rails", "~> 2.
|
19
|
+
s.add_development_dependency "rspec-rails", "~> 2.9"
|
20
20
|
s.add_development_dependency "rails", "~> 3.0.3"
|
21
21
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: truncate_html
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.5
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,22 +9,22 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-04-24 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec-rails
|
16
|
-
requirement: &
|
16
|
+
requirement: &70108244979240 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
20
20
|
- !ruby/object:Gem::Version
|
21
|
-
version: '2.
|
21
|
+
version: '2.9'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70108244979240
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: rails
|
27
|
-
requirement: &
|
27
|
+
requirement: &70108244978720 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ~>
|
@@ -32,7 +32,7 @@ dependencies:
|
|
32
32
|
version: 3.0.3
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70108244978720
|
36
36
|
description: Truncates html so you don't have to
|
37
37
|
email:
|
38
38
|
- harold.gimenez@gmail.com
|
@@ -99,7 +99,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
99
99
|
version: '0'
|
100
100
|
segments:
|
101
101
|
- 0
|
102
|
-
hash:
|
102
|
+
hash: -2387288071087328503
|
103
103
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
104
104
|
none: false
|
105
105
|
requirements:
|
@@ -108,7 +108,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
108
108
|
version: '0'
|
109
109
|
segments:
|
110
110
|
- 0
|
111
|
-
hash:
|
111
|
+
hash: -2387288071087328503
|
112
112
|
requirements: []
|
113
113
|
rubyforge_project:
|
114
114
|
rubygems_version: 1.8.10
|