| 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 445
445: def initialize(args)
446: super
447: @name = "Haml"
448: @options[:requires] = []
449: @options[:load_paths] = []
450: end
Processes the options set by the command-line arguments, and runs the Haml compiler appropriately.
# File lib/haml/exec.rb, line 501
501: def process_result
502: super
503: input = @options[:input]
504: output = @options[:output]
505:
506: template = input.read()
507: input.close() if input.is_a? File
508:
509: begin
510: engine = ::Haml::Engine.new(template, @options[:for_engine])
511: if @options[:check_syntax]
512: puts "Syntax OK"
513: return
514: end
515:
516: @options[:load_paths].each {|p| $LOAD_PATH << p}
517: @options[:requires].each {|f| require f}
518:
519: if @options[:debug]
520: puts engine.precompiled
521: puts '=' * 100
522: end
523:
524: result = engine.to_html
525: rescue Exception => e
526: raise e if @options[:trace]
527:
528: case e
529: when ::Haml::SyntaxError; raise "Syntax error on line #{get_line e}: #{e.message}"
530: when ::Haml::Error; raise "Haml error on line #{get_line e}: #{e.message}"
531: else raise "Exception on line #{get_line e}: #{e.message}\n Use --trace for backtrace."
532: end
533: end
534:
535: output.write(result)
536: output.close() if output.is_a? File
537: end
Tells optparse how to parse the arguments.
@param opts [OptionParser]
# File lib/haml/exec.rb, line 455
455: def set_opts(opts)
456: super
457:
458: opts.on('-t', '--style NAME',
459: 'Output style. Can be indented (default) or ugly.') do |name|
460: @options[:for_engine][:ugly] = true if name.to_sym == :ugly
461: end
462:
463: opts.on('-f', '--format NAME',
464: 'Output format. Can be xhtml (default), html4, or html5.') do |name|
465: @options[:for_engine][:format] = name.to_sym
466: end
467:
468: opts.on('-e', '--escape-html',
469: 'Escape HTML characters (like ampersands and angle brackets) by default.') do
470: @options[:for_engine][:escape_html] = true
471: end
472:
473: opts.on('-q', '--double-quote-attributes',
474: 'Set attribute wrapper to double-quotes (default is single).') do
475: @options[:for_engine][:attr_wrapper] = '"'
476: end
477:
478: opts.on('-r', '--require FILE', "Same as 'ruby -r'.") do |file|
479: @options[:requires] << file
480: end
481:
482: opts.on('-I', '--load-path PATH', "Same as 'ruby -I'.") do |path|
483: @options[:load_paths] << path
484: end
485:
486: unless ::Haml::Util.ruby1_8?
487: opts.on('-E ex[:in]', 'Specify the default external and internal character encodings.') do |encoding|
488: external, internal = encoding.split(':')
489: Encoding.default_external = external if external && !external.empty?
490: Encoding.default_internal = internal if internal && !internal.empty?
491: end
492: end
493:
494: opts.on('--debug', "Print out the precompiled Ruby source.") do
495: @options[:debug] = true
496: end
497: end