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.



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

def dependencies
  @dependencies
end

#handlerObject

Returns the value of attribute handler.



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

def handler
  @handler
end

#siteObject

Returns the value of attribute site.



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

def site
  @site
end

Instance Method Details

#==(other_page) ⇒ Object



176
177
178
# File 'lib/awestruct/page.rb', line 176

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

#all_dependenciesObject



114
115
116
# File 'lib/awestruct/page.rb', line 114

def all_dependencies
  @dependencies + handler.dependencies
end

#collective_dependencies_mtimeObject



102
103
104
105
106
107
108
109
110
111
112
# File 'lib/awestruct/page.rb', line 102

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



172
173
174
# File 'lib/awestruct/page.rb', line 172

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

#content_syntaxObject



118
119
120
# File 'lib/awestruct/page.rb', line 118

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
# File 'lib/awestruct/page.rb', line 44

def inherit_front_matter_from(hash)
  hash.each do |k,v|
    $LOG.debug "#{self.output_path} overwrite key: #{k}:#{self[k]} -> #{v}" if ( key?( k ) and !self[k].nil? and !self[k].eql? v) if $LOG.debug?
    unless ( key?( k ) )
      self[k.to_sym] = v
    end
  end
end

#input_mtimeObject



98
99
100
# File 'lib/awestruct/page.rb', line 98

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

#output_extensionObject



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

def output_extension
  handler.output_extension
end

#output_filenameObject



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

def output_filename
  handler.output_filename
end

#output_pathObject



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

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

#output_path=(path) ⇒ Object



69
70
71
72
73
74
# File 'lib/awestruct/page.rb', line 69

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



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

def raw_content
  handler.raw_content
end

#relative_source_pathObject



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

def relative_source_path
  @relative_source_path || handler.relative_source_path
end

#relative_source_path=(path) ⇒ Object



57
58
59
# File 'lib/awestruct/page.rb', line 57

def relative_source_path=(path)
  @relative_source_path = path
end

#rendered_content(context = create_context(), with_layouts = true) ⇒ Object



126
127
128
129
130
131
132
133
134
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
# File 'lib/awestruct/page.rb', line 126

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}" if $LOG.debug?
    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



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

def simple_name
  handler.simple_name
end

#source_pathObject



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

def source_path
  handler.path.to_s
end

#stale?Boolean

Returns:

  • (Boolean)


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

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

#stale_output?(output_path) ⇒ Boolean

Returns:

  • (Boolean)


92
93
94
95
96
# File 'lib/awestruct/page.rb', line 92

def stale_output?(output_path)
  return true if ! File.exist?( output_path )
  return true if input_mtime > File.mtime( output_path )
  false
end