| Class | Haml::Exec::Haml |
| In: |
lib/haml/exec.rb
|
| Parent: | HamlSass |
The `haml` executable.
@param args [Array<String>] The command-line arguments
# File lib/haml/exec.rb, line 427
427: def initialize(args)
428: super
429: @name = "Haml"
430: @options[:requires] = []
431: @options[:load_paths] = []
432: end
Processes the options set by the command-line arguments, and runs the Haml compiler appropriately.
# File lib/haml/exec.rb, line 475
475: def process_result
476: super
477: input = @options[:input]
478: output = @options[:output]
479:
480: template = input.read()
481: input.close() if input.is_a? File
482:
483: begin
484: engine = ::Haml::Engine.new(template, @options[:for_engine])
485: if @options[:check_syntax]
486: puts "Syntax OK"
487: return
488: end
489:
490: @options[:load_paths].each {|p| $LOAD_PATH << p}
491: @options[:requires].each {|f| require f}
492:
493: if @options[:debug]
494: puts engine.precompiled
495: puts '=' * 100
496: end
497:
498: result = engine.to_html
499: rescue Exception => e
500: raise e if @options[:trace]
501:
502: case e
503: when ::Haml::SyntaxError; raise "Syntax error on line #{get_line e}: #{e.message}"
504: when ::Haml::Error; raise "Haml error on line #{get_line e}: #{e.message}"
505: else raise "Exception on line #{get_line e}: #{e.message}\n Use --trace for backtrace."
506: end
507: end
508:
509: output.write(result)
510: output.close() if output.is_a? File
511: end
Tells optparse how to parse the arguments.
@param opts [OptionParser]
# File lib/haml/exec.rb, line 437
437: def set_opts(opts)
438: super
439:
440: opts.on('-t', '--style NAME',
441: 'Output style. Can be indented (default) or ugly.') do |name|
442: @options[:for_engine][:ugly] = true if name.to_sym == :ugly
443: end
444:
445: opts.on('-f', '--format NAME',
446: 'Output format. Can be xhtml (default), html4, or html5.') do |name|
447: @options[:for_engine][:format] = name.to_sym
448: end
449:
450: opts.on('-e', '--escape-html',
451: 'Escape HTML characters (like ampersands and angle brackets) by default.') do
452: @options[:for_engine][:escape_html] = true
453: end
454:
455: opts.on('-q', '--double-quote-attributes',
456: 'Set attribute wrapper to double-quotes (default is single).') do
457: @options[:for_engine][:attr_wrapper] = '"'
458: end
459:
460: opts.on('-r', '--require FILE', "Same as 'ruby -r'.") do |file|
461: @options[:requires] << file
462: end
463:
464: opts.on('-I', '--load-path PATH', "Same as 'ruby -I'.") do |path|
465: @options[:load_paths] << path
466: end
467:
468: opts.on('--debug', "Print out the precompiled Ruby source.") do
469: @options[:debug] = true
470: end
471: end