Class: Premailer

Inherits:
Object
  • Object
show all
Includes:
CssParser, HtmlToPlainText, Warnings
Defined in:
lib/premailer/adapter.rb,
lib/premailer/version.rb,
lib/premailer/premailer.rb,
lib/premailer/cached_rule_set.rb,
lib/premailer/adapter/nokogiri.rb,
lib/premailer/adapter/nokogumbo.rb,
lib/premailer/adapter/nokogiri_fast.rb

Defined Under Namespace

Modules: Adapter, Warnings Classes: CachedRuleSet

Constant Summary collapse

VERSION =

Premailer version.

'1.30.0'
CLIENT_SUPPORT_FILE =
File.dirname(__FILE__) + '/../../misc/client_support.yaml'
RE_UNMERGABLE_SELECTORS =

Unmergable selectors regexp.

/(:(visited|active|hover|focus|after|before|selection|target|first-(line|letter))|^@)/i
RE_RESET_SELECTORS =

Reset selectors regexp.

/^(:\#outlook|body.*|\.ReadMsgBody|\.ExternalClass|img|\#backgroundTable)$/
HTML_ENTITIES =
{
  "’" => "'",
  "…" => "...",
  "‘" => "'",
  "‚" => ',',
  "‛" => "'",
  "“" => '"',
  "”" => '"',
  "‐" => '-',
  "–" => '-',
  "—" => '--',
  "―" => '--'
}.freeze
{
  'h1' => { 'text-align' => 'align' },
  'h2' => { 'text-align' => 'align' },
  'h3' => { 'text-align' => 'align' },
  'h4' => { 'text-align' => 'align' },
  'h5' => { 'text-align' => 'align' },
  'h6' => { 'text-align' => 'align' },
  'p' => { 'text-align' => 'align' },
  'div' => { 'text-align' => 'align' },
  'blockquote' => { 'text-align' => 'align' },
  'body' => { 'background-color' => 'bgcolor' },
  'table' => {
    '-premailer-align' => 'align',
    'background-color' => 'bgcolor',
    'background-image' => 'background',
    '-premailer-width' => 'width',
    '-premailer-height' => 'height',
    '-premailer-cellpadding' => 'cellpadding',
    '-premailer-cellspacing' => 'cellspacing'
  },
  'tr' => {
    'text-align' => 'align',
    'background-color' => 'bgcolor',
    '-premailer-height' => 'height'
  },
  'th' => {
    'text-align' => 'align',
    'background-color' => 'bgcolor',
    'vertical-align' => 'valign',
    '-premailer-width' => 'width',
    '-premailer-height' => 'height'
  },
  'td' => {
    'text-align' => 'align',
    'background-color' => 'bgcolor',
    'vertical-align' => 'valign',
    '-premailer-width' => 'width',
    '-premailer-height' => 'height'
  },
  'img' => {
    'float' => 'align',
    '-premailer-width' => 'width',
    '-premailer-height' => 'height'
  }
}.freeze
WARN_LABEL =

Waning level names

['NONE', 'SAFE', 'POOR', 'RISKY'].freeze

Constants included from Warnings

Warnings::NONE, Warnings::POOR, Warnings::RISKY, Warnings::SAFE

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from HtmlToPlainText

#convert_to_text, #word_wrap

Constructor Details

#initialize(html, options = {}) ⇒ Premailer

Create a new Premailer object.

Parameters:

  • html

    is the HTML data to process. It can be either an IO object, the URL of a remote file, a local path or a raw HTML string. If passing an HTML string you must set the with_html_string option to true.

  • options (Hash) (defaults to: {})

    the options to handle html with.

Options Hash (options):

  • :line_length (Fixnum)

    Line length used by to_plain_text. Default is 65.

  • :warn_level (Fixnum)

    What level of CSS compatibility warnings to show (see Warnings, default is Warnings::SAFE).

  • :link_query_string (String)

    A string to append to every a href="" link. Do not include the initial ?.

  • :base_url (String)

    Used to calculate absolute URLs for local files.

  • :css (Array(String))

    Manually specify CSS stylesheets.

  • :css_to_attributes (Boolean)

    Copy related CSS attributes into HTML attributes (e.g. background-color to bgcolor). Default is true.

  • :preserve_style_attribute (Boolean)

    Preserve original style attribute

  • :css_string (String)

    Pass CSS as a string

  • :rgb_to_hex_attributes (Boolean)

    Convert RBG to Hex colors. Default is false.

  • :remove_ids (Boolean)

    Remove ID attributes whenever possible and convert IDs used as anchors to hashed to avoid collisions in webmail programs. Default is false.

  • :remove_classes (Boolean)

    Remove class attributes. Default is false.

  • :remove_comments (Boolean)

    Remove html comments. Default is false.

  • :remove_scripts (Boolean)

    Remove script elements. Default is true.

  • :reset_contenteditable (Boolean)

    Remove contenteditable attributes. Default is true.

  • :preserve_styles (Boolean)

    Whether to preserve any link rel=stylesheet and style elements. Default is false.

  • :preserve_reset (Boolean)

    Whether to preserve styles associated with the MailChimp reset code. Default is true.

  • :with_html_string (Boolean)

    Whether the html param should be treated as a raw string. Default is false.

  • :verbose (Boolean)

    Whether to print errors and warnings to $stderr. Default is false.

  • :io_exceptions (Boolean)

    Throws exceptions on I/O errors. Default is false.

  • :rule_set_exceptions (Boolean)

    Throws exceptions on invalid values in CSS Parser rule sets. Default is true.

  • :include_link_tags (Boolean)

    Whether to include css from link rel=stylesheet tags. Default is true.

  • :include_style_tags (Boolean)

    Whether to include css from style tags. Default is true.

  • :input_encoding (String)

    Manually specify the source documents encoding. This is a good idea. Default is ASCII-8BIT.

  • :replace_html_entities (Boolean)

    Convert HTML entities to actual characters. Default is false.

  • :escape_url_attributes (Boolean)

    URL Escapes href, src, and background attributes on elements. Default is true.

  • :adapter (Symbol)

    Which HTML parser to use, :nokogiri, :nokogiri_fast or :nokogumbo. Default is :nokogiri.

  • :output_encoding (String)

    Output encoding option for Nokogiri adapter. Should be set to "US-ASCII" to output HTML entities instead of Unicode characters.

  • :create_shorthands (Boolean)

    Combine several properties into a shorthand one, e.g. font: style weight size. Default is true.

  • :html_fragment (Boolean)

    Handle HTML fragment without any HTML content wrappers. Default is false.

  • :drop_unmergeable_css_rules (Boolean)

    Do not include unmergeable css rules in a