Skip to content

Commit

Permalink
Merge pull request #142 from juanjogeta/juanjo/fix-cc-issues
Browse files Browse the repository at this point in the history
Fix CC issues
  • Loading branch information
kpumuk committed Aug 22, 2017
2 parents 2c266d1 + 5eed659 commit 95b1361
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
4 changes: 2 additions & 2 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ Style/DefWithParentheses:
StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#method-parens'
Enabled: false

Style/DeprecatedHashMethods:
Style/PreferredHashMethods:
Description: 'Checks for use of deprecated Hash methods.'
StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#hash-key'
Enabled: false
Expand Down Expand Up @@ -716,7 +716,7 @@ Style/LineEndConcatenation:
line end.
Enabled: false

Style/MethodCallParentheses:
Style/MethodCallWithoutArgsParentheses:
Description: 'Do not use parentheses for method calls with no arguments.'
StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#no-args-no-parens'
Enabled: false
Expand Down
30 changes: 20 additions & 10 deletions lib/meta_tags/text_normalizer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,12 @@ module TextNormalizer
# @return [Array<String>] array of title parts with tags removed.
#
def self.normalize_title(site_title, title, separator, reverse = false)
title = Array(title).flatten.map(&method(:strip_tags))
title.reject!(&:blank?)
title = Array(title).flatten.map(&method(:strip_tags)).reject(&:blank?)
site_title = strip_tags(site_title)
separator = strip_tags(separator)

if MetaTags.config.title_limit
limit = if site_title.present?
MetaTags.config.title_limit - separator.length
else
# separtor won't be used: ignore its length
MetaTags.config.title_limit
end
limit = calculate_limit(site_title, separator)

if limit > site_title.length
title = truncate_array(title, limit - site_title.length, separator)
Expand Down Expand Up @@ -140,21 +134,37 @@ def self.truncate(string, limit = nil, natural_separator = ' ')
# @return [String] truncated string.
#
def self.truncate_array(string_array, limit = nil, separator = '', natural_separator = ' ')
return string_array if limit.nil? || limit == 0
return string_array if limit.nil? || limit.zero?

length = 0
result = []

string_array.each do |string|
limit_left = limit - length - (result.any? ? separator.length : 0)
limit_left = calculate_limit_left(limit, length, result, separator)

if string.length > limit_left
result << truncate(string, limit_left, natural_separator)
break
end

length += (result.any? ? separator.length : 0) + string.length
result << string

# No more strings will fit
break if length + separator.length >= limit
end

result
end

def self.calculate_limit_left(limit, length, result, separator)
limit - length - (result.any? ? separator.length : 0)
end

def self.calculate_limit(site_title, separator)
return MetaTags.config.title_limit unless site_title.present?

MetaTags.config.title_limit - separator.length
end
end
end

0 comments on commit 95b1361

Please sign in to comment.