public
Rubygem
Description: A lightweight and flexible website management system.
Homepage: http://webby.rubyforge.org/
Clone URL: git://github.com/TwP/webby.git
improving loop detection and error reporting in the renderer
TwP (author)
Sat Mar 08 08:27:29 -0800 2008
commit  517caf68c5eae00ec8283e7f2c13e5667a5a0682
tree    4f14c897c5f1834c595564a595ab472baca67f55
parent  8e16b6fb920b55f688fc82118f47506236897027
...
1
 
2
3
4
5
6
 
7
8
 
9
10
11
...
 
1
2
3
4
5
 
6
7
8
9
10
11
12
0
@@ -1,11 +1,12 @@
0
-== 0.8.1 / 2008-
0
+== 0.8.1 / 2008-03-
0
 
0
 * 1 major enhancement
0
   - Added support for UltraViolet syntas highlighting
0
     (code provided by Guillaume Carbonneau)
0
-* 1 minor enhancement
0
+* 2 minor enhancements
0
   - Added rake tasks to validate the hyperlinks for the HTML files
0
     in the output directory
0
+ - Improved error handling and reporting during the build process
0
 * 1 bug fix
0
   - Fixed an error with the prev / next calls in the paginator and
0
     rendering links to those pages
...
17
18
19
 
 
 
20
21
22
...
53
54
55
56
 
57
58
59
...
17
18
19
20
21
22
23
24
25
...
56
57
58
 
59
60
61
62
0
@@ -17,6 +17,9 @@ Logging::Appender.stdout.layout = Logging::Layouts::Pattern.new(
0
     :date_pattern => "%H:%M:%S" # date == HH:MM:SS
0
 )
0
 
0
+# TODO: send rendered page text to a temporary location?
0
+# this might be useful for parsing through all the rendered pages
0
+# looking for links to other pages
0
 
0
 module Webby
0
 
0
@@ -53,7 +56,7 @@ module Webby
0
       # Items for running the heel webserver
0
       :heel_port => 4331,
0
 
0
- # Items used to deploy the webiste
0
+ # Items used to deploy the website
0
       :host => 'user@hostname.tld',
0
       :remote_dir => '/not/a/valid/dir',
0
       :rsync_args => %w(-av --delete),
...
88
89
90
91
92
93
94
...
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
...
88
89
90
 
91
92
93
...
127
128
129
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
130
131
132
0
@@ -88,7 +88,6 @@ class Builder
0
     end
0
 
0
     load_files if opts[:load_files]
0
- loop_check
0
 
0
     Resources.pages.each do |page|
0
       next unless page.dirty? or opts[:rebuild]
0
@@ -128,27 +127,6 @@ class Builder
0
     end
0
   end
0
 
0
- # Loop over all the layout resources looking for circular reference -- a
0
- # layout that eventually refers back to itself. These are bad. Raise an
0
- # error if one is detected.
0
- #
0
- def loop_check
0
- layouts = Resources.layouts
0
-
0
- layouts.each do |lyt|
0
- stack = []
0
- while lyt
0
- if stack.include? lyt.filename
0
- stack << lyt.filename
0
- raise Error,
0
- "loop detected in layout references: #{stack.join(' > ')}"
0
- end
0
- stack << lyt.filename
0
- lyt = layouts.find :filename => lyt.layout
0
- end # while
0
- end # each
0
- end
0
-
0
   %w(output_dir layout_dir content_dir).each do |key|
0
     self.class_eval <<-CODE
0
       def #{key}( ) ::Webby.site.#{key} end
...
27
28
29
30
31
32
33
34
35
36
...
159
160
161
162
 
163
164
165
...
27
28
29
 
 
 
 
30
31
32
...
155
156
157
 
158
159
160
161
0
@@ -27,10 +27,6 @@ module Filters
0
 class Outline
0
   include ERB::Util
0
 
0
- # :stopdoc:
0
- class Error < StandardError; end
0
- # :startdoc:
0
-
0
   # call-seq:
