Class: Pith::RenderContext

Inherits:
Object
  • Object
show all
Defined in:
lib/pith/render_context.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(output) ⇒ RenderContext

Returns a new instance of RenderContext.



10
11
12
13
14
15
16
# File 'lib/pith/render_context.rb', line 10

def initialize(output)
  @output = output
  @page = @output.input
  @project = @page.project
  @input_stack = []
  self.extend(project.config.helper_module)
end

Instance Attribute Details

#outputObject (readonly)

Returns the value of attribute output.



18
19
20
# File 'lib/pith/render_context.rb', line 18

def output
  @output
end

#pageObject (readonly)

Returns the value of attribute page.



19
20
21
# File 'lib/pith/render_context.rb', line 19

def page
  @page
end

#projectObject (readonly)

Returns the value of attribute project.



20
21
22
# File 'lib/pith/render_context.rb', line 20

def project
  @project
end

Instance Method Details

#content_forObject



49
50
51
# File 'lib/pith/render_context.rb', line 49

def content_for
  @content_for_hash ||= Hash.new { "" }
end

#current_inputObject



26
27
28
# File 'lib/pith/render_context.rb', line 26

def current_input
  @input_stack.last
end

#href(target_ref) ⇒ Object



61
62
63
# File 'lib/pith/render_context.rb', line 61

def href(target_ref)
  relative_url_to(resolve_reference(target_ref))
end

#include(template_ref, locals = {}, &block) ⇒ Object Also known as: inside



39
40
41
42
43
44
45
# File 'lib/pith/render_context.rb', line 39

def include(template_ref, locals = {}, &block)
  content_block = if block_given?
    content = capture_haml(&block)
    proc { content }
  end
  render_ref(template_ref, locals, &content_block)
end


65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/pith/render_context.rb', line 65

def link(target_ref, label = nil, attrs={})
  if absolute_url?(target_ref)
    attrs['href'] = target_ref
  else
    target_path = resolve_reference(target_ref)
    attrs['href'] = relative_url_to(target_path)
    label ||= begin
      target_input = input(target_path)
      output.record_dependency_on(target_input)
      target_input.title
    rescue ReferenceError
      "???"
    end
  end

  # Loop through attrs hash, flatten the key, value
  # pairs for appending to the dom element/link
  attrs_flatten = attrs.each_pair.collect do |key, value|
                    %Q{#{key}="#{value}"}
                  end.join(' ')

  "<a #{attrs_flatten}>#{label}</a>"
end

#relative_url_to(target_path) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/pith/render_context.rb', line 53

def relative_url_to(target_path)
  url = target_path.relative_path_from(page.path.parent).to_s
  url = url.sub(/index\.html$/, "") if project.config.assume_directory_index
  url = url.sub(/\.html$/, "") if project.config.assume_content_negotiation
  url = "./" if url.empty?
  Pathname(url)
end

#render(input, locals = {}, &block) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/pith/render_context.rb', line 30

def render(input, locals = {}, &block)
  with_input(input) do
    result     = input.render(self, locals, &block)
    layout_ref = input.meta["layout"]
    result     = render_ref(layout_ref) { result } if layout_ref
    result
  end
end