Class: Generator::HamlGenerator

Inherits:
Base
  • Object
show all
Defined in:
lib/generator/haml_generator.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

cache, checksum, #compile_file, #write

Constructor Details

#initializeHamlGenerator

Returns a new instance of HamlGenerator.



12
13
14
15
16
# File 'lib/generator/haml_generator.rb', line 12

def initialize()
  @example_boolean = false
  @haml_options = { attr_wrapper: '"', format: :html5 }
  @compressor = HtmlCompressor::Compressor.new
end

Class Method Details

.changed?(path) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/generator/haml_generator.rb', line 50

def self.changed?(path)
  true #allways recompile haml because of partials etc.
end

Instance Method Details

#compile(input, output_file, layout, context, scope) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/generator/haml_generator.rb', line 36

def compile(input, output_file, layout, context, scope)
  # If the file being processed by Haml contains a yield statement, the block passed to
  # "render" will be called when it's hit.
  result = layout.render(context, body_class: scope) do
    # Render the actual page contents in place of the call to "yield".
    body = Haml::Engine.new(input, @haml_options)
    body.render(context)
  end

  @compressor.compress result
rescue Exception => e
  raise $!, "#{$!} TEMPLATE::#{output_file} ", $!.backtrace
end

#generate(input_folder, output_folder) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/generator/haml_generator.rb', line 18

def generate(input_folder, output_folder)
  layout_path = "#{input_folder}/layout.haml"
  return unless File.file? layout_path
  layout      = Haml::Engine.new(File.read(layout_path), @haml_options)

  Dir.glob("#{input_folder}/*.haml").select do |input_file|
    next unless File.file? input_file and !input_file.include? 'layout.haml'

    output_file_name = input_file.split('/')[-1].gsub('.haml', '.html')
    output_file      = File.join(output_folder, output_file_name)
    scope            = output_file_name.split('.').first
    context          = Context.new(@example_boolean, scope, @haml_options,
                                  input_folder, output_folder)

    compile_file(input_file, output_file, layout, context, scope)
  end
end