trunction 0.0.2 → 0.0.3

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.
@@ -6,22 +6,22 @@ module Trunction
6
6
  extend self
7
7
 
8
8
  def truncate_html(html, max)
9
- doc = Nokogiri::HTML::DocumentFragment.parse(html)
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(doc, max)
17
- @doc = doc
14
+ def initialize(html, max)
15
+ @doc = Nokogiri::HTML::DocumentFragment.parse(html)
18
16
  @max = max
19
- @size = 0
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
- @size += text_node.text.length
43
- throw(:done) if @size >= @max
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)
@@ -1,3 +1,3 @@
1
1
  module Trunction
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -1,25 +1,28 @@
1
+ # encoding: utf-8
2
+
1
3
  require 'trunction'
2
4
 
3
5
  describe Trunction do
4
6
 
5
- let(:total_length) { Nokogiri::HTML::DocumentFragment.parse(input).text.length }
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, max_length).gsub("\n", '')
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>one</p><p>two</p><p>three</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(:max_length) { total_length + 1 }
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(:max_length) { total_length - 1 }
35
+ let(:max_words) { total_words - 1 }
33
36
 
34
- it "drops elements until beneath max-length" do
35
- result.should == "<p>one</p><p>two</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
- end
41
-
42
- context "given multiple paragraphs, wrapped in a div" do
43
+ context "with max-length shorter than first paragraph" do
43
44
 
44
- let(:input) { "<div><p>one</p><p>two</p><p>three</p></div>" }
45
+ let(:max_words) { 1 }
45
46
 
46
- context "with max-length shorter than input" do
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 "a final paragraph with inline elements" do
55
+ context "given multiple paragraphs, wrapped in a div" do
59
56
 
60
- let(:input) { "<p>one</p><p>two</p><p><b>one</b>, <b>two</b>, <b>three</b>, <b>four</b></p>" }
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(:max_length) { total_length - 1 }
61
+ let(:max_words) { total_words - 1 }
65
62
 
66
- it "drops the entire final paragraph" do
67
- result.should == "<p>one</p><p>two</p>"
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.2
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: &70242606069220 !ruby/object:Gem::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: *70242606069220
24
+ version_requirements: *70116915083800
25
25
  description: Trunction implements intelligent truncation of HTML text.
26
26
  email:
27
27
  - mdub@dogbiscuit.org