Module: MetaTags::TextNormalizer

Extended by:
TextNormalizer
Included in:
TextNormalizer
Defined in:
lib/meta_tags/text_normalizer.rb,
sig/lib/meta_tags/text_normalizer.rbs

Overview

Module contains helpers that normalize text meta tag values.

Instance Method Summary collapse

Instance Method Details

#calculate_limit_left(limit, length, result, separator) ⇒ Object

Parameters:

  • limit (Integer)
  • length (Integer)
  • result (Array[String])
  • separator (String)

Returns:



186
187
188
# File 'lib/meta_tags/text_normalizer.rb', line 186

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

#calculate_title_limits(site_title, title, separator, global_limit) ⇒ Object

Parameters:

  • site_title (String)
  • title (Array[String])
  • separator (String)
  • global_limit (Integer)

Returns:



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/meta_tags/text_normalizer.rb', line 204

def calculate_title_limits(site_title, title, separator, global_limit)
  # What should we truncate first: site title or page title?
  main_title = MetaTags.config.truncate_site_title_first ? title : [site_title]

  main_length = main_title.map(&:length).sum + ((main_title.size - 1) * separator.length)
  main_limited_length = global_limit

  secondary_limited_length = global_limit - (main_length > 0 ? main_length + separator.length : 0)
  secondary_limited_length = [0, secondary_limited_length].max

  if MetaTags.config.truncate_site_title_first
    [ secondary_limited_length, main_limited_length ]
  else
    [ main_limited_length, secondary_limited_length ]
  end
end

#cleanup_string(string, strip: true) ⇒ String

Removes HTML tags and squashes down all the spaces.

space characters squashed into a single space.

Parameters:

  • string (String, nil) —

    input string.

  • strip: (bool strip) (defaults to: true)

Returns:

  • (String) —

    input string with no HTML tags and consequent white

Raises:

  • (ArgumentError)


109
110
111
112
113
114
115
116
117
118
119
# File 'lib/meta_tags/text_normalizer.rb', line 109

def cleanup_string(string, strip: true)
  return '' if string.nil?
  raise ArgumentError, 'Expected a string or an object that implements #to_str' unless string.respond_to?(:to_str)

  s = strip_tags(string.to_str)
  s = s.dup if s.frozen?
  s.gsub!(/\s+/, ' ')
  s.strip! if strip

  s
end

#cleanup_strings(strings, strip: true) ⇒ Array<String>

Cleans multiple strings up.

Parameters:

  • strings (String, Array<String>) —

    input string(s).

  • strip: (bool strip) (defaults to: true)

Returns:

  • (Array<String>) —

    clean strings.

See Also:



127
128
129
130
131
# File 'lib/meta_tags/text_normalizer.rb', line 127

def cleanup_strings(strings, strip: true)
  strings = Array(strings).flatten.map! { |s| cleanup_string(s, strip: strip) }
  strings.reject!(&:blank?)
  strings
end

#helpers ⇒ ActionView::Base

Easy way to get access to Rails helpers.

Returns:

  • (ActionView::Base) —

    proxy object to access Rails helpers.



72
73
74
# File 'lib/meta_tags/text_normalizer.rb', line 72

def helpers
  ActionController::Base.helpers
end

#normalize_description(description) ⇒ String

Normalize description value.

to 200 characters.

Parameters:

  • description (String) —

    description string.

Returns:

  • (String) —

    text with tags removed, squashed spaces, truncated



42
43
44
45
46
47
48
49
50
# File 'lib/meta_tags/text_normalizer.rb', line 42

def normalize_description(description)
  # description could be another object not a string, but since it probably
  # serves the same purpose we could just as it to convert itself to str
  # and continue from there
  description = cleanup_string(description)
  return '' if description.blank?

  truncate(description, MetaTags.config.description_limit)
end

#normalize_keywords(keywords) ⇒ String

Normalize keywords value.

Parameters:

  • keywords (String, Array<String>) —

    list of keywords as a string or Array.

Returns:

  • (String) —

    list of keywords joined with comma, with tags removed.



57
58
59
60
61
62
63
64
65
66
# File 'lib/meta_tags/text_normalizer.rb', line 57

def normalize_keywords(keywords)
  keywords = cleanup_strings(keywords)
  return '' if keywords.blank?

  keywords.each(&:downcase!) if MetaTags.config.keywords_lowercase
  separator = cleanup_string MetaTags.config.keywords_separator, strip: false

  keywords = truncate_array(keywords, MetaTags.config.keywords_limit, separator)
  safe_join(keywords, separator)
