truncate_html 0.1.2 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/Rakefile +1 -0
- data/VERSION +1 -1
- data/lib/truncate_html/html_truncator.rb +4 -3
- data/spec/helpers/truncate_html_helper_spec.rb +14 -10
- data/spec/truncate_html/html_truncator_spec.rb +4 -3
- data/truncate_html.gemspec +5 -2
- metadata +2 -2
data/.gitignore
CHANGED
data/Rakefile
CHANGED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.2.0
|
@@ -11,14 +11,14 @@ module TruncateHtml
|
|
11
11
|
return '' if @original_html.nil?
|
12
12
|
options[:length] ||= 100
|
13
13
|
options[:omission] ||= '...'
|
14
|
-
@chars_remaining = options[:length]
|
14
|
+
@chars_remaining = options[:length] - options[:omission].length
|
15
15
|
@open_tags, result = [], []
|
16
16
|
|
17
17
|
html_tokens.each do |str|
|
18
18
|
if @chars_remaining > 0
|
19
19
|
if html_tag?(str)
|
20
20
|
if open_tag?(str)
|
21
|
-
@open_tags << str
|
21
|
+
@open_tags << str
|
22
22
|
else
|
23
23
|
open_tags = remove_latest_open_tag(str)
|
24
24
|
end
|
@@ -27,7 +27,8 @@ module TruncateHtml
|
|
27
27
|
end
|
28
28
|
result << str
|
29
29
|
else
|
30
|
-
result[-1] += options[:omission]
|
30
|
+
#result[-1] += options[:omission]
|
31
|
+
result[-1] = result[-1].rstrip + options[:omission]
|
31
32
|
@open_tags.reverse_each do |open_tag|
|
32
33
|
result << matching_close_tag(open_tag)
|
33
34
|
end
|
@@ -10,10 +10,14 @@ describe TruncateHtmlHelper do
|
|
10
10
|
|
11
11
|
describe '#truncate_html' do
|
12
12
|
|
13
|
+
it "includes the omission text's length in the returned truncated html" do
|
14
|
+
truncate_html('a b c', :length => 4, :omission => '...').should == 'a...'
|
15
|
+
end
|
16
|
+
|
13
17
|
context 'the input html is nil' do
|
14
18
|
it 'returns an empty string' do
|
15
19
|
truncate_html(nil).should be_empty
|
16
|
-
truncate_html(nil).should be_kind_of
|
20
|
+
truncate_html(nil).should be_kind_of(String)
|
17
21
|
end
|
18
22
|
end
|
19
23
|
|
@@ -23,7 +27,7 @@ describe TruncateHtmlHelper do
|
|
23
27
|
end
|
24
28
|
|
25
29
|
it 'truncates, and closes the <a>, and closes any remaining open tags' do
|
26
|
-
truncate_html(@html, :length =>
|
30
|
+
truncate_html(@html, :length => 14).should == '<div><ul><li>Look at <a href="foo">this...</a></li></ul></div>'
|
27
31
|
end
|
28
32
|
end
|
29
33
|
|
@@ -33,7 +37,7 @@ describe TruncateHtmlHelper do
|
|
33
37
|
@html = "<p>Look at <strong>this</strong>#{char} More words here</p>"
|
34
38
|
end
|
35
39
|
it 'places the punctuation after the tag without any whitespace' do
|
36
|
-
truncate_html(@html, :length =>
|
40
|
+
truncate_html(@html, :length => 19).should == "<p>Look at <strong>this</strong>#{char} More...</p>"
|
37
41
|
end
|
38
42
|
end
|
39
43
|
end
|
@@ -43,7 +47,7 @@ describe TruncateHtmlHelper do
|
|
43
47
|
@html = '<p>Look at <a href="awesomeful.net">this</a> link for randomness</p>'
|
44
48
|
end
|
45
49
|
it 'leaves a whitespace between the closing tag and the following word character' do
|
46
|
-
truncate_html(@html, :length =>
|
50
|
+
truncate_html(@html, :length => 21).should == '<p>Look at <a href="awesomeful.net">this</a> link...</p>'
|
47
51
|
end
|
48
52
|
end
|
49
53
|
|
@@ -59,7 +63,7 @@ describe TruncateHtmlHelper do
|
|
59
63
|
end
|
60
64
|
|
61
65
|
it 'recognizes the multiline html properly' do
|
62
|
-
truncate_html(@html, :length =>
|
66
|
+
truncate_html(@html, :length => 12).should == ' <div id="foo" class="bar"> This is...</div>'
|
63
67
|
end
|
64
68
|
end
|
65
69
|
|
@@ -72,19 +76,19 @@ describe TruncateHtmlHelper do
|
|
72
76
|
@html_caps = "<div>Some before. <#{unpaired_tag.capitalize}>and some after</div>"
|
73
77
|
end
|
74
78
|
it "does not close the #{unpaired_tag} tag" do
|
75
|
-
truncate_html(@html, :length =>
|
76
|
-
truncate_html(@html_caps, :length =>
|
79
|
+
truncate_html(@html, :length => 19).should == "<div>Some before. <#{unpaired_tag}>and...</div>"
|
80
|
+
truncate_html(@html_caps, :length => 19).should == "<div>Some before. <#{unpaired_tag.capitalize}>and...</div>"
|
77
81
|
end
|
78
82
|
end
|
79
83
|
|
80
|
-
context "and the #{unpaired_tag} does
|
84
|
+
context "and the #{unpaired_tag} does have the closing slash" do
|
81
85
|
before(:each) do
|
82
86
|
@html = "<div>Some before. <#{unpaired_tag} />and some after</div>"
|
83
87
|
@html_caps = "<div>Some before. <#{unpaired_tag.capitalize} />and some after</div>"
|
84
88
|
end
|
85
89
|
it "does not close the #{unpaired_tag} tag" do
|
86
|
-
truncate_html(@html, :length =>
|
87
|
-
truncate_html(@html_caps, :length =>
|
90
|
+
truncate_html(@html, :length => 19).should == "<div>Some before. <#{unpaired_tag} />and...</div>"
|
91
|
+
truncate_html(@html_caps, :length => 19).should == "<div>Some before. <#{unpaired_tag.capitalize} />and...</div>"
|
88
92
|
end
|
89
93
|
end
|
90
94
|
|
@@ -8,12 +8,13 @@ describe TruncateHtml::HtmlTruncator do
|
|
8
8
|
|
9
9
|
describe '#html_tokens' do
|
10
10
|
before(:each) do
|
11
|
-
@html = '<h1>Hi there</h1> <p>This
|
11
|
+
@html = '<h1>Hi there</h1> <p>This is sweet!</p>'
|
12
12
|
end
|
13
13
|
|
14
|
-
it 'returns each token in the string as an array element' do
|
14
|
+
it 'returns each token in the string as an array element removing any consecutive whitespace from the string' do
|
15
15
|
truncator(@html).html_tokens.should == ['<h1>', 'Hi', ' ', 'there', '</h1>', ' ', '<p>', 'This', ' ', 'is', ' ', 'sweet!', '</p>']
|
16
16
|
end
|
17
|
+
|
17
18
|
end
|
18
19
|
|
19
20
|
describe '#html_tag?' do
|
@@ -63,7 +64,7 @@ describe TruncateHtml::HtmlTruncator do
|
|
63
64
|
|
64
65
|
it 'returns an empty string' do
|
65
66
|
truncator(nil).truncate.should be_empty
|
66
|
-
truncator(nil).truncate.should be_kind_of
|
67
|
+
truncator(nil).truncate.should be_kind_of(String)
|
67
68
|
end
|
68
69
|
end
|
69
70
|
|
data/truncate_html.gemspec
CHANGED
@@ -1,12 +1,15 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
|
1
4
|
# -*- encoding: utf-8 -*-
|
2
5
|
|
3
6
|
Gem::Specification.new do |s|
|
4
7
|
s.name = %q{truncate_html}
|
5
|
-
s.version = "0.
|
8
|
+
s.version = "0.2.0"
|
6
9
|
|
7
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
8
11
|
s.authors = ["hgimenez"]
|
9
|
-
s.date = %q{2009-
|
12
|
+
s.date = %q{2009-11-23}
|
10
13
|
s.description = %q{Truncates html so you don't have to}
|
11
14
|
s.email = %q{harold.gimenez@gmail.com}
|
12
15
|
s.extra_rdoc_files = [
|
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.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- hgimenez
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-
|
12
|
+
date: 2009-11-23 00:00:00 -05:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|