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