diff --git a/lib/truncate_html.rb b/lib/truncate_html.rb index 95b7923..dfefc72 100644 --- a/lib/truncate_html.rb +++ b/lib/truncate_html.rb @@ -8,6 +8,7 @@ config.length = 100 config.omission = '...' config.word_boundary = /\S/ + config.break_tokens = [] end diff --git a/lib/truncate_html/configuration.rb b/lib/truncate_html/configuration.rb index 8027e5b..f4cd337 100644 --- a/lib/truncate_html/configuration.rb +++ b/lib/truncate_html/configuration.rb @@ -1,6 +1,6 @@ module TruncateHtml class Configuration - attr_accessor :length, :omission, :word_boundary, :break_token + attr_accessor :length, :omission, :word_boundary, :break_token, :break_tokens end class << self diff --git a/lib/truncate_html/html_truncator.rb b/lib/truncate_html/html_truncator.rb index 036f312..27343af 100644 --- a/lib/truncate_html/html_truncator.rb +++ b/lib/truncate_html/html_truncator.rb @@ -7,11 +7,20 @@ def initialize(original_html, options = {}) @omission = options[:omission] || TruncateHtml.configuration.omission @word_boundary = (options.has_key?(:word_boundary) ? options[:word_boundary] : TruncateHtml.configuration.word_boundary) @break_token = options[:break_token] || TruncateHtml.configuration.break_token || nil + @break_tokens = options[:break_tokens] || TruncateHtml.configuration.break_tokens || [] + @break_tokens << @break_token @chars_remaining = length - @omission.length @open_tags, @truncated_html = [], [''] end def truncate + unless @break_token.nil? + ActiveSupport::Deprecation.warn( + "You try to use attribute break_token, this attribute is deprecated. " \ + "Please use break_tokens." + ) + end + return @omission if @chars_remaining < 0 @original_html.html_tokens.each do |token| if @chars_remaining <= 0 || truncate_token?(token) @@ -85,7 +94,7 @@ def remove_latest_open_tag(close_tag) end def truncate_token?(token) - @break_token and token == @break_token + @break_tokens.include?(token) end end end diff --git a/spec/truncate_html/html_truncator_spec.rb b/spec/truncate_html/html_truncator_spec.rb index 2258f42..c315630 100644 --- a/spec/truncate_html/html_truncator_spec.rb +++ b/spec/truncate_html/html_truncator_spec.rb @@ -146,51 +146,51 @@ def truncate(html, opts = {}) result.should include "

“我现在使用的是中文的拼音。”
" end - context 'when the break_token option is set as ' do - it 'does not truncate abnormally if the break_token is not present' do - truncate('This is line one. This is line two.', :length => 30, :break_token => '').should == 'This is line one. This is...' + context 'when the break_tokens option is set as ' do + it 'does not truncate abnormally if the break_tokens is not present' do + truncate('This is line one. This is line two.', :length => 30, :break_tokens => ['']).should == 'This is line one. This is...' end - it 'does not truncate abnormally if the break_token is present, but beyond the length param' do - truncate('This is line one. This is line two.', :length => 30, :break_token => '').should == 'This is line one. This is...' + it 'does not truncate abnormally if the break_tokens is present, but beyond the length param' do + truncate('This is line one. This is line two.', :length => 30, :break_tokens => ['']).should == 'This is line one. This is...' end - it 'truncates before the length param if the break_token is before the token at "length"' do - truncate('This is line one. This is line two.', :length => 30, :break_token => '').should == 'This is line one.' + it 'truncates before the length param if the break_tokens is before the token at "length"' do + truncate('This is line one. This is line two.', :length => 30, :break_tokens => ['']).should == 'This is line one.' end end - context 'when the break_token option is customized as a comment' do - it 'does not truncate abnormally if the break_token is not present' do - truncate('This is line one. This is line two.', :length => 30, :break_token => '').should == 'This is line one. This is...' + context 'when the break_tokens option is customized as a comment' do + it 'does not truncate abnormally if the break_tokens is not present' do + truncate('This is line one. This is line two.', :length => 30, :break_tokens => ['']).should == 'This is line one. This is...' end - it 'does not truncate abnormally if the break_token is present, but beyond the length param' do - truncate('This is line one. This is line two.', :length => 30, :break_token => '').should == 'This is line one. This is...' + it 'does not truncate abnormally if the break_tokens is present, but beyond the length param' do + truncate('This is line one. This is line two.', :length => 30, :break_tokens => ['']).should == 'This is line one. This is...' end - it 'truncates before the length param if the break_token is before the token at "length"' do - truncate('This is line one. This is line two.', :length => 30, :break_token => '').should == 'This is line one.' + it 'truncates before the length param if the break_tokens is before the token at "length"' do + truncate('This is line one. This is line two.', :length => 30, :break_tokens => ['']).should == 'This is line one.' end end - context 'when the break_token option is customized as an html tag' do - it 'does not truncate abnormally if the break_token is not present' do - truncate('This is line one. This is line two.', :length => 30, :break_token => '').should == 'This is line one. This is...' + context 'when the break_tokens option is customized as an html tag' do + it 'does not truncate abnormally if the break_tokens is not present' do + truncate('This is line one. This is line two.', :length => 30, :break_tokens => ['']).should == 'This is line one. This is...' end - it 'does not truncate abnormally if the break_token is present, but beyond the length param' do - truncate('This is line one. This is line two.', :length => 30, :break_token => '').should == 'This is line one. This is...' + it 'does not truncate abnormally if the break_tokens is present, but beyond the length param' do + truncate('This is line one. This is line two.', :length => 30, :break_tokens => ['']).should == 'This is line one. This is...' end - it 'truncates before the length param if the break_token is before the token at "length"' do - truncate('This is line one. This is line two.', :length => 30, :break_token => '').should == 'This is line one.' + it 'truncates before the length param if the break_tokens is before the token at "length"' do + truncate('This is line one. This is line two.', :length => 30, :break_tokens => ['']).should == 'This is line one.' end end - context 'when the break_token option is customized as a word' do - it 'does not truncate abnormally if the break_token is not present' do - truncate('This is line one. This is line two.', :length => 30, :break_token => 'foobar').should == 'This is line one. This is...' + context 'when the break_tokens option is customized as a word' do + it 'does not truncate abnormally if the break_tokens is not present' do + truncate('This is line one. This is line two.', :length => 30, :break_tokens => ['foobar']).should == 'This is line one. This is...' end - it 'does not truncate abnormally if the break_token is present, but beyond the length param' do - truncate('This is line one. This is line foobar two.', :length => 30, :break_token => 'foobar').should == 'This is line one. This is...' + it 'does not truncate abnormally if the break_tokens is present, but beyond the length param' do + truncate('This is line one. This is line foobar two.', :length => 30, :break_tokens => ['foobar']).should == 'This is line one. This is...' end - it 'truncates before the length param if the break_token is before the token at "length"' do - truncate('This is line one. foobar This is line two.', :length => 30, :break_token => 'foobar').should == 'This is line one.' + it 'truncates before the length param if the break_tokens is before the token at "length"' do + truncate('This is line one. foobar This is line two.', :length => 30, :break_tokens => ['foobar']).should == 'This is line one.' end end @@ -200,4 +200,4 @@ def truncate(html, opts = {}) '

hello and ...

' end end -end +end \ No newline at end of file