peppercorn 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.yardopts +1 -0
- data/CHANGELOG.md +5 -0
- data/Guardfile +1 -0
- data/{README → README.md} +2 -1
- data/lib/peppercorn/string.rb +2 -3
- data/lib/peppercorn/version.rb +1 -1
- data/lib/peppercorn/xml/node.rb +15 -9
- data/peppercorn.gemspec +3 -2
- data/spec/peppercorn/string_spec.rb +25 -0
- data/spec/peppercorn/xml/node_spec.rb +7 -3
- metadata +29 -16
data/.yardopts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
- README.md CHANGELOG.md
|
data/CHANGELOG.md
ADDED
data/Guardfile
CHANGED
data/{README → README.md}
RENAMED
data/lib/peppercorn/string.rb
CHANGED
@@ -8,7 +8,7 @@ module Peppercorn
|
|
8
8
|
# @since 0.0.1
|
9
9
|
# @param [Fixnum] length the number of word to truncate on
|
10
10
|
# @param [Hash] opts hash of truncation options
|
11
|
-
# @option opts [
|
11
|
+
# @option opts [#to_s, nil] :tail ("…") the object to append to the truncated string
|
12
12
|
# @return [String] the truncated string
|
13
13
|
def truncate(length=30, opts={})
|
14
14
|
opts = Peppercorn::DEFAULT_TRUNCATION_OPTIONS.merge(opts)
|
@@ -45,8 +45,7 @@ module Peppercorn
|
|
45
45
|
# @return [String] the truncated html
|
46
46
|
# @option opts [String, Nokogiri::XML::Node, nil] :tail ("…") string or xml node to append to the truncated string
|
47
47
|
def truncate_html(length=30, opts={})
|
48
|
-
|
49
|
-
return string
|
48
|
+
Nokogiri::HTML::DocumentFragment.parse(self).truncate(length, opts)
|
50
49
|
end
|
51
50
|
end
|
52
51
|
end
|
data/lib/peppercorn/version.rb
CHANGED
data/lib/peppercorn/xml/node.rb
CHANGED
@@ -42,21 +42,27 @@ module Peppercorn
|
|
42
42
|
end
|
43
43
|
|
44
44
|
if !opts[:tail].to_s.empty? and overran
|
45
|
-
|
46
|
-
loop do
|
47
|
-
if append_to.text? or append_to.description.inline? # We want to append to the last block-level element
|
48
|
-
append_to = append_to.parent
|
49
|
-
break;
|
50
|
-
end
|
51
|
-
append_to = append_to.children.last
|
52
|
-
end
|
53
|
-
append_to << Nokogiri::HTML::DocumentFragment.parse(opts[:tail])
|
45
|
+
target.appendable_node << Nokogiri::HTML::DocumentFragment.parse(opts[:tail])
|
54
46
|
end
|
55
47
|
|
56
48
|
target = target.inner_html unless opts[:return_node]
|
57
49
|
return opts[:return_hash] ? {:text => target, :count => [count, length].min, :overran => overran} : target
|
58
50
|
|
59
51
|
end
|
52
|
+
|
53
|
+
# Returns the last appendable child node (the last block-level element)
|
54
|
+
# return [Nokogiri::XML::Node]
|
55
|
+
# since 0.0.2
|
56
|
+
def appendable_node
|
57
|
+
current = children.last
|
58
|
+
loop do
|
59
|
+
if current.text? or current.description.inline?
|
60
|
+
return current.parent
|
61
|
+
end
|
62
|
+
current = current.children.last
|
63
|
+
end
|
64
|
+
return current
|
65
|
+
end
|
60
66
|
|
61
67
|
# Returns the last child node that is a text node
|
62
68
|
# @return [Nokogiri::XML::Node] the last text node
|
data/peppercorn.gemspec
CHANGED
@@ -7,7 +7,7 @@ Gem::Specification.new do |s|
|
|
7
7
|
s.version = Peppercorn::VERSION
|
8
8
|
s.authors = ["Wade West"]
|
9
9
|
s.email = ["wwest81@gmail.com"]
|
10
|
-
s.homepage = ""
|
10
|
+
s.homepage = "https://github.com/wadewest/peppercorn"
|
11
11
|
s.summary = %q{A simple gem to truncate HTML, with other features coming in the future.}
|
12
12
|
s.description = %q{A simple gem to truncate HTML, with other features coming in the future.}
|
13
13
|
|
@@ -23,7 +23,8 @@ Gem::Specification.new do |s|
|
|
23
23
|
s.add_development_dependency "rspec", "~> 2.7"
|
24
24
|
s.add_development_dependency "guard-rspec", "~> 0.5"
|
25
25
|
s.add_development_dependency "guard-yard", "~> 1.0"
|
26
|
+
s.add_development_dependency "rdiscount", "~> 1.6"
|
26
27
|
# s.add_runtime_dependency "rest-client"
|
27
28
|
# s.add_dependency "htmlentities"
|
28
|
-
s.add_dependency "nokogiri"
|
29
|
+
s.add_dependency "nokogiri", "~> 1.5"
|
29
30
|
end
|
@@ -6,6 +6,16 @@ describe String do
|
|
6
6
|
@string = "Hello World, I make HTML truncation easy."
|
7
7
|
end
|
8
8
|
|
9
|
+
it "should be able to strip whitespaces from the end of the string" do
|
10
|
+
" \t\t\t#{@string}\t \r\n".strip_end.should eql " \t\t\t#{@string}"
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should be able to strip HTML tags from the string" do
|
14
|
+
string = "The quick brown fox jumps over the lazy dog."
|
15
|
+
html = "<p class=\"example\">The <em>quick</em> brown <i>fox jumps</i> over the lazy dog.</p>"
|
16
|
+
html.strip_html.should eql string
|
17
|
+
end
|
18
|
+
|
9
19
|
it "should properly truncate a string" do
|
10
20
|
@string.truncate(5).should eql "Hello World, I make HTML…"
|
11
21
|
end
|
@@ -18,4 +28,19 @@ describe String do
|
|
18
28
|
"<em>#{@string}</em>".truncate_html(5).should eql "<em>Hello World, I make HTML</em>…"
|
19
29
|
end
|
20
30
|
|
31
|
+
it "should truncate HTML just like plain text when the HTML contains no tags" do
|
32
|
+
html = @string
|
33
|
+
html.truncate_html(3).should eql @string.truncate(3)
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should allow a link in the tail" do
|
37
|
+
opts = {:tail => "...<a href=\"some_page.html\">Read More</a>"}
|
38
|
+
@string.truncate(3, opts).should eql "Hello World, I#{opts[:tail]}"
|
39
|
+
end
|
40
|
+
|
41
|
+
it "should be able to use a Nokogiri::XML::Node as the tail" do
|
42
|
+
opts = {:tail => Nokogiri::HTML::DocumentFragment.parse("...<a href=\"some_page.html\">Read More</a>")}
|
43
|
+
@string.truncate(3, opts).should eql "Hello World, I#{opts[:tail].to_s}"
|
44
|
+
end
|
45
|
+
|
21
46
|
end
|
@@ -3,16 +3,20 @@ require 'spec_helper'
|
|
3
3
|
describe String do
|
4
4
|
|
5
5
|
before do
|
6
|
-
@string = "<p><em class
|
6
|
+
@string = "<p class=\"appendable\"><em class=\"strong\">Hello World</em>, I <i>can</i> make HTML truncation <i>easy.</i></p>"
|
7
7
|
@html = Nokogiri::HTML::DocumentFragment.parse @string
|
8
8
|
end
|
9
9
|
|
10
10
|
it "should properly truncate some simple HTML" do
|
11
|
-
@html.truncate(6).should eql "<p><em class=\"strong\">Hello World</em>, I <i>can</i> make HTML…</p>"
|
11
|
+
@html.truncate(6).should eql "<p class=\"appendable\"><em class=\"strong\">Hello World</em>, I <i>can</i> make HTML…</p>"
|
12
12
|
end
|
13
13
|
|
14
14
|
it "should not truncate some HTML that is shorter than length" do
|
15
|
-
@html.truncate(30).should eql @
|
15
|
+
@html.truncate(30).should eql @string
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should return the last appendable node" do
|
19
|
+
@html.appendable_node.attribute('class').to_s.should eql "appendable"
|
16
20
|
end
|
17
21
|
|
18
22
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: peppercorn
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-10-
|
12
|
+
date: 2011-10-28 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: yard
|
16
|
-
requirement: &
|
16
|
+
requirement: &70336758482100 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0.7'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70336758482100
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: rspec
|
27
|
-
requirement: &
|
27
|
+
requirement: &70336758481140 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ~>
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '2.7'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70336758481140
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: guard-rspec
|
38
|
-
requirement: &
|
38
|
+
requirement: &70336758479500 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ~>
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: '0.5'
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70336758479500
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: guard-yard
|
49
|
-
requirement: &
|
49
|
+
requirement: &70336758478160 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ~>
|
@@ -54,18 +54,29 @@ dependencies:
|
|
54
54
|
version: '1.0'
|
55
55
|
type: :development
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *70336758478160
|
58
|
+
- !ruby/object:Gem::Dependency
|
59
|
+
name: rdiscount
|
60
|
+
requirement: &70336758476840 !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ~>
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '1.6'
|
66
|
+
type: :development
|
67
|
+
prerelease: false
|
68
|
+
version_requirements: *70336758476840
|
58
69
|
- !ruby/object:Gem::Dependency
|
59
70
|
name: nokogiri
|
60
|
-
requirement: &
|
71
|
+
requirement: &70336758475660 !ruby/object:Gem::Requirement
|
61
72
|
none: false
|
62
73
|
requirements:
|
63
|
-
- -
|
74
|
+
- - ~>
|
64
75
|
- !ruby/object:Gem::Version
|
65
|
-
version: '
|
76
|
+
version: '1.5'
|
66
77
|
type: :runtime
|
67
78
|
prerelease: false
|
68
|
-
version_requirements: *
|
79
|
+
version_requirements: *70336758475660
|
69
80
|
description: A simple gem to truncate HTML, with other features coming in the future.
|
70
81
|
email:
|
71
82
|
- wwest81@gmail.com
|
@@ -74,10 +85,12 @@ extensions: []
|
|
74
85
|
extra_rdoc_files: []
|
75
86
|
files:
|
76
87
|
- .gitignore
|
88
|
+
- .yardopts
|
89
|
+
- CHANGELOG.md
|
77
90
|
- Gemfile
|
78
91
|
- Guardfile
|
79
92
|
- Procfile
|
80
|
-
- README
|
93
|
+
- README.md
|
81
94
|
- Rakefile
|
82
95
|
- lib/peppercorn.rb
|
83
96
|
- lib/peppercorn/string.rb
|
@@ -88,7 +101,7 @@ files:
|
|
88
101
|
- spec/peppercorn/string_spec.rb
|
89
102
|
- spec/peppercorn/xml/node_spec.rb
|
90
103
|
- spec/spec_helper.rb
|
91
|
-
homepage:
|
104
|
+
homepage: https://github.com/wadewest/peppercorn
|
92
105
|
licenses: []
|
93
106
|
post_install_message:
|
94
107
|
rdoc_options: []
|