Class: HtmlEmailCreator::Processor

Inherits:
Object
  • Object
show all
Defined in:
lib/html_email_creator/processor.rb

Instance Method Summary collapse

Constructor Details

#initialize(email_string, settings = HtmlEmailCreator::settings) ⇒ Processor

Returns a new instance of Processor.



8
9
10
11
# File 'lib/html_email_creator/processor.rb', line 8

def initialize(email_string, settings = HtmlEmailCreator::settings)
  @email = email_string
  @settings = settings
end

Instance Method Details

#original_emailObject



13
14
15
# File 'lib/html_email_creator/processor.rb', line 13

def original_email
  @email
end

#to_htmlObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/html_email_creator/processor.rb', line 17

def to_html
  return @processed_html_email if @processed_html_email
  
  doc = Nokogiri::HTML(@email)
  headers = ["h1", "h2", "h3", "h4", "h5"]

  # wrap headers with div

  headers.each do |hx|
    nodes = doc.css hx
    nodes.wrap("<div class='#{hx}'></div>")
  end

  # replace headers with divs

  headers.each do |hx|
    doc.css(hx).each { |h| h.name = "div" }
  end

  # replace paragraphs with divs

  #doc.css("p").each { |p| p.name = "div" }

  # store file

  html_email = doc.to_html
  
  # process and then call callback
  inlined_html_email = InlineStyle.process(html_email)  
  HtmlEmailCreator::callbacks(@settings).html_created(inlined_html_email)
end

#to_plain_textObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/html_email_creator/processor.rb', line 49

def to_plain_text
  return @processed_plain_text_email if @processed_plain_text_email
  
  doc = Nokogiri::HTML(to_html)
  doc.css('style').each { |node| node.remove }
  doc.css('title').each { |node| node.remove }
  doc.css('script').each { |node| node.remove }
  doc.css('link').each { |node| node.remove }
  doc.css('a').each do |node|
    img = node.at_css('img')
    node.content = if img
      "#{img['alt']} (#{node['href']})"
    else
      "#{node.content} (#{node['href']})"
    end
  end
  doc.css('img').each do |node|
    node.content = "#{node['alt']} (#{node['src']})"
  end
  doc.css('strong').each do |node| 
    node.content = "*#{node.content}*"
  end
  doc.css('li').each { |node| node.content = "- #{node.content.strip}" }

  # format all content that must have an empty line on top of them

  doc.css('div.h1 div').each do |node| 
    node.content = "\n#{'=' * 77}\n#{node.content}"
  end
  doc.css('div.h2 div').each do |node| 
    node.content = "\n#{'-' * 77}\n#{node.content}"
  end
  doc.css('div.h3 div').each do |node| 
    node.content = "\n#{'- ' * 39}\n#{node.content}"
  end
  doc.css('div.h4 div').each do |node| 
    node.content = "\n#{node.content}"
  end
  doc.css('p').each do |node| 
    node.content = "\n#{node.content}"
  end 

  doc.css('table.inline').each do |table|
    content = []
    table.css('tr').each do |row|
      # each_index method is missing
      i = 0
      row.css('td').each do |column|
        empty_column = column.content.strip.gsub(/^\p{Space}+|\p{Space}+$/, "").empty?
        if content[i]
          content[i] = content[i] + "\n* #{column.content}" unless empty_column
        else
          content[i] = "\n#{column.content}" unless empty_column
        end
        i = i + 1
      end
    end

    table.content = "#{content.join("\n")}\n"
  end

  # remove unnecessary whitespaces and empty lines
  previous = ""
  @processed_plain_text_email = doc.css('body').text.split("\n").map do |line|
    # Nokogiri uses UTF-8 whitespace character for &nbsp and strip doesn't support those
    res = line.gsub(/^\p{Space}+|\p{Space}+$/, "")
    new_previous = res
    res = nil if res.empty? && previous.empty?
    previous = new_previous
    res
  end.compact.join("\n")
end