Class: Haml::Exec::Generic

Inherits:
Object show all
Defined in:
lib/haml/exec.rb

Overview

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

Direct Known Subclasses

CSS2Sass, HTML2Haml, HamlSass

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Generic

Returns a new instance of Generic.

Parameters:

  • args (Array<String>)

    The command-line arguments



10
11
12
13
# File 'lib/haml/exec.rb', line 10

def initialize(args)
  @args = args
  @options = {}
end

Instance Method Details

#get_line(exception) ⇒ String (protected)

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

Parameters:

  • exception (Exception)

    The exception

Returns:

  • (String)

    The line number



46
47
48
49
50
51
52
# File 'lib/haml/exec.rb', line 46

def get_line(exception)
  # SyntaxErrors have weird line reporting
  # when there's trailing whitespace,
  # which there is for Haml documents.
  return exception.message.scan(/:(\d+)/).first.first if exception.is_a?(::SyntaxError)
  exception.backtrace[0].scan(/:(\d+)/).first.first
end

#parse!Object

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



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/haml/exec.rb', line 17

def parse!
  begin
    @opts = OptionParser.new(&method(:set_opts))
    @opts.parse!(@args)

    process_result

    @options
  rescue Exception => e
    raise e if @options[:trace] || e.is_a?(SystemExit)

    $stderr.puts e.message
    exit 1
  end
  exit 0
end

#process_resultObject (protected)

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.



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/haml/exec.rb', line 87

def process_result
  input, output = @options[:input], @options[:output]
  input_file, output_file = if input
                              [nil, open_file(@args[0], 'w')]
                            else
                              @options[:filename] = @args[0]
                              [open_file(@args[0]), open_file(@args[1], 'w')]
                            end

  input  ||= input_file
  output ||= output_file
  input  ||= $stdin
  output ||= $stdout

  @options[:input], @options[:output] = input, output
end

#set_opts(opts) ⇒ Object (protected)

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.

Parameters:

  • opts (OptionParser)


61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/haml/exec.rb', line 61

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/Sass #{::Haml.version[:string]}")
    exit
  end
end

#to_sString

Returns A description of the executable.

Returns:

  • (String)

    A description of the executable



35
36
37
# File 'lib/haml/exec.rb', line 35

def to_s
  @opts.to_s
end