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



99
100
101
# File 'lib/mlist/util/email_helpers.rb', line 99

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

#escape_once(text) ⇒ Object



136
137
138
# File 'lib/mlist/util/email_helpers.rb', line 136

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

#header_sanitizer(name) ⇒ Object



86
87
88
# File 'lib/mlist/util/email_helpers.rb', line 86

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

#html_to_text(html) ⇒ Object



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

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

#normalize_new_lines(text) ⇒ Object



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

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

#remove_brackets(string) ⇒ Object



103
104
105
# File 'lib/mlist/util/email_helpers.rb', line 103

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

#remove_regard(string) ⇒ Object



108
109
110
111
112
113
# File 'lib/mlist/util/email_helpers.rb', line 108

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

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



82
83
84
# File 'lib/mlist/util/email_helpers.rb', line 82

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

#text_to_html(text) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
# File 'lib/mlist/util/email_helpers.rb', line 115

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



127
128
129
130
131
132
133
# File 'lib/mlist/util/email_helpers.rb', line 127

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