Module: MetaTags::TextNormalizer

Defined in:
lib/meta_tags-rails/text_normalizer.rb

Overview

Module contains helpers that normalize text meta tag values.

Class Method Summary collapse

Class Method Details

.cleanup_string(string) ⇒ String

Removes HTML tags and squashes down all the spaces.

space characters squashed into a single space.

Parameters:

  • string (String)

    input string.

Returns:

  • (String)

    input string with no HTML tags and consequent white



96
97
98
# File 'lib/meta_tags-rails/text_normalizer.rb', line 96

def self.cleanup_string(string)
  strip_tags(string).gsub(/\s+/, ' ').strip.html_safe
end

.cleanup_strings(strings) ⇒ Array<String>

Cleans multiple strings up.

Parameters:

  • strings (Array<String>)

    input strings.

Returns:

  • (Array<String>)

    clean strings.

See Also:



106
107
108
# File 'lib/meta_tags-rails/text_normalizer.rb', line 106

def self.cleanup_strings(strings)
  Array(strings).flatten.map(&method(:cleanup_string))
end

.helpersActionView::Base

Easy way to get access to Rails helpers.

Returns:

  • (ActionView::Base)

    proxy object to access Rails helpers.



64
65
66
# File 'lib/meta_tags-rails/text_normalizer.rb', line 64

def self.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



40
41
42
43
44
# File 'lib/meta_tags-rails/text_normalizer.rb', line 40

def self.normalize_description(description)
  return '' if description.blank?
  description = cleanup_string(description)
  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.



51
52
53
54
55
56
57
58
# File 'lib/meta_tags-rails/text_normalizer.rb', line 51

def self.normalize_keywords(keywords)
  return '' if keywords.blank?
  keywords = cleanup_strings(keywords).each(&:downcase!)
  separator = strip_tags MetaTags.config.keywords_separator

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

.normalize_title(site_title, title, separator, reverse = false) ⇒ Array<String>

Normalize title value.

Parameters:

  • site_title (String)

    site title.

  • title (String, 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:

  • (Array<String>)

    array of title parts with tags removed.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/meta_tags-rails/text_normalizer.rb', line 12

def self.normalize_title(site_title, title, separator, reverse = false)
  title = Array(title).flatten.map(&method(:strip_tags))
  title.reject!(&:blank?)
  site_title = strip_tags(site_title)
  separator = strip_tags(separator)

  if MetaTags.config.title_limit
    limit = MetaTags.config.title_limit - separator.length
    if limit > site_title.length
      title = truncate_array(title, limit - site_title.length, separator)
    else
      site_title = truncate(site_title, limit)
      # Site title is too long, we have to skip page title
      title = []
    end
  end

  title.unshift(site_title) if site_title.present?
  title.reverse! if reverse
  safe_join(title, separator)
end

.safe_join(array, sep = $,) ⇒ 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: $,)

    separator to join strings with.

Returns:

  • (String)

    input strings joined together using a given separator.



86
87
88
# File 'lib/meta_tags-rails/text_normalizer.rb', line 86

def self.safe_join(array, sep = $,)
  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)

    string with no HTML tags.



73
74
75
# File 'lib/meta_tags-rails/text_normalizer.rb', line 73

def self.strip_tags(string)
  ERB::Util.html_escape helpers.strip_tags(string)
end

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

Truncates a string to a specific limit.

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.



117
118
119
120
# File 'lib/meta_tags-rails/text_normalizer.rb', line 117

def self.truncate(string, limit = nil, natural_separator = ' ')
  string = helpers.truncate(string, length: limit, separator: natural_separator, omission: '') if limit
  string
end

.truncate_array(string_array, limit = nil, separator = '', natural_separator = ' ') ⇒ String

Truncates a string 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:

  • (String)

    truncated string.



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/meta_tags-rails/text_normalizer.rb', line 130

def self.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 = limit - length - (result.any? ? separator.length : 0)
    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