class Haml::Exec::Haml

The ‘haml` executable.

def initialize(args)

Parameters:
  • args (Array) -- The command-line arguments
def initialize(args)
  super
  @options[:for_engine] = {}
  @options[:requires] = []
  @options[:load_paths] = []
end

def process_result

and runs the Haml compiler appropriately.
Processes the options set by the command-line arguments,
def process_result
  super
  @options[:for_engine][:filename] = @options[:filename]
  input = @options[:input]
  output = @options[:output]
  template = input.read()
  input.close() if input.is_a? File
  @options[:load_paths].each {|p| $LOAD_PATH << p}
  @options[:requires].each {|f| require f}
  begin
    if @options[:parse]
      parser = ::Haml::Parser.new(::Haml::Options.new(@options))
      pp parser.call(template)
      return
    end
    engine = ::Haml::Engine.new(template, @options[:for_engine])
    if @options[:check_syntax]
      error = validate_ruby(engine.precompiled)
      if error
        puts error.message.split("\n").first
        exit 1
      end
      puts "Syntax OK"
      return
    end
    if @options[:debug]
      puts engine.precompiled
      error = validate_ruby(engine.precompiled)
      if error
        puts '=' * 100
        puts error.message.split("\n")[0]
        exit 1
      end
      return
    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}"
    end
  end
  output.write(result)
  output.close() if output.is_a? File
end

def set_opts(opts)

Parameters:
  • opts (OptionParser) --
def set_opts(opts)
  super
  opts.banner = <<END
 haml [options] [INPUT] [OUTPUT]
ption:
erts Haml files to HTML.
s:
  opts.on('-c', '--check', "Just check syntax, don't evaluate.") do
    require 'stringio'
    @options[:check_syntax] = true
    @options[:output] = StringIO.new
  end
  opts.on('-f', '--format NAME',
          'Output format. Can be html5 (default), xhtml, or html4.') 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('--no-escape-attrs',
          "Don't escape HTML characters (like ampersands and angle brackets) in attributes.") do
    @options[:for_engine][:escape_attrs] = false
  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('--remove-whitespace',
          'Remove whitespace surrounding and within tags') do
    @options[:for_engine][:remove_whitespace] = true
  end
  opts.on('--cdata',
          'Always add CDATA sections to javascript and css blocks.') do
    @options[:for_engine][:cdata] = true
  end
  opts.on('--autoclose LIST',
          'Comma separated list of elements to be automatically self-closed.') do |list|
    @options[:for_engine][:autoclose] = list.split(',')
  end
  opts.on('--suppress-eval',
          'Don\'t evaluate Ruby scripts.') do
    @options[:for_engine][:suppress_eval] = true
  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('-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
  opts.on('-d', '--debug', "Print out the precompiled Ruby source, and show syntax errors in the Ruby code.") do
    @options[:debug] = true
  end
  opts.on('-p', '--parse', "Print out Haml parse tree.") do
    @options[:parse] = true
  end
end

def validate_ruby(code)

def validate_ruby(code)
  eval("BEGIN {return nil}; #{code}", binding, @options[:filename] || "")
rescue ::SyntaxError # Not to be confused with Haml::SyntaxError
  $!
end