trunction 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/trunction.rb +8 -8
- data/lib/trunction/version.rb +1 -1
- data/spec/trunction_spec.rb +19 -22
- metadata +3 -3
data/lib/trunction.rb
CHANGED
@@ -6,22 +6,22 @@ module Trunction
|
|
6
6
|
extend self
|
7
7
|
|
8
8
|
def truncate_html(html, max)
|
9
|
-
|
10
|
-
Truncation.new(doc, max).execute
|
11
|
-
doc.to_html
|
9
|
+
Truncation.new(html, max).execute
|
12
10
|
end
|
13
11
|
|
14
12
|
class Truncation
|
15
13
|
|
16
|
-
def initialize(
|
17
|
-
@doc =
|
14
|
+
def initialize(html, max)
|
15
|
+
@doc = Nokogiri::HTML::DocumentFragment.parse(html)
|
18
16
|
@max = max
|
19
|
-
@
|
17
|
+
@word_count = 0
|
20
18
|
end
|
21
19
|
|
22
20
|
def execute
|
23
21
|
find_the_last_block
|
22
|
+
return "" if @last_block_element.nil?
|
24
23
|
remove_everything_after(@last_block_element)
|
24
|
+
@doc.to_html
|
25
25
|
end
|
26
26
|
|
27
27
|
private
|
@@ -39,8 +39,8 @@ module Trunction
|
|
39
39
|
end
|
40
40
|
|
41
41
|
def accumulate_text(text_node)
|
42
|
-
@
|
43
|
-
throw(:done) if @
|
42
|
+
@word_count += text_node.text.split.length
|
43
|
+
throw(:done) if @word_count >= @max
|
44
44
|
end
|
45
45
|
|
46
46
|
def block?(node)
|
data/lib/trunction/version.rb
CHANGED
data/spec/trunction_spec.rb
CHANGED
@@ -1,25 +1,28 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
1
3
|
require 'trunction'
|
2
4
|
|
3
5
|
describe Trunction do
|
4
6
|
|
5
|
-
let(:
|
7
|
+
let(:full_text) { Nokogiri::HTML::DocumentFragment.parse(input).text }
|
8
|
+
let(:total_words) { full_text.split.length }
|
6
9
|
let(:ellipsis) { "…" }
|
7
10
|
|
8
11
|
include Trunction
|
9
12
|
|
10
13
|
let(:result) do
|
11
|
-
truncate_html(input,
|
14
|
+
truncate_html(input, max_words).gsub("\n", '')
|
12
15
|
end
|
13
16
|
|
14
17
|
describe "#truncate_html" do
|
15
18
|
|
16
19
|
context "given multiple paragraphs" do
|
17
20
|
|
18
|
-
let(:input) { "<p>
|
21
|
+
let(:input) { "<p>One two three.</p> <p>Four five six.</p>" }
|
19
22
|
|
20
23
|
context "with max-length longer than input" do
|
21
24
|
|
22
|
-
let(:
|
25
|
+
let(:max_words) { total_words + 1 }
|
23
26
|
|
24
27
|
it "returns input intact" do
|
25
28
|
result.should == input
|
@@ -29,42 +32,36 @@ describe Trunction do
|
|
29
32
|
|
30
33
|
context "with max-length shorter than input" do
|
31
34
|
|
32
|
-
let(:
|
35
|
+
let(:max_words) { total_words - 1 }
|
33
36
|
|
34
|
-
it "drops elements until beneath max-length" do
|
35
|
-
result.should == "<p>
|
37
|
+
it "drops block elements until beneath max-length" do
|
38
|
+
result.should == "<p>One two three.</p>"
|
36
39
|
end
|
37
40
|
|
38
41
|
end
|
39
42
|
|
40
|
-
|
41
|
-
|
42
|
-
context "given multiple paragraphs, wrapped in a div" do
|
43
|
+
context "with max-length shorter than first paragraph" do
|
43
44
|
|
44
|
-
|
45
|
+
let(:max_words) { 1 }
|
45
46
|
|
46
|
-
|
47
|
-
|
48
|
-
let(:max_length) { total_length - 1 }
|
49
|
-
|
50
|
-
it "drops elements until beneath max-length" do
|
51
|
-
result.should == "<div><p>one</p><p>two</p></div>"
|
47
|
+
it "returns empty String" do
|
48
|
+
result.should == ""
|
52
49
|
end
|
53
50
|
|
54
51
|
end
|
55
52
|
|
56
53
|
end
|
57
54
|
|
58
|
-
context "
|
55
|
+
context "given multiple paragraphs, wrapped in a div" do
|
59
56
|
|
60
|
-
let(:input) { "<
|
57
|
+
let(:input) { "<div><p>One two three.</p> <p>Four five six.</p></div>" }
|
61
58
|
|
62
59
|
context "with max-length shorter than input" do
|
63
60
|
|
64
|
-
let(:
|
61
|
+
let(:max_words) { total_words - 1 }
|
65
62
|
|
66
|
-
it "drops
|
67
|
-
result.should == "<
|
63
|
+
it "drops elements until beneath max-length" do
|
64
|
+
result.should == "<div><p>One two three.</p></div>"
|
68
65
|
end
|
69
66
|
|
70
67
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: trunction
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -13,7 +13,7 @@ date: 2011-12-05 00:00:00.000000000 Z
|
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: nokogiri
|
16
|
-
requirement: &
|
16
|
+
requirement: &70116915083800 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,7 +21,7 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70116915083800
|
25
25
|
description: Trunction implements intelligent truncation of HTML text.
|
26
26
|
email:
|
27
27
|
- mdub@dogbiscuit.org
|