lib/webby/builder.rb in webby-0.4.0 vs lib/webby/builder.rb in webby-0.5.0
- old
+ new
@@ -1,6 +1,6 @@
-# $Id: builder.rb 16 2007-08-25 19:55:00Z tim_pease $
+# $Id: builder.rb 46 2007-11-27 03:31:29Z tim_pease $
require 'find'
require 'fileutils'
require 'erb'
@@ -36,22 +36,32 @@
def create( page, opts = {} )
tmpl = opts[:from]
raise Error, "template not given" unless tmpl
raise Error, "#{page} already exists" if test ?e, page
- puts "creating #{page}"
+ Logging::Logger[self].info "creating #{page}"
FileUtils.mkdir_p File.dirname(page)
str = ERB.new(::File.read(tmpl), nil, '-').result
::File.open(page, 'w') {|fd| fd.write str}
return nil
end
end # class << self
# call-seq:
- # run( :rebuild => false )
+ # Builder.new
#
+ # Creates a new Builder object for creating pages from the content and
+ # layout directories.
+ #
+ def initialize
+ @log = Logging::Logger[self]
+ end
+
+ # call-seq:
+ # run( :rebuild => false, :load_files => true )
+ #
# Runs the Webby builder by loading in the layout files from the
# <code>layouts/</code> folder and the content from the
# <code>contents/</code> folder. Content is analyzed, and those that need
# to be copied or compiled (filtered using ERB, Texttile, Markdown, etc.)
# are handled. The results are placed in the <code>output/</code> folder.
@@ -68,80 +78,78 @@
# A content file needs to be built if the age of the file is less then the
# age of the output product -- i.e. the content file has been modified
# more recently than the output file.
#
def run( opts = {} )
- Resource.clear
+ opts[:load_files] = true unless opts.has_key?(:load_files)
unless test(?d, output_dir)
- puts "creating #{output_dir}"
+ @log.info "creating #{output_dir}"
FileUtils.mkdir output_dir
end
- load_files
+ load_files if opts[:load_files]
+ loop_check
Resource.pages.each do |page|
next unless page.dirty? or opts[:rebuild]
- puts "creating #{page.destination}"
+ @log.info "creating #{page.destination}"
# make sure the directory exists
FileUtils.mkdir_p ::File.dirname(page.destination)
# copy the resource to the output directory if it is static
if page.is_static?
FileUtils.cp page.path, page.destination
# otherwise, layout the resource and write the results to
# the output directory
- else
- ::File.open(page.destination, 'w') do |fd|
- fd.write Renderer.new(page).layout_page
- end
- end
+ else Renderer.write(page) end
end
- # touch the output directory so we know when the
- # website was last generated
- FileUtils.touch output_dir
+ # touch the cairn so we know when the website was last generated
+ FileUtils.touch ::Webby.cairn
- return nil
+ nil
end
private
# Scan the <code>layouts/</code> folder and the <code>content/</code>
# folder and create a new Resource object for each file found there.
#
def load_files
- excl = Regexp.new exclude.join('|')
-
::Find.find(layout_dir, content_dir) do |path|
next unless test ?f, path
- next if path =~ excl
+ next if path =~ ::Webby.exclude
Resource.new path
end
+ end
+ # Loop over all the layout resources looking for circular reference -- a
+ # layout that eventually refers back to itself. These are bad. Raise an
+ # error if one is detected.
+ #
+ def loop_check
layouts = Resource.layouts
- # look for loops in the layout references -- i.e. a layout
- # eventually refers back to itself
layouts.each do |lyt|
stack = []
while lyt
if stack.include? lyt.filename
stack << lyt.filename
raise Error,
"loop detected in layout references: #{stack.join(' > ')}"
end
stack << lyt.filename
- lyt = layouts.find_by_name lyt.layout
+ lyt = layouts.find :filename => lyt.layout
end # while
end # each
end
- %w(output_dir layout_dir content_dir exclude).each do |key|
+ %w(output_dir layout_dir content_dir).each do |key|
self.class_eval <<-CODE
def #{key}( ) ::Webby.config['#{key}'] end
CODE
end