trunction 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/trunction/version.rb +1 -1
- data/lib/trunction.rb +12 -23
- data/spec/trunction_spec.rb +0 -16
- metadata +3 -3
data/lib/trunction/version.rb
CHANGED
data/lib/trunction.rb
CHANGED
@@ -16,49 +16,38 @@ module Trunction
|
|
16
16
|
def initialize(doc, max)
|
17
17
|
@doc = doc
|
18
18
|
@max = max
|
19
|
-
@
|
19
|
+
@size = 0
|
20
20
|
end
|
21
21
|
|
22
22
|
def execute
|
23
|
-
|
24
|
-
|
23
|
+
find_the_last_block
|
24
|
+
remove_everything_after(@last_block_element)
|
25
25
|
end
|
26
26
|
|
27
27
|
private
|
28
28
|
|
29
|
-
def
|
29
|
+
def find_the_last_block
|
30
30
|
catch(:done) do
|
31
31
|
@doc.traverse do |node|
|
32
|
-
|
33
|
-
|
32
|
+
if node.text?
|
33
|
+
accumulate_text(node)
|
34
|
+
elsif block?(node)
|
35
|
+
@last_block_element = node
|
36
|
+
end
|
34
37
|
end
|
35
38
|
end
|
36
39
|
end
|
37
40
|
|
38
41
|
def accumulate_text(text_node)
|
39
|
-
@
|
40
|
-
throw(:done) if
|
41
|
-
end
|
42
|
-
|
43
|
-
def limit_reached?
|
44
|
-
@chars_remaining <= 0
|
45
|
-
end
|
46
|
-
|
47
|
-
def record(node)
|
48
|
-
@last_block_element = node if block?(node)
|
49
|
-
@last_element = node
|
50
|
-
end
|
51
|
-
|
52
|
-
def last_node
|
53
|
-
@last_block_element || @last_element
|
42
|
+
@size += text_node.text.length
|
43
|
+
throw(:done) if @size >= @max
|
54
44
|
end
|
55
45
|
|
56
46
|
def block?(node)
|
57
47
|
node.description && node.description.block?
|
58
48
|
end
|
59
49
|
|
60
|
-
def
|
61
|
-
node = last_node
|
50
|
+
def remove_everything_after(node)
|
62
51
|
while node
|
63
52
|
node.next.remove while node.next
|
64
53
|
break unless node.respond_to?(:parent)
|
data/spec/trunction_spec.rb
CHANGED
@@ -55,22 +55,6 @@ describe Trunction do
|
|
55
55
|
|
56
56
|
end
|
57
57
|
|
58
|
-
context "a single paragraph with inline elements" do
|
59
|
-
|
60
|
-
let(:input) { "<p><b>one</b>, <b>two</b>, <b>three</b>, <b>four</b></p>" }
|
61
|
-
|
62
|
-
context "with max-length shorter than input" do
|
63
|
-
|
64
|
-
let(:max_length) { total_length - 1 }
|
65
|
-
|
66
|
-
it "drops the last word" do
|
67
|
-
result.should == "<p><b>one</b>, <b>two</b>, <b>three</b>, </p>"
|
68
|
-
end
|
69
|
-
|
70
|
-
end
|
71
|
-
|
72
|
-
end
|
73
|
-
|
74
58
|
context "a final paragraph with inline elements" do
|
75
59
|
|
76
60
|
let(:input) { "<p>one</p><p>two</p><p><b>one</b>, <b>two</b>, <b>three</b>, <b>four</b></p>" }
|
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.2
|
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: &70242606069220 !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: *70242606069220
|
25
25
|
description: Trunction implements intelligent truncation of HTML text.
|
26
26
|
email:
|
27
27
|
- mdub@dogbiscuit.org
|