truncate_html 0.9 → 0.9.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- truncate_html (0.9)
4
+ truncate_html (0.9.1)
5
5
 
6
6
  GEM
7
7
  remote: http://rubygems.org/
@@ -27,9 +27,9 @@ A few notes:
27
27
  * `<script>` tags will pass right through - they will not count toward the resulting string's length, or be truncated.
28
28
 
29
29
  * The default options are:
30
- * :length => 100
31
- * :omission => '...'
32
- * :word_boundary => true
30
+ * `:length`: 100
31
+ * `:omission`: '...'
32
+ * `:word_boundary`: /\S/
33
33
 
34
34
  You may also set global configuration options.
35
35
  For example, place the following on application boot,
@@ -39,8 +39,6 @@ something like `config/initializers/truncate_html.rb`
39
39
  TruncateHtml.configure do |config|
40
40
  config.length = 50
41
41
  config.omission = '...(continued)'
42
- config.word_boundary = false
43
- config.break_token = '<!-- break -->'
44
42
  end
45
43
  ```
46
44
 
@@ -49,8 +47,6 @@ For example, to truncate at the end of the nearest sentence:
49
47
 
50
48
  ```ruby
51
49
  TruncateHtml.configure do |config|
52
- config.length = 50
53
- config.omission = ''
54
50
  config.word_boundary = /\S[\.\?\!]/
55
51
  end
56
52
  ```
@@ -65,7 +61,7 @@ If the `:break_token` is in your content after the specified :length,
65
61
 
66
62
  ```ruby
67
63
  TruncateHtml.configure do |config|
68
- config.break_token = '<!-- truncate -->
64
+ config.break_token = '<!-- truncate -->'
69
65
  end
