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