awestruct 0.5.4.rc → 0.5.4.rc2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,181 +0,0 @@
1
-
2
- require 'awestruct/context'
3
-
4
- require 'awestruct/handlers/no_op_handler'
5
- require 'awestruct/handlers/page_delegating_handler'
6
- require 'ostruct'
7
- require 'awestruct/astruct'
8
-
9
- module Awestruct
10
-
11
- class Page < Awestruct::AStruct
12
-
13
- attr_accessor :site
14
- attr_accessor :handler
15
- attr_reader :dependencies
16
-
17
- def initialize(site, handler=nil)
18
- @site = site
19
- case (handler)
20
- when Page
21
- @handler = Awestruct::Handlers::LayoutHandler.new( site, Awestruct::Handlers::PageDelegatingHandler.new( site, handler ) )
22
- when nil
23
- @handler = Awestruct::Handlers::NoOpHandler.new( site )
24
- else
25
- @handler = handler
26
- end
27
- @dependencies = Awestruct::Dependencies.new( self )
28
- end
29
-
30
- def prepare!
31
- handler.inherit_front_matter( self )
32
- end
33
-
34
- def inspect
35
- "Awestruct::Page{ #{self.object_id}: output_path=>#{output_path}, source_path=>#{source_path}, layout=>#{layout} }"
36
- end
37
-
38
- def create_context(content='')
39
- context = Awestruct::Context.new( :site=>site, :page=>self, :content=>content )
40
- site.engine.pipeline.mixin_helpers( context )
41
- context
42
- end
43
-
44
- def inherit_front_matter_from(hash)
45
- hash.each do |k,v|
46
- $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?
47
- unless ( key?( k ) )
48
- self[k.to_sym] = v
49
- end
50
- end
51
- end
52
-
53
- def relative_source_path
54
- @relative_source_path || handler.relative_source_path
55
- end
56
-
57
- def relative_source_path=(path)
58
- @relative_source_path = path
59
- end
60
-
61
- def simple_name
62
- handler.simple_name
63
- end
64
-
65
- def output_path
66
- (@output_path || handler.output_path).to_s
67
- end
68
-
69
- def output_path=(path)
70
- case ( path )
71
- when Pathname then @output_path = path
72
- else @output_path = Pathname.new( path )
73
- end
74
- end
75
-
76
- def output_extension
77
- handler.output_extension
78
- end
79
-
80
- def output_filename
81
- handler.output_filename
82
- end
83
-
84
- def source_path
85
- handler.path.to_s
86
- end
87
-
88
- def stale?
89
- handler.stale? || @dependencies.dependencies.any?(&:stale?)
90
- end
91
-
92
- def stale_output?(output_path)
93
- return true if ! File.exist?( output_path )
94
- return true if input_mtime > File.mtime( output_path )
95
- false
96
- end
97
-
98
- def input_mtime
99
- handler.input_mtime( self )
100
- end
101
-
102
- def collective_dependencies_mtime
103
- t = nil
104
- @dependencies.each do |e|
105
- if ( t == nil )
106
- t = e.mtime
107
- elsif ( t < e.mtime )
108
- t = e.mtime
109
- end
110
- end
111
- t
112
- end
113
-
114
- def all_dependencies
115
- @dependencies + handler.dependencies
116
- end
117
-
118
- def content_syntax
119
- handler.content_syntax
120
- end
121
-
122
- def raw_content
123
- handler.raw_content
124
- end
125
-
126
- def rendered_content(context=create_context(), with_layouts=true)
127
- if context.site.config.track_dependencies
128
- Awestruct::Dependencies.push_page( self )
129
- end
130
- c = nil
131
-
132
- begin
133
- $LOG.debug "calling rendered_content on handler for page #{self.output_path}" if $LOG.debug?
134
- c = handler.rendered_content( context, with_layouts )
135
- # c = site.engine.pipeline.apply_transformers( context.site, self, c )
136
- rescue StandardError => e
137
- lineno = nil
138
- if e.respond_to?(:lineno)
139
- lineno = e.lineno
140
- elsif e.respond_to?(:line)
141
- lineno = e.line
142
- end
143
- if lineno
144
- if self.handler.respond_to?(:content_line_offset)
145
- lineno += self.handler.content_line_offset
146
- end
147
- end
148
- lineinfo = lineno ? " at line #{lineno}" : ''
149
- raise StandardError, "ERROR: Failed to render: #{self.relative_source_path}#{lineinfo}\n#{e.class}: #{e.message.rstrip}\n" + e.backtrace.join("\n")
150
- end
151
-
152
- if context.site.config.track_dependencies
153
- Awestruct::Dependencies.pop_page
154
-
155
- # temp disable traqcking when we collect the hash to not dirty the results
156
- Awestruct::Dependencies.track_dependencies = false
157
- if with_layouts
158
- @dependencies.content_hash = Digest::SHA2.hexdigest(c)
159
-
160
- # create a new Page so we can inherit the updated values not reflected in self
161
- tmp_page = Awestruct::Page.new @site
162
- @handler.inherit_front_matter(tmp_page)
163
- 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
164
- hash = Digest::SHA2.hexdigest(string_to_hash)
165
- @dependencies.key_hash = hash
166
- end
167
- Awestruct::Dependencies.track_dependencies = true
168
- end
169
- c
170
- end
171
-
172
- def content(with_layouts=false)
173
- rendered_content( create_context(), with_layouts )
174
- end
175
-
176
- def ==(other_page)
177
- self.object_id == other_page.object_id
178
- end
179
-
180
- end
181
- end