Module: MList::Util::EmailHelpers

Included in:
Email, EmailPost, MailList, Message, TMailBuilder
Defined in:
lib/mlist/util/email_helpers.rb

Constant Summary collapse

BRACKETS_RE =
/\A<(.*?)>\Z/
REGARD_RE =
/(^|[^\w])re: /i
HTML_ESCAPE =
{ '&' => '&amp;',  '>' => '&gt;',   '<' => '&lt;', '"' => '&quot;' }

Instance Method Summary collapse

Instance Method Details

#bracket(string) ⇒ Object



112
113
114
# File 'lib/mlist/util/email_helpers.rb', line 112

def bracket(string)
  string.blank? || string =~ BRACKETS_RE ? string : "<#{string}>"
end

#escape_once(text) ⇒ Object



149
150
151
# File 'lib/mlist/util/email_helpers.rb', line 149

def escape_once(text)
  text.gsub(/[\"><]|&(?!([a-zA-Z]+|(#\d+));)/) { |special| HTML_ESCAPE[special] }
end

#header_sanitizer(name) ⇒ Object



93
94
95
# File 'lib/mlist/util/email_helpers.rb', line 93

def header_sanitizer(name)
  Util.default_header_sanitizers[name]
end

#html_to_text(html) ⇒ Object



97
98
99
# File 'lib/mlist/util/email_helpers.rb', line 97

def html_to_text(html)
  HtmlTextExtraction.new(html).execute
end

#normalize_new_lines(text) ⇒ Object



101
102
103
# File 'lib/mlist/util/email_helpers.rb', line 101

def normalize_new_lines(text)
  text.to_s.gsub(/\r\n?/, "\n")
end

#remove_brackets(string) ⇒ Object



116
117
118
# File 'lib/mlist/util/email_helpers.rb', line 116

def remove_brackets(string)
  string =~ BRACKETS_RE ? $1 : string
end

#remove_regard(string) ⇒ Object



121
122
123
124
125
126
# File 'lib/mlist/util/email_helpers.rb', line 121

def remove_regard(string)
  while string =~ REGARD_RE
    string = string.sub(REGARD_RE, ' ')
  end
  string.strip
end

#sanitize_header(charset, name, *values) ⇒ Object



89
90
91
# File 'lib/mlist/util/email_helpers.rb', line 89

def sanitize_header(charset, name, *values)
  header_sanitizer(name).call(charset, *values)
end

#subscriber_name_and_address(subscriber) ⇒ Object



105
106
107
108
109
# File 'lib/mlist/util/email_helpers.rb', line 105

def subscriber_name_and_address(subscriber)
  a = subscriber.email_address
  a = "#{subscriber.display_name} #{bracket(a)}" if subscriber.respond_to?(:display_name)
  a
end

#text_to_html(text) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
# File 'lib/mlist/util/email_helpers.rb', line 128

def text_to_html(text)
  lines = normalize_new_lines(text).split("\n")
  lines.collect! do |line|
    line = escape_once(line)
    line = ("&nbsp;" * $1.length) + $2 if line =~ /^(\s+)(.*?)$/
    line = %{<span class="quote">#{line}</span>} if line =~ /^(&gt;|[|]|[A-Za-z]+&gt;)/
    line = line.gsub(/\s\s/, ' &nbsp;')
    line
  end
  lines.join("<br />\n")
end

#text_to_quoted(text) ⇒ Object



140
141
142
143
144
145
146
# File 'lib/mlist/util/email_helpers.rb', line 140

def text_to_quoted(text)
  lines = normalize_new_lines(text).split("\n")
  lines.collect! do |line|
    '> ' + line
  end
  lines.join("\n")
end