Class Haml::Exec::Generic
In: lib/haml/exec.rb
Parent: Object

An abstract class that encapsulates the executable code for all three executables.

Methods

color   get_line   new   parse   parse!   process_result   puts_action   set_opts   to_s  

Constants

COLORS = { :red => 31, :green => 32, :yellow => 33 }

Public Class methods

@param args [Array<String>] The command-line arguments

[Source]

    # File lib/haml/exec.rb, line 11
11:       def initialize(args)
12:         @args = args
13:         @options = {}
14:       end

Public Instance methods

Parses the command-line arguments and runs the executable. This does not handle exceptions or exit the program.

@see parse!

[Source]

    # File lib/haml/exec.rb, line 38
38:       def parse
39:         @opts = OptionParser.new(&method(:set_opts))
40:         @opts.parse!(@args)
41: 
42:         process_result
43: 
44:         @options
45:       end

Parses the command-line arguments and runs the executable. Calls `Kernel#exit` at the end, so it never returns.

@see parse

[Source]

    # File lib/haml/exec.rb, line 20
20:       def parse!
21:         begin
22:           parse
23:         rescue Exception => e
24:           raise e if @options[:trace] || e.is_a?(SystemExit)
25: 
26:           $stderr.print "#{e.class}: " unless e.class == RuntimeError
27:           $stderr.puts "#{e.message}"
28:           $stderr.puts "  Use --trace for backtrace."
29:           exit 1
30:         end
31:         exit 0
32:       end

@return [String] A description of the executable

[Source]

    # File lib/haml/exec.rb, line 48
48:       def to_s
49:         @opts.to_s
50:       end

Protected Instance methods

Wraps the given string in terminal escapes causing it to have the given color. If terminal esapes aren‘t supported on this platform, just returns the string instead.

@param color [Symbol] The name of the color to use.

  Can be `:red`, `:green`, or `:yellow`.

@param str [String] The string to wrap in the given color. @return [String] The wrapped string.

[Source]

     # File lib/haml/exec.rb, line 142
142:       def color(color, str)
143:         raise "[BUG] Unrecognized color #{color}" unless COLORS[color]
144: 
145:         # Almost any real Unix terminal will support color,
146:         # so we just filter for Windows terms (which don't set TERM)
147:         # and not-real terminals, which aren't ttys.
148:         return str if ENV["TERM"].nil? || ENV["TERM"].empty? || !STDOUT.tty?
149:         return "\e[#{COLORS[color]}m#{str}\e[0m"
150:       end

Finds the line of the source template on which an exception was raised.

@param exception [Exception] The exception @return [String] The line number

[Source]

    # File lib/haml/exec.rb, line 59
59:       def get_line(exception)
60:         # SyntaxErrors have weird line reporting
61:         # when there's trailing whitespace,
62:         # which there is for Haml documents.
63:         return (exception.message.scan(/:(\d+)/).first || ["??"]).first if exception.is_a?(::SyntaxError)
64:         (exception.backtrace[0].scan(/:(\d+)/).first || ["??"]).first
65:       end

Processes the options set by the command-line arguments. In particular, sets `@options[:input]` and `@options[:output]` to appropriate IO streams.

This is meant to be overridden by subclasses so they can run their respective programs.

[Source]

     # File lib/haml/exec.rb, line 106
106:       def process_result
107:         input, output = @options[:input], @options[:output]
108:         args = @args.dup
109:         input ||=
110:           begin
111:             filename = args.shift
112:             @options[:filename] = filename
113:             open_file(filename) || $stdin
114:           end
115:         output ||= open_file(args.shift, 'w') || $stdout
116: 
117:         @options[:input], @options[:output] = input, output
118:       end

Prints a status message about performing the given action, colored using the given color (via terminal escapes) if possible.

@param name [to_s] A short name for the action being performed.

  Shouldn't be longer than 11 characters.

@param color [Symbol] The name of the color to use for this action.

  Can be `:red`, `:green`, or `:yellow`.

[Source]

     # File lib/haml/exec.rb, line 129
129:       def puts_action(name, color, arg)
130:         printf color(color, "%11s %s\n"), name, arg
131:       end

Tells optparse how to parse the arguments available for all executables.

This is meant to be overridden by subclasses so they can add their own options.

@param opts [OptionParser]

[Source]

    # File lib/haml/exec.rb, line 74
74:       def set_opts(opts)
75:         opts.on('-s', '--stdin', :NONE, 'Read input from standard input instead of an input file') do
76:           @options[:input] = $stdin
77:         end
78: 
79:         opts.on('--trace', :NONE, 'Show a full traceback on error') do
80:           @options[:trace] = true
81:         end
82: 
83:         if RbConfig::CONFIG['host_os'] =~ /mswin|windows/i
84:           opts.on('--unix-newlines', 'Use Unix-style newlines in written files.') do
85:             @options[:unix_newlines] = true
86:           end
87:         end
88: 
89:         opts.on_tail("-?", "-h", "--help", "Show this message") do
90:           puts opts
91:           exit
92:         end
93: 
94:         opts.on_tail("-v", "--version", "Print version") do
95:           puts("Haml/Sass #{::Haml.version[:string]}")
96:           exit
97:         end
98:       end

[Validate]