Class List Method List File List

# Class: Haml::Exec::Haml

Inherits:
  Generic
  - Object
  - Generic
  - Haml::Exec::Haml
  show all
Defined in:
  lib/haml/exec.rb

## Overview

The `haml` executable.

## Constant Summary

## Instance Method Summary (collapse)

- - (Haml) initialize(args) constructor

  A new instance of Haml.

- - process_result

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

- - set_opts(opts)

  Tells optparse how to parse the arguments.

### Methods inherited from Generic

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

## Constructor Details

### - (`Haml`) initialize(args)

Returns a new instance of Haml

Parameters:

- args (`Array<String>`) —

  The command-line arguments

    187
    188
    189
    190
    191
    192

    # File 'lib/haml/exec.rb', line 187

    def initialize(args)
      super
      @options[:for_engine] = {}
      @options[:requires] = []
      @options[:load_paths] = []
    end

## Instance Method Details

### - process_result

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

    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326

    # File 'lib/haml/exec.rb', line 283

    def process_result
      super
      @options[:for_engine][:filename] = @options[:filename]
      input = @options[:input]
      output = @options[:output]

      template = input.read()
      input.close() if input.is_a? File

      @options[:load_paths].each {|p| $LOAD_PATH << p}
      @options[:requires].each {|f| require f}

      begin

        engine = ::Haml::Engine.new(template, @options[:for_engine])
        if @options[:check_syntax]
          puts "Syntax OK"
          return
        end

        if @options[:parse]
          pp engine.parser.root
          return
        end

        if @options[:debug]
          puts engine.precompiled
          puts '=' * 100
        end

        result = engine.to_html
      rescue Exception => e
        raise e if @options[:trace]

        case e
        when ::Haml::SyntaxError; raise "Syntax error on line #{get_line e}: #{e.message}"
        when ::Haml::Error;       raise "Haml error on line #{get_line e}: #{e.message}"
        else raise "Exception on line #{get_line e}: #{e.message}"
        end
      end

      output.write(result)
      output.close() if output.is_a? File
    end

### - set_opts(opts)

Tells optparse how to parse the arguments.

Parameters:

- opts (`OptionParser`)

    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279

    # File 'lib/haml/exec.rb', line 197

    def set_opts(opts)
      super

      opts.banner = <<END
    Usage: haml [options] [INPUT] [OUTPUT]

    Description:
      Converts Haml files to HTML.

    Options:
    END

      opts.on('-c', '--check', "Just check syntax, don't evaluate.") do
        require 'stringio'
        @options[:check_syntax] = true
        @options[:output] = StringIO.new
      end

      opts.on('-t', '--style NAME',
              'Output style. Can be indented (default) or ugly.') do |name|
        @options[:for_engine][:ugly] = true if name.to_sym == :ugly
      end

      opts.on('-f', '--format NAME',
              'Output format. Can be html5 (default), xhtml, or html4.') do |name|
        @options[:for_engine][:format] = name.to_sym
      end

      opts.on('-e', '--escape-html',
              'Escape HTML characters (like ampersands and angle brackets) by default.') do
        @options[:for_engine][:escape_html] = true
      end

      opts.on('--no-escape-attrs',
              "Don't escape HTML characters (like ampersands and angle brackets) in attributes.") do
        @options[:for_engine][:escape_attrs] = false
      end

      opts.on('-q', '--double-quote-attributes',
              'Set attribute wrapper to double-quotes (default is single).') do
        @options[:for_engine][:attr_wrapper] = '"'
      end

      opts.on('--cdata',
              'Always add CDATA sections to javascript and css blocks.') do
        @options[:for_engine][:cdata] = true
      end

      opts.on('--autoclose LIST',
              'Comma separated list of elements to be automatically self-closed.') do |list|
        @options[:for_engine][:autoclose] = list.split(',')
      end

      opts.on('--suppress-eval',
              'Don\'t evaluate Ruby scripts.') do
        @options[:for_engine][:suppress_eval] = true
      end

      opts.on('-r', '--require FILE', "Same as 'ruby -r'.") do |file|
        @options[:requires] << file
      end

      opts.on('-I', '--load-path PATH', "Same as 'ruby -I'.") do |path|
        @options[:load_paths] << path
      end

      unless RUBY_VERSION < "1.9"
        opts.on('-E ex[:in]', 'Specify the default external and internal character encodings.') do |encoding|
          external, internal = encoding.split(':')
          Encoding.default_external = external if external && !external.empty?
          Encoding.default_internal = internal if internal && !internal.empty?
        end
      end

      opts.on('-d', '--debug', "Print out the precompiled Ruby source.") do
        @options[:debug] = true
      end

      opts.on('-p', '--parse', "Print out Haml parse tree.") do
        @options[:parse] = true
      end

    end