class Mail2FrontMatter::Parser
Constants
- ALLOWED_TYPES
Attributes
body[RW]
message[RW]
metadata[RW]
Public Class Methods
new(message)
click to toggle source
# File lib/mail2frontmatter/parser.rb, line 16 def initialize(message) @message = message raw_parsed_html = Nokogiri::HTML.parse(@message.html_part.decoded.strip) @body = raw_parsed_html.at('body') # remove extraneous nesting while @body.children.count == 1 && @body.children.first.name == 'div' @body = @body.children.first end attachments = {} @message.attachments.each do |attachment| if Parser::ALLOWED_TYPES.keys.include?(attachment.main_type) # save attachments media_type_directory = File.join(Mail2FrontMatter.config[:media_directory], Parser::ALLOWED_TYPES[attachment.main_type]) FileUtils.mkdir_p(media_type_directory) filepath = File.join(media_type_directory, attachment.filename) # save attachment File.open(filepath, 'w+b', 0644) { |f| f.write attachment.body.decoded } # retain metadata attachments[attachment.cid] = { maintype: attachment.main_type, mimetype: attachment.mime_type, filename: attachment.filename, filepath: filepath } # file type not allowed else # remove cooresponding node from html @body.xpath("//*[@src='cid:#{attachment.content_id}']").remove end end # convert body immediately to a string, why? # Plugins / pre-processors may wish to manipulate the body # however Nokogiri is strict and won't allow template tags # for obvious good reasons @body = @body.inner_html @metadata = { from: message[:from].value, to: message[:to].value, received: message.date, subject: message.subject, attachments: attachments } end