Class Haml::Exec::Haml
In: lib/haml/exec.rb
Parent: HamlSass

The `haml` executable.

Methods

Public Class methods

@param args [Array<String>] The command-line arguments

[Source]

     # File lib/haml/exec.rb, line 438
438:       def initialize(args)
439:         super
440:         @name = "Haml"
441:         @options[:requires] = []
442:         @options[:load_paths] = []
443:       end

Public Instance methods

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

[Source]

     # File lib/haml/exec.rb, line 486
486:       def process_result
487:         super
488:         input = @options[:input]
489:         output = @options[:output]
490: 
491:         template = input.read()
492:         input.close() if input.is_a? File
493: 
494:         begin
495:           engine = ::Haml::Engine.new(template, @options[:for_engine])
496:           if @options[:check_syntax]
497:             puts "Syntax OK"
498:             return
499:           end
500: 
501:           @options[:load_paths].each {|p| $LOAD_PATH << p}
502:           @options[:requires].each {|f| require f}
503: 
504:           if @options[:debug]
505:             puts engine.precompiled
506:             puts '=' * 100
507:           end
508: 
509:           result = engine.to_html
510:         rescue Exception => e
511:           raise e if @options[:trace]
512: 
513:           case e
514:           when ::Haml::SyntaxError; raise "Syntax error on line #{get_line e}: #{e.message}"
515:           when ::Haml::Error;       raise "Haml error on line #{get_line e}: #{e.message}"
516:           else raise "Exception on line #{get_line e}: #{e.message}\n  Use --trace for backtrace."
517:           end
518:         end
519: 
520:         output.write(result)
521:         output.close() if output.is_a? File
522:       end

Tells optparse how to parse the arguments.

@param opts [OptionParser]

[Source]

     # File lib/haml/exec.rb, line 448
448:       def set_opts(opts)
449:         super
450: 
451:         opts.on('-t', '--style NAME',
452:                 'Output style. Can be indented (default) or ugly.') do |name|
453:           @options[:for_engine][:ugly] = true if name.to_sym == :ugly
454:         end
455: 
456:         opts.on('-f', '--format NAME',
457:                 'Output format. Can be xhtml (default), html4, or html5.') do |name|
458:           @options[:for_engine][:format] = name.to_sym
459:         end
460: 
461:         opts.on('-e', '--escape-html',
462:                 'Escape HTML characters (like ampersands and angle brackets) by default.') do
463:           @options[:for_engine][:escape_html] = true
464:         end
465: 
466:         opts.on('-q', '--double-quote-attributes',
467:                 'Set attribute wrapper to double-quotes (default is single).') do
468:           @options[:for_engine][:attr_wrapper] = '"'
469:         end
470: 
471:         opts.on('-r', '--require FILE', "Same as 'ruby -r'.") do |file|
472:           @options[:requires] << file
473:         end
474: 
475:         opts.on('-I', '--load-path PATH', "Same as 'ruby -I'.") do |path|
476:           @options[:load_paths] << path
477:         end
478: 
479:         opts.on('--debug', "Print out the precompiled Ruby source.") do
480:           @options[:debug] = true
481:         end
482:       end

[Validate]