class Awestruct::Page

Attributes

dependencies[R]
handler[RW]
partial[W]
site[RW]

Public Class Methods

new(site, handler=nil) click to toggle source
# File lib/awestruct/page.rb, line 17
def initialize(site, handler=nil)
  @site          = site
  case (handler)
    when Page
      @handler = Awestruct::Handlers::LayoutHandler.new( site, Awestruct::Handlers::PageDelegatingHandler.new( site, handler ) )
    when nil
      @handler = Awestruct::Handlers::NoOpHandler.new( site )
    else
      @handler = handler
  end
  @dependencies = Awestruct::Dependencies.new( self )
end

Public Instance Methods

==(other_page) click to toggle source
# File lib/awestruct/page.rb, line 185
def ==(other_page)
  self.object_id == other_page.object_id
end
all_dependencies() click to toggle source
# File lib/awestruct/page.rb, line 123
def all_dependencies
  @dependencies + handler.dependencies
end
collective_dependencies_mtime() click to toggle source
# File lib/awestruct/page.rb, line 111
def collective_dependencies_mtime
  t = nil
  @dependencies.each do |e|
    if ( t == nil )
      t = e.mtime
    elsif ( t < e.mtime )
      t = e.mtime
    end
  end
  t
end
content(with_layouts=false) click to toggle source
# File lib/awestruct/page.rb, line 181
def content(with_layouts=false)
  rendered_content( create_context(), with_layouts )
end
content_syntax() click to toggle source
# File lib/awestruct/page.rb, line 127
def content_syntax
  handler.content_syntax
end
create_context(content='') click to toggle source
# File lib/awestruct/page.rb, line 38
def create_context(content='')
  context = Awestruct::Context.new( :site=>site, :page=>self, :content=>content )
  site.engine.pipeline.mixin_helpers( context )
  context
end
inherit_front_matter_from(hash) click to toggle source
# File lib/awestruct/page.rb, line 44
def inherit_front_matter_from(hash)
  $LOG.debug "inherit_front_matter_from for #{self.inspect}"
  hash.each do |k,v|
    $LOG.debug "#{output_path} overwrite key: #{k}:#{self[k]} -> #{v}" if ( key?( k ) and !self[k].nil? and !self[k].eql? v)
    unless ( key?( k ) )
      self[k.to_sym] = v
    end
  end
end
input_mtime() click to toggle source
# File lib/awestruct/page.rb, line 107
def input_mtime
  handler.input_mtime( self )
end
inspect() click to toggle source
# File lib/awestruct/page.rb, line 34
def inspect
  "Awestruct::Page{ #{self.object_id}: output_path=>#{output_path}, source_path=>#{source_path}, layout=>#{layout} }"
end
is_partial?() click to toggle source
# File lib/awestruct/page.rb, line 189
def is_partial?
  @partial
end
output_extension() click to toggle source
# File lib/awestruct/page.rb, line 77
def output_extension
  handler.output_extension
end
output_filename() click to toggle source
# File lib/awestruct/page.rb, line 81
def output_filename
  handler.output_filename
end
output_path() click to toggle source
# File lib/awestruct/page.rb, line 66
def output_path
  (@output_path || handler.output_path).to_s
end
output_path=(path) click to toggle source
# File lib/awestruct/page.rb, line 70
def output_path=(path)
  case ( path )
    when Pathname then @output_path = path
    else @output_path = Pathname.new( path )
  end
end
prepare!() click to toggle source
# File lib/awestruct/page.rb, line 30
def prepare!
  handler.inherit_front_matter( self )
end
raw_content() click to toggle source
# File lib/awestruct/page.rb, line 131
def raw_content
  handler.raw_content
end
relative_source_path() click to toggle source
# File lib/awestruct/page.rb, line 54
def relative_source_path
  @relative_source_path || handler.relative_source_path
end
relative_source_path=(path) click to toggle source
# File lib/awestruct/page.rb, line 58
def relative_source_path=(path)
  @relative_source_path = path
end
rendered_content(context=create_context(), with_layouts=true) click to toggle source
# File lib/awestruct/page.rb, line 135
def rendered_content(context=create_context(), with_layouts=true)
  if context.site.config.track_dependencies
    Awestruct::Dependencies.push_page( self )
  end
  c = nil

  begin
    $LOG.debug "calling rendered_content on handler for page #{self.output_path}"
    c = handler.rendered_content( context, with_layouts )
    # c = site.engine.pipeline.apply_transformers( context.site, self, c )
  rescue StandardError => e
    lineno = nil
    if e.respond_to?(:lineno)
      lineno = e.lineno
    elsif e.respond_to?(:line)
      lineno = e.line
    end
    if lineno
      if self.handler.respond_to?(:content_line_offset)
        lineno += self.handler.content_line_offset
      end
    end
    lineinfo = lineno ? " at line #{lineno}" : ''
    raise StandardError, "ERROR: Failed to render: #{self.relative_source_path}#{lineinfo}\n#{e.class}: #{e.message.rstrip}\n" + e.backtrace.join("\n")
  end

  if context.site.config.track_dependencies
    Awestruct::Dependencies.pop_page

    # temp disable traqcking when we collect the hash to not dirty the results
    Awestruct::Dependencies.track_dependencies = false
    if with_layouts
      @dependencies.content_hash = Digest::SHA2.hexdigest(c)

      # create a new Page so we can inherit the updated values not reflected in self
      tmp_page = Awestruct::Page.new @site
      @handler.inherit_front_matter(tmp_page)
      string_to_hash = tmp_page.to_a.each{|x| x[0]=x[0].to_s; x[1]=x[1].to_s; x}.sort.to_s
      hash = Digest::SHA2.hexdigest(string_to_hash)
      @dependencies.key_hash = hash
    end
    Awestruct::Dependencies.track_dependencies = true
  end
  c
end
simple_name() click to toggle source
# File lib/awestruct/page.rb, line 62
def simple_name
  handler.simple_name
end
source_path() click to toggle source
# File lib/awestruct/page.rb, line 85
def source_path
  handler.path.to_s
end
stale?() click to toggle source
# File lib/awestruct/page.rb, line 89
def stale?
  handler.stale? || @dependencies.dependencies.any?(&:stale?)
end
stale_output?(output_path) click to toggle source
# File lib/awestruct/page.rb, line 93
def stale_output?(output_path)
  return true if ! File.exist?( output_path )
  return true if input_mtime > File.mtime( output_path )
  return true if stale?
  return true if @stale_output_callback && @stale_output_callback.call(self)
  false
end
stale_output_callback=(code) click to toggle source

Define a stale check specific for this page. The parameter is expected to be a lambda or proc and is called with self as the argument.

# File lib/awestruct/page.rb, line 103
def stale_output_callback= code
  @stale_output_callback = block
end