Class: Generator::HamlGenerator

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

Instance Method Summary (collapse)

Constructor Details

- (HamlGenerator) initialize

Returns a new instance of HamlGenerator



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

def initialize()
  @example_boolean = false
  @haml_options = { attr_wrapper: '"', format: :html5 }
end

Instance Method Details

- (Object) compile(file, layout, input_folder, output_folder)



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/generator/haml_generator.rb', line 30

def compile file, layout, input_folder, output_folder
  scope = file.split('/')[-1].split('.').first
  layout = Haml::Engine.new(File.read(layout), @haml_options)
  c = Context.new @example_boolean, scope, @haml_options, input_folder, output_folder

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

- (Object) generate(input_folder, output_folder)



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/generator/haml_generator.rb', line 18

def generate input_folder, output_folder
  layout = "#{input_folder}/layout.haml"

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

    result    = compile(file, layout, input_folder, output_folder)
    file_name = file.split('/')[-1].gsub('.haml', '.html')
    write File.join(output_folder, file_name), result
  end
end

- (Object) write(file, content)



44
45
46
47
48
# File 'lib/generator/haml_generator.rb', line 44

def write file, content
  File.open(file, "w") do |f|
    f.write content
  end
end