lib/webby/builder.rb in webby-0.8.2 vs lib/webby/builder.rb in webby-0.8.3
- old
+ new
@@ -1,7 +1,5 @@
-# $Id: builder.rb 192 2008-03-08 16:27:29Z tim_pease $
-
require 'find'
require 'fileutils'
require 'erb'
module Webby
@@ -24,28 +22,76 @@
def run( opts = {} )
self.new.run opts
end
# call-seq:
- # Builder.create( page, :from => template )
+ # Builder.create( page, :from => template, :locals => {} )
#
# This mehod is used to create a new _page_ in the content folder based
# on the specified template. _page_ is the relative path to the new page
# from the <code>content/</code> folder. The _template_ is the name of
# the template to use from the <code>templates/</code> folder.
#
def create( page, opts = {} )
tmpl = opts[:from]
raise Error, "template not given" unless tmpl
+
+ name = ::Webby::Resources::File.basename(page)
+ ext = ::Webby::Resources::File.extname(page)
+ dir = ::File.dirname(page)
+ dir = '' if dir == '.'
+
+ if tmpl.pathmap('%n') =~ %r/^_/
+ page = ::File.join(::Webby.site.content_dir, dir, '_'+name)
+ page << '.' << (ext.empty? ? 'txt' : ext)
+ elsif ::Webby.site.create_mode == 'directory' and name != 'index'
+ page = ::File.join(::Webby.site.content_dir, dir, name, 'index')
+ page << '.' << (ext.empty? ? 'txt' : ext)
+ else
+ page = ::File.join(::Webby.site.content_dir, page)
+ page << '.txt' if ext.empty?
+ end
raise Error, "#{page} already exists" if test ?e, page
Logging::Logger[self].info "creating #{page}"
FileUtils.mkdir_p ::File.dirname(page)
- str = ERB.new(::File.read(tmpl), nil, '-').result
+
+ context = scope
+ opts[:locals].each do |k,v|
+ Thread.current[:value] = v
+ definition = "#{k} = Thread.current[:value]"
+ eval(definition, context)
+ end if opts.has_key?(:locals)
+
+ str = ERB.new(::File.read(tmpl), nil, '-').result(context)
::File.open(page, 'w') {|fd| fd.write str}
- return nil
+ page
end
+
+ # call-seq:
+ # Builder.new_page_info( task ) => [page, title, directory]
+ #
+ def new_page_info( task )
+ return @new_page_info if defined? @new_page_info
+
+ raise "Usage: rake #{ARGV.first} path" unless ARGV.length > 1
+ page = task.application.top_level_tasks.slice!(1..-1).join('-')
+ title = ::Webby::Resources::File.basename(page).
+ split('-').map {|w| w.capitalize}.join(' ')
+ dir = ::File.dirname(page)
+ dir = '' if dir == '.'
+
+ @new_page_info = [page, title, dir]
+ end
+
+
+ private
+
+ # Returns the binding in the scope of the Builder class object.
+ #
+ def scope() binding end
+
end # class << self
# call-seq:
# Builder.new
#