Sha256: eef27b259521361c0a9e68627cac1209fbdc3eaf06cea8032b04faaa2c20c00d

Contents?: true

Size: 1.29 KB

Versions: 1

Compression:

Stored size: 1.29 KB

Contents

require 'strscan'

module MailExtract
  class Parser
    attr_reader :body
    
    # Initialize a new MailExtract::Parser object
    #   text - Email message body
    #
    def initialize(text)
      @lines     = []
      @text      = text.strip
      @body      = ""
      @last_type = :text
      @type      = :text
      parse
    end
    
    private
    
    # Process email message body
    #
    def parse
      scanner = StringScanner.new(@text)
      while str = scanner.scan_until(/\n/)
        parse_line(str)
      end
      if (last_line = scanner.rest.to_s).size > 0
        parse_line(last_line)
      end
      @body = @lines.join("\n").strip
    end
    
    # Process a single line
    #
    def parse_line(str)
      line = MailExtract::Line.new(str)
      if line.type == :quote
        if @last_type == :text
          @type = :quote
        end
      elsif line.type == :text
        if @last_type == :quote
          @type = :text
        end
        if @last_type == :signature
          @type = :signature
        end
      elsif line.type == :signature
        if @last_type == :text
          @type = :signature
        elsif @last_type == :quote
          @type = :quote
        end
      end
      @last_type = line.type
      @lines << line.body.strip if @type == :text
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mail_extract-0.1.0 lib/mail_extract/parser.rb