class Pith::RenderContext

Attributes

output[R]
page[R]
project[R]

Public Class Methods

new(output) click to toggle source
# 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

Public Instance Methods

content_for() click to toggle source
# File lib/pith/render_context.rb, line 49
def content_for
  @content_for_hash ||= Hash.new { "" }
end
current_input() click to toggle source
# File lib/pith/render_context.rb, line 26
def current_input
  @input_stack.last
end
href(target_ref) click to toggle source
# 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) click to toggle source
# 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
Also aliased as: inside
inside(template_ref, locals = {}, &block)
Alias for: include
relative_url_to(target_path) click to toggle source
# 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) click to toggle source
# 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

Private Instance Methods

absolute_url?(ref) click to toggle source
# File lib/pith/render_context.rb, line 91
def absolute_url?(ref)
  ref.respond_to?(:to_str) && ref.to_str =~ %r{^\w+:/}
end
input(path) click to toggle source
# File lib/pith/render_context.rb, line 104
def input(path)
  project.input(path) ||
  input_with_output_path(path) ||
  raise(ReferenceError, %{Can't find "#{path}"})
end
input_with_output_path(path) click to toggle source
# File lib/pith/render_context.rb, line 110
def input_with_output_path(path)
  o = project.output(path)
  o ? o.input : nil
end
render_ref(template_ref, locals = {}, &block) click to toggle source
# File lib/pith/render_context.rb, line 125
def render_ref(template_ref, locals = {}, &block)
  template_input = input(resolve_reference(template_ref))
  render(template_input, locals, &block)
end
resolve_reference(ref) click to toggle source
# File lib/pith/render_context.rb, line 95
def resolve_reference(ref)
  if ref.kind_of?(Pith::Input)
    raise(ReferenceError, %{No output for "#{ref.path}"}) if ref.output.nil?
    ref.output.path
  else
    current_input.resolve_path(ref)
  end
end
with_input(input) { || ... } click to toggle source
# File lib/pith/render_context.rb, line 115
def with_input(input)
  output.record_dependency_on(input)
  @input_stack.push(input)
  begin
    yield
  ensure
    @input_stack.pop
  end
end