Class: Html2haml::Exec::HTML2Haml

Inherits:
Generic
  • Object
show all
Defined in:
lib/html2haml/exec.rb

Overview

The html2haml executable.

Instance Method Summary collapse

Methods inherited from Generic

#parse, #parse!, #to_s

Constructor Details

#initialize(args) ⇒ HTML2Haml

Returns a new instance of HTML2Haml.

Parameters:

  • args (Array<String>)

    The command-line arguments



189
190
191
192
# File 'lib/html2haml/exec.rb', line 189

def initialize(args)
  super
  @module_opts = {}
end

Instance Method Details

#process_resultObject

Processes the options set by the command-line arguments, and runs the HTML compiler appropriately.



233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/html2haml/exec.rb', line 233

def process_result
  super

  require 'html2haml/html'

  input = @options[:input]
  output = @options[:output]

  @module_opts[:erb] ||= input.respond_to?(:path) && input.path =~ /\.(rhtml|erb)$/
  @module_opts[:erb] &&= @options[:no_erb] != false

  output.write(HTML.new(input, @module_opts).render)
rescue ::Haml::Error => e
  raise "#{e.is_a?(::Haml::SyntaxError) ? "Syntax error" : "Error"} on line " +
    "#{get_line e}: #{e.message}"
rescue LoadError => err
  handle_load_error(err)
end

#set_opts(opts) ⇒ Object

Tells optparse how to parse the arguments.

Parameters:

  • opts (OptionParser)


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
# File 'lib/html2haml/exec.rb', line 197

def set_opts(opts)
  opts.banner = <<END
Usage: html2haml [options] [INPUT] [OUTPUT]

Description: Transforms an HTML file into corresponding Haml code.

Options:
END

  opts.on('-e', '--erb', 'Parse ERB tags.') do
    @module_opts[:erb] = true
  end

  opts.on('--no-erb', "Don't parse ERB tags.") do
    @options[:no_erb] = true
  end

  opts.on("--html-attributes", "Use HTML style attributes instead of Ruby hash style.") do
    @module_opts[:html_style_attributes] = true
  end

  opts.on("--ruby19-attributes", "Use Ruby 1.9-style attributes when possible.") do
    @module_opts[:ruby19_style_attributes] = true
  end

  opts.on('-E ex[:in]', 'Specify the default external and internal character encodings.') do |encoding|
    external, internal = encoding.split(':')
    Encoding.default_external = external if external && !external.empty?
    Encoding.default_internal = internal if internal && !internal.empty?
  end

  super
end