From 8903a198bef81388656353e4f7b069076d14ae4f Mon Sep 17 00:00:00 2001 From: Maksim Baev Date: Sat, 12 Jan 2019 15:04:24 +0400 Subject: [PATCH 1/5] issue #46: New lines should be interpreted (save all special symbols from source) --- lib/truncate_html/html_string.rb | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/truncate_html/html_string.rb b/lib/truncate_html/html_string.rb index 76d82e9..35fb481 100644 --- a/lib/truncate_html/html_string.rb +++ b/lib/truncate_html/html_string.rb @@ -13,9 +13,7 @@ def html_tokens scan(REGEX).map do |token| HtmlString.new( token.gsub( - /\n/,' ' #replace newline characters with a whitespace - ).gsub( - /\s+/, ' ' #clean out extra consecutive whitespace + / +/, ' ' # Clean out extra consecutive whitespace ) ) end From f233d5a3a24649dce15c9f731dbf51130952108d Mon Sep 17 00:00:00 2001 From: Maksim Baev Date: Sat, 12 Jan 2019 18:49:23 +0400 Subject: [PATCH 2/5] issue #46: New lines should be interpreted --- 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 52b707f..d01be86 100644 --- a/lib/truncate_html/html_truncator.rb +++ b/lib/truncate_html/html_truncator.rb @@ -38,7 +38,7 @@ def word_boundary def build_output out = @truncated_html.join if word_boundary - term_regexp = Regexp.new("^.*#{word_boundary.source}") + term_regexp = Regexp.new("^.*#{word_boundary.source}", Regexp::MULTILINE) match = out.match(term_regexp) if match && match[0] != out out = match[0] + @closing_tags.join From 93bc77a273553bc4db65dc99232fc04638079db6 Mon Sep 17 00:00:00 2001 From: Maksim Baev Date: Sat, 12 Jan 2019 20:00:15 +0400 Subject: [PATCH 3/5] issue #46: New lines should be interpreted --- lib/truncate_html/html_string.rb | 2 +- spec/truncate_html/html_truncator_spec.rb | 11 ++++++++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/truncate_html/html_string.rb b/lib/truncate_html/html_string.rb index 35fb481..8b04e89 100644 --- a/lib/truncate_html/html_string.rb +++ b/lib/truncate_html/html_string.rb @@ -32,7 +32,7 @@ def html_comment? end def matching_close_tag - gsub(/<(\w+)\s?.*>/, '').strip + gsub(/<(\w+)\s?.*>/m, '').strip end end diff --git a/spec/truncate_html/html_truncator_spec.rb b/spec/truncate_html/html_truncator_spec.rb index 0da14d6..31342b8 100644 --- a/spec/truncate_html/html_truncator_spec.rb +++ b/spec/truncate_html/html_truncator_spec.rb @@ -101,15 +101,20 @@ def truncate(html, opts = {}) truncate(html, :length => html.length).should == html end - #unusual, but just covering my ass + # Unusual, but just covering my ass it 'recognizes the multiline html properly' do html = <<-END_HTML
-This is ugly html. +This + is + ugly + html.
END_HTML - truncate(html, :length => 12).should == '
This is...
' + truncate(html, :length => 12).should == '
+This + is...
' end %w(br hr img).each do |unpaired_tag| From aed08c392e98883314bfde88bcf7bb64279f74f4 Mon Sep 17 00:00:00 2001 From: Maksim Baev Date: Sat, 12 Jan 2019 20:29:17 +0400 Subject: [PATCH 4/5] issue #46: New lines should be interpreted --- spec/truncate_html/html_truncator_spec.rb | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/spec/truncate_html/html_truncator_spec.rb b/spec/truncate_html/html_truncator_spec.rb index 31342b8..e1837d1 100644 --- a/spec/truncate_html/html_truncator_spec.rb +++ b/spec/truncate_html/html_truncator_spec.rb @@ -104,15 +104,14 @@ def truncate(html, opts = {}) # Unusual, but just covering my ass it 'recognizes the multiline html properly' do html = <<-END_HTML -
+
This is ugly html.
- END_HTML - truncate(html, :length => 12).should == '
+END_HTML + truncate(html, :length => 12).should == '
This is...
' end From f9abe717a9a759f9b82d22706eef25e2cb6f4071 Mon Sep 17 00:00:00 2001 From: Maksim Baev Date: Sat, 12 Jan 2019 20:33:42 +0400 Subject: [PATCH 5/5] issue #46: New lines should be interpreted --- spec/truncate_html/html_truncator_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/truncate_html/html_truncator_spec.rb b/spec/truncate_html/html_truncator_spec.rb index e1837d1..aa3d039 100644 --- a/spec/truncate_html/html_truncator_spec.rb +++ b/spec/truncate_html/html_truncator_spec.rb @@ -111,7 +111,7 @@ def truncate(html, opts = {}) html.
END_HTML - truncate(html, :length => 12).should == '
+ truncate(html, :length => 12).should == '
This is...
' end