Class: EasyHtmlGenerator::Generator::Compile::Haml

Inherits:
Base
  • Object
show all
Defined in:
lib/easy_html_generator/generator/compile/haml.rb

Overview

this generator compiles haml files from src folder and copies them to the dist folder

Defined Under Namespace

Classes: Context, Layout

Instance Attribute Summary

Attributes inherited from Base

#config

Instance Method Summary collapse

Methods inherited from Base

#dest_path, #do_file, #log, #log_running, #resolve_path_prefix, #src_path, #walk_files

Constructor Details

#initialize(project, config) ⇒ Haml

Returns a new instance of Haml.



14
15
16
17
18
# File 'lib/easy_html_generator/generator/compile/haml.rb', line 14

def initialize(project, config)
  super(project, config)
  @config.src  = project.config.paths.src.views
  @config.dest = project.config.paths.dist.views
end

Instance Method Details

#compile_file(src, target) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/easy_html_generator/generator/compile/haml.rb', line 57

def compile_file(src, target)
  file_name = File.basename src
  scope     = file_name.split('.').first
  context   = Context.new(@project, @config, scope)
  layout    = Layout.layout_from_file(@project.src_path_to(:views),
                                      src, @config.renderer)

  begin
    do_file(src, target, layout, context, scope)
  rescue StandardError => e
    raise e, "#{e.message} in #{src} ", e.backtrace
  end
end

#do_input(input, layout, context, scope, _input_file) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/easy_html_generator/generator/compile/haml.rb', line 20

def do_input(input, layout, context, scope, _input_file)
  # 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, @config.renderer)
    body.render(context)
  end

  return result unless @config.minimize

  EasyHtmlGenerator::Generator::Minimize::Html.compress result
end

#generateObject



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/easy_html_generator/generator/compile/haml.rb', line 43

def generate
  return unless @config.enabled

  log_running

  FileUtils.mkdir_p dest_path

  walk_files(File.join(src_path, @config.selector)) do |i, o|
    next unless should_do_file? i

    compile_file i, o
  end
end

#input_to_output_file(i) ⇒ Object



39
40
41
# File 'lib/easy_html_generator/generator/compile/haml.rb', line 39

def input_to_output_file(i)
  super(i).gsub('.html.haml', '.html')
end

#should_do_file?(i) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
37
# File 'lib/easy_html_generator/generator/compile/haml.rb', line 34

def should_do_file?(i)
  # dont check if file changed, because partials could have changed
  !File.basename(i).start_with? '_'
end