diff --git a/lib/truncate_html/html_truncator.rb b/lib/truncate_html/html_truncator.rb
index 52b707f..50f2cb1 100644
--- a/lib/truncate_html/html_truncator.rb
+++ b/lib/truncate_html/html_truncator.rb
@@ -1,18 +1,20 @@
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)
+ @word_boundary = (options.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
- @open_tags, @closing_tags, @truncated_html = [], [], ['']
+ @open_tags = []
+ @closing_tags = []
+ @truncated_html = ['']
end
def truncate
return @omission if @chars_remaining < 0
+
@original_html.html_tokens.each do |token|
if @chars_remaining <= 0 || truncate_token?(token)
close_open_tags
@@ -40,9 +42,7 @@ def build_output
if word_boundary
term_regexp = Regexp.new("^.*#{word_boundary.source}")
match = out.match(term_regexp)
- if match && match[0] != out
- out = match[0] + @closing_tags.join
- end
+ out = match[0] + @closing_tags.join if match && match[0] != out
end
out
@@ -82,7 +82,7 @@ def close_open_tags
end
def remove_latest_open_tag(close_tag)
- (0...@open_tags.length).to_a.reverse.each do |index|
+ (0...@open_tags.length).to_a.reverse_each do |index|
if @open_tags[index].matching_close_tag == close_tag
@open_tags.delete_at(index)
break
@@ -91,7 +91,7 @@ def remove_latest_open_tag(close_tag)
end
def truncate_token?(token)
- @break_token and token == @break_token
+ @break_token && (token == @break_token)
end
end
end
diff --git a/spec/helpers/truncate_html_helper_spec.rb b/spec/helpers/truncate_html_helper_spec.rb
index a438122..06b81ec 100644
--- a/spec/helpers/truncate_html_helper_spec.rb
+++ b/spec/helpers/truncate_html_helper_spec.rb
@@ -7,7 +7,6 @@ class Truncator
end
describe TruncateHtmlHelper do
-
def truncator
@truncator ||= Truncator.new
end
@@ -40,5 +39,4 @@ def truncator
expect(truncator.truncate_html(nil)).to be_kind_of(String)
end
end
-
end
diff --git a/truncate_html.gemspec b/truncate_html.gemspec
index f8d6adc..0691ca2 100644
--- a/truncate_html.gemspec
+++ b/truncate_html.gemspec
@@ -1,19 +1,18 @@
-# -*- encoding: utf-8 -*-
-require File.expand_path("../lib/truncate_html/version", __FILE__)
+require File.expand_path('lib/truncate_html/version', __dir__)
Gem::Specification.new do |s|
- s.name = "truncate_html"
+ s.name = 'truncate_html'
s.version = TruncateHtml::VERSION
- s.authors = ["Harold Giménez"]
- s.email = ["harold.gimenez@gmail.com"]
- s.homepage = "https://github.com/hgmnz/truncate_html"
- s.summary = %q{Uses an API similar to Rails' truncate helper to truncate HTML and close any lingering open tags.}
- s.description = %q{Truncates html so you don't have to}
+ s.authors = ['Harold Giménez']
+ s.email = ['harold.gimenez@gmail.com']
+ s.homepage = 'https://github.com/hgmnz/truncate_html'
+ s.summary = "Uses an API similar to Rails' truncate helper to truncate HTML and close any lingering open tags."
+ s.description = "Truncates html so you don't have to"
s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
- s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
- s.require_paths = ["lib"]
+ s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
+ s.require_paths = ['lib']
s.required_ruby_version = '>= 2.3'