Class: Pith::RenderContext

Inherits:
Object
  • Object
show all
Includes:
Tilt::CompileSite
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.



13
14
15
16
17
18
19
# File 'lib/pith/render_context.rb', line 13

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.



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

def output
  @output
end

#pageObject (readonly)

Returns the value of attribute page.



22
23
24
# File 'lib/pith/render_context.rb', line 22

def page
  @page
end

#projectObject (readonly)

Returns the value of attribute project.



23
24
25
# File 'lib/pith/render_context.rb', line 23

def project
  @project
end

Instance Method Details

#content_forObject



52
53
54
# File 'lib/pith/render_context.rb', line 52

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

#current_inputObject



29
30
31
# File 'lib/pith/render_context.rb', line 29

def current_input
  @input_stack.last
end

#href(target_ref) ⇒ Object



64
65
66
# File 'lib/pith/render_context.rb', line 64

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

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



42
43
44
45
46
47
48
# File 'lib/pith/render_context.rb', line 42

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


68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/pith/render_context.rb', line 68

def link(target_ref, label = nil)
  target_path = resolve_reference(target_ref)
  label ||= begin
    target_input = input(target_path)
    output.record_dependency_on(target_input)
    target_input.title
  rescue ReferenceError
    "???"
  end
  url = relative_url_to(target_path)
  %{<a href="#{url}">#{label}</a>}
end

#relative_url_to(target_path) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/pith/render_context.rb', line 56

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



33
34
35
36
37
38
39
40
# File 'lib/pith/render_context.rb', line 33

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