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
-
.cleanup_string(string) ⇒ String
Removes HTML tags and squashes down all the spaces.
-
.cleanup_strings(strings) ⇒ Array<String>
Cleans multiple strings up.
-
.helpers ⇒ ActionView::Base
Easy way to get access to Rails helpers.
-
.normalize_description(description) ⇒ String
Normalize description value.
-
.normalize_keywords(keywords) ⇒ String
Normalize keywords value.
-
.normalize_title(site_title, title, separator, reverse = false) ⇒ Array<String>
Normalize title value.
-
.safe_join(array, sep = $,) ⇒ String
This method returns a html safe string similar to what
Array#joinwould return. -
.strip_tags(string) ⇒ String
Strips all HTML tags from the
html, including comments. -
.truncate(string, limit = nil, natural_separator = ' ') ⇒ String
Truncates a string to a specific limit.
-
.truncate_array(string_array, limit = nil, separator = '', natural_separator = ' ') ⇒ String
Truncates a string to a specific limit.
Class Method Details
.cleanup_string(string) ⇒ String
Removes HTML tags and squashes down all the spaces.
space characters squashed into a single space.
96 97 98 |
# File 'lib/meta_tags-rails/text_normalizer.rb', line 96 def self.cleanup_string(string) (string).gsub(/\s+/, ' ').strip.html_safe end |
.cleanup_strings(strings) ⇒ Array<String>
Cleans multiple strings up.
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 |
.helpers ⇒ ActionView::Base
Easy way to get access to 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.
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.
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 = 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.
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 = (site_title) separator = (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.
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.
73 74 75 |
# File 'lib/meta_tags-rails/text_normalizer.rb', line 73 def self.(string) ERB::Util.html_escape helpers.(string) end |
.truncate(string, limit = nil, natural_separator = ' ') ⇒ String
Truncates a string to a specific limit.
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.
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 |