Class: Getinline::Transformer

Inherits:
Object
  • Object
show all
Defined in:
lib/getinline/transformer.rb

Instance Method Summary collapse

Constructor Details

#initialize(raw_text, options = {}, premailer_options = {}) ⇒ Transformer

Returns a new instance of Transformer.



9
10
11
12
13
# File 'lib/getinline/transformer.rb', line 9

def initialize(raw_text, options = {}, premailer_options = {})
  @raw_text = raw_text
  @options = options
  @premailer_options = premailer_options
end

Instance Method Details

#transformObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/getinline/transformer.rb', line 15

def transform
  matches = @raw_text.scan(/<%.+?%>/)
  tokenized_text = @raw_text.dup
  matches.each do |match|
    tokenized_text.sub!(match, TOKEN)
  end

  File.write(TOKENIZED_ERB_FILE_NAME, tokenized_text)

  @premailer = Premailer.new(TOKENIZED_ERB_FILE_NAME, @premailer_options)
  premailed_tokenized_text = @options[:mode] == :txt ?
    @premailer.to_plain_text : Nokogiri::HTML(@premailer.to_inline_css).to_html(encoding:'US-ASCII')
  premailed_text = premailed_tokenized_text.dup

  matches.each do |match|
    premailed_text.sub!(TOKEN, match)
  end

  premailed_text
end