In Files

Parent

Haml::Exec::Generic

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

Constants

COLORS

Public Class Methods

new(args) click to toggle source

@param args [Array] The command-line arguments

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

Public Instance Methods

parse() click to toggle source

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

@see #

    # 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
parse!() click to toggle source

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

@see #

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

@return [String] A description of the executable

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

Protected Instance Methods

color(color, str) click to toggle source

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.

     # File lib/haml/exec.rb, line 148
148:       def color(color, str)
149:         raise "[BUG] Unrecognized color #{color}" unless COLORS[color]
150: 
151:         # Almost any real Unix terminal will support color,
152:         # so we just filter for Windows terms (which don't set TERM)
153:         # and not-real terminals, which aren't ttys.
154:         return str if ENV["TERM"].nil? || ENV["TERM"].empty? || !STDOUT.tty?
155:         return "\e[#{COLORS[color]}m#{str}\e[0m"
156:       end
get_line(exception) click to toggle source

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

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

    # 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
process_result() click to toggle source

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.

     # File lib/haml/exec.rb, line 103
103:       def process_result
104:         input, output = @options[:input], @options[:output]
105:         args = @args.dup
106:         input ||=
107:           begin
108:             filename = args.shift
109:             @options[:filename] = filename
110:             open_file(filename) || $stdin
111:           end
112:         output ||= open_file(args.shift, 'w') || $stdout
113: 
114:         @options[:input], @options[:output] = input, output
115:       end
puts(*args) click to toggle source

Same as {Kernel.puts}, but doesn’t print anything if the `—quiet` option is set.

@param args [Array] Passed on to {Kernel.puts}

     # File lib/haml/exec.rb, line 134
134:       def puts(*args)
135:         return if @options[:for_engine][:quiet]
136:         Kernel.puts(*args)
137:       end
puts_action(name, color, arg) click to toggle source

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

@param name [#] 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`.
     # File lib/haml/exec.rb, line 126
126:       def puts_action(name, color, arg)
127:         return if @options[:for_engine][:quiet]
128:         printf color(color, "%11s %s\n"), name, arg
129:       end
set_opts(opts) click to toggle source

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]

    # 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:         opts.on('--unix-newlines', 'Use Unix-style newlines in written files.') do
83:           @options[:unix_newlines] = true if ::Haml::Util.windows?
84:         end
85: 
86:         opts.on_tail("-?", "-h", "--help", "Show this message") do
87:           puts opts
88:           exit
89:         end
90: 
91:         opts.on_tail("-v", "--version", "Print version") do
92:           puts("Haml #{::Haml.version[:string]}")
93:           exit
94:         end
95:       end

Private Instance Methods

handle_load_error(err) click to toggle source
     # File lib/haml/exec.rb, line 166
166:       def handle_load_error(err)
167:         dep = err.message[/^no such file to load -- (.*)/, 1]
168:         raise err if @options[:trace] || dep.nil? || dep.empty?
169:         $stderr.puts Required dependency #{dep} not found!    Run "gem install #{dep}" to get it.  Use --trace for backtrace.
170:         exit 1
171:       end
open_file(filename, flag = 'r') click to toggle source
     # File lib/haml/exec.rb, line 160
160:       def open_file(filename, flag = 'r')
161:         return if filename.nil?
162:         flag = 'wb' if @options[:unix_newlines] && flag == 'w'
163:         File.open(filename, flag)
164:       end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.