Method: Premailer#initialize

Defined in:
lib/premailer/premailer.rb

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

Create a new Premailer object.

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

[+line_length+] Line length used by to_plain_text. Boolean, default is 65. [+warn_level+] What level of CSS compatibility warnings to show (see Warnings). [+link_query_string+] A string to append to every a href="" link. Do not include the initial ?. [+base_url+] Used to calculate absolute URLs for local files. [+css+] Manually specify CSS stylesheets. [+css_to_attributes+] Copy related CSS attributes into HTML attributes (e.g. background-color to bgcolor) [+css_string+] Pass CSS as a string [+remove_ids+] Remove ID attributes whenever possible and convert IDs used as anchors to hashed to avoid collisions in webmail programs. Default is false. [+remove_classes+] Remove class attributes. Default is false. [+remove_comments+] Remove html comments. Default is false. [+preserve_styles+] Whether to preserve any link rel=stylesheet and style elements. Default is false. [+preserve_reset+] Whether to preserve styles associated with the MailChimp reset code [+with_html_string+] Whether the html param should be treated as a raw string. [+verbose+] Whether to print errors and warnings to $stderr. Default is false. [+adapter+] Which HTML parser to use, either :nokogiri or :hpricot. Default is :hpricot.



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/premailer/premailer.rb', line 136

def initialize(html, options = {})
  @options = {:warn_level => Warnings::SAFE,
              :line_length => 65,
              :link_query_string => nil,
              :base_url => nil,
              :remove_classes => false,
              :remove_ids => false,
              :remove_comments => false,
              :css => [],
              :css_to_attributes => true,
              :with_html_string => false,
              :css_string => nil,
              :preserve_styles => false,
              :preserve_reset => true,
              :verbose => false,
              :debug => false,
              :io_exceptions => false,
              :adapter => Adapter.use}.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 = URI.parse(@options.delete(:base_url))
  elsif not @is_local_file
    @base_url = 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.class.send(:include, @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