From da1ff6e29b25b01fae819d4c4ab4739bdb7c0acc Mon Sep 17 00:00:00 2001 From: Dominik Filip Date: Thu, 1 Jun 2017 11:55:52 +0200 Subject: [PATCH] allow to specify at which break token occurance to truncate --- README.md | 10 ++++++++++ lib/truncate_html/configuration.rb | 2 +- lib/truncate_html/html_truncator.rb | 20 ++++++++++++-------- spec/truncate_html/html_truncator_spec.rb | 12 ++++++++++++ 4 files changed, 35 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index fd32252..7ef33b0 100644 --- a/README.md +++ b/README.md @@ -65,6 +65,16 @@ TruncateHtml.configure do |config| config.break_token = '' end ``` + +You can specify at which `:break_token` occurance to truncate the HTML. By default it is +the very first occurance of `:break_token`. When there is no `:break_token` then +`:break_token_at_count` has no effect. + +```ruby +TruncateHtml.configure do |config| + config.break_token = '

' + config.break_token_at_count = 3 +``` Installation ------------ diff --git a/lib/truncate_html/configuration.rb b/lib/truncate_html/configuration.rb index 8027e5b..56e549a 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_token_at_count end class << self diff --git a/lib/truncate_html/html_truncator.rb b/lib/truncate_html/html_truncator.rb index 52b707f..ed09bf8 100644 --- a/lib/truncate_html/html_truncator.rb +++ b/lib/truncate_html/html_truncator.rb @@ -1,20 +1,24 @@ module TruncateHtml class HtmlTruncator - def initialize(original_html, options = {}) - @original_html = original_html - length = options[:length] || TruncateHtml.configuration.length - @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 - @chars_remaining = length - @omission.length + @original_html = original_html + length = options[:length] || TruncateHtml.configuration.length + @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_token_at_count = options[:break_token_at_count] || TruncateHtml.configuration.break_token_at_count || 1 + @chars_remaining = length - @omission.length @open_tags, @closing_tags, @truncated_html = [], [], [''] end def truncate return @omission if @chars_remaining < 0 + + break_token_counter = 0 @original_html.html_tokens.each do |token| - if @chars_remaining <= 0 || truncate_token?(token) + break_token_counter += 1 if truncate_token?(token) + + if @chars_remaining <= 0 || @break_token_at_count == break_token_counter close_open_tags break else diff --git a/spec/truncate_html/html_truncator_spec.rb b/spec/truncate_html/html_truncator_spec.rb index 0da14d6..36e7eec 100644 --- a/spec/truncate_html/html_truncator_spec.rb +++ b/spec/truncate_html/html_truncator_spec.rb @@ -198,6 +198,18 @@ def truncate(html, opts = {}) end end + context 'when break_token_at_count not set for break_token' do + it 'truncates at first break token' do + truncate('This is line one. This is line two.', break_token: '').should == 'This is line one. This is line' + end + end + + context 'when break_token_at_count is set for break_token' do + it 'truncates at specified break_token occurance' do + truncate('This is line one. This is line two. This is line three.', break_token: '', break_token_at_count: 3).should == 'This is line one. This is line two. This is line' + end + end + context 'a string with comments' do it 'does not duplicate comments (issue #32)' do truncate('

hello and goodbye

', length: 15).should ==