module MetaTags::TextNormalizer

Experimental RBS support (using type sampling data from the type_fusion project).

# sig/meta_tags/text_normalizer.rbs

module MetaTags::TextNormalizer
  def calculate_limit_left: (Integer limit, Integer length, Array[] result, String separator) -> Integer
  def calculate_title_limits: (String site_title, Array[] title, String separator, Integer global_limit) -> untyped
  def cleanup_string: ((String | ActiveSupport::SafeBuffer) string, strip: bool) -> String
  def cleanup_strings: (Array[String] strings, strip: true) -> untyped
  def helpers: () -> ActionView::Base
  def normalize_description: (String description) -> ActiveSupport::SafeBuffer
  def normalize_keywords: (Array[String] keywords) -> ActiveSupport::SafeBuffer
  def normalize_title: (ActiveSupport::SafeBuffer site_title, Array[] title, String separator, ?true reverse) -> ActiveSupport::SafeBuffer
  def safe_join: (Array[String] array, ?String sep) -> ActiveSupport::SafeBuffer
  def strip_tags: (String string) -> String
  def truncate: (String string, ?Integer limit, ?String natural_separator) -> ActiveSupport::SafeBuffer
  def truncate_array: ((Array[String] | Array[]) string_array, ?Integer limit, ?String separator, ?String natural_separator) -> Array[String]
  def truncate_title: (ActiveSupport::SafeBuffer site_title, (Array[] | Array[String]) title, String separator) -> Array[ActiveSupport::SafeBuffer]
end

def calculate_limit_left(limit, length, result, separator)

Experimental RBS support (using type sampling data from the type_fusion project).

def calculate_limit_left: (Integer limit, Integer length,  result, String separator) -> Integer

This signature was generated using 3 samples from 1 application.

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

def calculate_title_limits(site_title, title, separator, global_limit)

Experimental RBS support (using type sampling data from the type_fusion project).

def calculate_title_limits: (String site_title,  title, String separator, Integer global_limit) -> untyped

This signature was generated using 1 sample from 1 application.

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

def cleanup_string(string, strip: true)

Experimental RBS support (using type sampling data from the type_fusion project).

def cleanup_string: ((String | ActiveSupport::SafeBuffer) string, strip: bool) -> String

This signature was generated using 11 samples from 1 application.

Returns:
  • (String) - input string with no HTML tags and consequent white

Parameters:
  • string (String, nil) -- input string.
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

def cleanup_strings(strings, strip: true)

Experimental RBS support (using type sampling data from the type_fusion project).

def cleanup_strings: ( strings, strip: true) -> untyped

This signature was generated using 1 sample from 1 application.

Other tags:
    See: cleanup_string -

Returns:
  • (Array) - clean strings.

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

def helpers

Experimental RBS support (using type sampling data from the type_fusion project).

def helpers: () -> ActionView::Base

This signature was generated using 6 samples from 1 application.

Returns:
  • (ActionView::Base) - proxy object to access Rails helpers.
def helpers
  ActionController::Base.helpers
end

def normalize_description(description)

Experimental RBS support (using type sampling data from the type_fusion project).

def normalize_description: (String description) -> ActiveSupport::SafeBuffer

This signature was generated using 2 samples from 1 application.

Returns:
  • (String) - text with tags removed, squashed spaces, truncated

Parameters:
  • description (String) -- description string.
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

def normalize_keywords(keywords)

Experimental RBS support (using type sampling data from the type_fusion project).

def normalize_keywords: ( keywords) -> ActiveSupport::SafeBuffer

This signature was generated using 1 sample from 1 application.

Returns:
  • (String) - list of keywords joined with comma, with tags removed.

Parameters:
  • keywords (String, Array) -- list of keywords as a string or Array.
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

def normalize_title(site_title, title, separator, reverse = false)

Experimental RBS support (using type sampling data from the type_fusion project).

def normalize_title: (ActiveSupport::SafeBuffer site_title,  title, String separator, ?true reverse) -> ActiveSupport::SafeBuffer

This signature was generated using 1 sample from 1 application.

Returns:
  • (String) - title with HTML tags removed.

Parameters:
  • reverse (true, false) -- whether title should be reversed.
  • separator (String) -- a string to join title parts with.
  • title (Array) -- title string.
  • site_title (String) -- site title.
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

def safe_join(array, sep = $OFS)

Experimental RBS support (using type sampling data from the type_fusion project).

def safe_join: ((String | String | ) array, ?String sep) -> ActiveSupport::SafeBuffer

This signature was generated using 2 samples from 1 application.

Returns:
  • (String) - input strings joined together using a given separator.

Parameters:
  • sep (String) -- separator to join strings with.
  • array (Array) -- list of strings to join.
def safe_join(array, sep = $OFS)
  helpers.safe_join(array, sep)
end

def strip_tags(string)

Experimental RBS support (using type sampling data from the type_fusion project).

def strip_tags: (String string) -> String

This signature was generated using 6 samples from 1 application.

Returns:
  • (String) - html_safe string with no HTML tags.

Parameters:
  • string (String) -- HTML string.
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

def truncate(string, limit = nil, natural_separator = ' ')

Experimental RBS support (using type sampling data from the type_fusion project).

def truncate: (String string, ?Integer limit, ?String natural_separator) -> ActiveSupport::SafeBuffer

This signature was generated using 5 samples from 1 application.

Returns:
  • (String) - truncated string.

Parameters:
  • natural_separator (String) -- natural separator to truncate at.
  • limit (Integer, nil) -- characters number to truncate to.
  • string (String) -- input strings.
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

def truncate_array(string_array, limit = nil, separator = '', natural_separator = ' ')

Experimental RBS support (using type sampling data from the type_fusion project).

def truncate_array: ( string_array, ?Integer limit, ?String separator, ?String natural_separator) ->

This signature was generated using 2 samples from 2 applications.

Returns:
  • (Array) - truncated array of strings.

Parameters:
  • natural_separator (String) -- natural separator to truncate at.
  • separator (String) -- separator that will be used to join array later.
  • limit (Integer, nil) -- characters number to truncate to.
  • string_array (Array) -- input strings.
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

def truncate_title(site_title, title, separator)

Experimental RBS support (using type sampling data from the type_fusion project).

def truncate_title: (ActiveSupport::SafeBuffer site_title,  title, String separator) -> (Array |  | Array | String)

This signature was generated using 3 samples from 1 application.

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