require File.dirname(__FILE__) + '/../haml' require 'optparse' module Haml # This module contains code for working with the # haml, sass, and haml2html executables, # such as command-line parsing stuff. # It shouldn't need to be invoked by client code. module Exec # :nodoc: # A class that encapsulates the executable code # for all three executables. class Generic # :nodoc: def initialize(args) @args = args @options = {} end def parse! begin @opts = OptionParser.new(&(method(:set_opts).to_proc)) @opts.parse!(@args) process_result @options rescue Exception => e raise e if e.is_a? SystemExit line = e.backtrace[0].scan(/:(.*)/)[0] puts "#{e.class} on line #{line}: #{e.message}" if @options[:trace] e.backtrace[1..-1].each { |t| puts " #{t}" } else puts " Use --trace to see traceback" end exit 1 end exit 0 end def to_s @opts.to_s end private def set_opts(opts) opts.on('s', '--stdin', :NONE, 'Read input from standard input instead of an input file') do @options[:input] = $stdin end opts.on('--trace', :NONE, 'Show a full traceback on error') do @options[:trace] = true end opts.on_tail("-?", "-h", "--help", "Show this message") do puts opts exit end opts.on_tail("-v", "--version", "Print version") do puts("Haml " + File.read(File.dirname(__FILE__) + '/../../VERSION')) exit end end def process_result input, output = @options[:input], @options[:output] input_file, output_file = if input [nil, open_file(ARGV[0], 'w')] else [open_file(ARGV[0]), open_file(ARGV[1], 'w')] end input ||= input_file output ||= output_file input ||= $stdin output ||= $stdout @options[:input], @options[:output] = input, output end def open_file(filename, flag = 'r') return if filename.nil? File.open(filename, flag) end end # A class encapsulating the executable functionality # specific to Haml and Sass. class HamlSass < Generic # :nodoc: private def set_opts(opts) opts.banner = < err dep = err.message.scan(/^no such file to load -- (.*)/)[0] puts "Required dependency #{dep} not found!" exit 1 end end def set_opts(opts) opts.banner = <