Class: Haml::Exec::HTML2Haml

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

Overview

The html2haml executable.

Constant Summary

Constants inherited from Generic

Generic::COLORS

Instance Method Summary collapse

Methods inherited from Generic

#color, #get_line, #parse, #parse!, #puts_action, #to_s

Constructor Details

#initialize(args) ⇒ HTML2Haml

Returns a new instance of HTML2Haml.

Parameters:

  • args (Array<String>)

    The command-line arguments



503
504
505
506
# File 'lib/haml/exec.rb', line 503

def initialize(args)
  super
  @module_opts = {}
end

Instance Method Details

#process_result

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



545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
# File 'lib/haml/exec.rb', line 545

def process_result
  super

  require 'haml/html'

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

  @module_opts[:erb] ||= input.respond_to?(:path) && input.path =~ /\.(rhtml|erb)$/
  @module_opts[:erb] &&= @options[:no_erb] != false

  output.write(::Haml::HTML.new(input, @module_opts).render)
rescue ::Haml::Error => e
  raise "#{e.is_a?(::Haml::SyntaxError) ? "Syntax error" : "Error"} on line " +
    "#{get_line e}: #{e.message}"
rescue LoadError => err
  dep = err.message.scan(/^no such file to load -- (.*)/)[0]
  raise err if @options[:trace] || dep.nil? || dep.empty?
  $stderr.puts "Required dependency #{dep} not found!\n  Use --trace for backtrace."
  exit 1
end

#set_opts(opts)

Tells optparse how to parse the arguments.

Parameters:

  • opts (OptionParser)


511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
# File 'lib/haml/exec.rb', line 511

def set_opts(opts)
  opts.banner = <<END
Usage: html2haml [options] [INPUT] [OUTPUT]

Description: Transforms an HTML file into corresponding Haml code.

Options:
END

  opts.on('-e', '--erb', 'Parse ERb tags.') do
    @module_opts[:erb] = true
  end

  opts.on('--no-erb', "Don't parse ERb tags.") do
    @options[:no_erb] = true
  end

  opts.on('-r', '--rhtml', 'Deprecated; same as --erb.') do
    @module_opts[:erb] = true
  end

  opts.on('--no-rhtml', "Deprecated; same as --no-erb.") do
    @options[:no_erb] = true
  end

  opts.on('-x', '--xhtml', 'Parse the input using the more strict XHTML parser.') do
    @module_opts[:xhtml] = true
  end

  super
end