Class: Awestruct::Page

Inherits:
AStruct
  • Object
show all
Defined in:
lib/awestruct/page.rb

Constant Summary

Constants included from AStructMixin

AStructMixin::UNTRACKED_KEYS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from AStruct

#hash

Methods included from AStructMixin

#[], #cascade_for_nils!, extended, #key?, #method_missing, #transform_entry

Constructor Details

#initialize(site, handler = nil) ⇒ Page

Returns a new instance of Page



17
18
19
20
21
22
23
24
25
26
27
28
# 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Awestruct::AStructMixin

Instance Attribute Details

#dependenciesObject (readonly)

Returns the value of attribute dependencies



14
15
16
# File 'lib/awestruct/page.rb', line 14

def dependencies
  @dependencies
end

#handlerObject

Returns the value of attribute handler



13
14
15
# File 'lib/awestruct/page.rb', line 13

def handler
  @handler
end

#partial=(value) ⇒ Object (writeonly)

Sets the attribute partial

Parameters:

  • value

    the value to set the attribute partial to.



15
16
17
# File 'lib/awestruct/page.rb', line 15

def partial=(value)
  @partial = value
end

#siteObject

Returns the value of attribute site



12
13
14
# File 'lib/awestruct/page.rb', line 12

def site
  @site
end

Instance Method Details

#==(other_page) ⇒ Object



185
186
187
# File 'lib/awestruct/page.rb', line 185

def ==(other_page)
  self.object_id == other_page.object_id
end

#all_dependenciesObject



123
124
125
# File 'lib/awestruct/page.rb', line 123

def all_dependencies
  @dependencies + handler.dependencies
end

#collective_dependencies_mtimeObject



111
112
113
114
115
116
117
118
119
120
121
# 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) ⇒ Object



181
182
183
# File 'lib/awestruct/page.rb', line 181

def content(with_layouts=false)
  rendered_content( create_context(), with_layouts )
end

#content_syntaxObject



127
128
129
# File 'lib/awestruct/page.rb', line 127

def content_syntax
  handler.content_syntax
end

#create_context(content = '') ⇒ Object



38
39
40
41
42
# 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) ⇒ Object



44
45
46
47
48
49
50
51
52
# 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_mtimeObject



107
108
109
# File 'lib/awestruct/page.rb', line 107

def input_mtime
  handler.input_mtime( self )
end

#inspectObject



34
35
36
# 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?Boolean

Returns:

  • (Boolean)


189
190
191
# File 'lib/awestruct/page.rb', line 189

def is_partial?
  @partial
end

#output_extensionObject



77
78
79
# File 'lib/awestruct/page.rb', line 77

def output_extension
  handler.output_extension
end

#output_filenameObject



81
82
83
# File 'lib/awestruct/page.rb', line 81

def output_filename
  handler.output_filename
end

#output_pathObject



66
67
68
# File 'lib/awestruct/page.rb', line 66

def output_path
  (@output_path || handler.output_path).to_s
end

#output_path=(path) ⇒ Object



70
71
72
73
74
75
# 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!Object



30
31
32
# File 'lib/awestruct/page.rb', line 30

def prepare!
  handler.inherit_front_matter( self )
end

#raw_contentObject



131
132
133
# File 'lib/awestruct/page.rb', line 131

def raw_content
  handler.raw_content
end

#relative_source_pathObject



54
55
56
# File 'lib/awestruct/page.rb', line 54

def relative_source_path
  @relative_source_path || handler.relative_source_path
end

#relative_source_path=(path) ⇒ Object



58
59
60
# 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) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# 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_nameObject



62
63
64
# File 'lib/awestruct/page.rb', line 62

def simple_name
  handler.simple_name
end

#source_pathObject



85
86
87
# File 'lib/awestruct/page.rb', line 85

def source_path
  handler.path.to_s
end

#stale?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/awestruct/page.rb', line 89

def stale?
  handler.stale? || @dependencies.dependencies.any?(&:stale?)
end

#stale_output?(output_path) ⇒ Boolean

Returns:

  • (Boolean)


93
94
95
96
97
98
99
# 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) ⇒ Object

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.



103
104
105
# File 'lib/awestruct/page.rb', line 103

def stale_output_callback= code
  @stale_output_callback = block
end