0
   # Outline.new( html )
0
   #
0
@@ -159,7 +155,7 @@ class Outline
0
     end
0
 
0
     if level < @base_level
0
- raise Error, "heading tags are not in order, cannot outline"
0
+ raise ::Webby::Error, "heading tags are not in order, cannot outline"
0
     end
0
 
0
     if level == @cur_level
...
20
21
22
23
24
25
26
27
28
29
...
20
21
22
 
 
 
 
23
24
25
0
@@ -20,10 +20,6 @@ module Filters
0
 #
0
 class Tidy
0
 
0
- # :stopdoc:
0
- class Error < StandardError; end
0
- # :startdoc:
0
-
0
   # call-seq:
0
   # Tidy.new( html )
0
   #
...
6
7
8
9
10
11
12
13
14
15
...
20
21
22
23
 
24
25
26
...
6
7
8
 
 
 
 
9
10
11
...
16
17
18
 
19
20
21
22
0
@@ -6,10 +6,6 @@ require 'tempfile'
0
 module Webby::Helpers
0
 module GraphvizHelper
0
 
0
- # :stopdoc:
0
- class Error < StandardError; end
0
- # :startdoc:
0
-
0
   # call-seq:
0
   # GraphvizHelper.error_check( file )
0
   #
0
@@ -20,7 +16,7 @@ module GraphvizHelper
0
   def self.error_check( file )
0
     if ::File.size(file.path) != 0
0
       msg = "\n" << ::File.read(file.path).strip
0
- raise Error, msg
0
+ raise ::Webby::Error, msg
0
     end
0
   end
0
 
...
6
7
8
9
10
11
12
13
14
15
...
6
7
8
 
 
 
 
9
10
11
0
@@ -6,10 +6,6 @@ require 'fileutils'
0
 module Webby::Helpers
0
 module TexImgHelper
0
 
0
- # :stopdoc:
0
- class Error < StandardError; end
0
- # :startdoc:
0
-
0
   # The +tex2img+ method converts a a section of mathematical TeX script
0
   # into an image and embeds the resulting image into the page. The TeX
0
   # engine must be installed on your system along with the ImageMagick
...
215
216
217
218
 
 
219
220
221
...
215
216
217
 
218
219
220
221
222
0
@@ -215,7 +215,8 @@ module UrlHelper
0
     if Webby::Resources::Resource === name
0
       p, name = name, nil
0
     elsif opts.empty? && name
0
- p = @pages.find(Webby.site.find_by.to_sym => name)
0
+ opts[Webby.site.find_by.to_sym] = name
0
+ p = @pages.find(opts)
0
     else
0
       p = @pages.find(opts)
0
     end
...
24
25
26
27
28
29
30
...
40
41
42
43
 
44
45
46
...
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
...
136
137
138
139
 
140
141
142
143
 
144
145
146
...
182
183
184
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
185
186
187
...
222
223
224
225
 
226
227
228
...
24
25
26
 
27
28
29
...
39
40
41
 
42
43
44
45
...
68
69
70
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71
72
73
...
101
102
103
 
104
105
106
107
 
108
109
110
111
...
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
...
233
234
235
 
236
237
238
239
0
@@ -24,7 +24,6 @@ class Renderer
0
   include ERB::Util
0
 
0
   # :stopdoc:
0
- class Error < StandardError; end
0
   @@stack = []
0
   # :startdoc:
0
 
0
@@ -40,7 +39,7 @@ class Renderer
0
 
0
     loop {
0
       ::File.open(page.destination, 'w') do |fd|
0
- fd.write renderer.layout_page
0
+ fd.write(renderer.__send__(:_layout_page))
0
       end
0
       break unless renderer.__send__(:_next_page)
0
     }
0
@@ -69,40 +68,6 @@ class Renderer
0
   end
0
 
0
   # call-seq:
