Method: Premailer#initialize

Defined in:
lib/premailer/premailer.rb

#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.

  • :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 <tt><style><tt> tag. Default is false.


182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/premailer/premailer.rb', line 182

def initialize(html, options = {})
  @options = {:warn_level => Warnings::SAFE,
              :line_length => 65,
              :link_query_string => nil,
              :base_url => nil,
              :rgb_to_hex_attributes => true,
              :remove_classes => false,
              :remove_ids => false,
              :remove_comments => false,
              :remove_scripts => true,
              :reset_contenteditable => true,
              :css => [],
              :css_to_attributes => true,
              :preserve_style_attribute => false,
              :with_html_string => false,
              :css_string => nil,
              :preserve_styles => false,
              :preserve_reset => true,
              :verbose => false,
              :debug => false,
              :io_exceptions => false,
              :include_link_tags => true,
              :include_style_tags => true,
              :input_encoding => 'ASCII-8BIT',
              :output_encoding => nil,
              :replace_html_entities => false,
              :escape_url_attributes => true,
              :unescaped_ampersand => false,
              :create_shorthands => true,
              :html_fragment => false,
              :adapter => Adapter.use,
              :drop_unmergeable_css_rules => false
              }.merge(options)

  @html_file = html
  @is_local_file = @options[:with_html_string] || Premailer.local_data?(html)

  @css_files = [@options[:css]].flatten

  @css_warnings = []

  @base_url = nil
  @base_dir = nil
  @unmergable_rules = nil

  if @options[:base_url]
    @base_url = Addressable::URI.parse(@options.delete(:base_url))
  elsif not @is_local_file
    @base_url = Addressable::URI.parse(@html_file)
  end

  @css_parser = CssParser::Parser.new({
    :absolute_paths => true,
    :import => true,
    :io_exceptions => @options[:io_exceptions]
  })

  @adapter_class = Adapter.find @options[:adapter]

  self.extend(@adapter_class)

  @doc = load_html(@html_file)

  @processed_doc = @doc
  @processed_doc = convert_inline_links(@processed_doc, @base_url) if @base_url
  if options[:link_query_string]
    @processed_doc = append_query_string(@processed_doc, options[:link_query_string])
  end
  load_css_from_options!
  load_css_from_html!
end