Module: MetaTags::TextNormalizer
- Extended by:
- TextNormalizer
- Included in:
- TextNormalizer
- Defined in:
- lib/meta_tags/text_normalizer.rb
Overview
This module contains helpers that normalize text meta tag values.
Instance Method Summary collapse
-
#cleanup_string(string, strip: true) ⇒ String
Removes HTML tags and squashes down all the spaces.
-
#cleanup_strings(strings, strip: true) ⇒ Array<String>
Cleans up multiple strings.
-
#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) ⇒ String
Normalize title value.
-
#safe_join(array, sep = $OFS) ⇒ String
This method returns an HTML-safe string similar to what
Array#joinwould return. -
#strip_tags(string) ⇒ String
Strips all HTML tags from
string, including comments. -
#truncate(string, limit = nil) ⇒ String
Truncates a string to a specific limit.
-
#truncate_array(string_array, limit = nil, separator = "") ⇒ Array<String>
Truncates an array of strings to a specific limit.
Instance Method Details
#cleanup_string(string, strip: true) ⇒ String
Removes HTML tags and squashes down all the spaces.
103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/meta_tags/text_normalizer.rb', line 103 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 = (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 up multiple strings.
121 122 123 124 125 |
# File 'lib/meta_tags/text_normalizer.rb', line 121 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.
68 69 70 |
# File 'lib/meta_tags/text_normalizer.rb', line 68 def helpers ActionController::Base.helpers end |
#normalize_description(description) ⇒ String
Normalize description value.
40 41 42 43 44 45 46 47 48 |
# File 'lib/meta_tags/text_normalizer.rb', line 40 def normalize_description(description) # description could be another object not a string, but since it probably # serves the same purpose, we can ask it to convert itself to a string # 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.
54 55 56 57 58 59 60 61 62 63 |
# File 'lib/meta_tags/text_normalizer.rb', line 54 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.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/meta_tags/text_normalizer.rb', line 15 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 an 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.
93 94 95 |
# File 'lib/meta_tags/text_normalizer.rb', line 93 def safe_join(array, sep = $OFS) helpers.safe_join(array, sep) end |
#strip_tags(string) ⇒ String
Strips all HTML tags from string, including comments.
76 77 78 79 80 81 82 83 |
# File 'lib/meta_tags/text_normalizer.rb', line 76 def (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.(string) end end |
#truncate(string, limit = nil) ⇒ String
Truncates a string to a specific limit. Returns the string without truncation when the limit is 0 or nil.
133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/meta_tags/text_normalizer.rb', line 133 def truncate(string, limit = nil) return string if limit.to_i == 0 helpers.truncate( string, length: limit, separator: MetaTags.config.truncate_on_natural_separator, omission: "", escape: true ) end |
#truncate_array(string_array, limit = nil, separator = "") ⇒ Array<String>
Truncates an array of strings to a specific limit.
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/meta_tags/text_normalizer.rb', line 151 def truncate_array(string_array, limit = nil, separator = "") return string_array if limit.nil? || limit <= 0 length = 0 result = [] # : Array[String] string_array.each do |string| limit_left = calculate_limit_left(limit, length, result, separator) if string.length > limit_left result << truncate(string, limit_left) 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 |