2009-12-16 20:23:08 -05:00
|
|
|
module ActionMailer
|
|
|
|
module MailHelper
|
2012-02-15 01:09:58 -05:00
|
|
|
# Take the text and format it, indented two spaces for each line, and wrapped at 72 columns.
|
2009-12-16 20:23:08 -05:00
|
|
|
def block_format(text)
|
2012-02-15 01:31:23 -05:00
|
|
|
formatted = text.split(/\n\r?\n/).collect { |paragraph|
|
2011-03-08 15:40:03 -05:00
|
|
|
format_paragraph(paragraph)
|
2012-02-15 01:31:23 -05:00
|
|
|
}.join("\n\n")
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2009-12-16 20:23:08 -05:00
|
|
|
# Make list points stand on their own line
|
|
|
|
formatted.gsub!(/[ ]*([*]+) ([^*]*)/) { |s| " #{$1} #{$2.strip}\n" }
|
|
|
|
formatted.gsub!(/[ ]*([#]+) ([^#]*)/) { |s| " #{$1} #{$2.strip}\n" }
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2009-12-16 20:23:08 -05:00
|
|
|
formatted
|
|
|
|
end
|
2009-12-25 15:35:40 -05:00
|
|
|
|
|
|
|
# Access the mailer instance.
|
2010-01-24 13:52:44 -05:00
|
|
|
def mailer
|
2010-01-23 05:37:34 -05:00
|
|
|
@_controller
|
2009-12-25 15:35:40 -05:00
|
|
|
end
|
2010-01-22 05:01:21 -05:00
|
|
|
|
|
|
|
# Access the message instance.
|
2010-01-24 13:52:44 -05:00
|
|
|
def message
|
2010-01-24 13:36:42 -05:00
|
|
|
@_message
|
2010-01-22 05:01:21 -05:00
|
|
|
end
|
2010-06-07 21:54:53 -04:00
|
|
|
|
|
|
|
# Access the message attachments list.
|
|
|
|
def attachments
|
|
|
|
@_message.attachments
|
|
|
|
end
|
2011-02-09 13:11:29 -05:00
|
|
|
|
2011-03-09 14:06:48 -05:00
|
|
|
# Returns +text+ wrapped at +len+ columns and indented +indent+ spaces.
|
|
|
|
#
|
|
|
|
# === Examples
|
|
|
|
#
|
|
|
|
# my_text = "Here is a sample text with more than 40 characters"
|
|
|
|
#
|
2011-03-09 17:17:30 -05:00
|
|
|
# format_paragraph(my_text, 25, 4)
|
2011-03-09 14:06:48 -05:00
|
|
|
# # => " Here is a sample text with\n more than 40 characters"
|
2011-03-08 15:40:03 -05:00
|
|
|
def format_paragraph(text, len = 72, indent = 2)
|
2011-02-09 13:11:29 -05:00
|
|
|
sentences = [[]]
|
|
|
|
|
|
|
|
text.split.each do |word|
|
2012-02-15 01:04:27 -05:00
|
|
|
if sentences.first.present? && (sentences.last + [word]).join(' ').length > len
|
2011-02-09 13:11:29 -05:00
|
|
|
sentences << [word]
|
|
|
|
else
|
|
|
|
sentences.last << word
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
sentences.map { |sentence|
|
|
|
|
"#{" " * indent}#{sentence.join(' ')}"
|
|
|
|
}.join "\n"
|
|
|
|
end
|
2009-12-16 20:23:08 -05:00
|
|
|
end
|
|
|
|
end
|