Class: Haml::Exec::Haml

Inherits:
HamlSass show all
Defined in:
/var/www/haml-pages/.haml/lib/haml/exec.rb

Overview

The haml executable.

Constant Summary

Instance Method Summary

Methods inherited from Generic

#color, #get_line, #parse, #parse!, #puts_action, #to_s

Constructor Details

- (Haml) initialize(args)

A new instance of Haml

Parameters:

  • (Array<String>) args — The command-line arguments


485
486
487
488
489
490
# File '/var/www/haml-pages/.haml/lib/haml/exec.rb', line 485

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.



541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
# File '/var/www/haml-pages/.haml/lib/haml/exec.rb', line 541

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:

  • (OptionParser) opts


495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
# File '/var/www/haml-pages/.haml/lib/haml/exec.rb', line 495

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

  unless ::Haml::Util.ruby1_8?
    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
  end

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