| Class | Haml::Exec::Haml |
| In: |
lib/haml/exec.rb
|
| Parent: | HamlSass |
The `haml` executable.
@param args [Array<String>] The command-line arguments
# File lib/haml/exec.rb, line 285
285: def initialize(args)
286: super
287: @name = "Haml"
288: @options[:requires] = []
289: @options[:load_paths] = []
290: end
Processes the options set by the command-line arguments, and runs the Haml compiler appropriately.
# File lib/haml/exec.rb, line 328
328: def process_result
329: super
330: input = @options[:input]
331: output = @options[:output]
332:
333: template = input.read()
334: input.close() if input.is_a? File
335:
336: begin
337: engine = ::Haml::Engine.new(template, @options[:for_engine])
338: if @options[:check_syntax]
339: puts "Syntax OK"
340: return
341: end
342:
343: @options[:load_paths].each {|p| $LOAD_PATH << p}
344: @options[:requires].each {|f| require f}
345:
346: if @options[:debug]
347: puts engine.precompiled
348: puts '=' * 100
349: end
350:
351: result = engine.to_html
352: rescue Exception => e
353: raise e if @options[:trace]
354:
355: case e
356: when ::Haml::SyntaxError; raise "Syntax error on line #{get_line e}: #{e.message}"
357: when ::Haml::Error; raise "Haml error on line #{get_line e}: #{e.message}"
358: else raise "Exception on line #{get_line e}: #{e.message}\n Use --trace for backtrace."
359: end
360: end
361:
362: output.write(result)
363: output.close() if output.is_a? File
364: end
Tells optparse how to parse the arguments.
@param opts [OptionParser]
# File lib/haml/exec.rb, line 295
295: def set_opts(opts)
296: super
297:
298: opts.on('-t', '--style NAME',
299: 'Output style. Can be indented (default) or ugly.') do |name|
300: @options[:for_engine][:ugly] = true if name.to_sym == :ugly
301: end
302:
303: opts.on('-f', '--format NAME',
304: 'Output format. Can be xhtml (default), html4, or html5.') do |name|
305: @options[:for_engine][:format] = name.to_sym
306: end
307:
308: opts.on('-e', '--escape-html',
309: 'Escape HTML characters (like ampersands and angle brackets) by default.') do
310: @options[:for_engine][:escape_html] = true
311: end
312:
313: opts.on('-r', '--require FILE', "Same as 'ruby -r'.") do |file|
314: @options[:requires] << file
315: end
316:
317: opts.on('-I', '--load-path PATH', "Same as 'ruby -I'.") do |path|
318: @options[:load_paths] << path
319: end
320:
321: opts.on('--debug', "Print out the precompiled Ruby source.") do
322: @options[:debug] = true
323: end
324: end