Class: Haml::Exec::HTML2Haml

Inherits:
Generic show all
Defined in:
lib/haml/exec.rb

Overview

The html2haml executable.

Instance Method Summary collapse

Methods inherited from Generic

#get_line, #parse!, #to_s

Constructor Details

#initialize(args) ⇒ HTML2Haml

Returns a new instance of HTML2Haml.

Parameters:

  • args (Array<String>)

    The command-line arguments



363
364
365
366
367
368
369
370
371
372
373
374
375
376
# File 'lib/haml/exec.rb', line 363

def initialize(args)
  super

  @module_opts = {}

  begin
    require 'haml/html'
  rescue LoadError => err
    dep = err.message.scan(/^no such file to load -- (.*)/)[0]
    raise err if @options[:trace] || dep.nil? || dep.empty?
    $stderr.puts "Required dependency #{dep} not found!\n  Use --trace for backtrace."
    exit 1
  end
end

Instance Method Details

#process_resultObject

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



407
408
409
410
411
412
413
414
415
416
417
418
419
420
# File 'lib/haml/exec.rb', line 407

def process_result
  super

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

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

  output.write(::Haml::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}"
end

#set_opts(opts) ⇒ Object

Tells optparse how to parse the arguments.

Parameters:

  • opts (OptionParser)


381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
# File 'lib/haml/exec.rb', line 381

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('-r', '--rhtml', 'Parse RHTML tags.') do
    @module_opts[:rhtml] = true
  end

  opts.on('--no-rhtml', "Don't parse RHTML tags.") do
    @options[:no_rhtml] = true
  end

  opts.on('-x', '--xhtml', 'Parse the input using the more strict XHTML parser.') do
    @module_opts[:xhtml] = true
  end

  super
end