end

#normalize_title(site_title, title, separator, reverse = false) ⇒ String

Normalize title value.

Parameters:

  • site_title (String) —

    site title.

  • title (Array<String>) —

    title string.

  • separator (String) —

    a string to join title parts with.

  • reverse (true, false) (defaults to: false) —

    whether title should be reversed.

Returns:

  • (String) —

    title with HTML tags removed.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/meta_tags/text_normalizer.rb', line 16

def normalize_title(site_title, title, separator, reverse = false)
  clean_title = cleanup_strings(title)
  clean_title.reverse! if reverse

  site_title = cleanup_string(site_title)
  separator = cleanup_string(separator, strip: false)

  # Truncate title and site title
  site_title, clean_title = truncate_title(site_title, clean_title, separator)

  if site_title.present?
    if reverse
      clean_title.push(site_title)
    else
      clean_title.unshift(site_title)
    end
  end
  safe_join(clean_title, separator)
end

#safe_join(array, sep = $OFS) ⇒ String

This method returns a html safe string similar to what Array#join would return. All items in the array, including the supplied separator, are html escaped unless they are html safe, and the returned string is marked as html safe.

Parameters:

  • array (Array<String>) —

    list of strings to join.

  • sep (String) (defaults to: $OFS) —

    separator to join strings with.

Returns:

  • (String) —

    input strings joined together using a given separator.



99
100
101
# File 'lib/meta_tags/text_normalizer.rb', line 99

def safe_join(array, sep = $OFS)
  helpers.safe_join(array, sep)
end

#strip_tags(string) ⇒ String

Strips all HTML tags from the html, including comments.

Parameters:

  • string (String) —

    HTML string.

Returns:

  • (String) —

    html_safe string with no HTML tags.



81
82
83
84
85
86
87
88
# File 'lib/meta_tags/text_normalizer.rb', line 81

def strip_tags(string)
  if defined?(Loofah)
    # Instead of strip_tags we will use Loofah to strip tags from now on
    Loofah.fragment(string).text(encode_special_chars: false)
  else
    helpers.strip_tags(string)
  end
end

#truncate(string, limit = nil, natural_separator = ' ') ⇒ String

Truncates a string to a specific limit. Return string without truncation when limit 0 or nil

Parameters:

  • string (String) —

    input strings.

  • limit (Integer, nil) (defaults to: nil) —

    characters number to truncate to.

  • natural_separator (String) (defaults to: ' ') —

    natural separator to truncate at.

Returns:

  • (String) —

    truncated string.



140
141
142
143
144
145
146
147
148
149
150
# File 'lib/meta_tags/text_normalizer.rb', line 140

def truncate(string, limit = nil, natural_separator = ' ')
  return string if limit.to_i == 0 # rubocop:disable Lint/NumberConversion

  helpers.truncate(
    string,
    length:    limit,
    separator: natural_separator,
    omission:  '',
    escape:    true,
  )
end

#truncate_array(string_array, limit = nil, separator = '', natural_separator = ' ') ⇒ Array<String>

Truncates an array of strings to a specific limit.

Parameters:

  • string_array (Array<String>) —

    input strings.

  • limit (Integer, nil) (defaults to: nil) —

    characters number to truncate to.

  • separator (String) (defaults to: '') —

    separator that will be used to join array later.

  • natural_separator (String) (defaults to: ' ') —

    natural separator to truncate at.

Returns:

  • (Array<String>) —

    truncated array of strings.



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/meta_tags/text_normalizer.rb', line 160

def truncate_array(string_array, limit = nil, separator = '', natural_separator = ' ')
  return string_array if limit.nil? || limit <= 0

  length = 0
  result = []

  string_array.each do |string|
    limit_left = calculate_limit_left(limit, length, result, separator)

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

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

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

  result
end

#truncate_title(site_title, title, separator) ⇒ ::Array[untyped]

Parameters:

  • site_title (String)
  • title (Array[String])
  • separator (String)

Returns:

  • (::Array[untyped])


190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/meta_tags/text_normalizer.rb', line 190

def truncate_title(site_title, title, separator)
  global_limit = MetaTags.config.title_limit.to_i # rubocop:disable Lint/NumberConversion
  if global_limit > 0
    site_title_limited_length, title_limited_length = calculate_title_limits(
      site_title, title, separator, global_limit,
    )

    title = title_limited_length > 0 ? truncate_array(title, title_limited_length, separator) : []
    site_title = site_title_limited_length > 0 ? truncate(site_title, site_title_limited_length) : nil
  end

  [site_title, title]
end