class PrettyMailer::HTML

Public Class Methods

new(body) click to toggle source
# File lib/pretty_mailer/html.rb, line 7
def initialize body
  @doc = Nokogiri::HTML body
end

Public Instance Methods

add_rules(selector, declarations, specificity) click to toggle source
# File lib/pretty_mailer/html.rb, line 11
def add_rules selector, declarations, specificity
  if selector =~ /^[\.\#a-z]/i
    begin
      @doc.css(selector).each do |tag|
        tag['specificities'] = "#{tag['specificities']}#{specificity};"
        tag[specificity.to_s] = declarations.squeeze ' '
      end
    rescue Nokogiri::CSS::SyntaxError
      Rails.logger.debug "Ignoring invalid selector: #{selector}"
    end
  end
end
reduce_specificities!() click to toggle source
# File lib/pretty_mailer/html.rb, line 24
def reduce_specificities!
  tags_with_specificities.each do |tag|
    specificities_on_tag(tag).each do |specificity|
      if tag['style'].blank?
        tag['style'] = tag[specificity]
      else
        merge_in_styles tag, specificity
      end
      tag.delete specificity
    end
    tag.delete 'specificities'
  end
end
to_s() click to toggle source
# File lib/pretty_mailer/html.rb, line 38
def to_s
  @doc.to_s
end

Private Instance Methods

merge_in_styles(tag, specificity) click to toggle source
# File lib/pretty_mailer/html.rb, line 52
def merge_in_styles tag, specificity
  if tag[specificity]
    tag[specificity].split(';').each do |rule|
      tag['style'] = tag['style'].sub /#{rule.split(':').first.strip}:\s*[a-z0-9]+\s*;/i, "#{rule};"
      tag['style'] += "#{rule};" unless $~
    end
  end
end
specificities_on_tag(tag) click to toggle source
# File lib/pretty_mailer/html.rb, line 48
def specificities_on_tag tag
  tag['specificities'].split(';').sort
end
tags_with_specificities() click to toggle source
# File lib/pretty_mailer/html.rb, line 44
def tags_with_specificities
  @doc.xpath '//*[@specificities]'
end