discourse/lib/email/renderer.rb
Sam Saffron 30990006a9 DEV: enable frozen string literal on all files
This reduces chances of errors where consumers of strings mutate inputs
and reduces memory usage of the app.

Test suite passes now, but there may be some stuff left, so we will run
a few sites on a branch prior to merging
2019-05-13 09:31:32 +08:00

34 lines
705 B
Ruby

# frozen_string_literal: true
require_dependency 'email/styles'
module Email
class Renderer
def initialize(message, opts = nil)
@message = message
@opts = opts || {}
end
def text
return @text if @text
@text = (+(@message.text_part ? @message.text_part : @message).body.to_s).force_encoding('UTF-8')
@text = CGI.unescapeHTML(@text)
end
def html
if @message.html_part
style = Email::Styles.new(@message.html_part.body.to_s, @opts)
style.format_basic
style.format_html
else
style = Email::Styles.new(PrettyText.cook(text), @opts)
style.format_basic
end
style.to_html
end
end
end