70
66
  ```
71
67
  Installation
@@ -3,7 +3,7 @@ module TruncateHtmlHelper
3
3
  def truncate_html(html, options={})
4
4
  return '' if html.nil?
5
5
  html_string = TruncateHtml::HtmlString.new(html)
6
- TruncateHtml::HtmlTruncator.new(html_string).truncate(options).html_safe
6
+ TruncateHtml::HtmlTruncator.new(html_string, options).truncate.html_safe
7
7
  end
8
8
 
9
9
  end
@@ -1,18 +1,17 @@
1
1
  module TruncateHtml
2
2
  class HtmlTruncator
3
3
 
4
- def initialize(original_html)
5
- @original_html = original_html
6
- end
7
-
8
- def truncate(options = {})
4
+ def initialize(original_html, options = {})
5
+ @original_html = original_html
9
6
  length = options[:length] || TruncateHtml.configuration.length
10
7
  @omission = options[:omission] || TruncateHtml.configuration.omission
11
8
  @word_boundary = (options.has_key?(:word_boundary) ? options[:word_boundary] : TruncateHtml.configuration.word_boundary)
12
9
  @break_token = options[:break_token] || TruncateHtml.configuration.break_token || nil
13
10
  @chars_remaining = length - @omission.length
14
11
  @open_tags, @truncated_html = [], ['']
12
+ end
15
13
 
14
+ def truncate
16
15
  return @omission if @chars_remaining < 0
17
16
  @original_html.html_tokens.each do |token|
18
17
  if @chars_remaining <= 0 || truncate_token?(token)
@@ -25,8 +24,8 @@ module TruncateHtml
25
24
 
26
25
  out = @truncated_html.join
27
26
 
28
- if @word_boundary
29
- term_regexp = Regexp.new("^.*#{@word_boundary.source}")
27
+ if word_boundary
28
+ term_regexp = Regexp.new("^.*#{word_boundary.source}")
30
29
  match = out.match(term_regexp)
31
30
  match ? match[0] : out
32
31
  else
@@ -36,6 +35,14 @@ module TruncateHtml
36
35
 
37
36
  private
38
37
 
38
+ def word_boundary
39
+ if @word_boundary == true
40
+ TruncateHtml.configuration.word_boundary
41
+ else
42
+ @word_boundary
43
+ end
44
+ end
45
+
39
46
  def process_token(token)
40
47
  append_to_result(token)
41
48
  if token.html_tag?
@@ -1,3 +1,3 @@
1
1
  module TruncateHtml
2
- VERSION = "0.9"
2
+ VERSION = "0.9.1"
3
3
  end
@@ -17,20 +17,20 @@ describe TruncateHtmlHelper do
17
17
  end
18
18
 
19
19
  before(:each) do
20
- @html_truncator_mock = mock(TruncateHtml::HtmlTruncator)
21
20
  @original_html = '<p>foo</p>'
22
21
  @original_html.stub!(:html_safe).and_return(@original_html)
23
22
  end
24
23
 
25
- it 'creates an instance of HtmlTruncator and calls truncate() on it' do
26
- @html_truncator_mock.stub!(:truncate).and_return(@original_html)
27
- TruncateHtml::HtmlTruncator.should_receive(:new).and_return(@html_truncator_mock)
24
+ it 'creates an instance of HtmlTruncator and calls truncate on it' do
25
+ truncator = double(truncate: @original_html)
26
+ TruncateHtml::HtmlTruncator.should_receive(:new).and_return(truncator)
28
27
  truncator.truncate_html(@original_html)
29
28
  end
30
29
 
31
- it 'calls truncate() on the HtmlTruncator object' do
32
- TruncateHtml::HtmlTruncator.stub!(:new).and_return(@html_truncator_mock)
33
- @html_truncator_mock.should_receive(:truncate).with({}).once.and_return(@original_html)
30
+ it 'calls truncate on the HtmlTruncator object' do
31
+ truncator = double(truncate: @original_html)
32
+ TruncateHtml::HtmlTruncator.stub!(:new).and_return(truncator)
33
+ truncator.should_receive(:truncate).and_return(@original_html)
34
34
  truncator.truncate_html('foo')
35
35
  end
36
36
 
@@ -5,7 +5,7 @@ describe TruncateHtml::HtmlTruncator do
5
5
 
6
6
  def truncate(html, opts = {})
7
7
  html_string = TruncateHtml::HtmlString.new(html)
8
- TruncateHtml::HtmlTruncator.new(html_string).truncate(opts)
8
+ TruncateHtml::HtmlTruncator.new(html_string, opts).truncate
9
9
  end
10
10
 
11
11
  context 'when the word_boundary option is set to false' do
@@ -20,18 +20,24 @@ describe TruncateHtml::HtmlTruncator do
20
20
 
21
21
  context 'and a custom omission value is passed' do
22
22
  it 'retains the omission text' do
23
- truncate_html("testtest", :length => 10, :omission => '..', :word_boundary => false).should == 'testtest..'
23
+ truncate("testtest", :length => 10, :omission => '..', :word_boundary => false).should == 'testtest..'
24
24
  end
25
25
 
26
26
  it 'handles multibyte characters' do
27
- truncate_html("prüfenprüfen", :length => 8, :omission => '..', :word_boundary => false). should == 'prüfen..'
27
+ truncate("prüfenprüfen", :length => 8, :omission => '..', :word_boundary => false). should == 'prüfen..'
28
28
  end
29
29
  end
30
30
  end
31
31
 
32
+ context 'when the word_boundary option is set to true' do
33
+ it 'truncates using the default word_boundary option' do
34
+ truncate('hello there. or maybe not?', :length => 16, :omission => '', :word_boundary => true).should == 'hello there. or'
35
+ end
36
+ end
37
+
32
38
  context 'when the word_boundary option is a custom value (for splitting on sentences)' do
33
39
  it 'truncates to the end of the nearest sentence' do
34
- truncate_html('hello there. or maybe not?', :length => 16, :omission => '', :word_boundary => /\S[\.\?\!]/).should == 'hello there.'
40
+ truncate('hello there. or maybe not?', :length => 16, :omission => '', :word_boundary => /\S[\.\?\!]/).should == 'hello there.'
35
41
  end
36
42
  end
37
43
 
@@ -16,6 +16,8 @@ Gem::Specification.new do |s|
16
16
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
17
17
  s.require_paths = ["lib"]
18
18
 
19
+ s.required_ruby_version = '>= 1.9'
20
+
19
21
  s.add_development_dependency "rspec-rails", "~> 2.9"
20
22
  s.add_development_dependency "rails", "~> 3.0.3"
21
23
  end
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.9'
4
+ version: 0.9.1
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: 2013-01-03 00:00:00.000000000 Z
12
+ date: 2013-01-05 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec-rails
16
- requirement: &70117859351640 !ruby/object:Gem::Requirement
16
+ requirement: &70114780358240 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '2.9'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70117859351640
24
+ version_requirements: *70114780358240
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rails
27
- requirement: &70117859350560 !ruby/object:Gem::Requirement
27
+ requirement: &70114780357540 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,7 +32,7 @@ dependencies:
32
32
  version: 3.0.3
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70117859350560
35
+ version_requirements: *70114780357540
36
36
  description: Truncates html so you don't have to
37
37
  email:
38
38
  - harold.gimenez@gmail.com
@@ -46,7 +46,7 @@ files:
46
46
  - Gemfile.lock
47
47
  - History.txt
48
48
  - LICENSE
49
- - README.markdown
49
+ - README.md
50
50
  - Rakefile
51
51
  - VERSION
52
52
  - init.rb
@@ -96,10 +96,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
96
96
  requirements:
97
97
  - - ! '>='
98
98
  - !ruby/object:Gem::Version
99
- version: '0'
100
- segments:
101
- - 0
102
- hash: 2344297215831564369
99
+ version: '1.9'
103
100
  required_rubygems_version: !ruby/object:Gem::Requirement
104
101
  none: false
105
102
  requirements:
@@ -108,7 +105,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
108
105
  version: '0'
109
106
  segments:
110
107
  - 0
111
- hash: 2344297215831564369
108
+ hash: -1260152275599267568
112
109
  requirements: []
113
110
  rubyforge_project:
114
111
  rubygems_version: 1.8.10