0
- # layout_page => string
0
- #
0
- # Apply the desired filters to the page and then render the filtered page
0
- # into the desired layout. The filters to apply to the page are determined
0
- # from the page's meta-data. The layout to use is also determined from the
0
- # page's meta-data.
0
- #
0
- def layout_page
0
- layouts = Resources.layouts
0
- obj = @page
0
- str = @page.render(self)
0
-
0
- @@stack << @page.path
0
- loop do
0
- lyt = layouts.find :filename => obj.layout
0
- break if lyt.nil?
0
-
0
- @content, str = str, ::Webby::Resources::File.read(lyt.path)
0
- str = _track_rendering(lyt.path) {
0
- Filters.process(self, lyt, str)
0
- }
0
- @content, obj = nil, lyt
0
- end
0
-
0
- @@stack.pop if @page.path == @@stack.last
0
- raise Error, "rendering stack corrupted" unless @@stack.empty?
0
-
0
- str
0
- rescue => err
0
- @log.error "while rendering page '#{@page.path}'"
0
- @log.error err
0
- end
0
-
0
- # call-seq:
0
   # render_page => string
0
   #
0
   # Apply the desired filters to the page. The filters to apply are
0
@@ -136,11 +101,11 @@ class Renderer
0
         p = Resources.partials.find(
0
             :filename => fn, :in_directory => @page.dir ) rescue nil
0
         p ||= Resources.partials.find(:filename => fn)
0
- raise Error, "could not find partial '#{part}'" if p.nil?
0
+ raise ::Webby::Error, "could not find partial '#{part}'" if p.nil?
0
         p
0
       when ::Webby::Resources::Partial
0
         part
0
- else raise Error, "expecting a partial or a partial name" end
0
+ else raise ::Webby::Error, "expecting a partial or a partial name" end
0
 
0
     _track_rendering(part.path) {
0
       Filters.process(self, part, ::Webby::Resources::File.read(part.path))
0
@@ -182,6 +147,52 @@ class Renderer
0
   private
0
 
0
   # call-seq:
0
+ # _layout_page => string
0
+ #
0
+ # Apply the desired filters to the page and then render the filtered page
0
+ # into the desired layout. The filters to apply to the page are determined
0
+ # from the page's meta-data. The layout to use is also determined from the
0
+ # page's meta-data.
0
+ #
0
+ def _layout_page
0
+ @content = @page.render(self)
0
+
0
+ _track_rendering(@page.path) {
0
+ _render_layout_for(@page)
0
+ }
0
+ raise ::Webby::Error, "rendering stack corrupted" unless @@stack.empty?
0
+
0
+ @content
0
+ rescue ::Webby::Error => err
0
+ @log.error "while rendering page '#{@page.path}'"
0
+ @log.error err.message
0
+ rescue => err
0
+ @log.error "while rendering page '#{@page.path}'"
0
+ @log.fatal err
0
+ exit 1
0
+ ensure
0
+ @content = nil
0
+ @@stack.clear
0
+ end
0
+
0
+ # call-seq:
0
+ # _render_layout_for( resource )
0
+ #
0
+ # Render the layout for the given resource. If the resource does not have
0
+ # a layout, then this method returns immediately.
0
+ #
0
+ def _render_layout_for( res )
0
+ lyt = Resources.layouts.find :filename => res.layout
0
+ return if lyt.nil?
0
+
0
+ _track_rendering(lyt.path) {
0
+ @content = Filters.process(
0
+ self, lyt, ::Webby::Resources::File.read(lyt.path))
0
+ _render_layout_for(lyt)
0
+ }
0
+ end
0
+
0
+ # call-seq:
0
   # _next_page => true or false
0
   #
0
   # Returns +true+ if there is a next page to render. Returns +false+ if
0
@@ -222,7 +233,7 @@ class Renderer
0
       msg = "rendering loop detected for '#{path}'\n"
0
       msg << " current rendering stack\n\t"
0
       msg << @@stack.join("\n\t")
0
- raise Error, msg
0
+ raise ::Webby::Error, msg
0
     end
0
 
0
     yield

Comments

    No one has commented yet.