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

The `haml` executable.

Methods

Public Class methods

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

[Source]

     # File lib/haml/exec.rb, line 277
277:       def initialize(args)
278:         super
279:         @name = "Haml"
280:         @options[:requires] = []
281:         @options[:load_paths] = []
282:       end

Public Instance methods

Processes the options set by the command-line arguments, and runs the Haml compiler appropriately.

[Source]

     # File lib/haml/exec.rb, line 320
320:       def process_result
321:         super
322:         input = @options[:input]
323:         output = @options[:output]
324: 
325:         template = input.read()
326:         input.close() if input.is_a? File
327: 
328:         begin
329:           engine = ::Haml::Engine.new(template, @options[:for_engine])
330:           if @options[:check_syntax]
331:             puts "Syntax OK"
332:             return
333:           end
334: 
335:           @options[:load_paths].each {|p| $LOAD_PATH << p}
336:           @options[:requires].each {|f| require f}
337: 
338:           if @options[:debug]
339:             puts engine.precompiled
340:             puts '=' * 100
341:           end
342: 
343:           result = engine.to_html
344:         rescue Exception => e
345:           raise e if @options[:trace]
346: 
347:           case e
348:           when ::Haml::SyntaxError; raise "Syntax error on line #{get_line e}: #{e.message}"
349:           when ::Haml::Error;       raise "Haml error on line #{get_line e}: #{e.message}"
350:           else raise "Exception on line #{get_line e}: #{e.message}\n  Use --trace for backtrace."
351:           end
352:         end
353: 
354:         output.write(result)
355:         output.close() if output.is_a? File
356:       end

Tells optparse how to parse the arguments.

@param opts [OptionParser]

[Source]

     # File lib/haml/exec.rb, line 287
287:       def set_opts(opts)
288:         super
289: 
290:         opts.on('-t', '--style NAME',
291:                 'Output style. Can be indented (default) or ugly.') do |name|
292:           @options[:for_engine][:ugly] = true if name.to_sym == :ugly
293:         end
294: 
295:         opts.on('-f', '--format NAME',
296:                 'Output format. Can be xhtml (default), html4, or html5.') do |name|
297:           @options[:for_engine][:format] = name.to_sym
298:         end
299: 
300:         opts.on('-e', '--escape-html',
301:                 'Escape HTML characters (like ampersands and angle brackets) by default.') do
302:           @options[:for_engine][:escape_html] = true
303:         end
304: 
305:         opts.on('-r', '--require FILE', "Same as 'ruby -r'.") do |file|
306:           @options[:requires] << file
307:         end
308: 
309:         opts.on('-I', '--load-path PATH', "Same as 'ruby -I'.") do |path|
310:           @options[:load_paths] << path
311:         end
312: 
313:         opts.on('--debug', "Print out the precompiled Ruby source.") do
314:           @options[:debug] = true
315:         end
316:       end

[Validate]