| 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 492
492: def initialize(args)
493: super
494: @name = "Haml"
495: @options[:requires] = []
496: @options[:load_paths] = []
497: end
Processes the options set by the command-line arguments, and runs the Haml compiler appropriately.
# File lib/haml/exec.rb, line 548
548: def process_result
549: super
550: input = @options[:input]
551: output = @options[:output]
552:
553: template = input.read()
554: input.close() if input.is_a? File
555:
556: begin
557: engine = ::Haml::Engine.new(template, @options[:for_engine])
558: if @options[:check_syntax]
559: puts "Syntax OK"
560: return
561: end
562:
563: @options[:load_paths].each {|p| $LOAD_PATH << p}
564: @options[:requires].each {|f| require f}
565:
566: if @options[:debug]
567: puts engine.precompiled
568: puts '=' * 100
569: end
570:
571: result = engine.to_html
572: rescue Exception => e
573: raise e if @options[:trace]
574:
575: case e
576: when ::Haml::SyntaxError; raise "Syntax error on line #{get_line e}: #{e.message}"
577: when ::Haml::Error; raise "Haml error on line #{get_line e}: #{e.message}"
578: else raise "Exception on line #{get_line e}: #{e.message}\n Use --trace for backtrace."
579: end
580: end
581:
582: output.write(result)
583: output.close() if output.is_a? File
584: end
Tells optparse how to parse the arguments.
@param opts [OptionParser]
# File lib/haml/exec.rb, line 502
502: def set_opts(opts)
503: super
504:
505: opts.on('-t', '--style NAME',
506: 'Output style. Can be indented (default) or ugly.') do |name|
507: @options[:for_engine][:ugly] = true if name.to_sym == :ugly
508: end
509:
510: opts.on('-f', '--format NAME',
511: 'Output format. Can be xhtml (default), html4, or html5.') do |name|
512: @options[:for_engine][:format] = name.to_sym
513: end
514:
515: opts.on('-e', '--escape-html',
516: 'Escape HTML characters (like ampersands and angle brackets) by default.') do
517: @options[:for_engine][:escape_html] = true
518: end
519:
520: opts.on('-q', '--double-quote-attributes',
521: 'Set attribute wrapper to double-quotes (default is single).') do
522: @options[:for_engine][:attr_wrapper] = '"'
523: end
524:
525: opts.on('-r', '--require FILE', "Same as 'ruby -r'.") do |file|
526: @options[:requires] << file
527: end
528:
529: opts.on('-I', '--load-path PATH', "Same as 'ruby -I'.") do |path|
530: @options[:load_paths] << path
531: end
532:
533: unless ::Haml::Util.ruby1_8?
534: opts.on('-E ex[:in]', 'Specify the default external and internal character encodings.') do |encoding|
535: external, internal = encoding.split(':')
536: Encoding.default_external = external if external && !external.empty?
537: Encoding.default_internal = internal if internal && !internal.empty?
538: end
539: end
540:
541: opts.on('--debug', "Print out the precompiled Ruby source.") do
542: @options[:debug] = true
543: end
544: end