Class: Ichiban::HTMLCompiler

Inherits:
Object
  • Object
show all
Defined in:
lib/ichiban/html_compiler.rb

Defined Under Namespace

Classes: Context, Eruby

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(html_file) ⇒ HTMLCompiler

Takes an instance of Ichiban::HTMLFile or Ichiban::PartialHTMLFile



46
47
48
# File 'lib/ichiban/html_compiler.rb', line 46

def initialize(html_file)
  @html_file = html_file
end

Instance Attribute Details

#ivars=(value) ⇒ Object (writeonly)

Sets the attribute ivars

Parameters:

  • value

    the value to set the attribute ivars to.



50
51
52
# File 'lib/ichiban/html_compiler.rb', line 50

def ivars=(value)
  @ivars = value
end

Instance Method Details

#compileObject



3
4
5
6
7
8
9
# File 'lib/ichiban/html_compiler.rb', line 3

def compile
  FileUtils.mkdir_p File.dirname(@html_file.dest)
  File.open(@html_file.dest, 'w') do |f|
    f << compile_to_str
  end
  Ichiban.logger.compilation(@html_file.abs, @html_file.dest)
end

#compile_to_strObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/ichiban/html_compiler.rb', line 11

def compile_to_str 
  # @_template_path is a path relative to the html folder. It points to the current *complete*
  # HTML file being rendered. I.e. if we're currently rendering a partial, @_template_path
  # will *not* point to the partial file.
  #
  # _template_path may be overwritten later when we look at @ivars. This is good, because
  # if we're in a partial, and the including template has already set _template_path, we want
  # to inherit that value. (When you call partial from a template, all of the including template's
  # instance variables, including @_template_path, will be put into @ivars.)
  ivars_for_ctx = {:_template_path => @html_file.rel.slice('html/'.length..-1)}
  
  if @html_file.is_a?(Ichiban::HTMLFile)
    ivars_for_ctx[:_current_path] = @html_file.web_path
  end
  ivars_for_ctx.merge!(@ivars) if @ivars
  
  ctx = Ichiban::HTMLCompiler::Context.new(ivars_for_ctx)
  
  inner_html = Eruby.new(File.read(@html_file.abs), :filename => @html_file.abs).evaluate(ctx)
  
  # Compile Markdown if necessary
  if (@html_file.abs.end_with?('.markdown') or @html_file.abs.end_with?('.md'))
    inner_html = Ichiban::Markdown.compile(inner_html) # Will look for installed Markdown gems
  end
  
  # Do layouts if appropriate
  if @html_file.is_a?(Ichiban::HTMLFile)
    wrap_in_layouts(ctx, inner_html)
  else
    # It's a PartialHTMLFile
    inner_html
  end
end

#wrap_in_layouts(ctx, inner_rhtml) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/ichiban/html_compiler.rb', line 52

def wrap_in_layouts(ctx, inner_rhtml)
  ctx.layout_stack.reverse.inject(inner_rhtml) do |html, layout_name|
    layout_path = File.join(Ichiban.project_root, 'layouts', layout_name + '.html')
    unless File.exists?(layout_path)
      raise "Layout does not exist: #{layout_path}"
    end
    eruby = Eruby.new(
      File.read(layout_path),
      :filename => layout_path
    )
    html = eruby.evaluate(ctx) { html }
    html
  end
end