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