From 7421e298622c435a7a17e4d660adb70afbb7dad2 Mon Sep 17 00:00:00 2001 From: Kirill Zaitsev Date: Tue, 17 Jun 2014 18:27:08 +0400 Subject: [PATCH 1/2] Use many break tokens. Create new break_tokens attribute, break_token is deprecated. Signed-off-by: Kirill Zaitsev --- lib/truncate_html.rb | 1 + lib/truncate_html/configuration.rb | 2 +- lib/truncate_html/html_truncator.rb | 11 ++++- spec/truncate_html/html_truncator_spec.rb | 58 +++++++++++------------ 4 files changed, 41 insertions(+), 31 deletions(-) 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..6814847 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_tokens @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 From 842c6c9d68dde9dcdaa88b370cf73e8ebfe7d396 Mon Sep 17 00:00:00 2001 From: Bugagazavr Date: Wed, 23 Jul 2014 21:11:38 +0400 Subject: [PATCH 2/2] Typo --- lib/truncate_html/html_truncator.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/truncate_html/html_truncator.rb b/lib/truncate_html/html_truncator.rb index 6814847..27343af 100644 --- a/lib/truncate_html/html_truncator.rb +++ b/lib/truncate_html/html_truncator.rb @@ -8,7 +8,7 @@ def initialize(original_html, options = {}) @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_tokens + @break_tokens << @break_token @chars_remaining = length - @omission.length @open_tags, @truncated_html = [], [''] end