Module: Padrino::Helpers::FormatHelpers
- Defined in:
- lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/format_helpers.rb
Overview
Helpers related to formatting or manipulating text within templates.
Instance Method Summary collapse
-
#distance_of_time_in_words(from_time, to_time = 0, include_seconds = false, options = {}) ⇒ String
Reports the approximate distance in time between two Time or Date objects or integers as seconds.
-
#escape_html(text) ⇒ String
(also: #h, #sanitize_html)
Returns escaped text to protect against malicious content.
-
#h!(text, blank_text = ' ') ⇒ String
Returns escaped text to protect against malicious content Returns blank if the text is empty.
-
#highlight(text, words, options = {}) ⇒ String
Highlights one or more words everywhere in text by inserting it into a :highlighter string.
-
#js_escape_html(html_content) ⇒ String
(also: #escape_javascript)
Used in xxxx.js.erb files to escape html so that it can be passed to javascript from Padrino.
-
#pluralize(count, singular, plural = nil) ⇒ String
Attempts to pluralize the singular word unless count is 1.
-
#simple_format(text, options = {}) ⇒ String
Returns text transformed into HTML using simple formatting rules.
-
#strip_tags(html) ⇒ String
Strips all HTML tags from the html.
-
#time_ago_in_words(from_time, include_seconds = false) ⇒ String
Like distance_of_time_in_words, but where
to_time
is fixed toTime.now
. -
#truncate(text, options = {}) ⇒ String
Truncates a given text after a given :length if text is longer than :length (defaults to 30).
-
#truncate_words(text, options = {}) ⇒ String
Truncates words of a given text after a given :length if number of words in text is more than :length (defaults to 30).
-
#word_wrap(text, options = {}) ⇒ String
Wraps the text into lines no longer than line_width width.
Instance Method Details
#distance_of_time_in_words(from_time, to_time = 0, include_seconds = false, options = {}) ⇒ String
Reports the approximate distance in time between two Time or Date objects or integers as seconds. Set include_seconds
to true if you want more detailed approximations when distance < 1 min, 29 secs Distances are reported based on the following table:
0 <-> 29 secs # => less than a minute
30 secs <-> 1 min, 29 secs # => 1 minute
1 min, 30 secs <-> 44 mins, 29 secs # => [2..44] minutes
44 mins, 30 secs <-> 89 mins, 29 secs # => about 1 hour
89 mins, 29 secs <-> 23 hrs, 59 mins, 29 secs # => about [2..24] hours
23 hrs, 59 mins, 29 secs <-> 47 hrs, 59 mins, 29 secs # => 1 day
47 hrs, 59 mins, 29 secs <-> 29 days, 23 hrs, 59 mins, 29 secs # => [2..29] days
29 days, 23 hrs, 59 mins, 30 secs <-> 59 days, 23 hrs, 59 mins, 29 secs # => about 1 month
59 days, 23 hrs, 59 mins, 30 secs <-> 1 yr minus 1 sec # => [2..12] months
1 yr <-> 1 yr, 3 months # => about 1 year
1 yr, 3 months <-> 1 yr, 9 months # => over 1 year
1 yr, 9 months <-> 2 yr minus 1 sec # => almost 2 years
2 yrs <-> max time or date # => (same rules as 1 yr)
With include_seconds
= true and the difference < 1 minute 29 seconds:
0-4 secs # => less than 5 seconds
5-9 secs # => less than 10 seconds
10-19 secs # => less than 20 seconds
20-39 secs # => half a minute
40-59 secs # => less than a minute
60-89 secs # => 1 minute
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 |
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/format_helpers.rb', line 299 def distance_of_time_in_words(from_time, to_time = 0, include_seconds = false, = {}) from_time = from_time.to_time if from_time.respond_to?(:to_time) to_time = to_time.to_time if to_time.respond_to?(:to_time) distance_in_minutes = (((to_time.to_i - from_time.to_i).abs)/60).round distance_in_seconds = ((to_time.to_i - from_time.to_i).abs).round I18n. :locale => [:locale], :scope => :'datetime.distance_in_words' do |locale| case distance_in_minutes when 0..1 return distance_in_minutes == 0 ? locale.t(:less_than_x_minutes, :count => 1) : locale.t(:x_minutes, :count => distance_in_minutes) unless include_seconds case distance_in_seconds when 0..4 then locale.t :less_than_x_seconds, :count => 5 when 5..9 then locale.t :less_than_x_seconds, :count => 10 when 10..19 then locale.t :less_than_x_seconds, :count => 20 when 20..39 then locale.t :half_a_minute when 40..59 then locale.t :less_than_x_minutes, :count => 1 else locale.t :x_minutes, :count => 1 end when 2..44 then locale.t :x_minutes, :count => distance_in_minutes when 45..89 then locale.t :about_x_hours, :count => 1 when 90..1439 then locale.t :about_x_hours, :count => (distance_in_minutes.to_f / 60.0).round when 1440..2529 then locale.t :x_days, :count => 1 when 2530..43199 then locale.t :x_days, :count => (distance_in_minutes.to_f / 1440.0).round when 43200..86399 then locale.t :about_x_months, :count => 1 when 86400..525599 then locale.t :x_months, :count => (distance_in_minutes.to_f / 43200.0).round else distance_in_years = distance_in_minutes / 525600 minute_offset_for_leap_year = (distance_in_years / 4) * 1440 remainder = ((distance_in_minutes - minute_offset_for_leap_year) % 525600) if remainder < 131400 locale.t(:about_x_years, :count => distance_in_years) elsif remainder < 394200 locale.t(:over_x_years, :count => distance_in_years) else locale.t(:almost_x_years, :count => distance_in_years + 1) end end end end |
#escape_html(text) ⇒ String Also known as: h, sanitize_html
Returns escaped text to protect against malicious content
21 22 23 |
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/format_helpers.rb', line 21 def escape_html(text) Rack::Utils.escape_html(text).html_safe end |
#h!(text, blank_text = ' ') ⇒ String
Returns escaped text to protect against malicious content Returns blank if the text is empty
43 44 45 46 |
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/format_helpers.rb', line 43 def h!(text, blank_text = ' ') return blank_text.html_safe if text.nil? || text.empty? h(text) end |
#highlight(text, words, options = {}) ⇒ String
Highlights one or more words everywhere in text by inserting it into a :highlighter string.
The highlighter can be customized by passing :highlighter
as a single-quoted string with 1 where the phrase is to be inserted.
227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/format_helpers.rb', line 227 def highlight(text, words, *args) = args. .reverse_merge!(:highlighter => '<strong class="highlight">\1</strong>') if text.blank? || words.blank? text else match = Array(words).map { |p| Regexp.escape(p) }.join('|') text.gsub(/(#{match})(?!(?:[^<]*?)(?:["'])[^<>]*>)/i, [:highlighter]) end end |
#js_escape_html(html_content) ⇒ String Also known as: escape_javascript
Used in xxxx.js.erb files to escape html so that it can be passed to javascript from Padrino
375 376 377 378 379 380 381 |
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/format_helpers.rb', line 375 def js_escape_html(html_content) return '' unless html_content javascript_mapping = { '\\' => '\\\\', '</' => '<\/', "\r\n" => '\n', "\n" => '\n', "\r" => '\n', '"' => '\\"', "'" => "\\'" } escaped_content = html_content.gsub(/(\\|<\/|\r\n|[\n\r"'])/){ |m| javascript_mapping[m] } escaped_content = escaped_content.html_safe if html_content.html_safe? escaped_content end |
#pluralize(count, singular, plural = nil) ⇒ String
Attempts to pluralize the singular word unless count is 1. If plural is supplied, it will use that when count is > 1, otherwise it will use the Inflector to determine the plural form
112 113 114 |
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/format_helpers.rb', line 112 def pluralize(count, singular, plural = nil) "#{count || 0} " + ((count == 1 || count == '1') ? singular : (plural || singular.pluralize)) end |
#simple_format(text, options = {}) ⇒ String
Returns text transformed into HTML using simple formatting rules. Two or more consecutive newlines(nn) are considered as a paragraph and wrapped in <p> or your own tags. One newline (n) is considered as a linebreak and a <br /> tag is appended. This method does not remove the newlines from the text.
83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/format_helpers.rb', line 83 def simple_format(text, ={}) t = .delete(:tag) || :p start_tag = tag(t, , true) text = escape_html(text.to_s.dup) text.gsub!(/\r\n?/, "\n") # \r\n and \r -> \n text.gsub!(/\n\n+/, "</#{t}>\n\n#{start_tag}") # 2+ newline -> paragraph text.gsub!(/([^\n]\n)(?=[^\n])/, '\1<br />') # 1 newline -> br text.insert 0, start_tag text << "</#{t}>" text.html_safe end |
#strip_tags(html) ⇒ String
Strips all HTML tags from the html
60 61 62 |
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/format_helpers.rb', line 60 def (html) html.gsub(/<\/?[^>]*>/, "") if html end |
#time_ago_in_words(from_time, include_seconds = false) ⇒ String
Like distance_of_time_in_words, but where to_time
is fixed to Time.now
.
359 360 361 |
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/format_helpers.rb', line 359 def time_ago_in_words(from_time, include_seconds = false) distance_of_time_in_words(from_time, Time.now, include_seconds) end |
#truncate(text, options = {}) ⇒ String
Truncates a given text after a given :length if text is longer than :length (defaults to 30). The last characters will be replaced with the :omission (defaults to “…”) for a total length not exceeding :length.
135 136 137 138 139 140 141 142 |
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/format_helpers.rb', line 135 def truncate(text, ={}) .reverse_merge!(:length => 30, :omission => "...") if text len = [:length] - [:omission].length chars = text (chars.length > [:length] ? chars[0...len] + [:omission] : text).to_s end end |
#truncate_words(text, options = {}) ⇒ String
Truncates words of a given text after a given :length if number of words in text is more than :length (defaults to 30). The last words will be replaced with the :omission (defaults to “…”) for a total number of words not exceeding :length.
163 164 165 166 167 168 169 |
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/format_helpers.rb', line 163 def truncate_words(text, ={}) .reverse_merge!(:length => 30, :omission => "...") if text words = text.split() words[0..([:length]-1)].join(' ') + (words.length > [:length] ? [:omission] : '') end end |
#word_wrap(text, options = {}) ⇒ String
Wraps the text into lines no longer than line_width width. This method breaks on the first whitespace character that does not exceed line_width (which is 80 by default).
189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/format_helpers.rb', line 189 def word_wrap(text, *args) = args. unless args.blank? [:line_width] = args[0] || 80 end .reverse_merge!(:line_width => 80) text.split("\n").map do |line| line.length > [:line_width] ? line.gsub(/(.{1,#{[:line_width]}})(\s+|$)/, "\\1\n").strip : line end * "\n" end |