2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-06-11 03:33:37 +08:00
|
|
|
require_dependency 'email/styles'
|
|
|
|
|
|
|
|
module Email
|
|
|
|
class Renderer
|
|
|
|
|
2017-07-28 09:20:09 +08:00
|
|
|
def initialize(message, opts = nil)
|
2013-06-11 03:33:37 +08:00
|
|
|
@message = message
|
|
|
|
@opts = opts || {}
|
|
|
|
end
|
|
|
|
|
|
|
|
def text
|
2013-11-29 06:20:56 +08:00
|
|
|
return @text if @text
|
2018-06-05 15:29:17 +08:00
|
|
|
@text = (+(@message.text_part ? @message.text_part : @message).body.to_s).force_encoding('UTF-8')
|
2014-10-07 01:57:38 +08:00
|
|
|
@text = CGI.unescapeHTML(@text)
|
2013-06-11 03:33:37 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def html
|
2013-11-29 06:20:56 +08:00
|
|
|
if @message.html_part
|
2015-10-23 01:10:07 +08:00
|
|
|
style = Email::Styles.new(@message.html_part.body.to_s, @opts)
|
2013-11-29 06:20:56 +08:00
|
|
|
style.format_basic
|
2013-06-14 00:15:05 +08:00
|
|
|
style.format_html
|
2013-06-11 03:33:37 +08:00
|
|
|
else
|
2015-10-23 01:10:07 +08:00
|
|
|
style = Email::Styles.new(PrettyText.cook(text), @opts)
|
2013-11-29 06:20:56 +08:00
|
|
|
style.format_basic
|
2013-06-11 03:33:37 +08:00
|
|
|
end
|
2013-11-29 06:20:56 +08:00
|
|
|
|
|
|
|
style.to_html
|
2013-06-11 03:33:37 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
2014-10-07 01:57:38 +08:00
|
|
|
end
|