Class: Haml::Exec::Haml

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

Overview

The haml executable.

Instance Method Summary collapse

Methods inherited from Generic

#get_line, #parse!, #to_s

Constructor Details

#initialize(args) ⇒ Haml

Returns a new instance of Haml.

Parameters:

  • args (Array<String>)

    The command-line arguments



376
377
378
379
380
381
# File 'lib/haml/exec.rb', line 376

def initialize(args)
  super
  @name = "Haml"
  @options[:requires] = []
  @options[:load_paths] = []
end

Instance Method Details

#process_result

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



424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
# File 'lib/haml/exec.rb', line 424

def process_result
  super
  input = @options[:input]
  output = @options[:output]

  template = input.read()
  input.close() if input.is_a? File

  begin
    engine = ::Haml::Engine.new(template, @options[:for_engine])
    if @options[:check_syntax]
      puts "Syntax OK"
      return
    end

    @options[:load_paths].each {|p| $LOAD_PATH << p}
    @options[:requires].each {|f| require f}

    if @options[:debug]
      puts engine.precompiled
      puts '=' * 100
    end

    result = engine.to_html
  rescue Exception => e
    raise e if @options[:trace]

    case e
    when ::Haml::SyntaxError; raise "Syntax error on line #{get_line e}: #{e.message}"
    when ::Haml::Error;       raise "Haml error on line #{get_line e}: #{e.message}"
    else raise "Exception on line #{get_line e}: #{e.message}\n  Use --trace for backtrace."
    end
  end

  output.write(result)
  output.close() if output.is_a? File
end

#set_opts(opts)

Tells optparse how to parse the arguments.

Parameters:

  • opts (OptionParser)


386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
# File 'lib/haml/exec.rb', line 386

def set_opts(opts)
  super

  opts.on('-t', '--style NAME',
          'Output style. Can be indented (default) or ugly.') do |name|
    @options[:for_engine][:ugly] = true if name.to_sym == :ugly
  end

  opts.on('-f', '--format NAME',
          'Output format. Can be xhtml (default), html4, or html5.') do |name|
    @options[:for_engine][:format] = name.to_sym
  end

  opts.on('-e', '--escape-html',
          'Escape HTML characters (like ampersands and angle brackets) by default.') do
    @options[:for_engine][:escape_html] = true
  end

  opts.on('-q', '--double-quote-attributes',
          'Set attribute wrapper to double-quotes (default is single).') do
    @options[:for_engine][:attr_wrapper] = '"'
  end

  opts.on('-r', '--require FILE', "Same as 'ruby -r'.") do |file|
    @options[:requires] << file
  end

  opts.on('-I', '--load-path PATH', "Same as 'ruby -I'.") do |path|
    @options[:load_paths] << path
  end

  opts.on('--debug', "Print out the precompiled Ruby source.") do
    @options[:debug] = true
  end
end