Module: MList::Util::EmailHelpers
- Included in:
- Email, EmailPost, MailList, Message, TMailBuilder
- Defined in:
- lib/mlist/util/email_helpers.rb
Constant Summary collapse
- AUTO_LINK_RE =
%r{ ( https?:// | www\. ) [^\s<]+ }x
- BRACKETS =
{ ']' => '[', ')' => '(', '}' => '{' }
- BRACKETS_RE =
/\A<(.*?)>\Z/
- REGARD_RE =
/(^|[^\w])re: /i
- HTML_ESCAPE =
{ '&' => '&', '>' => '>', '<' => '<', '"' => '"' }
Instance Method Summary collapse
-
#auto_link_urls(text) ⇒ Object
Turns all urls into clickable links.
- #bracket(string) ⇒ Object
- #escape_once(text) ⇒ Object
- #header_sanitizer(name) ⇒ Object
- #html_to_text(html) ⇒ Object
- #normalize_new_lines(text) ⇒ Object
- #remove_brackets(string) ⇒ Object
- #remove_regard(string) ⇒ Object
- #sanitize_header(charset, name, *values) ⇒ Object
- #subscriber_name_and_address(subscriber) ⇒ Object
- #text_to_html(text) ⇒ Object
- #text_to_quoted(text) ⇒ Object
Instance Method Details
#auto_link_urls(text) ⇒ Object
Turns all urls into clickable links. If a block is given, each url is yielded and the result is used as the link text.
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/mlist/util/email_helpers.rb', line 120 def auto_link_urls(text) text.gsub(AUTO_LINK_RE) do href = $& punctuation = '' left, right = $`, $' # detect already linked URLs and URLs in the middle of a tag if left =~ /<[^>]+$/ && right =~ /^[^>]*>/ # do not change string; URL is alreay linked href else # don't include trailing punctuation character as part of the URL if href.sub!(/[^\w\/-]$/, '') and punctuation = $& and opening = BRACKETS[punctuation] if href.scan(opening).size > href.scan(punctuation).size href << punctuation punctuation = '' end end link_text = block_given?? yield(href) : href href = 'http://' + href unless href.index('http') == 0 %Q(<a href="#{href}">#{link_text}</a>) end end end |
#bracket(string) ⇒ Object
148 149 150 |
# File 'lib/mlist/util/email_helpers.rb', line 148 def bracket(string) string.blank? || string =~ BRACKETS_RE ? string : "<#{string}>" end |
#escape_once(text) ⇒ Object
185 186 187 |
# File 'lib/mlist/util/email_helpers.rb', line 185 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
152 153 154 |
# File 'lib/mlist/util/email_helpers.rb', line 152 def remove_brackets(string) string =~ BRACKETS_RE ? $1 : string end |
#remove_regard(string) ⇒ Object
157 158 159 160 161 162 |
# File 'lib/mlist/util/email_helpers.rb', line 157 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.rfc5322_email a = "#{subscriber.display_name} #{bracket(a)}" if subscriber.respond_to?(:display_name) a end |
#text_to_html(text) ⇒ Object
164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/mlist/util/email_helpers.rb', line 164 def text_to_html(text) lines = normalize_new_lines(text).split("\n") lines.collect! do |line| line = escape_once(line) line = (" " * $1.length) + $2 if line =~ /^(\s+)(.*?)$/ line = %{<span class="quote">#{line}</span>} if line =~ /^(>|[|]|[A-Za-z]+>)/ line = line.gsub(/\s\s/, ' ') line end lines.join("<br />\n") end |
#text_to_quoted(text) ⇒ Object
176 177 178 179 180 181 182 |
# File 'lib/mlist/util/email_helpers.rb', line 176 def text_to_quoted(text) lines = normalize_new_lines(text).split("\n") lines.collect! do |line| '> ' + line end lines.join("\n") end |