truncate_html 0.1.1 → 0.1.2
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 +1 -0
- data/README.markdown +4 -5
- data/VERSION +1 -1
- data/lib/truncate_html/html_truncator.rb +1 -3
- data/spec/helpers/truncate_html_helper_spec.rb +14 -7
- data/spec/truncate_html/html_truncator_spec.rb +31 -32
- data/truncate_html.gemspec +4 -3
- metadata +3 -2
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
pkg
|
data/README.markdown
CHANGED
@@ -29,14 +29,13 @@ Installation
|
|
29
29
|
#### As a gem
|
30
30
|
Add this to your <code>config/environment.rb</code>:
|
31
31
|
|
32
|
-
config.gem '
|
33
|
-
:source => 'http://
|
34
|
-
:lib => 'truncate_html'
|
32
|
+
config.gem 'truncate_html',
|
33
|
+
:source => 'http://gemcutter.org'
|
35
34
|
|
36
35
|
Then either
|
37
|
-
<code>sudo gem install hgimenez-truncate_html</code>
|
38
|
-
or
|
39
36
|
<code>sudo rake gems:install</code>
|
37
|
+
or
|
38
|
+
<code>sudo gem install truncate_html</code>
|
40
39
|
|
41
40
|
#### As a plugin:
|
42
41
|
<code>script/plugin install git://github.com/hgimenez/truncate_html.git</code>
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.2
|
@@ -8,6 +8,7 @@ module TruncateHtml
|
|
8
8
|
end
|
9
9
|
|
10
10
|
def truncate(options = {})
|
11
|
+
return '' if @original_html.nil?
|
11
12
|
options[:length] ||= 100
|
12
13
|
options[:omission] ||= '...'
|
13
14
|
@chars_remaining = options[:length]
|
@@ -36,9 +37,6 @@ module TruncateHtml
|
|
36
37
|
result.join('')
|
37
38
|
end
|
38
39
|
|
39
|
-
|
40
|
-
private #############################
|
41
|
-
|
42
40
|
def html_tokens
|
43
41
|
@original_html.scan(/<\/?[^>]+>|[\w\|`~!@#\$%^&*\(\)\-_\+=\[\]{}:;'",\.\/?]+|\s+/).map do
|
44
42
|
|t| t.gsub(
|
@@ -4,18 +4,25 @@ include TruncateHtmlHelper
|
|
4
4
|
|
5
5
|
describe TruncateHtmlHelper do
|
6
6
|
|
7
|
-
it '
|
7
|
+
it 'is included in ActionView::Base' do
|
8
8
|
ActionView::Base.included_modules.should include(TruncateHtmlHelper)
|
9
9
|
end
|
10
10
|
|
11
11
|
describe '#truncate_html' do
|
12
12
|
|
13
|
+
context 'the input html is nil' do
|
14
|
+
it 'returns an empty string' do
|
15
|
+
truncate_html(nil).should be_empty
|
16
|
+
truncate_html(nil).should be_kind_of String
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
13
20
|
context 'truncating in the middle of a link' do
|
14
21
|
before(:each) do
|
15
22
|
@html = '<div><ul><li>Look at <a href="foo">this</a> link </li></ul></div>'
|
16
23
|
end
|
17
24
|
|
18
|
-
it '
|
25
|
+
it 'truncates, and closes the <a>, and closes any remaining open tags' do
|
19
26
|
truncate_html(@html, :length => 10).should == '<div><ul><li>Look at <a href="foo">this...</a></li></ul></div>'
|
20
27
|
end
|
21
28
|
end
|
@@ -25,7 +32,7 @@ describe TruncateHtmlHelper do
|
|
25
32
|
before(:each) do
|
26
33
|
@html = "<p>Look at <strong>this</strong>#{char} More words here</p>"
|
27
34
|
end
|
28
|
-
it '
|
35
|
+
it 'places the punctuation after the tag without any whitespace' do
|
29
36
|
truncate_html(@html, :length => 16).should == "<p>Look at <strong>this</strong>#{char} More...</p>"
|
30
37
|
end
|
31
38
|
end
|
@@ -35,7 +42,7 @@ describe TruncateHtmlHelper do
|
|
35
42
|
before(:each) do
|
36
43
|
@html = '<p>Look at <a href="awesomeful.net">this</a> link for randomness</p>'
|
37
44
|
end
|
38
|
-
it '
|
45
|
+
it 'leaves a whitespace between the closing tag and the following word character' do
|
39
46
|
truncate_html(@html, :length => 17).should == '<p>Look at <a href="awesomeful.net">this</a> link...</p>'
|
40
47
|
end
|
41
48
|
end
|
@@ -51,7 +58,7 @@ describe TruncateHtmlHelper do
|
|
51
58
|
END_HTML
|
52
59
|
end
|
53
60
|
|
54
|
-
it '
|
61
|
+
it 'recognizes the multiline html properly' do
|
55
62
|
truncate_html(@html, :length => 8).should == ' <div id="foo" class="bar"> This is...</div>'
|
56
63
|
end
|
57
64
|
end
|
@@ -64,7 +71,7 @@ describe TruncateHtmlHelper do
|
|
64
71
|
@html = "<div>Some before. <#{unpaired_tag}>and some after</div>"
|
65
72
|
@html_caps = "<div>Some before. <#{unpaired_tag.capitalize}>and some after</div>"
|
66
73
|
end
|
67
|
-
it "
|
74
|
+
it "does not close the #{unpaired_tag} tag" do
|
68
75
|
truncate_html(@html, :length => 15).should == "<div>Some before. <#{unpaired_tag}>and...</div>"
|
69
76
|
truncate_html(@html_caps, :length => 15).should == "<div>Some before. <#{unpaired_tag.capitalize}>and...</div>"
|
70
77
|
end
|
@@ -75,7 +82,7 @@ describe TruncateHtmlHelper do
|
|
75
82
|
@html = "<div>Some before. <#{unpaired_tag} />and some after</div>"
|
76
83
|
@html_caps = "<div>Some before. <#{unpaired_tag.capitalize} />and some after</div>"
|
77
84
|
end
|
78
|
-
it "
|
85
|
+
it "does not close the #{unpaired_tag} tag" do
|
79
86
|
truncate_html(@html, :length => 15).should == "<div>Some before. <#{unpaired_tag} />and...</div>"
|
80
87
|
truncate_html(@html_caps, :length => 15).should == "<div>Some before. <#{unpaired_tag.capitalize} />and...</div>"
|
81
88
|
end
|
@@ -2,69 +2,68 @@ require File.join(File.dirname(__FILE__), '..', 'spec_helper')
|
|
2
2
|
|
3
3
|
describe TruncateHtml::HtmlTruncator do
|
4
4
|
|
5
|
+
def truncator(html = nil)
|
6
|
+
@truncator ||= TruncateHtml::HtmlTruncator.new(html)
|
7
|
+
end
|
8
|
+
|
5
9
|
describe '#html_tokens' do
|
6
10
|
before(:each) do
|
7
11
|
@html = '<h1>Hi there</h1> <p>This is sweet!</p>'
|
8
|
-
@truncator = TruncateHtml::HtmlTruncator.new @html
|
9
12
|
end
|
10
13
|
|
11
|
-
it '
|
12
|
-
|
14
|
+
it 'returns each token in the string as an array element' do
|
15
|
+
truncator(@html).html_tokens.should == ['<h1>', 'Hi', ' ', 'there', '</h1>', ' ', '<p>', 'This', ' ', 'is', ' ', 'sweet!', '</p>']
|
13
16
|
end
|
14
17
|
end
|
15
18
|
|
16
19
|
describe '#html_tag?' do
|
17
20
|
|
18
|
-
|
19
|
-
|
20
|
-
end
|
21
|
-
|
22
|
-
it 'should be false when the string parameter is not an html tag' do
|
23
|
-
@truncator.send(:html_tag?, 'no tags').should be_false
|
21
|
+
it 'returns false when the string parameter is not an html tag' do
|
22
|
+
truncator.html_tag?('no tags').should be_false
|
24
23
|
end
|
25
24
|
|
26
|
-
it '
|
27
|
-
|
28
|
-
|
25
|
+
it 'returns true when the string parameter is an html tag' do
|
26
|
+
truncator.html_tag?('<img src="foo">').should be_true
|
27
|
+
truncator.html_tag?('</img>').should be_true
|
29
28
|
end
|
30
29
|
|
31
30
|
end
|
32
31
|
|
33
32
|
describe '#open_tag?' do
|
34
33
|
|
35
|
-
|
36
|
-
|
34
|
+
it 'returns true if the tag is an open tag' do
|
35
|
+
truncator.open_tag?('<a>').should be_true
|
37
36
|
end
|
38
37
|
|
39
|
-
it '
|
40
|
-
|
38
|
+
it 'returns true if the tag is an open tag, and has whitespace and html properties with either single or double quotes' do
|
39
|
+
truncator.open_tag?(' <a href="http://awesomeful.net">').should be_true
|
40
|
+
truncator.open_tag?(" <a href='http://awesomeful.net' >").should be_true
|
41
41
|
end
|
42
42
|
|
43
|
-
it '
|
44
|
-
|
45
|
-
@truncator.send(:open_tag?, " <a href='http://awesomeful.net' >").should be_true
|
43
|
+
it 'returns false if the tag is a close tag' do
|
44
|
+
truncator.open_tag?('</a>').should be_false
|
46
45
|
end
|
47
46
|
|
48
|
-
it '
|
49
|
-
|
50
|
-
end
|
51
|
-
|
52
|
-
it 'should be false if the string is not an html tag' do
|
53
|
-
@truncator.send(:open_tag?, 'foo bar').should be_false
|
47
|
+
it 'returns false if the string is not an html tag' do
|
48
|
+
truncator.open_tag?('foo bar').should be_false
|
54
49
|
end
|
55
50
|
end
|
56
51
|
|
57
52
|
describe '#matching_close_tag' do
|
58
53
|
|
59
|
-
|
60
|
-
|
54
|
+
it 'closes a tag given an open tag' do
|
55
|
+
truncator.matching_close_tag('<a>').should == '</a>'
|
56
|
+
truncator.matching_close_tag(' <div>').should == '</div>'
|
57
|
+
truncator.matching_close_tag('<h1>').should == '</h1>'
|
58
|
+
truncator.matching_close_tag('<a href="foo">').should == '</a>'
|
61
59
|
end
|
60
|
+
end
|
61
|
+
|
62
|
+
describe 'nil string' do
|
62
63
|
|
63
|
-
it '
|
64
|
-
|
65
|
-
|
66
|
-
@truncator.send(:matching_close_tag, '<h1>').should == '</h1>'
|
67
|
-
@truncator.send(:matching_close_tag, '<a href="foo">').should == '</a>'
|
64
|
+
it 'returns an empty string' do
|
65
|
+
truncator(nil).truncate.should be_empty
|
66
|
+
truncator(nil).truncate.should be_kind_of String
|
68
67
|
end
|
69
68
|
end
|
70
69
|
|
data/truncate_html.gemspec
CHANGED
@@ -2,18 +2,19 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = %q{truncate_html}
|
5
|
-
s.version = "0.1.
|
5
|
+
s.version = "0.1.2"
|
6
6
|
|
7
7
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
8
8
|
s.authors = ["hgimenez"]
|
9
|
-
s.date = %q{2009-
|
9
|
+
s.date = %q{2009-09-25}
|
10
10
|
s.description = %q{Truncates html so you don't have to}
|
11
11
|
s.email = %q{harold.gimenez@gmail.com}
|
12
12
|
s.extra_rdoc_files = [
|
13
13
|
"README.markdown"
|
14
14
|
]
|
15
15
|
s.files = [
|
16
|
-
"
|
16
|
+
".gitignore",
|
17
|
+
"README.markdown",
|
17
18
|
"Rakefile",
|
18
19
|
"VERSION",
|
19
20
|
"init.rb",
|
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.1.
|
4
|
+
version: 0.1.2
|
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-09-25 00:00:00 -04:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -22,6 +22,7 @@ extensions: []
|
|
22
22
|
extra_rdoc_files:
|
23
23
|
- README.markdown
|
24
24
|
files:
|
25
|
+
- .gitignore
|
25
26
|
- README.markdown
|
26
27
|
- Rakefile
|
27
28
|
- VERSION
|