Class: HTML::Pipeline::EmailReplyFilter

Inherits:
TextFilter show all
Includes:
EscapeUtils
Defined in:
lib/html/pipeline/email_reply_filter.rb

Overview

HTML Filter that converts email reply text into an HTML DocumentFragment. It must be used as the first filter in a pipeline.

Context options:

None

This filter does not write any additional information to the context hash.

Constant Summary collapse

EMAIL_HIDDEN_HEADER =
%(<span class="email-hidden-toggle"><a href="#">&hellip;</a></span><div class="email-hidden-reply" style="display:none">).freeze
EMAIL_QUOTED_HEADER =
%(<div class="email-quoted-reply">).freeze
EMAIL_SIGNATURE_HEADER =
%(<div class="email-signature-reply">).freeze
EMAIL_FRAGMENT_HEADER =
%(<div class="email-fragment">).freeze
EMAIL_HEADER_END =
'</div>'.freeze
EMAIL_REGEX =
/[^@\s.][^@\s]*@\[?[a-z0-9.-]+\]?/
HIDDEN_EMAIL_PATTERN =
'***@***.***'.freeze

Instance Attribute Summary

Attributes inherited from TextFilter

#text

Attributes inherited from Filter

#context, #result

Instance Method Summary collapse

Methods inherited from TextFilter

#initialize

Methods inherited from Filter

#base_url, call, #current_user, #doc, #has_ancestor?, #html, #initialize, #needs, #parse_html, #repository, to_document, to_html, #validate

Constructor Details

This class inherits a constructor from HTML::Pipeline::TextFilter

Instance Method Details

#callObject

Scans an email body to determine which bits are quoted and which should be hidden. EmailReplyParser is used to split the comment into an Array of quoted or unquoted Blocks. Now, we loop through them and attempt to add <div> tags around them so we can hide the hidden blocks, and style the quoted blocks differently. Since multiple blocks may be hidden, be sure to keep the “email-hidden-reply” <div>s around “email-quoted-reply” <div> tags. Call this on each comment of a visible thread in the order that they are displayed. Note: all comments are processed so we can maintain a Set of SHAs of paragraphs. Only plaintext comments skip the markdown step.

Returns the email comment HTML as a String



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/html/pipeline/email_reply_filter.rb', line 38

def call
  found_hidden = nil
  paragraphs = EmailReplyParser.read(text.dup).fragments.map do |fragment|
    pieces = [escape_html(fragment.to_s.strip).gsub(/^\s*(>|&gt;)/, '')]
    if fragment.quoted?
      if context[:hide_quoted_email_addresses]
        pieces.map! do |piece|
          piece.gsub(EMAIL_REGEX, HIDDEN_EMAIL_PATTERN)
        end
      end
      pieces.unshift EMAIL_QUOTED_HEADER
      pieces << EMAIL_HEADER_END
    elsif fragment.signature?
      pieces.unshift EMAIL_SIGNATURE_HEADER
      pieces << EMAIL_HEADER_END
    else
      pieces.unshift EMAIL_FRAGMENT_HEADER
      pieces << EMAIL_HEADER_END
    end
    if fragment.hidden? && !found_hidden
      found_hidden = true
      pieces.unshift EMAIL_HIDDEN_HEADER
    end
    pieces.join
  end
  paragraphs << EMAIL_HEADER_END if found_hidden
  paragraphs.join("\n")
end