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 37
37:       def parse
38:         @opts = OptionParser.new(&method(:set_opts))
39:         @opts.parse!(@args)
40: 
41:         process_result
42: 
43:         @options
44:       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.puts "#{e.class}: #{e.message}"
27:           $stderr.puts "  Use --trace for backtrace."
28:           exit 1
29:         end
30:         exit 0
31:       end

@return [String] A description of the executable

[Source]

    # File lib/haml/exec.rb, line 47
47:       def to_s
48:         @opts.to_s
49:       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 141
141:       def color(color, str)
142:         raise "[BUG] Unrecognized color #{color}" unless COLORS[color]
143: 
144:         # Almost any real Unix terminal will support color,
145:         # so we just filter for Windows terms (which don't set TERM)
146:         # and not-real terminals, which aren't ttys.
147:         return str if ENV["TERM"].nil? || ENV["TERM"].empty? || !STDOUT.tty?
148:         return "\e[#{COLORS[color]}m#{str}\e[0m"
149:       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 58
58:       def get_line(exception)
59:         # SyntaxErrors have weird line reporting
60:         # when there's trailing whitespace,
61:         # which there is for Haml documents.
62:         return (exception.message.scan(/:(\d+)/).first || ["??"]).first if exception.is_a?(::SyntaxError)
63:         (exception.backtrace[0].scan(/:(\d+)/).first || ["??"]).first
64:       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 105
105:       def process_result
106:         input, output = @options[:input], @options[:output]
107:         args = @args.dup
108:         input ||=
109:           begin
110:             filename = args.shift
111:             @options[:filename] = filename
112:             open_file(filename) || $stdin
113:           end
114:         output ||= open_file(args.shift, 'w') || $stdout
115: 
116:         @options[:input], @options[:output] = input, output
117:       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 128
128:       def puts_action(name, color, arg)
129:         printf color(color, "%11s %s\n"), name, arg
130:       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 73
73:       def set_opts(opts)
74:         opts.on('-s', '--stdin', :NONE, 'Read input from standard input instead of an input file') do
75:           @options[:input] = $stdin
76:         end
77: 
78:         opts.on('--trace', :NONE, 'Show a full traceback on error') do
79:           @options[:trace] = true
80:         end
81: 
82:         if RbConfig::CONFIG['host_os'] =~ /mswin|windows/i
83:           opts.on('--unix-newlines', 'Use Unix-style newlines in written files.') do
84:             @options[:unix_newlines] = true
85:           end
86:         end
87: 
88:         opts.on_tail("-?", "-h", "--help", "Show this message") do
89:           puts opts
90:           exit
91:         end
92: 
93:         opts.on_tail("-v", "--version", "Print version") do
94:           puts("Haml/Sass #{::Haml.version[:string]}")
95:           exit
96:         end
97:       end

[Validate]